8000 Fix N+1 recipient user fetches by mwoods79 · Pull Request #139 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/huddlz/communities/huddl/changes/recipient_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ defmodule Huddlz.Communities.Huddl.Changes.RecipientHelpers do
end

@doc """
Fan a notification trigger out to a list of user_ids, fetching each
user with `authorize?: false` and skipping any that no longer exist
Fan a notification trigger out to a list of user_ids, fetching users
with `authorize?: false` and skipping any that no longer exist
(e.g. raced deletion). Used by the C/E-series fanout notifiers.
"""
@spec deliver_each([Ecto.UUID.t()], atom(), map()) :: :ok
def deliver_each([], _trigger, _payload), do: :ok

def deliver_each(user_ids, trigger, payload) do
for user_id <- user_ids do
case Ash.get(User, user_id, authorize?: false) do
{:ok, user} -> Notifications.deliver_async(user, trigger, payload)
_ -> :noop
end
end
User
|> Ash.Query.filter(id in ^user_ids)
|> Ash.read!(authorize?: false)
|> Enum.each(&Notifications.deliver_async(&1, trigger, payload))

:ok
end
Expand Down
3 changes: 3 additions & 0 deletions test/huddlz/notifications/senders/huddl_reminder_1h_test.exs
Original file line number Diff line numbe 6B1F r Diff line change
Expand Up @@ -22,6 +22,9 @@ defmodule Huddlz.Notifications.Senders.HuddlReminder1hTest do
title: attrs[:title] || "Morning Standup",
group_id: group.id,
creator_id: owner.id,
event_type: :in_person,
physical_location: "123 Main St, Anytown, USA",
virtual_link: nil,
actor: owner
]

Expand Down
Loading
0