Skip to content

Commit

Permalink
add Mastodon.get_blocklist_ids()
Browse files Browse the repository at this point in the history
for #173
  • Loading branch information
snarfed committed Oct 30, 2019
1 parent 2b6b5de commit 4b7aa77
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
25 changes: 24 additions & 1 deletion granary/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

API_ACCOUNT = '/api/v1/accounts/%s'
API_ACCOUNT_STATUSES = '/api/v1/accounts/%s/statuses'
API_BLOCKS = '/api/v1/blocks?limit=1000'
API_CONTEXT = '/api/v1/statuses/%s/context'
API_FAVORITE = '/api/v1/statuses/%s/favourite'
API_FAVORITED_BY = '/api/v1/statuses/%s/favourited_by'
Expand Down Expand Up @@ -109,7 +110,7 @@ def _post(self, *args, **kwargs):
def _delete(self, *args, **kwargs):
return self._api(util.requests_delete, *args, **kwargs)

def _api(self, fn, path, *args, **kwargs):
def _api(self, fn, path, return_json=True, *args, **kwargs):
headers = kwargs.setdefault('headers', {})
headers['Authorization'] = 'Bearer ' + self.access_token

Expand All @@ -121,6 +122,8 @@ def _api(self, fn, path, *args, **kwargs):
util.interpret_http_exception(e)
raise

if not return_json:
return resp
if fn == util.requests_delete:
return {}
else:
Expand Down Expand Up @@ -783,3 +786,23 @@ def preview_delete(self, id):
return source.creation_result(
description='<span class="verb">delete</span> <a href="%s">this toot</a>.' %
self.status_url(id))

def get_blocklist_ids(self):
"""Returns the current user's block list as a list of integer account ids.
May make multiple API calls to fully fetch large block lists.
https://docs.joinmastodon.org/api/rest/blocks/#pagination
Returns:
sequence of integer Mastodon account ids on the current instance
"""
ids = []
url = API_BLOCKS
while True:
resp = self._get(url, return_json=False)
ids.extend(util.trim_nulls([rel.get('id') for rel in json_loads(resp.text)]))
url = resp.links.get('next', {}).get('url')
if not url:
break

return ids
9 changes: 9 additions & 0 deletions granary/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,15 @@ def get_blocklist(self):
"""
raise NotImplementedError()

def get_blocklist_ids(self):
"""Returns the current user's block list as a list of silo-specific user ids.
Returns:
sequence of user ids, not globally unique across other sources. May be
integers or strings.
"""
raise NotImplementedError()

def user_to_actor(self, user):
"""Converts a user to an actor.
Expand Down
25 changes: 25 additions & 0 deletions granary/tests/test_mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from granary.mastodon import (
API_ACCOUNT,
API_ACCOUNT_STATUSES,
API_BLOCKS,
API_CONTEXT,
API_FAVORITE,
API_FAVORITED_BY,
Expand Down Expand Up @@ -759,3 +760,27 @@ def test_preview_delete(self):
self.assertIn('<span class="verb">delete</span> <a href="http://foo.com/web/statuses/456">this toot</a>.', got.description)
self.assertIsNone(got.error_plain)
self.assertIsNone(got.error_html)

def test_get_blocklist_ids(self):
self.expect_get(API_BLOCKS, [
{'id': 1},
{'id': 2},
])
self.mox.ReplayAll()
self.assert_equals([1, 2], self.mastodon.get_blocklist_ids())

def test_get_blocklist_ids_paging(self):
self.expect_get(API_BLOCKS, [
{'id': 1},
{'id': 2},
], response_headers={
'Link': '<http://foo.com/prev>; rel="prev", <http://foo.com/next>; rel="next"',
})
self.expect_get('/next', [
{'id': 3},
{'id': 4},
], response_headers={
'Link': '<http://foo.com/prev>; rel="prev"',
})
self.mox.ReplayAll()
self.assert_equals([1, 2, 3, 4], self.mastodon.get_blocklist_ids())

0 comments on commit 4b7aa77

Please sign in to comment.