Skip to content

Commit

Permalink
mastodon: read max toot size from v2 API
Browse files Browse the repository at this point in the history
relates to snarfed#1533
  • Loading branch information
jpcaruana committed Aug 4, 2023
1 parent db16a9d commit 0c0fdb7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 6 additions & 2 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,15 @@ def __getattr__(self, name):
elif self.key.kind() == 'Mastodon':
args = (auth_entity.instance(),) + args
inst = auth_entity.app.get().instance_info
if inst:
j = json_loads(inst)
truncate_text_length = j.get("configuration", {}).get('statuses', {}).get('max_characters', None) or j.get('max_toot_chars', None)
else:
truncate_text_length = None
kwargs = {
'user_id': json_loads(auth_entity.user_json).get('id'),
# https://docs-develop.pleroma.social/backend/API/differences_in_mastoapi_responses/#instance
'truncate_text_length':
json_loads(inst).get('max_toot_chars') if inst else None,
'truncate_text_length': truncate_text_length,
}
elif self.key.kind() == 'Twitter':
kwargs = {'username': self.key_id()}
Expand Down
12 changes: 8 additions & 4 deletions tests/test_mastodon.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""Unit tests for mastodon.py.
"""
from granary.mastodon import API_BLOCKS, API_SEARCH
from granary.tests.test_mastodon import STATUS
from granary.mastodon import API_BLOCKS
from oauth_dropins import mastodon as oauth_mastodon
from oauth_dropins.webutil import util
from oauth_dropins.webutil.util import json_dumps, json_loads
from oauth_dropins.webutil.util import json_dumps

from . import testutil
from mastodon import Mastodon
Expand Down Expand Up @@ -59,3 +57,9 @@ def test_gr_class_with_max_toot_chars(self):
app.instance_info = '{"max_toot_chars": 999}'
app.put()
self.assert_equals(999, self.m.gr_source.TRUNCATE_TEXT_LENGTH)

def test_gr_class_with_max_characters(self):
app = self.auth_entity.app.get()
app.instance_info = '{"configuration": {"statuses": {"max_characters": 888}}}'
app.put()
self.assert_equals(888, self.m.gr_source.TRUNCATE_TEXT_LENGTH)

0 comments on commit 0c0fdb7

Please sign in to comment.