Skip to content

Commit

Permalink
mastodon: get_activities(): only fetch replies/favorites/boosts based…
Browse files Browse the repository at this point in the history
… on counts
  • Loading branch information
snarfed committed Oct 20, 2019
1 parent a92bd25 commit 4a02f52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
7 changes: 3 additions & 4 deletions granary/mastodon.py
Expand Up @@ -5,7 +5,6 @@
independent of AP. API docs: https://docs.joinmastodon.org/api/
TODO:
* get_activities(): start_index, count, min_id
* caching
* custom emoji. see ~/mastodon.status.custom-emoji.json
* https://docs.joinmastodon.org/api/entities/#emoji
Expand Down Expand Up @@ -185,19 +184,19 @@ def get_activities_response(self, user_id=None, group_id=None, app_id=None,
id = status.get('id')
if id:
obj = activity['object']
if fetch_replies:
if fetch_replies and status.get('replies_count'):
context = self._get(API_CONTEXT % id)
obj['replies'] = {
'items': [self.status_to_activity(reply)
for reply in context.get('descendants', [])]
}
tags = obj.setdefault('tags', [])

if fetch_likes:
if fetch_likes and status.get('favourites_count'):
likers = self._get(API_FAVORITED_BY % id)
tags.extend(self._make_like(status, l) for l in likers)

if fetch_shares:
if fetch_shares and status.get('reblogs_count'):
sharers = self._get(API_REBLOGGED_BY % id)
tags.extend(self._make_share(status, s) for s in sharers)

Expand Down
22 changes: 16 additions & 6 deletions granary/tests/test_mastodon.py
Expand Up @@ -86,9 +86,6 @@ def tag_uri(name):
'account': ACCOUNT,
'content': '<p>foo ☕ bar <a ...>@alice</a> <a ...>#IndieWeb</a></p>',
'created_at': '2019-07-29T18:35:53.446Z',
'replies_count': 1,
'favourites_count': 0,
'reblogs_count': 0,
'visibility': 'public',
'mentions': [{
'username': 'alice',
Expand Down Expand Up @@ -274,6 +271,13 @@ def tag_uri(name):
'status': MEDIA_STATUS,
'created_at': '2019-10-15T00:23:37.969Z',
}
STATUS_WITH_COUNTS = copy.deepcopy(STATUS)
STATUS_WITH_COUNTS.update({ # Mastodon
'replies_count': 1,
'favourites_count': 2,
'reblogs_count': 3,
})


class MastodonTest(testutil.TestCase):

Expand Down Expand Up @@ -316,7 +320,7 @@ def test_get_activities_group_id_friends(self):
self.mastodon.get_activities(group_id=source.FRIENDS))

def test_get_activities_fetch_replies(self):
self.expect_get(API_TIMELINE, params={}, response=[STATUS])
self.expect_get(API_TIMELINE, params={}, response=[STATUS_WITH_COUNTS])
self.expect_get(API_CONTEXT % STATUS['id'], {
'ancestors': [],
'descendants': [REPLY_STATUS, REPLY_STATUS],
Expand All @@ -330,7 +334,7 @@ def test_get_activities_fetch_replies(self):
self.assert_equals([with_replies], self.mastodon.get_activities(fetch_replies=True))

def test_get_activities_fetch_likes(self):
self.expect_get(API_TIMELINE, params={}, response=[STATUS])
self.expect_get(API_TIMELINE, params={}, response=[STATUS_WITH_COUNTS])
self.expect_get(API_FAVORITED_BY % STATUS['id'], [ACCOUNT, ACCOUNT])
self.mox.ReplayAll()

Expand All @@ -339,14 +343,20 @@ def test_get_activities_fetch_likes(self):
self.assert_equals([with_likes], self.mastodon.get_activities(fetch_likes=True))

def test_get_activities_fetch_shares(self):
self.expect_get(API_TIMELINE, params={}, response=[STATUS])
self.expect_get(API_TIMELINE, params={}, response=[STATUS_WITH_COUNTS])
self.expect_get(API_REBLOGGED_BY % STATUS['id'], [ACCOUNT, ACCOUNT_REMOTE])
self.mox.ReplayAll()

with_shares = copy.deepcopy(ACTIVITY)
with_shares['object']['tags'].extend([SHARE, SHARE_BY_REMOTE])
self.assert_equals([with_shares], self.mastodon.get_activities(fetch_shares=True))

def test_get_activities_fetch_replies_likes_shares_counts_zero(self):
self.expect_get(API_TIMELINE, params={}, response=[STATUS])
self.mox.ReplayAll()
self.assert_equals([ACTIVITY], self.mastodon.get_activities(
fetch_replies=True, fetch_likes=True, fetch_shares=True))

def test_get_activities_fetch_mentions(self):
self.expect_get(API_TIMELINE, params={}, response=[STATUS])
self.expect_get(API_NOTIFICATIONS, [MENTION_NOTIFICATION], json={
Expand Down

0 comments on commit 4a02f52

Please sign in to comment.