Improve notifier filter handing to avoid compiler cycle

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-10-25 13:21:19 +02:00
parent bac6628aea
commit 59edfe26cc
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
3 changed files with 17 additions and 11 deletions

View File

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

View File

@ -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}),

View File

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