1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-22 07:52:43 +00:00

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]) |> Repo.preload([:settings])
end end
def get_user_with_activity_settings!(id) do
User
|> Repo.get(id)
|> Repo.preload([:settings, :activity_settings])
end
@doc """ @doc """
Gets an user by its email. Gets an user by its email.
""" """
@ -430,12 +436,6 @@ defmodule Mobilizon.Users do
|> Repo.all() |> Repo.all()
end 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 """ @doc """
Creates an activity setting. Overrides existing values if present 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 Module to filter activities to notify according to user's activity settings
""" """
alias Mobilizon.Activities.Activity alias Mobilizon.Activities.Activity
alias Mobilizon.Users
alias Mobilizon.Users.{ActivitySetting, User} alias Mobilizon.Users.{ActivitySetting, User}
@type method :: String.t() @type method :: String.t()
@spec can_send_activity?(Activity.t(), method(), User.t(), function()) :: boolean() @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 case map_activity_to_activity_setting(activity) do
false -> false ->
false false
key when is_binary(key) -> 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
end end
@spec enabled?(ActivitySetting.t() | nil, String.t(), function()) :: boolean() @spec enabled?(ActivitySetting.t() | nil, String.t(), function()) :: boolean()
defp enabled?(nil, activity_setting, get_default), do: get_default.(activity_setting) 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 # Mention
defp map_activity_to_activity_setting(%Activity{subject: :event_comment_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.map(& &1.user_id)
|> Enum.filter(& &1) |> Enum.filter(& &1)
|> Enum.uniq() |> Enum.uniq()
|> Enum.map(&Users.get_user_with_settings!/1) |> Enum.map(&Users.get_user_with_activity_settings!/1)
end end
defp notify_anonymous_participants(nil, _activity), do: :ok defp notify_anonymous_participants(nil, _activity), do: :ok