90
lib/service/activity/conversation.ex
Normal file
90
lib/service/activity/conversation.ex
Normal file
@@ -0,0 +1,90 @@
|
||||
defmodule Mobilizon.Service.Activity.Conversation do
|
||||
@moduledoc """
|
||||
Insert a conversation activity
|
||||
"""
|
||||
alias Mobilizon.Conversations
|
||||
alias Mobilizon.Conversations.{Conversation, ConversationParticipant}
|
||||
alias Mobilizon.Discussions.Comment
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Service.Activity
|
||||
alias Mobilizon.Service.Workers.LegacyNotifierBuilder
|
||||
|
||||
@behaviour Activity
|
||||
|
||||
@impl Activity
|
||||
def insert_activity(conversation, options \\ [])
|
||||
|
||||
def insert_activity(
|
||||
%Conversation{} = conversation,
|
||||
options
|
||||
) do
|
||||
subject = Keyword.fetch!(options, :subject)
|
||||
|
||||
send_participant_notifications(subject, conversation, conversation.last_comment, options)
|
||||
end
|
||||
|
||||
def insert_activity(_, _), do: {:ok, nil}
|
||||
|
||||
@impl Activity
|
||||
def get_object(conversation_id) do
|
||||
Conversations.get_conversation(conversation_id)
|
||||
end
|
||||
|
||||
# An actor is mentionned
|
||||
@spec send_participant_notifications(String.t(), Discussion.t(), Comment.t(), Keyword.t()) ::
|
||||
{:ok, Oban.Job.t()} | {:ok, :skipped}
|
||||
defp send_participant_notifications(
|
||||
subject,
|
||||
%Conversation{
|
||||
id: conversation_id
|
||||
} = conversation,
|
||||
%Comment{actor_id: actor_id},
|
||||
_options
|
||||
)
|
||||
when subject in [
|
||||
"conversation_created",
|
||||
"conversation_replied",
|
||||
"conversation_event_announcement"
|
||||
] do
|
||||
# We need to send each notification individually as the conversation URL varies for each participant
|
||||
|
||||
conversation_id
|
||||
|> Conversations.list_conversation_participants_for_conversation()
|
||||
|> Enum.each(fn %ConversationParticipant{id: conversation_participant_id} =
|
||||
conversation_participant ->
|
||||
LegacyNotifierBuilder.enqueue(
|
||||
:legacy_notify,
|
||||
%{
|
||||
"subject" => subject,
|
||||
"subject_params" =>
|
||||
Map.merge(
|
||||
%{
|
||||
conversation_id: conversation_id,
|
||||
conversation_participant_id: conversation_participant_id
|
||||
},
|
||||
event_subject_params(conversation)
|
||||
),
|
||||
"type" => :conversation,
|
||||
"object_type" => :conversation,
|
||||
"author_id" => actor_id,
|
||||
"object_id" => to_string(conversation_id),
|
||||
"participant" => Map.take(conversation_participant, [:id, :actor_id])
|
||||
}
|
||||
)
|
||||
end)
|
||||
|
||||
{:ok, :enqueued}
|
||||
end
|
||||
|
||||
defp send_participant_notifications(_, _, _, _), do: {:ok, :skipped}
|
||||
|
||||
defp event_subject_params(%Conversation{
|
||||
event: %Event{id: conversation_event_id, title: conversation_event_title}
|
||||
}),
|
||||
do: %{
|
||||
conversation_event_id: conversation_event_id,
|
||||
conversation_event_title: conversation_event_title
|
||||
}
|
||||
|
||||
defp event_subject_params(_), do: %{}
|
||||
end
|
||||
73
lib/service/activity/renderer/conversation.ex
Normal file
73
lib/service/activity/renderer/conversation.ex
Normal file
@@ -0,0 +1,73 @@
|
||||
defmodule Mobilizon.Service.Activity.Renderer.Conversation do
|
||||
@moduledoc """
|
||||
Render a conversation activity
|
||||
"""
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Activity.Renderer
|
||||
alias Mobilizon.Web.Endpoint
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@behaviour Renderer
|
||||
|
||||
@impl Renderer
|
||||
def render(%Activity{} = activity, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
profile = profile(activity)
|
||||
|
||||
case activity.subject do
|
||||
:conversation_created ->
|
||||
%{
|
||||
body:
|
||||
dgettext(
|
||||
"activity",
|
||||
"%{profile} sent you a message",
|
||||
%{
|
||||
profile: profile
|
||||
}
|
||||
),
|
||||
url: conversation_url(activity)
|
||||
}
|
||||
|
||||
:conversation_replied ->
|
||||
%{
|
||||
body:
|
||||
dgettext(
|
||||
"activity",
|
||||
"%{profile} replied to your message",
|
||||
%{
|
||||
profile: profile
|
||||
}
|
||||
),
|
||||
url: conversation_url(activity)
|
||||
}
|
||||
|
||||
:conversation_event_announcement ->
|
||||
%{
|
||||
body:
|
||||
dgettext(
|
||||
"activity",
|
||||
"%{profile} sent a private message about event %{event}",
|
||||
%{
|
||||
profile: profile,
|
||||
event: event_title(activity)
|
||||
}
|
||||
),
|
||||
url: conversation_url(activity)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
defp conversation_url(activity) do
|
||||
Routes.page_url(
|
||||
Endpoint,
|
||||
:conversation,
|
||||
activity.subject_params["conversation_id"]
|
||||
)
|
||||
end
|
||||
|
||||
defp profile(activity), do: Actor.display_name_and_username(activity.author)
|
||||
defp event_title(activity), do: activity.subject_params["conversation_event_title"]
|
||||
end
|
||||
@@ -51,17 +51,25 @@ defmodule Mobilizon.Service.Activity.Renderer do
|
||||
res
|
||||
end
|
||||
|
||||
@types_map %{
|
||||
discussion: Discussion,
|
||||
conversation: Conversation,
|
||||
event: Event,
|
||||
group: Group,
|
||||
member: Member,
|
||||
post: Post,
|
||||
resource: Resource,
|
||||
comment: Comment
|
||||
}
|
||||
|
||||
@spec do_render(Activity.t(), Keyword.t()) :: common_render()
|
||||
defp do_render(%Activity{type: type} = activity, options) do
|
||||
case type do
|
||||
:discussion -> Discussion.render(activity, options)
|
||||
:event -> Event.render(activity, options)
|
||||
:group -> Group.render(activity, options)
|
||||
:member -> Member.render(activity, options)
|
||||
:post -> Post.render(activity, options)
|
||||
:resource -> Resource.render(activity, options)
|
||||
:comment -> Comment.render(activity, options)
|
||||
_ -> nil
|
||||
case Map.get(@types_map, type) do
|
||||
nil ->
|
||||
nil
|
||||
|
||||
mod ->
|
||||
mod.render(activity, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -70,6 +70,9 @@ defmodule Mobilizon.Service.Notifier.Email do
|
||||
@always_direct_subjects [
|
||||
:participation_event_comment,
|
||||
:event_comment_mention,
|
||||
:conversation_mention,
|
||||
:conversation_created,
|
||||
:conversation_replied,
|
||||
:discussion_mention,
|
||||
:event_new_comment
|
||||
]
|
||||
@@ -175,6 +178,9 @@ defmodule Mobilizon.Service.Notifier.Email do
|
||||
"member_updated" => false,
|
||||
"user_email_password_updated" => true,
|
||||
"event_comment_mention" => true,
|
||||
"conversation_mention" => true,
|
||||
"conversation_created" => true,
|
||||
"conversation_replied" => true,
|
||||
"discussion_mention" => true,
|
||||
"event_new_comment" => true
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ defmodule Mobilizon.Service.Notifier.Filter do
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :event_comment_mention}),
|
||||
do: "event_comment_mention"
|
||||
|
||||
defp map_activity_to_activity_setting(%Activity{subject: subject})
|
||||
when subject in [:conversation_mention, :conversation_created, :conversation_replied],
|
||||
do: to_string(subject)
|
||||
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :discussion_mention}),
|
||||
do: "discussion_mention"
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ defmodule Mobilizon.Service.Notifier.Push do
|
||||
"member_updated" => false,
|
||||
"user_email_password_updated" => false,
|
||||
"event_comment_mention" => true,
|
||||
"conversation_mention" => true,
|
||||
"discussion_mention" => false,
|
||||
"event_new_comment" => false
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events.{Event, Participant}
|
||||
alias Mobilizon.Service.Notifier
|
||||
require Logger
|
||||
|
||||
use Mobilizon.Service.Workers.Helper, queue: "activity"
|
||||
|
||||
@@ -15,6 +16,7 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
def perform(%Job{args: args}) do
|
||||
{"legacy_notify", args} = Map.pop(args, "op")
|
||||
activity = build_activity(args)
|
||||
Logger.debug("Handling activity #{activity.subject} to notify in LegacyNotifierBuilder")
|
||||
|
||||
if args["subject"] == "participation_event_comment" do
|
||||
notify_anonymous_participants(get_in(args, ["subject_params", "event_id"]), activity)
|
||||
@@ -22,7 +24,7 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
|
||||
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))
|
||||
|> Enum.each(¬ify_user(&1, activity))
|
||||
end
|
||||
|
||||
defp build_activity(args) do
|
||||
@@ -48,6 +50,15 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
users_from_actor_ids(mentionned_actor_ids, Keyword.fetch!(options, :author_id))
|
||||
end
|
||||
|
||||
@spec users_to_notify(map(), Keyword.t()) :: list(Users.t())
|
||||
defp users_to_notify(
|
||||
%{"subject" => subject, "participant" => %{"actor_id" => actor_id}},
|
||||
options
|
||||
)
|
||||
when subject in ["conversation_created", "conversation_replied"] do
|
||||
users_from_actor_ids([actor_id], Keyword.fetch!(options, :author_id))
|
||||
end
|
||||
|
||||
defp users_to_notify(
|
||||
%{"subject" => "discussion_mention", "mentions" => mentionned_actor_ids},
|
||||
options
|
||||
@@ -114,4 +125,9 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
)
|
||||
end)
|
||||
end
|
||||
|
||||
defp notify_user(user, activity) do
|
||||
Logger.debug("Notifying #{user.email} for activity #{activity.subject}")
|
||||
Notifier.notify(user, activity, single_activity: true)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user