2021-06-01 16:08:03 +00:00
|
|
|
defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
|
|
|
@moduledoc """
|
|
|
|
Worker to push legacy notifications
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Activities.Activity
|
2021-06-02 09:55:50 +00:00
|
|
|
alias Mobilizon.{Actors, Events, Users}
|
2021-06-27 09:21:34 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-06-27 16:16:34 +00:00
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
2021-06-01 16:08:03 +00:00
|
|
|
alias Mobilizon.Service.Notifier
|
|
|
|
|
|
|
|
use Mobilizon.Service.Workers.Helper, queue: "activity"
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
|
|
|
def perform(%Job{args: args}) do
|
2021-09-24 14:46:42 +00:00
|
|
|
{"legacy_notify", args} = Map.pop(args, "op")
|
|
|
|
activity = build_activity(args)
|
2021-06-01 16:08:03 +00:00
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
if args["subject"] == "participation_event_comment" do
|
|
|
|
notify_anonymous_participants(get_in(args, ["subject_params", "event_id"]), activity)
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
2021-09-24 14:46:42 +00:00
|
|
|
|
|
|
|
args
|
|
|
|
|> users_to_notify(author_id: args["author_id"], group_id: Map.get(args, "group_id"))
|
|
|
|
|> Enum.each(&Notifier.notify(&1, activity, single_activity: true))
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
|
|
|
|
2021-06-02 09:55:50 +00:00
|
|
|
defp build_activity(args) do
|
2021-06-01 16:08:03 +00:00
|
|
|
author = Actors.get_actor(args["author_id"])
|
|
|
|
|
|
|
|
%Activity{
|
|
|
|
type: String.to_existing_atom(args["type"]),
|
|
|
|
subject: String.to_existing_atom(args["subject"]),
|
|
|
|
subject_params: args["subject_params"],
|
|
|
|
inserted_at: DateTime.utc_now(),
|
|
|
|
object_type: String.to_existing_atom(args["object_type"]),
|
|
|
|
object_id: args["object_id"],
|
|
|
|
group: nil,
|
|
|
|
author: author
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-06-23 17:49:10 +00:00
|
|
|
@spec users_to_notify(map(), Keyword.t()) :: list(Users.t())
|
2021-06-01 16:08:03 +00:00
|
|
|
defp users_to_notify(
|
|
|
|
%{"subject" => "event_comment_mention", "mentions" => mentionned_actor_ids},
|
2021-06-23 17:49:10 +00:00
|
|
|
options
|
2021-06-01 16:08:03 +00:00
|
|
|
) do
|
2021-06-23 17:49:10 +00:00
|
|
|
users_from_actor_ids(mentionned_actor_ids, Keyword.fetch!(options, :author_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp users_to_notify(
|
|
|
|
%{"subject" => "discussion_mention", "mentions" => mentionned_actor_ids},
|
|
|
|
options
|
|
|
|
) do
|
|
|
|
mentionned_actor_ids
|
|
|
|
|> Enum.filter(&Actors.is_member?(&1, Keyword.fetch!(options, :group_id)))
|
|
|
|
|> users_from_actor_ids(Keyword.fetch!(options, :author_id))
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp users_to_notify(
|
|
|
|
%{
|
|
|
|
"subject" => "participation_event_comment",
|
|
|
|
"subject_params" => subject_params
|
|
|
|
},
|
2021-06-23 17:49:10 +00:00
|
|
|
options
|
2021-06-01 16:08:03 +00:00
|
|
|
) do
|
|
|
|
subject_params
|
|
|
|
|> Map.get("event_id")
|
|
|
|
|> Events.list_actors_participants_for_event()
|
|
|
|
|> Enum.map(& &1.id)
|
2021-06-23 17:49:10 +00:00
|
|
|
|> users_from_actor_ids(Keyword.fetch!(options, :author_id))
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
|
|
|
|
2021-06-27 09:21:34 +00:00
|
|
|
defp users_to_notify(
|
|
|
|
%{"subject" => "event_new_comment", "subject_params" => %{"event_uuid" => event_uuid}},
|
|
|
|
options
|
|
|
|
) do
|
|
|
|
event_uuid
|
|
|
|
|> Events.get_event_by_uuid_with_preload()
|
2021-11-26 13:30:46 +00:00
|
|
|
|> organizer_actor_id()
|
2021-06-27 09:21:34 +00:00
|
|
|
|> users_from_actor_ids(Keyword.fetch!(options, :author_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp users_to_notify(_, _), do: []
|
|
|
|
|
2021-11-26 13:30:46 +00:00
|
|
|
defp organizer_actor_id(%Event{organizer_actor: %Actor{id: actor_id}}) do
|
|
|
|
[actor_id]
|
|
|
|
end
|
|
|
|
|
2021-06-01 16:08:03 +00:00
|
|
|
@spec users_from_actor_ids(list(), integer() | String.t()) :: list(Users.t())
|
|
|
|
defp users_from_actor_ids(actor_ids, author_id) do
|
|
|
|
actor_ids
|
|
|
|
|> Enum.filter(&(&1 != author_id))
|
|
|
|
|> Enum.map(&Actors.get_actor/1)
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|> Enum.map(& &1.user_id)
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|> Enum.uniq()
|
2021-10-25 11:21:19 +00:00
|
|
|
|> Enum.map(&Users.get_user_with_activity_settings!/1)
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
2021-06-27 16:16:34 +00:00
|
|
|
|
|
|
|
defp notify_anonymous_participants(nil, _activity), do: :ok
|
|
|
|
|
|
|
|
defp notify_anonymous_participants(event_id, activity) do
|
|
|
|
event_id
|
|
|
|
|> Events.list_anonymous_participants_for_event()
|
|
|
|
|> Enum.filter(fn %Participant{metadata: metadata} ->
|
|
|
|
is_map(metadata) && is_binary(metadata.email)
|
|
|
|
end)
|
|
|
|
|> Enum.map(fn %Participant{metadata: metadata} -> metadata end)
|
2021-09-24 14:46:42 +00:00
|
|
|
|> Enum.map(fn %{email: email} = metadata ->
|
|
|
|
Notifier.Email.send_anonymous_activity(email, activity,
|
2021-06-27 16:16:34 +00:00
|
|
|
locale: Map.get(metadata, :locale, "en")
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
end
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|