From 3d69807118d523e9ba8b15ebb6b5091037bd4fe9 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 29 Jul 2024 19:13:08 -0400 Subject: [PATCH] Avoid creating notifications for user performing the action --- lib/philomena/comments.ex | 12 ++++------ lib/philomena/notifications.ex | 27 ++++++++++++++-------- lib/philomena/notifications/creator.ex | 32 ++++++++++++++++++-------- lib/philomena/posts.ex | 12 ++++------ lib/philomena/topics.ex | 7 ++++-- 5 files changed, 56 insertions(+), 34 deletions(-) diff --git a/lib/philomena/comments.ex b/lib/philomena/comments.ex index 281de6c0..4498dca8 100644 --- a/lib/philomena/comments.ex +++ b/lib/philomena/comments.ex @@ -72,14 +72,12 @@ defmodule Philomena.Comments do end def perform_notify(comment_id) do - comment = get_comment!(comment_id) + comment = + comment_id + |> get_comment!() + |> Repo.preload([:user, :image]) - image = - comment - |> Repo.preload(:image) - |> Map.fetch!(:image) - - Notifications.create_image_comment_notification(image, comment) + Notifications.create_image_comment_notification(comment.user, comment.image, comment) end @doc """ diff --git a/lib/philomena/notifications.ex b/lib/philomena/notifications.ex index cbed7413..f441c24f 100644 --- a/lib/philomena/notifications.ex +++ b/lib/philomena/notifications.ex @@ -78,7 +78,7 @@ defmodule Philomena.Notifications do """ def create_channel_live_notification(channel) do - Creator.create_single(ChannelSubscription, ChannelLiveNotification, :channel_id, channel) + Creator.create_single(ChannelSubscription, ChannelLiveNotification, nil, :channel_id, channel) end @doc """ @@ -86,14 +86,15 @@ defmodule Philomena.Notifications do ## Examples - iex> create_forum_post_notification(topic, post) + iex> create_forum_post_notification(user, topic, post) {:ok, 2} """ - def create_forum_post_notification(topic, post) do + def create_forum_post_notification(user, topic, post) do Creator.create_double( TopicSubscription, ForumPostNotification, + user, :topic_id, topic, :post_id, @@ -106,12 +107,12 @@ defmodule Philomena.Notifications do ## Examples - iex> create_forum_topic_notification(topic) + iex> create_forum_topic_notification(user, topic) {:ok, 2} """ - def create_forum_topic_notification(topic) do - Creator.create_single(ForumSubscription, ForumTopicNotification, :topic_id, topic) + def create_forum_topic_notification(user, topic) do + Creator.create_single(ForumSubscription, ForumTopicNotification, user, :topic_id, topic) end @doc """ @@ -124,7 +125,13 @@ defmodule Philomena.Notifications do """ def create_gallery_image_notification(gallery) do - Creator.create_single(GallerySubscription, GalleryImageNotification, :gallery_id, gallery) + Creator.create_single( + GallerySubscription, + GalleryImageNotification, + nil, + :gallery_id, + gallery + ) end @doc """ @@ -132,14 +139,15 @@ defmodule Philomena.Notifications do ## Examples - iex> create_image_comment_notification(image, comment) + iex> create_image_comment_notification(user, image, comment) {:ok, 2} """ - def create_image_comment_notification(image, comment) do + def create_image_comment_notification(user, image, comment) do Creator.create_double( ImageSubscription, ImageCommentNotification, + user, :image_id, image, :comment_id, @@ -160,6 +168,7 @@ defmodule Philomena.Notifications do Creator.create_double( ImageSubscription, ImageMergeNotification, + nil, :target_id, target, :source_id, diff --git a/lib/philomena/notifications/creator.ex b/lib/philomena/notifications/creator.ex index 5a04b724..95dd25cd 100644 --- a/lib/philomena/notifications/creator.ex +++ b/lib/philomena/notifications/creator.ex @@ -22,13 +22,13 @@ defmodule Philomena.Notifications.Creator do ## Example - iex> create_single(GallerySubscription, GalleryImageNotification, :gallery_id, gallery) + iex> create_single(GallerySubscription, GalleryImageNotification, nil, :gallery_id, gallery) {:ok, 2} """ - def create_single(subscription, notification, name, object) do + def create_single(subscription, notification, user, name, object) do subscription - |> create_notification_query(name, object) + |> create_notification_query(user, name, object) |> create_notification(notification, name) end @@ -45,6 +45,7 @@ defmodule Philomena.Notifications.Creator do iex> create_double( ...> ImageSubscription, ...> ImageCommentNotification, + ...> user, ...> :image_id, ...> image, ...> :comment_id, @@ -53,9 +54,9 @@ defmodule Philomena.Notifications.Creator do {:ok, 2} """ - def create_double(subscription, notification, name1, object1, name2, object2) do + def create_double(subscription, notification, user, name1, object1, name2, object2) do subscription - |> create_notification_query(name1, object1, name2, object2) + |> create_notification_query(user, name1, object1, name2, object2) |> create_notification(notification, name1) end @@ -80,10 +81,10 @@ defmodule Philomena.Notifications.Creator do # TODO: the following cannot be accomplished with a single query expression # due to this Ecto bug: https://github.com/elixir-ecto/ecto/issues/4430 - defp create_notification_query(subscription, name, object) do + defp create_notification_query(subscription, user, name, object) do now = DateTime.utc_now(:second) - from s in subscription, + from s in subscription_query(subscription, user), where: field(s, ^name) == ^object.id, select: %{ ^name => type(^object.id, :integer), @@ -94,10 +95,10 @@ defmodule Philomena.Notifications.Creator do } end - defp create_notification_query(subscription, name1, object1, name2, object2) do + defp create_notification_query(subscription, user, name1, object1, name2, object2) do now = DateTime.utc_now(:second) - from s in subscription, + from s in subscription_query(subscription, user), where: field(s, ^name1) == ^object1.id, select: %{ ^name1 => type(^object1.id, :integer), @@ -109,6 +110,19 @@ defmodule Philomena.Notifications.Creator do } end + defp subscription_query(subscription, user) do + case user do + %{id: user_id} -> + # Avoid sending notifications to the user which performed the action. + from s in subscription, + where: s.user_id != ^user_id + + _ -> + # When not created by a user, send notifications to all subscribers. + subscription + end + end + defp create_notification(query, notification, name) do {count, nil} = Repo.insert_all( diff --git a/lib/philomena/posts.ex b/lib/philomena/posts.ex index 2de6cfbb..af29c2e3 100644 --- a/lib/philomena/posts.ex +++ b/lib/philomena/posts.ex @@ -121,14 +121,12 @@ defmodule Philomena.Posts do end def perform_notify(post_id) do - post = get_post!(post_id) + post = + post_id + |> get_post!() + |> Repo.preload([:user, :topic]) - topic = - post - |> Repo.preload(:topic) - |> Map.fetch!(:topic) - - Notifications.create_forum_post_notification(topic, post) + Notifications.create_forum_post_notification(post.user, post.topic, post) end @doc """ diff --git a/lib/philomena/topics.ex b/lib/philomena/topics.ex index 3ff4aba5..02524ea3 100644 --- a/lib/philomena/topics.ex +++ b/lib/philomena/topics.ex @@ -91,9 +91,12 @@ defmodule Philomena.Topics do end def perform_notify([topic_id, _post_id]) do - topic = get_topic!(topic_id) + topic = + topic_id + |> get_topic!() + |> Repo.preload(:user) - Notifications.create_forum_topic_notification(topic) + Notifications.create_forum_topic_notification(topic.user, topic) end @doc """