Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cache in Bluesky #627

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions granary/bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,14 +1038,14 @@ def get_activities_response(self, user_id=None, group_id=None, app_id=None,
if fetch_likes and like_count and like_count != cache.get('ABL ' + id):
likers = self.client.app.bsky.feed.getLikes({}, uri=bs_post.get('uri'))
tags.extend(self._make_like(bs_post, l.get('actor')) for l in likers.get('likes'))
cache['ABL ' + id] = count
cache['ABL ' + id] = like_count

# Reposts
repost_count = bs_post.get('repostCount')
if fetch_shares and repost_count and repost_count != cache.get('ABRP ' + id):
reposters = self.client.app.bsky.feed.getRepostedBy({}, uri=bs_post.get('uri'))
tags.extend(self._make_share(bs_post, r) for r in reposters.get('repostedBy'))
cache['ABRP ' + id] = count
cache['ABRP ' + id] = repost_count

# Replies
reply_count = bs_post.get('replyCount')
Expand All @@ -1057,7 +1057,7 @@ def get_activities_response(self, user_id=None, group_id=None, app_id=None,
obj['replies'] = {
'items': replies,
}
cache['ABR ' + id] = count
cache['ABR ' + id] = reply_count

resp = self.make_activities_base_response(util.trim_nulls(activities))
return resp
Expand Down
12 changes: 9 additions & 3 deletions granary/tests/test_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,9 +993,10 @@ def test_get_activities_with_likes(self, mock_get):
})
]

cache = {}
self.assert_equals(
[POST_AUTHOR_PROFILE_WITH_LIKES_AS],
self.bs.get_activities(fetch_likes=True)
self.bs.get_activities(fetch_likes=True, cache=cache)
)
mock_get.assert_any_call(
'https://bsky.social/xrpc/app.bsky.feed.getTimeline',
Expand All @@ -1015,6 +1016,7 @@ def test_get_activities_with_likes(self, mock_get):
'User-Agent': util.user_agent,
},
)
self.assert_equals(1, cache.get('ABL at://did/app.bsky.feed.post/tid'))

@patch('requests.get')
def test_get_activities_with_reposts(self, mock_get):
Expand All @@ -1030,9 +1032,10 @@ def test_get_activities_with_reposts(self, mock_get):
})
]

cache = {}
self.assert_equals(
[POST_AUTHOR_PROFILE_WITH_REPOSTS_AS],
self.bs.get_activities(fetch_shares=True)
self.bs.get_activities(fetch_shares=True, cache=cache)
)
mock_get.assert_any_call(
'https://bsky.social/xrpc/app.bsky.feed.getTimeline',
Expand All @@ -1052,6 +1055,7 @@ def test_get_activities_with_reposts(self, mock_get):
'User-Agent': util.user_agent,
},
)
self.assert_equals(1, cache.get('ABRP at://did/app.bsky.feed.post/tid'))

@patch('requests.get')
def test_get_activities_include_shares(self, mock_get):
Expand All @@ -1077,8 +1081,9 @@ def test_get_activities_with_replies(self, mock_get):
})
]

cache = {}
self.assert_equals([THREAD_AS],
self.bs.get_activities(fetch_replies=True))
self.bs.get_activities(fetch_replies=True, cache=cache))
mock_get.assert_any_call(
'https://bsky.social/xrpc/app.bsky.feed.getTimeline',
json=None,
Expand All @@ -1097,6 +1102,7 @@ def test_get_activities_with_replies(self, mock_get):
'User-Agent': util.user_agent,
},
)
self.assert_equals(1, cache.get('ABR at://did/app.bsky.feed.post/tid'))

@patch('requests.get')
def test_get_actor(self, mock_get):
Expand Down