aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2018-07-30 14:37:20 +0200
committerLorenz Diener <[email protected]>2018-07-30 14:37:20 +0200
commitd1a3277a8b757b71d16dfde9d3cc4f941dafb4ff (patch)
tree9e2523eb77eda85738c233464525e037bd8818ed /mastodon
parent4f3deac968ff6b46f25fb665ea4587822e9ce445 (diff)
downloadmastodon.py-d1a3277a8b757b71d16dfde9d3cc4f941dafb4ff.tar.gz
Add status_reply, closes #140
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py47
1 files changed, 44 insertions, 3 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index b0e5f82..e33acc1 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -269,7 +269,9 @@ class Mastodon:
269 self.ratelimit_method = ratelimit_method 269 self.ratelimit_method = ratelimit_method
270 self._token_expired = datetime.datetime.now() 270 self._token_expired = datetime.datetime.now()
271 self._refresh_token = None 271 self._refresh_token = None
272 272
273 self.__logged_in_id = None
274
273 self.ratelimit_limit = 300 275 self.ratelimit_limit = 300
274 self.ratelimit_reset = time.time() 276 self.ratelimit_reset = time.time()
275 self.ratelimit_remaining = 300 277 self.ratelimit_remaining = 300
@@ -432,7 +434,9 @@ class Mastodon:
432 if to_file is not None: 434 if to_file is not None:
433 with open(to_file, 'w') as token_file: 435 with open(to_file, 'w') as token_file:
434 token_file.write(response['access_token'] + '\n') 436 token_file.write(response['access_token'] + '\n')
435 437
438 self.__logged_in_id = None
439
436 return response['access_token'] 440 return response['access_token']
437 441
438 ### 442 ###
@@ -1021,7 +1025,7 @@ class Mastodon:
1021 ### 1025 ###
1022 # Writing data: Statuses 1026 # Writing data: Statuses
1023 ### 1027 ###
1024 @api_version("1.0.0", "2.3.0", __DICT_VERSION_STATUS) 1028 @api_version("1.0.0", "2.3.0", __DICT_VERSION_STATUS)
1025 def status_post(self, status, in_reply_to_id=None, media_ids=None, 1029 def status_post(self, status, in_reply_to_id=None, media_ids=None,
1026 sensitive=False, visibility=None, spoiler_text=None, 1030 sensitive=False, visibility=None, spoiler_text=None,
1027 idempotency_key=None): 1031 idempotency_key=None):
@@ -1113,6 +1117,34 @@ class Mastodon:
1113 """ 1117 """
1114 return self.status_post(status) 1118 return self.status_post(status)
1115 1119
1120 @api_version("1.0.0", "2.3.0", __DICT_VERSION_STATUS)
1121 def status_reply(self, to_status, status, media_ids=None, sensitive=False, visibility=None, spoiler_text=None, idempotency_key=None):
1122 """
1123 Helper function - acts like status_post, but prepends the name of all
1124 the users that are being replied to to the status text and retains
1125 CW and visibility if not explicitly overridden.
1126 """
1127 user_id = self.__get_logged_in_id()
1128
1129 # Determine users to mention
1130 mentioned_accounts = collections.OrderedDict()
1131 mentioned_accounts[to_status.account.id] = to_status.account.acct
1132 for account in to_status.mentions:
1133 if account.id != user_id and not account.id in mentioned_accounts.keys():
1134 mentioned_accounts[account.id] = account.acct
1135
1136 # Join into one piece of text. The space is added inside because of self-replies.
1137 status = "".join(map(lambda x: "@" + x + " ", mentioned_accounts.values())) + status
1138
1139 # Retain visibility / cw
1140 if visibility == None and 'visibility' in to_status:
1141 visibility = to_status.visibility
1142 if spoiler_text == None and 'spoiler_text' in to_status:
1143 spoiler_text = to_status.spoiler_text
1144
1145 self.status_post(status, in_reply_to_id = to_status.id, media_ids = media_ids, sensitive = sensitive,
1146 visibility = visibility, spoiler_text = spoiler_text, idempotency_key = idempotency_key)
1147
1116 @api_version("1.0.0", "1.0.0", "1.0.0") 1148 @api_version("1.0.0", "1.0.0", "1.0.0")
1117 def status_delete(self, id): 1149 def status_delete(self, id):
1118 """ 1150 """
@@ -1818,6 +1850,15 @@ class Mastodon:
1818 1850
1819 return (date_time_utc - epoch_utc).total_seconds() 1851 return (date_time_utc - epoch_utc).total_seconds()
1820 1852
1853 def __get_logged_in_id(self):
1854 """
1855 Fetch the logged in users ID, with caching. ID is reset on calls to log_in.
1856 """
1857 if self.__logged_in_id == None:
1858 self.__logged_in_id = self.account_verify_credentials().id
1859 return self.__logged_in_id
1860
1861
1821 @staticmethod 1862 @staticmethod
1822 def __json_allow_dict_attrs(json_object): 1863 def __json_allow_dict_attrs(json_object):
1823 """ 1864 """
Powered by cgit v1.2.3 (git 2.41.0)