From 59edfe26cc0de8ae9039a8d6a4356eacdd2a3150 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 25 Oct 2021 13:21:19 +0200 Subject: [PATCH] Improve notifier filter handing to avoid compiler cycle Signed-off-by: Thomas Citharel --- lib/mobilizon/users/users.ex | 12 ++++++------ lib/service/notifier/filter.ex | 14 ++++++++++---- lib/service/workers/legacy_notifier_builder.ex | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/mobilizon/users/users.ex b/lib/mobilizon/users/users.ex index 1337d04dc..0e31514ff 100644 --- a/lib/mobilizon/users/users.ex +++ b/lib/mobilizon/users/users.ex @@ -72,6 +72,12 @@ defmodule Mobilizon.Users do |> Repo.preload([:settings]) end + def get_user_with_activity_settings!(id) do + User + |> Repo.get(id) + |> Repo.preload([:settings, :activity_settings]) + end + @doc """ Gets an user by its email. """ @@ -430,12 +436,6 @@ defmodule Mobilizon.Users do |> Repo.all() end - def activity_setting(%User{id: user_id}, key, method) do - ActivitySetting - |> where([a], a.user_id == ^user_id and a.key == ^key and a.method == ^method) - |> Repo.one() - end - @doc """ Creates an activity setting. Overrides existing values if present diff --git a/lib/service/notifier/filter.ex b/lib/service/notifier/filter.ex index c6af546c2..f794b77f3 100644 --- a/lib/service/notifier/filter.ex +++ b/lib/service/notifier/filter.ex @@ -3,25 +3,31 @@ defmodule Mobilizon.Service.Notifier.Filter do Module to filter activities to notify according to user's activity settings """ alias Mobilizon.Activities.Activity - alias Mobilizon.Users alias Mobilizon.Users.{ActivitySetting, User} @type method :: String.t() @spec can_send_activity?(Activity.t(), method(), User.t(), function()) :: boolean() - def can_send_activity?(%Activity{} = activity, method, %User{} = user, get_default) do + def can_send_activity?( + %Activity{} = activity, + method, + %User{activity_settings: activity_settings}, + get_default + ) do case map_activity_to_activity_setting(activity) do false -> false key when is_binary(key) -> - user |> Users.activity_setting(key, method) |> enabled?(key, get_default) + activity_settings + |> Enum.find(&(&1.key == key && &1.method == method)) + |> enabled?(key, get_default) end end @spec enabled?(ActivitySetting.t() | nil, String.t(), function()) :: boolean() defp enabled?(nil, activity_setting, get_default), do: get_default.(activity_setting) - defp enabled?(%ActivitySetting{enabled: enabled}, _activity_setting, _get_default), do: enabled + defp enabled?(%{enabled: enabled}, _activity_setting, _get_default), do: enabled # Mention defp map_activity_to_activity_setting(%Activity{subject: :event_comment_mention}), diff --git a/lib/service/workers/legacy_notifier_builder.ex b/lib/service/workers/legacy_notifier_builder.ex index 86b8bafc4..8fe29e3ef 100644 --- a/lib/service/workers/legacy_notifier_builder.ex +++ b/lib/service/workers/legacy_notifier_builder.ex @@ -92,7 +92,7 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do |> Enum.map(& &1.user_id) |> Enum.filter(& &1) |> Enum.uniq() - |> Enum.map(&Users.get_user_with_settings!/1) + |> Enum.map(&Users.get_user_with_activity_settings!/1) end defp notify_anonymous_participants(nil, _activity), do: :ok