2021-03-10 16:38:13 +00:00
|
|
|
defmodule Mobilizon.Service.Activity.Comment do
|
|
|
|
@moduledoc """
|
|
|
|
Insert a comment activity
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Actors.Actor
|
2021-06-02 16:37:17 +00:00
|
|
|
alias Mobilizon.{Discussions, Events}
|
2021-03-10 16:38:13 +00:00
|
|
|
alias Mobilizon.Discussions.Comment
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Service.Activity
|
2021-06-01 16:08:03 +00:00
|
|
|
alias Mobilizon.Service.Workers.{ActivityBuilder, LegacyNotifierBuilder}
|
2021-03-10 16:38:13 +00:00
|
|
|
|
2021-06-23 17:49:10 +00:00
|
|
|
import Mobilizon.Service.Activity.Utils, only: [maybe_inserted_at: 0]
|
|
|
|
|
2021-03-10 16:38:13 +00:00
|
|
|
@behaviour Activity
|
|
|
|
|
|
|
|
@impl Activity
|
|
|
|
def insert_activity(comment, options \\ [])
|
|
|
|
|
|
|
|
def insert_activity(
|
|
|
|
%Comment{
|
|
|
|
actor_id: actor_id,
|
2021-06-01 16:08:03 +00:00
|
|
|
event_id: event_id
|
2021-03-10 16:38:13 +00:00
|
|
|
} = comment,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
when not is_nil(actor_id) and not is_nil(event_id) do
|
2021-06-01 16:08:03 +00:00
|
|
|
with {:ok, %Event{} = event} <-
|
|
|
|
Events.get_event_with_preload(event_id) do
|
2021-06-02 16:37:17 +00:00
|
|
|
res =
|
|
|
|
[]
|
|
|
|
# Notify the actors mentionned
|
|
|
|
|> handle_notification(:mentionned, comment, event, options)
|
|
|
|
# Notify participants if there's a new announcement
|
|
|
|
|> handle_notification(:announcement, comment, event, options)
|
|
|
|
# Notify event organizer or group that there's new comments
|
|
|
|
|> handle_notification(:organizer, comment, event, options)
|
2021-06-01 16:08:03 +00:00
|
|
|
|
2021-06-02 16:37:17 +00:00
|
|
|
{:ok, res}
|
2021-03-10 16:38:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def insert_activity(_, _), do: {:ok, nil}
|
2021-03-23 14:18:03 +00:00
|
|
|
|
|
|
|
@impl Activity
|
|
|
|
def get_object(comment_id) do
|
|
|
|
Discussions.get_comment(comment_id)
|
|
|
|
end
|
2021-06-01 16:08:03 +00:00
|
|
|
|
2021-06-02 16:37:17 +00:00
|
|
|
@common_params %{
|
|
|
|
"type" => :comment,
|
|
|
|
"object_type" => :comment
|
|
|
|
}
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec handle_notification(Keyword.t(), notification_type, Comment.t(), Event.t(), Keyword.t()) ::
|
|
|
|
Keyword.t()
|
2021-06-02 16:37:17 +00:00
|
|
|
defp handle_notification(global_res, function, comment, event, options) do
|
2021-09-24 14:46:42 +00:00
|
|
|
{:ok, res} = notify(function, comment, event, options)
|
|
|
|
Keyword.put(global_res, function, res)
|
2021-06-02 16:37:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec legacy_notifier_enqueue(map()) :: :ok
|
|
|
|
defp legacy_notifier_enqueue(args) do
|
|
|
|
LegacyNotifierBuilder.enqueue(
|
|
|
|
:legacy_notify,
|
|
|
|
@common_params |> Map.merge(maybe_inserted_at()) |> Map.merge(args)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@type notification_type :: :mentionned | :announcement | :organizer
|
2021-06-02 16:37:17 +00:00
|
|
|
|
|
|
|
# An actor is mentionned
|
|
|
|
@spec notify(notification_type(), Comment.t(), Event.t(), Keyword.t()) ::
|
2021-09-24 14:46:42 +00:00
|
|
|
{:ok, :enqueued} | {:ok, :skipped}
|
2021-06-02 16:37:17 +00:00
|
|
|
defp notify(
|
|
|
|
:mentionned,
|
|
|
|
%Comment{actor_id: actor_id, id: comment_id, mentions: mentions},
|
|
|
|
%Event{
|
|
|
|
uuid: uuid,
|
|
|
|
title: title
|
|
|
|
},
|
|
|
|
_options
|
|
|
|
)
|
2021-06-01 16:08:03 +00:00
|
|
|
when length(mentions) > 0 do
|
2021-06-02 16:37:17 +00:00
|
|
|
legacy_notifier_enqueue(%{
|
2021-06-01 16:08:03 +00:00
|
|
|
"subject" => :event_comment_mention,
|
|
|
|
"subject_params" => %{
|
|
|
|
event_uuid: uuid,
|
|
|
|
event_title: title
|
|
|
|
},
|
|
|
|
"author_id" => actor_id,
|
|
|
|
"object_id" => to_string(comment_id),
|
|
|
|
"mentions" => Enum.map(mentions, & &1.actor_id)
|
|
|
|
})
|
|
|
|
|
2021-06-02 16:37:17 +00:00
|
|
|
{:ok, :enqueued}
|
|
|
|
end
|
2021-06-01 16:08:03 +00:00
|
|
|
|
2021-06-02 16:37:17 +00:00
|
|
|
# An event has a new announcement, send it to the participants
|
|
|
|
defp notify(
|
|
|
|
:announcement,
|
2021-06-01 16:08:03 +00:00
|
|
|
%Comment{actor_id: actor_id, is_announcement: true, id: comment_id},
|
|
|
|
%Event{
|
|
|
|
id: event_id,
|
|
|
|
uuid: uuid,
|
|
|
|
title: title
|
2021-06-02 16:37:17 +00:00
|
|
|
},
|
|
|
|
_options
|
2021-06-01 16:08:03 +00:00
|
|
|
) do
|
2021-06-02 16:37:17 +00:00
|
|
|
legacy_notifier_enqueue(%{
|
2021-06-01 16:08:03 +00:00
|
|
|
"subject" => :participation_event_comment,
|
|
|
|
"subject_params" => %{
|
|
|
|
event_id: event_id,
|
|
|
|
event_uuid: uuid,
|
|
|
|
event_title: title
|
|
|
|
},
|
|
|
|
"author_id" => actor_id,
|
2021-06-02 16:37:17 +00:00
|
|
|
"object_id" => to_string(comment_id)
|
2021-06-01 16:08:03 +00:00
|
|
|
})
|
|
|
|
|
2021-06-02 16:37:17 +00:00
|
|
|
{:ok, :enqueued}
|
|
|
|
end
|
2021-06-01 16:08:03 +00:00
|
|
|
|
2021-06-02 16:37:17 +00:00
|
|
|
# A group event has a new comment, send it as an activity
|
|
|
|
defp notify(
|
|
|
|
:announcement,
|
2021-06-01 16:08:03 +00:00
|
|
|
%Comment{
|
|
|
|
actor_id: actor_id,
|
|
|
|
in_reply_to_comment_id: in_reply_to_comment_id,
|
|
|
|
id: comment_id
|
|
|
|
},
|
|
|
|
%Event{
|
|
|
|
uuid: uuid,
|
|
|
|
title: title,
|
|
|
|
attributed_to: %Actor{type: :Group, id: group_id}
|
|
|
|
},
|
|
|
|
options
|
|
|
|
) do
|
|
|
|
ActivityBuilder.enqueue(:build_activity, %{
|
|
|
|
"type" => "event",
|
|
|
|
"subject" => Keyword.fetch!(options, :subject),
|
|
|
|
"subject_params" => %{
|
|
|
|
event_title: title,
|
|
|
|
event_uuid: uuid,
|
|
|
|
comment_reply_to: !is_nil(in_reply_to_comment_id)
|
|
|
|
},
|
|
|
|
"group_id" => group_id,
|
|
|
|
"author_id" => actor_id,
|
|
|
|
"object_type" => "comment",
|
|
|
|
"object_id" => to_string(comment_id),
|
|
|
|
"inserted_at" => DateTime.utc_now()
|
|
|
|
})
|
2021-06-02 16:37:17 +00:00
|
|
|
|
|
|
|
{:ok, :enqueued}
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
|
|
|
|
2021-06-02 16:37:17 +00:00
|
|
|
# An event has a new comment, send it to the organizer
|
|
|
|
defp notify(
|
|
|
|
:organizer,
|
2021-06-01 16:08:03 +00:00
|
|
|
%Comment{
|
|
|
|
actor_id: actor_id,
|
|
|
|
in_reply_to_comment_id: in_reply_to_comment_id,
|
2021-06-27 09:21:34 +00:00
|
|
|
id: comment_id,
|
|
|
|
uuid: comment_uuid
|
|
|
|
} = comment,
|
2021-06-01 16:08:03 +00:00
|
|
|
%Event{
|
2021-06-27 09:21:34 +00:00
|
|
|
uuid: event_uuid,
|
2021-06-01 16:08:03 +00:00
|
|
|
title: title,
|
|
|
|
attributed_to: nil,
|
|
|
|
organizer_actor_id: organizer_actor_id
|
|
|
|
},
|
|
|
|
_options
|
|
|
|
)
|
|
|
|
when actor_id !== organizer_actor_id do
|
2021-06-02 16:37:17 +00:00
|
|
|
legacy_notifier_enqueue(%{
|
2021-06-01 16:08:03 +00:00
|
|
|
"subject" => :event_new_comment,
|
|
|
|
"subject_params" => %{
|
|
|
|
event_title: title,
|
2021-06-27 09:21:34 +00:00
|
|
|
event_uuid: event_uuid,
|
|
|
|
comment_reply_to: !is_nil(in_reply_to_comment_id),
|
|
|
|
comment_uuid: comment_uuid,
|
|
|
|
comment_reply_to_uuid: reply_to_comment_uuid(comment)
|
2021-06-01 16:08:03 +00:00
|
|
|
},
|
|
|
|
"author_id" => actor_id,
|
2021-06-02 16:37:17 +00:00
|
|
|
"object_id" => to_string(comment_id)
|
2021-06-01 16:08:03 +00:00
|
|
|
})
|
2021-06-02 16:37:17 +00:00
|
|
|
|
|
|
|
{:ok, :enqueued}
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
|
|
|
|
2021-06-02 16:37:17 +00:00
|
|
|
defp notify(_, _, _, _), do: {:ok, :skipped}
|
2021-06-27 09:21:34 +00:00
|
|
|
|
|
|
|
@spec reply_to_comment_uuid(Comment.t()) :: String.t() | nil
|
|
|
|
defp reply_to_comment_uuid(%Comment{in_reply_to_comment: %Comment{uuid: comment_reply_to_uuid}}),
|
|
|
|
do: comment_reply_to_uuid
|
|
|
|
|
|
|
|
defp reply_to_comment_uuid(%Comment{in_reply_to_comment: _}), do: nil
|
2021-03-10 16:38:13 +00:00
|
|
|
end
|