Skip to content

Commit

Permalink
Always prefer shared inboxes when computing receipent lists
Browse files Browse the repository at this point in the history
This avoids duplicate submissions to remote instances when mentioning
followers (i.e., `POST /user/foo/inbox` followed by `POST /inbox`, which
results in two separate `add_status` tasks, and might generate duplicates
in the target instance).
  • Loading branch information
dato committed Jan 26, 2024
1 parent 193aeff commit 31babdf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bookwyrm/models/activitypub_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def get_recipients(self, software=None) -> list[str]:
mentions = self.recipients if hasattr(self, "recipients") else []

# we always send activities to explicitly mentioned users' inboxes
recipients = [u.inbox for u in mentions or [] if not u.local]
recipients = [u.shared_inbox or u.inbox for u in mentions if not u.local]

# unless it's a dm, all the followers should receive the activity
if privacy != "direct":
Expand Down
12 changes: 8 additions & 4 deletions bookwyrm/tests/models/test_activitypub_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,18 @@ def test_get_recipients_combine_inboxes(self, *_):
shared_inbox="http://example.com/inbox",
outbox="https://example.com/users/nutria/outbox",
)
MockSelf = namedtuple("Self", ("privacy", "user"))
mock_self = MockSelf("public", self.local_user)
MockSelf = namedtuple("Self", ("privacy", "user", "recipients"))
self.local_user.followers.add(self.remote_user)
self.local_user.followers.add(another_remote_user)

mock_self = MockSelf("public", self.local_user, [])
recipients = ActivitypubMixin.get_recipients(mock_self)
self.assertEqual(len(recipients), 1)
self.assertEqual(recipients[0], "http://example.com/inbox")
self.assertCountEqual(recipients, ["http://example.com/inbox"])

# should also work with recipient that is a follower
mock_self.recipients.append(another_remote_user)
recipients = ActivitypubMixin.get_recipients(mock_self)
self.assertCountEqual(recipients, ["http://example.com/inbox"])

def test_get_recipients_software(self, *_):
"""should differentiate between bookwyrm and other remote users"""
Expand Down

0 comments on commit 31babdf

Please sign in to comment.