Skip to content

Commit

Permalink
refactor: eagerly use a set in recipients, get_recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
dato committed Jan 26, 2024
1 parent 31babdf commit 8ac8734
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
25 changes: 13 additions & 12 deletions bookwyrm/models/activitypub_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ def get_recipients(self, software=None) -> list[str]:
# find anyone who's tagged in a status, for example
mentions = self.recipients if hasattr(self, "recipients") else []

# we always send activities to explicitly mentioned users' inboxes
recipients = [u.shared_inbox or u.inbox for u in mentions if not u.local]
# we always send activities to explicitly mentioned users (using shared inboxes
# where available to avoid duplicate submissions to a given instance)
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 All @@ -173,18 +174,18 @@ def get_recipients(self, software=None) -> list[str]:
if user:
queryset = queryset.filter(following=user)

# ideally, we will send to shared inboxes for efficiency
shared_inboxes = (
queryset.filter(shared_inbox__isnull=False)
.values_list("shared_inbox", flat=True)
.distinct()
# as above, we prefer shared inboxes if available
recipients.update(
queryset.filter(shared_inbox__isnull=False).values_list(
"shared_inbox", flat=True
)
)
# but not everyone has a shared inbox
inboxes = queryset.filter(shared_inbox__isnull=True).values_list(
"inbox", flat=True
recipients.update(
queryset.filter(shared_inbox__isnull=True).values_list(
"inbox", flat=True
)
)
recipients += list(shared_inboxes) + list(inboxes)
return list(set(recipients))
return list(recipients)

def to_activity_dataclass(self):
"""convert from a model to an activity"""
Expand Down
6 changes: 3 additions & 3 deletions bookwyrm/models/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ def delete(self, *args, **kwargs): # pylint: disable=unused-argument
@property
def recipients(self):
"""tagged users who definitely need to get this status in broadcast"""
mentions = [u for u in self.mention_users.all() if not u.local]
mentions = {u for u in self.mention_users.all() if not u.local}
if (
hasattr(self, "reply_parent")
and self.reply_parent
and not self.reply_parent.user.local
):
mentions.append(self.reply_parent.user)
return list(set(mentions))
mentions.add(self.reply_parent.user)
return list(mentions)

@classmethod
def ignore_activity(
Expand Down

0 comments on commit 8ac8734

Please sign in to comment.