2019-08-24 13:43:41 +02:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2023-06-30 11:06:17 +02:00
|
|
|
from toot.utils.datetime import parse_datetime
|
2019-08-27 14:34:51 +02:00
|
|
|
|
2019-10-01 09:27:27 +02:00
|
|
|
Author = namedtuple("Author", ["account", "display_name", "username"])
|
2019-08-24 13:43:41 +02:00
|
|
|
|
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
class Status:
|
|
|
|
"""
|
|
|
|
A wrapper around the Status entity data fetched from Mastodon.
|
|
|
|
|
|
|
|
https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md#status
|
2019-09-22 12:13:40 +02:00
|
|
|
|
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
reblog : Status or None
|
|
|
|
The reblogged status if it exists.
|
|
|
|
|
|
|
|
original : Status
|
|
|
|
If a reblog, the reblogged status, otherwise self.
|
2019-08-24 11:20:31 +02:00
|
|
|
"""
|
2019-09-22 12:13:40 +02:00
|
|
|
|
2019-09-04 16:16:16 +02:00
|
|
|
def __init__(self, data, is_mine, default_instance):
|
2019-09-22 12:13:40 +02:00
|
|
|
"""
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
data : dict
|
|
|
|
Status data as received from Mastodon.
|
|
|
|
https://docs.joinmastodon.org/api/entities/#status
|
|
|
|
|
|
|
|
is_mine : bool
|
|
|
|
Whether the status was created by the logged in user.
|
|
|
|
|
|
|
|
default_instance : str
|
|
|
|
The domain of the instance into which the user is logged in. Used to
|
|
|
|
create fully qualified account names for users on the same instance.
|
|
|
|
Mastodon only populates the name, not the domain.
|
|
|
|
"""
|
|
|
|
|
2019-08-24 11:20:31 +02:00
|
|
|
self.data = data
|
2019-09-04 16:16:16 +02:00
|
|
|
self.is_mine = is_mine
|
|
|
|
self.default_instance = default_instance
|
2019-08-24 11:20:31 +02:00
|
|
|
|
2019-08-29 14:01:26 +02:00
|
|
|
# This can be toggled by the user
|
|
|
|
self.show_sensitive = False
|
|
|
|
|
2022-12-11 22:51:32 +01:00
|
|
|
# Set when status is translated
|
|
|
|
self.show_translation = False
|
|
|
|
self.translation = None
|
|
|
|
self.translated_from = None
|
|
|
|
|
2019-09-22 12:13:40 +02:00
|
|
|
# TODO: clean up
|
2019-08-24 11:20:31 +02:00
|
|
|
self.id = self.data["id"]
|
2019-09-22 12:13:40 +02:00
|
|
|
self.account = self._get_account()
|
2019-08-24 11:20:31 +02:00
|
|
|
self.created_at = parse_datetime(data["created_at"])
|
2019-09-22 12:13:40 +02:00
|
|
|
self.author = self._get_author()
|
2019-08-24 12:53:55 +02:00
|
|
|
self.favourited = data.get("favourited", False)
|
|
|
|
self.reblogged = data.get("reblogged", False)
|
2022-12-27 11:39:50 +01:00
|
|
|
self.bookmarked = data.get("bookmarked", False)
|
2019-08-31 15:24:03 +02:00
|
|
|
self.in_reply_to = data.get("in_reply_to_id")
|
2019-09-22 12:13:40 +02:00
|
|
|
self.url = data.get("url")
|
|
|
|
self.mentions = data.get("mentions")
|
|
|
|
self.reblog = self._get_reblog()
|
2022-12-21 21:16:03 +01:00
|
|
|
self.visibility = data.get("visibility")
|
2019-08-24 12:53:55 +02:00
|
|
|
|
2019-09-22 12:13:40 +02:00
|
|
|
@property
|
|
|
|
def original(self):
|
|
|
|
return self.reblog or self
|
2019-09-06 16:01:04 +02:00
|
|
|
|
2019-09-22 12:13:40 +02:00
|
|
|
def _get_reblog(self):
|
|
|
|
reblog = self.data.get("reblog")
|
|
|
|
if not reblog:
|
|
|
|
return None
|
2019-09-13 14:02:02 +02:00
|
|
|
|
2019-09-22 12:13:40 +02:00
|
|
|
reblog_is_mine = self.is_mine and (
|
|
|
|
self.data["account"]["acct"] == reblog["account"]["acct"]
|
|
|
|
)
|
|
|
|
return Status(reblog, reblog_is_mine, self.default_instance)
|
|
|
|
|
|
|
|
def _get_author(self):
|
|
|
|
acct = self.data['account']['acct']
|
2019-09-04 16:16:16 +02:00
|
|
|
acct = acct if "@" in acct else "{}@{}".format(acct, self.default_instance)
|
2019-10-01 09:27:27 +02:00
|
|
|
return Author(acct, self.data['account']['display_name'], self.data['account']['username'])
|
2019-09-04 16:16:16 +02:00
|
|
|
|
2019-09-22 12:13:40 +02:00
|
|
|
def _get_account(self):
|
|
|
|
acct = self.data['account']['acct']
|
2019-09-04 16:16:16 +02:00
|
|
|
return acct if "@" in acct else "{}@{}".format(acct, self.default_instance)
|
2019-08-26 13:28:37 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2019-09-22 12:13:40 +02:00
|
|
|
return "<Status id={} account={}>".format(self.id, self.account)
|