Skip to content

Commit

Permalink
handle responses by blocked users
Browse files Browse the repository at this point in the history
ignore responses in poll, return 410 (deleted) in handlers

for #473
  • Loading branch information
snarfed committed Jul 23, 2017
1 parent 4f8df3c commit a9d181f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def get(self, type, source_short_name, string_id, *ids):
if not obj:
self.abort(404, label)

if self.source.is_blocked(obj):
self.abort(410, 'That user is currently blocked')

# use https for profile pictures so we don't cause SSL mixed mode errors
# when serving over https.
author = obj.get('author', {})
Expand Down
4 changes: 4 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ 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."""
return False


class Webmentions(StringIdModel):
"""A bundle of links to send webmentions for.
Expand Down
5 changes: 5 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ def backfeed(self, source, responses=None, activities=None):
logging.error('Skipping response without id: %s', json.dumps(resp, indent=2))
continue

if source.is_blocked(resp):
logging.info('Skipping response by blocked user: %s',
json.dumps(resp.get('author') or resp.get('actor'), indent=2))
continue

resp.setdefault('activities', []).append(activity)

# when we find two responses with the same id, the earlier one may have
Expand Down
11 changes: 9 additions & 2 deletions test/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# coding=utf-8
"""Unit tests for handlers.py.
"""

import json
import StringIO
import urllib2

from google.appengine.api import urlfetch_errors
import mox

import handlers
import models
import testutil
from testutil import FakeGrSource
from testutil import FakeGrSource, FakeSource


class HandlersTest(testutil.HandlerTest):
Expand Down Expand Up @@ -488,3 +488,10 @@ def test_cache(self):

cached = self.check_response('/post/fake/%s/000')
self.assert_multiline_equals(orig.body, cached.body)

def test_in_blocklist(self):
self.mox.StubOutWithMock(FakeSource, 'is_blocked')
FakeSource.is_blocked(mox.IgnoreArg()).AndReturn(True)
self.mox.ReplayAll()

orig = self.check_response('/comment/fake/%s/000/111', expected_status=410)
18 changes: 18 additions & 0 deletions test/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,24 @@ def _change_response_and_poll(self):
source = self.sources[0].key.get()
self.assert_equals([reply], json.loads(source.seen_responses_cache_json))

def test_in_blocklist(self):
"""Responses from blocked users should be ignored."""
self.mox.StubOutWithMock(FakeSource, 'is_blocked')
FakeSource.is_blocked(mox.IgnoreArg()).AndReturn(False)
FakeSource.is_blocked(mox.IgnoreArg()).AndReturn(True) # block second response
FakeSource.is_blocked(mox.IgnoreArg()).MultipleTimes(10).AndReturn(False)
self.mox.ReplayAll()

self.post_task()
self.assertEqual(11, Response.query().count())
expected = [self.responses[0]] + self.responses[2:]
self.assert_responses(expected)

tasks = self.taskqueue_stub.GetTasks('propagate')
keys = [ndb.Key(urlsafe=testutil.get_task_params(t)['response_key'])
for t in tasks]
self.assert_equals(keys, [r.key for r in expected])


class DiscoverTest(TaskQueueTest):

Expand Down

0 comments on commit a9d181f

Please sign in to comment.