mirror of
https://github.com/ihabunek/toot
synced 2024-12-23 15:37:47 +01:00
35e03a13b1
Adds two properties to `Status` entity: * `reblog` - contains the reblogged Status or None if not a reblog * `original`- contains the reblogged Status or self if not a reblog Anywhere where you wish to show a reblogged status's property when it's a reblog, or the base status proprety if not a reblog, use `status.original.<property>`.
84 lines
2.6 KiB
Python
84 lines
2.6 KiB
Python
from collections import namedtuple
|
|
|
|
from .utils import parse_datetime
|
|
|
|
Author = namedtuple("Author", ["account", "display_name"])
|
|
|
|
|
|
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
|
|
|
|
Attributes
|
|
----------
|
|
reblog : Status or None
|
|
The reblogged status if it exists.
|
|
|
|
original : Status
|
|
If a reblog, the reblogged status, otherwise self.
|
|
"""
|
|
|
|
def __init__(self, data, is_mine, default_instance):
|
|
"""
|
|
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.
|
|
"""
|
|
|
|
self.data = data
|
|
self.is_mine = is_mine
|
|
self.default_instance = default_instance
|
|
|
|
# This can be toggled by the user
|
|
self.show_sensitive = False
|
|
|
|
# TODO: clean up
|
|
self.id = self.data["id"]
|
|
self.account = self._get_account()
|
|
self.created_at = parse_datetime(data["created_at"])
|
|
self.author = self._get_author()
|
|
self.favourited = data.get("favourited", False)
|
|
self.reblogged = data.get("reblogged", False)
|
|
self.in_reply_to = data.get("in_reply_to_id")
|
|
self.url = data.get("url")
|
|
self.mentions = data.get("mentions")
|
|
self.reblog = self._get_reblog()
|
|
|
|
@property
|
|
def original(self):
|
|
return self.reblog or self
|
|
|
|
def _get_reblog(self):
|
|
reblog = self.data.get("reblog")
|
|
if not reblog:
|
|
return None
|
|
|
|
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']
|
|
acct = acct if "@" in acct else "{}@{}".format(acct, self.default_instance)
|
|
return Author(acct, self.data['account']['display_name'])
|
|
|
|
def _get_account(self):
|
|
acct = self.data['account']['acct']
|
|
return acct if "@" in acct else "{}@{}".format(acct, self.default_instance)
|
|
|
|
def __repr__(self):
|
|
return "<Status id={} account={}>".format(self.id, self.account)
|