From 4b66e68232d395b59ebcd06bb37080b4a3ed4e77 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Sat, 15 Jul 2023 14:33:54 -0700 Subject: [PATCH] Web.send: improve logic for skipping sends to followers should stop the unrelated webmentions going to @gRegorLove et al --- tests/test_web.py | 36 +++++++++++++++++++++++++++++------- web.py | 15 +++++---------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/tests/test_web.py b/tests/test_web.py index 5edfdde..339245e 100644 --- a/tests/test_web.py +++ b/tests/test_web.py @@ -1,7 +1,6 @@ # coding=utf-8 """Unit tests for webmention.py.""" import copy -from unittest import skip from unittest.mock import patch from urllib.parse import urlencode @@ -1962,7 +1961,35 @@ class WebUtilTest(TestCase): self.assertFalse(Web.send( Object(id='http://mas.to/note', as2=test_activitypub.NOTE), - 'https://user.com/post')) + 'https://user.com/')) + mock_get.assert_not_called() + mock_post.assert_not_called() + + def test_send_unrelated_repost_does_nothing(self, mock_get, mock_post): + Follower.get_or_create( + to=self.make_user('https://mas.to/bob', cls=ActivityPub), + from_=g.user) + + self.assertFalse(Web.send( + Object(id='http://mas.to/note', as2={ + **test_activitypub.REPOST, + 'actor': 'https://mas.to/bob', + }), + 'https://user.com/')) + mock_get.assert_not_called() + mock_post.assert_not_called() + + def test_send_unrelated_reply_does_nothing(self, mock_get, mock_post): + Follower.get_or_create( + to=self.make_user('https://mas.to/bob', cls=ActivityPub), + from_=g.user) + + self.assertFalse(Web.send( + Object(id='http://mas.to/note', as2={ + **test_activitypub.REPLY, + 'actor': 'https://mas.to/bob', + }), + 'https://user.com/')) mock_get.assert_not_called() mock_post.assert_not_called() @@ -2037,11 +2064,6 @@ class WebUtilTest(TestCase): """, html, ignore_blanks=True) self.assertEqual({'Content-Type': 'text/html; charset=utf-8'}, headers) - @skip - def test_target_for_not_web_fails(self, _, __): - with self.assertRaises(AssertionError): - Web.target_for(Object(id='x', source_protocol='ap')) - def test_target_for(self, _, __): self.assertIsNone(Web.target_for(Object(id='x', source_protocol='web'))) diff --git a/web.py b/web.py index fa6c1ff..102b73c 100644 --- a/web.py +++ b/web.py @@ -276,17 +276,12 @@ class Web(User, Protocol): or if webmention/microformats2 don't support the activity type. https://fed.brid.gy/docs#error-handling """ - # we only send webmentions for responses. for normal posts etc, we just - # update our stored objects (elsewhere) and web users consume them via - # feeds. - type = obj.as1.get('objectType') + # we only send webmentions for responses. for sending normal posts etc + # to followers, we just update our stored objects (elsewhere) and web + # users consume them via feeds. verb = obj.as1.get('verb') - in_reply_to = as1.get_object(obj.as1).get('inReplyTo') - if (type != 'activity' - # TODO: do we need undo for anything? - or verb in ('undo', 'accept') - or (verb in ('post', 'update', 'delete') and not in_reply_to)): - logger.info(f'Skipping sending {type} {verb} activity to {url}') + if verb in ('accept', 'undo') or url not in as1.targets(obj.as1): + logger.info(f'Skipping sending to {url}') return False source_url = obj.proxy_url()