Skip to content

Commit

Permalink
Twitter: implement is_blocked(), for #473
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jul 23, 2017
1 parent a9d181f commit 38aaab6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,11 @@ def is_beta_user(self):
"""
return self.bridgy_path() in util.BETA_USER_PATHS

def is_blocked(self, actor):
"""Returns True if an actor is being blocked, ie in this user's block list."""
def is_blocked(self, obj):
"""Returns True if an object's author is being blocked.
...ie they're in this user's block list.
"""
return False


Expand Down
11 changes: 11 additions & 0 deletions test/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,14 @@ def test_is_private(self):

def test_gr_source_username(self):
self.assertEqual('snarfed_org', self.tw.gr_source.username)

def test_is_blocked(self):
self.mox.StubOutWithMock(self.tw.gr_source, 'get_blocklist_ids')
self.tw.gr_source.get_blocklist_ids().MultipleTimes().AndReturn(['1', '2'])
self.mox.ReplayAll()

self.assertTrue(self.tw.is_blocked({'author': {'numeric_id': '1'}}))
self.assertTrue(self.tw.is_blocked({'object': {'actor': {'numeric_id': '2'}}}))
self.assertFalse(self.tw.is_blocked({'actor': {'numeric_id': '3'}}))
self.assertFalse(self.tw.is_blocked({'author': {'id': '0'}}))
self.assertFalse(self.tw.is_blocked({'actor': {'username': 'foo'}}))
11 changes: 11 additions & 0 deletions twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ def canonicalize_url(self, url, activity=None, **kwargs):
url = url.replace('/statuses/', '/status/')
return super(Twitter, self).canonicalize_url(url, **kwargs)

def is_blocked(self, obj):
"""Returns True if an object's author is being blocked.
...ie they're in this user's block list."""
blocked_ids = self.gr_source.get_blocklist_ids()

for o in obj, obj.get('object', {}):
for field in 'author', 'actor':
if o.get(field, {}).get('numeric_id') in blocked_ids:
return True


class AuthHandler(util.Handler):
"""Base OAuth handler class."""
Expand Down

0 comments on commit 38aaab6

Please sign in to comment.