mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
Avoid creating notifications for user performing the action
This commit is contained in:
parent
7d432c5114
commit
3d69807118
5 changed files with 56 additions and 34 deletions
|
@ -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 """
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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 """
|
||||
|
|
|
@ -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 """
|
||||
|
|
Loading…
Reference in a new issue