Web.send: improve logic for skipping sends to followers

should stop the unrelated webmentions going to @gRegorLove et al
circle
Ryan Barrett 2023-07-15 14:33:54 -07:00
rodzic e0a75b07bc
commit 4b66e68232
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 34 dodań i 17 usunięć

Wyświetl plik

@ -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')))

15
web.py
Wyświetl plik

@ -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()