From 3ed25bab818e459bc7623fe50ace56964b0f545d Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sun, 27 Jun 2021 11:21:34 +0200 Subject: [PATCH] Send notifications to event organizer when new comment is posted Signed-off-by: Thomas Citharel --- lib/mobilizon/events/events.ex | 11 +++++ lib/service/activity/comment.ex | 19 ++++++-- lib/service/activity/renderer/comment.ex | 41 +++++++++++++++++ lib/service/notifier/email.ex | 37 +++++++-------- lib/service/notifier/filter.ex | 10 +++- .../workers/legacy_notifier_builder.ex | 14 ++++++ .../activity/_comment_activity_item.html.eex | 33 +++++++++++++ .../activity/_comment_activity_item.text.eex | 46 ++++++------------- test/service/activity/comment_test.exs | 14 ++++-- 9 files changed, 161 insertions(+), 64 deletions(-) diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index 997c41e3..f1a99c06 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -214,6 +214,17 @@ defmodule Mobilizon.Events do |> Repo.exists?() end + @doc """ + Gets an event by its UUID, with all associations loaded. + """ + @spec get_event_by_uuid_with_preload(String.t()) :: Event.t() | nil + def get_event_by_uuid_with_preload(uuid) do + uuid + |> event_by_uuid_query() + |> preload_for_event() + |> Repo.one() + end + @doc """ Gets an event by its UUID, with all associations loaded. """ diff --git a/lib/service/activity/comment.ex b/lib/service/activity/comment.ex index 6d399348..b2d8b406 100644 --- a/lib/service/activity/comment.ex +++ b/lib/service/activity/comment.ex @@ -159,10 +159,11 @@ defmodule Mobilizon.Service.Activity.Comment do %Comment{ actor_id: actor_id, in_reply_to_comment_id: in_reply_to_comment_id, - id: comment_id - }, + id: comment_id, + uuid: comment_uuid + } = comment, %Event{ - uuid: uuid, + uuid: event_uuid, title: title, attributed_to: nil, organizer_actor_id: organizer_actor_id @@ -174,8 +175,10 @@ defmodule Mobilizon.Service.Activity.Comment do "subject" => :event_new_comment, "subject_params" => %{ event_title: title, - event_uuid: uuid, - comment_reply_to: !is_nil(in_reply_to_comment_id) + 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) }, "author_id" => actor_id, "object_id" => to_string(comment_id) @@ -185,4 +188,10 @@ defmodule Mobilizon.Service.Activity.Comment do end defp notify(_, _, _, _), do: {:ok, :skipped} + + @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 end diff --git a/lib/service/activity/renderer/comment.ex b/lib/service/activity/renderer/comment.ex index 6689a7d8..87f62796 100644 --- a/lib/service/activity/renderer/comment.ex +++ b/lib/service/activity/renderer/comment.ex @@ -45,6 +45,35 @@ defmodule Mobilizon.Service.Activity.Renderer.Comment do ), url: event_url(activity) } + + :event_new_comment -> + if activity.subject_params["comment_reply_to"] do + %{ + body: + dgettext( + "activity", + "%{profile} has posted a new reply under your event %{event}.", + %{ + profile: profile, + event: event_title(activity) + } + ), + url: event_comment_url(activity) + } + else + %{ + body: + dgettext( + "activity", + "%{profile} has posted a new comment under your event %{event}.", + %{ + profile: profile, + event: event_title(activity) + } + ), + url: event_comment_url(activity) + } + end end end @@ -56,6 +85,18 @@ defmodule Mobilizon.Service.Activity.Renderer.Comment do ) end + defp event_comment_url(activity) do + "#{event_url(activity)}#comment-#{comment_uuid(activity)}" + end + + defp comment_uuid(activity) do + if activity.subject_params["comment_reply_to"] do + "#{activity.subject_params["reply_to_comment_uuid"]}-#{activity.subject_params["comment_uuid"]}" + else + activity.subject_params["comment_uuid"] + end + end + defp profile(activity), do: Actor.display_name_and_username(activity.author) defp event_title(activity), do: activity.subject_params["event_title"] end diff --git a/lib/service/notifier/email.ex b/lib/service/notifier/email.ex index 5a8b3c7f..30407469 100644 --- a/lib/service/notifier/email.ex +++ b/lib/service/notifier/email.ex @@ -56,7 +56,8 @@ defmodule Mobilizon.Service.Notifier.Email do @always_direct_subjects [ :participation_event_comment, :event_comment_mention, - :discussion_mention + :discussion_mention, + :event_new_comment ] @spec can_send_activity?(Activity.t(), User.t(), Keyword.t()) :: boolean() @@ -96,30 +97,24 @@ defmodule Mobilizon.Service.Notifier.Email do when subject in @always_direct_subjects, do: true + # First notification EVER! + defp match_group_notifications_setting(:one_hour, _, last_notification_sent, _) + when is_nil(last_notification_sent), + do: true + + # Delay ok since last notification + defp match_group_notifications_setting(:one_hour, _, %DateTime{} = last_notification_sent, _) do + is_delay_ok_since_last_notification_sent(last_notification_sent) + end + + # This is a recap defp match_group_notifications_setting( - group_notifications, + _group_notifications, _subject, - last_notification_sent, + _last_notification_sent, options ) do - cond do - # This is a recap - Keyword.get(options, :recap, false) != false -> - true - - # First notification EVER! - group_notifications == :one_hour && is_nil(last_notification_sent) -> - true - - # Delay ok since last notification - group_notifications == :one_hour && - is_delay_ok_since_last_notification_sent(last_notification_sent) -> - true - - # Otherwise, no thanks - true -> - false - end + Keyword.get(options, :recap, false) != false end @default_behavior %{ diff --git a/lib/service/notifier/filter.ex b/lib/service/notifier/filter.ex index 8370247d..c6af546c 100644 --- a/lib/service/notifier/filter.ex +++ b/lib/service/notifier/filter.ex @@ -23,10 +23,13 @@ defmodule Mobilizon.Service.Notifier.Filter do defp enabled?(nil, activity_setting, get_default), do: get_default.(activity_setting) defp enabled?(%ActivitySetting{enabled: enabled}, _activity_setting, _get_default), do: enabled - # Comment mention + # Mention defp map_activity_to_activity_setting(%Activity{subject: :event_comment_mention}), do: "event_comment_mention" + defp map_activity_to_activity_setting(%Activity{subject: :discussion_mention}), + do: "discussion_mention" + # Participation @spec map_activity_to_activity_setting(Activity.t()) :: String.t() | false defp map_activity_to_activity_setting(%Activity{subject: :participation_event_updated}), @@ -42,6 +45,9 @@ defmodule Mobilizon.Service.Notifier.Filter do defp map_activity_to_activity_setting(%Activity{subject: :event_new_participation}), do: "event_new_participation" + defp map_activity_to_activity_setting(%Activity{subject: :event_new_comment}), + do: "event_new_comment" + # Event defp map_activity_to_activity_setting(%Activity{subject: :event_created}), do: "event_created" defp map_activity_to_activity_setting(%Activity{type: :event}), do: "event_updated" @@ -60,7 +66,7 @@ defmodule Mobilizon.Service.Notifier.Filter do defp map_activity_to_activity_setting(%Activity{subject: :member_request}), do: "member_request" - defp map_activity_to_activity_setting(%Activity{type: :member}), do: "member" + defp map_activity_to_activity_setting(%Activity{type: :member}), do: "member_updated" defp map_activity_to_activity_setting(_), do: false end diff --git a/lib/service/workers/legacy_notifier_builder.ex b/lib/service/workers/legacy_notifier_builder.ex index 25018b92..83ebe039 100644 --- a/lib/service/workers/legacy_notifier_builder.ex +++ b/lib/service/workers/legacy_notifier_builder.ex @@ -5,6 +5,8 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do alias Mobilizon.Activities.Activity alias Mobilizon.{Actors, Events, Users} + alias Mobilizon.Actors.Actor + alias Mobilizon.Events.Event alias Mobilizon.Service.Notifier use Mobilizon.Service.Workers.Helper, queue: "activity" @@ -66,6 +68,18 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do |> users_from_actor_ids(Keyword.fetch!(options, :author_id)) end + 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() + |> (fn %Event{organizer_actor: %Actor{id: actor_id}} -> [actor_id] end).() + |> users_from_actor_ids(Keyword.fetch!(options, :author_id)) + end + + defp users_to_notify(_, _), do: [] + @spec users_from_actor_ids(list(), integer() | String.t()) :: list(Users.t()) defp users_from_actor_ids(actor_ids, author_id) do actor_ids diff --git a/lib/web/templates/email/activity/_comment_activity_item.html.eex b/lib/web/templates/email/activity/_comment_activity_item.html.eex index df908b9c..4a9abd54 100644 --- a/lib/web/templates/email/activity/_comment_activity_item.html.eex +++ b/lib/web/templates/email/activity/_comment_activity_item.html.eex @@ -29,4 +29,37 @@ " } ) |> raw %> + + <% :event_new_comment -> %> + <%= if @activity.subject_params["comment_reply_to"] do %> + <%= + dgettext("activity", "%{profile} has posted a new reply under your event %{event}.", + %{ + profile: "#{Mobilizon.Actors.Actor.display_name_and_username(@activity.author)}", + event: " URI.decode()}#comment-#{@activity.subject_params["comment_reply_to_uuid"]}-#{@activity.subject_params["comment_uuid"]}\"> + #{@activity.subject_params["event_title"]} + " + } + ) |> raw %> + <% else %> + <%= + dgettext("activity", "%{profile} has posted a new comment under your event %{event}.", + %{ + profile: "#{Mobilizon.Actors.Actor.display_name_and_username(@activity.author)}", + event: " URI.decode()}#comment-#{@activity.subject_params["comment_uuid"]}\"> + #{@activity.subject_params["event_title"]} + " + } + ) |> raw %> + <% end %> <% end %> \ No newline at end of file diff --git a/lib/web/templates/email/activity/_comment_activity_item.text.eex b/lib/web/templates/email/activity/_comment_activity_item.text.eex index 7742f369..145a3d7c 100644 --- a/lib/web/templates/email/activity/_comment_activity_item.text.eex +++ b/lib/web/templates/email/activity/_comment_activity_item.text.eex @@ -1,34 +1,4 @@ -<%= case @activity.subject do %><% :discussion_created -> %><%= dgettext("activity", "%{profile} created the discussion %{discussion}.", - %{ - profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author), - discussion: @activity.subject_params["discussion_title"] - } -) %> -<%= page_url(Mobilizon.Web.Endpoint, :discussion, Mobilizon.Actors.Actor.preferred_username_and_domain(@activity.group), @activity.subject_params["discussion_slug"]) |> URI.decode() %><% :discussion_replied -> %><%= dgettext("activity", "%{profile} replied to the discussion %{discussion}.", - %{ - profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author), - discussion: @activity.subject_params["discussion_title"] - } -) %> -<%= page_url(Mobilizon.Web.Endpoint, :discussion, Mobilizon.Actors.Actor.preferred_username_and_domain(@activity.group), @activity.subject_params["discussion_slug"]) |> URI.decode() %><% :discussion_renamed -> %><%= dgettext("activity", "%{profile} renamed the discussion %{discussion}.", - %{ - profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author), - discussion: @activity.subject_params["discussion_title"] - } -) %> -<%= page_url(Mobilizon.Web.Endpoint, :discussion, Mobilizon.Actors.Actor.preferred_username_and_domain(@activity.group), @activity.subject_params["discussion_slug"]) |> URI.decode() %><% :discussion_archived -> %><%= dgettext("activity", "%{profile} archived the discussion %{discussion}.", - %{ - profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author), - discussion: @activity.subject_params["discussion_title"] - } -) %> -<%= page_url(Mobilizon.Web.Endpoint, :discussion, Mobilizon.Actors.Actor.preferred_username_and_domain(@activity.group), @activity.subject_params["discussion_slug"]) |> URI.decode() %><% :discussion_deleted -> %><%= dgettext("activity", "%{profile} deleted the discussion %{discussion}.", - %{ - profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author), - discussion: @activity.subject_params["discussion_title"] - } -) %> -<%= page_url(Mobilizon.Web.Endpoint, :discussion, Mobilizon.Actors.Actor.preferred_username_and_domain(@activity.group), @activity.subject_params["discussion_slug"]) |> URI.decode() %><% :event_comment_mention -> %><%= dgettext("activity", "%{profile} mentionned you in a comment under event %{event}.", +<%= case @activity.subject do %><% :event_comment_mention -> %><%= dgettext("activity", "%{profile} mentionned you in a comment under event %{event}.", %{ profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author), event: @activity.subject_params["event_title"] @@ -40,4 +10,16 @@ event: @activity.subject_params["event_title"] } ) %> -<%= page_url(Mobilizon.Web.Endpoint, :event, @activity.subject_params["event_uuid"]) |> URI.decode() %><% end %> \ No newline at end of file +<%= page_url(Mobilizon.Web.Endpoint, :event, @activity.subject_params["event_uuid"]) |> URI.decode() %><% :event_new_comment -> %><%= if @activity.subject_params["comment_reply_to"] do %><%=dgettext("activity", "%{profile} has posted a new reply under your event %{event}.", + %{ + profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author), + event: @activity.subject_params["event_title"] + } +) %> +<%= "#{page_url(Mobilizon.Web.Endpoint, :event, @activity.subject_params["event_uuid"]) |> URI.decode()}#comment-#{@activity.subject_params["comment_reply_to_uuid"]}-#{@activity.subject_params["comment_uuid"]}" %><% else %><%= dgettext("activity", "%{profile} has posted a new comment under your event %{event}.", +%{ + profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author), + event: @activity.subject_params["event_title"] +} +) %> +<%= "#{page_url(Mobilizon.Web.Endpoint, :event, @activity.subject_params["event_uuid"]) |> URI.decode()}#comment-#{@activity.subject_params["comment_uuid"]}"%><% end %><% end %> \ No newline at end of file diff --git a/test/service/activity/comment_test.exs b/test/service/activity/comment_test.exs index 95118a46..5e4ae623 100644 --- a/test/service/activity/comment_test.exs +++ b/test/service/activity/comment_test.exs @@ -18,7 +18,9 @@ defmodule Mobilizon.Service.Activity.CommentTest do describe "handle comment with mentions" do test "with no mentions" do %Event{title: event_title, uuid: event_uuid} = event = insert(:event) - %Comment{id: comment_id, actor_id: author_id} = comment = insert(:comment, event: event) + + %Comment{id: comment_id, actor_id: author_id, uuid: comment_uuid} = + comment = insert(:comment, event: event) assert {:ok, [organizer: :enqueued, announcement: :skipped, mentionned: :skipped]} == CommentActivity.insert_activity(comment) @@ -37,9 +39,11 @@ defmodule Mobilizon.Service.Activity.CommentTest do "op" => "legacy_notify", "subject" => "event_new_comment", "subject_params" => %{ + "comment_reply_to_uuid" => nil, "event_title" => event_title, "event_uuid" => event_uuid, - "comment_reply_to" => false + "comment_reply_to" => false, + "comment_uuid" => comment_uuid }, "type" => "comment" } @@ -51,7 +55,7 @@ defmodule Mobilizon.Service.Activity.CommentTest do %Actor{id: actor_id} = actor = insert(:actor, user: user) %Event{uuid: event_uuid, title: event_title} = event = insert(:event) - %Comment{id: comment_id, actor_id: author_id} = + %Comment{id: comment_id, actor_id: author_id, uuid: comment_uuid} = comment = insert(:comment, text: "Hey @you", event: event) comment = %Comment{ @@ -90,9 +94,11 @@ defmodule Mobilizon.Service.Activity.CommentTest do "op" => "legacy_notify", "subject" => "event_new_comment", "subject_params" => %{ + "comment_reply_to_uuid" => nil, "event_title" => event_title, "event_uuid" => event_uuid, - "comment_reply_to" => false + "comment_reply_to" => false, + "comment_uuid" => comment_uuid }, "type" => "comment" }