mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-19 20:04:23 +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
|
end
|
||||||
|
|
||||||
def perform_notify(comment_id) do
|
def perform_notify(comment_id) do
|
||||||
comment = get_comment!(comment_id)
|
comment =
|
||||||
|
comment_id
|
||||||
|
|> get_comment!()
|
||||||
|
|> Repo.preload([:user, :image])
|
||||||
|
|
||||||
image =
|
Notifications.create_image_comment_notification(comment.user, comment.image, comment)
|
||||||
comment
|
|
||||||
|> Repo.preload(:image)
|
|
||||||
|> Map.fetch!(:image)
|
|
||||||
|
|
||||||
Notifications.create_image_comment_notification(image, comment)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -78,7 +78,7 @@ defmodule Philomena.Notifications do
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_channel_live_notification(channel) 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
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -86,14 +86,15 @@ defmodule Philomena.Notifications do
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> create_forum_post_notification(topic, post)
|
iex> create_forum_post_notification(user, topic, post)
|
||||||
{:ok, 2}
|
{:ok, 2}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_forum_post_notification(topic, post) do
|
def create_forum_post_notification(user, topic, post) do
|
||||||
Creator.create_double(
|
Creator.create_double(
|
||||||
TopicSubscription,
|
TopicSubscription,
|
||||||
ForumPostNotification,
|
ForumPostNotification,
|
||||||
|
user,
|
||||||
:topic_id,
|
:topic_id,
|
||||||
topic,
|
topic,
|
||||||
:post_id,
|
:post_id,
|
||||||
|
@ -106,12 +107,12 @@ defmodule Philomena.Notifications do
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> create_forum_topic_notification(topic)
|
iex> create_forum_topic_notification(user, topic)
|
||||||
{:ok, 2}
|
{:ok, 2}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_forum_topic_notification(topic) do
|
def create_forum_topic_notification(user, topic) do
|
||||||
Creator.create_single(ForumSubscription, ForumTopicNotification, :topic_id, topic)
|
Creator.create_single(ForumSubscription, ForumTopicNotification, user, :topic_id, topic)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -124,7 +125,13 @@ defmodule Philomena.Notifications do
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_gallery_image_notification(gallery) 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
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -132,14 +139,15 @@ defmodule Philomena.Notifications do
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
iex> create_image_comment_notification(image, comment)
|
iex> create_image_comment_notification(user, image, comment)
|
||||||
{:ok, 2}
|
{:ok, 2}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_image_comment_notification(image, comment) do
|
def create_image_comment_notification(user, image, comment) do
|
||||||
Creator.create_double(
|
Creator.create_double(
|
||||||
ImageSubscription,
|
ImageSubscription,
|
||||||
ImageCommentNotification,
|
ImageCommentNotification,
|
||||||
|
user,
|
||||||
:image_id,
|
:image_id,
|
||||||
image,
|
image,
|
||||||
:comment_id,
|
:comment_id,
|
||||||
|
@ -160,6 +168,7 @@ defmodule Philomena.Notifications do
|
||||||
Creator.create_double(
|
Creator.create_double(
|
||||||
ImageSubscription,
|
ImageSubscription,
|
||||||
ImageMergeNotification,
|
ImageMergeNotification,
|
||||||
|
nil,
|
||||||
:target_id,
|
:target_id,
|
||||||
target,
|
target,
|
||||||
:source_id,
|
:source_id,
|
||||||
|
|
|
@ -22,13 +22,13 @@ defmodule Philomena.Notifications.Creator do
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
iex> create_single(GallerySubscription, GalleryImageNotification, :gallery_id, gallery)
|
iex> create_single(GallerySubscription, GalleryImageNotification, nil, :gallery_id, gallery)
|
||||||
{:ok, 2}
|
{:ok, 2}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_single(subscription, notification, name, object) do
|
def create_single(subscription, notification, user, name, object) do
|
||||||
subscription
|
subscription
|
||||||
|> create_notification_query(name, object)
|
|> create_notification_query(user, name, object)
|
||||||
|> create_notification(notification, name)
|
|> create_notification(notification, name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ defmodule Philomena.Notifications.Creator do
|
||||||
iex> create_double(
|
iex> create_double(
|
||||||
...> ImageSubscription,
|
...> ImageSubscription,
|
||||||
...> ImageCommentNotification,
|
...> ImageCommentNotification,
|
||||||
|
...> user,
|
||||||
...> :image_id,
|
...> :image_id,
|
||||||
...> image,
|
...> image,
|
||||||
...> :comment_id,
|
...> :comment_id,
|
||||||
|
@ -53,9 +54,9 @@ defmodule Philomena.Notifications.Creator do
|
||||||
{:ok, 2}
|
{:ok, 2}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_double(subscription, notification, name1, object1, name2, object2) do
|
def create_double(subscription, notification, user, name1, object1, name2, object2) do
|
||||||
subscription
|
subscription
|
||||||
|> create_notification_query(name1, object1, name2, object2)
|
|> create_notification_query(user, name1, object1, name2, object2)
|
||||||
|> create_notification(notification, name1)
|
|> create_notification(notification, name1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -80,10 +81,10 @@ defmodule Philomena.Notifications.Creator do
|
||||||
# TODO: the following cannot be accomplished with a single query expression
|
# TODO: the following cannot be accomplished with a single query expression
|
||||||
# due to this Ecto bug: https://github.com/elixir-ecto/ecto/issues/4430
|
# 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)
|
now = DateTime.utc_now(:second)
|
||||||
|
|
||||||
from s in subscription,
|
from s in subscription_query(subscription, user),
|
||||||
where: field(s, ^name) == ^object.id,
|
where: field(s, ^name) == ^object.id,
|
||||||
select: %{
|
select: %{
|
||||||
^name => type(^object.id, :integer),
|
^name => type(^object.id, :integer),
|
||||||
|
@ -94,10 +95,10 @@ defmodule Philomena.Notifications.Creator do
|
||||||
}
|
}
|
||||||
end
|
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)
|
now = DateTime.utc_now(:second)
|
||||||
|
|
||||||
from s in subscription,
|
from s in subscription_query(subscription, user),
|
||||||
where: field(s, ^name1) == ^object1.id,
|
where: field(s, ^name1) == ^object1.id,
|
||||||
select: %{
|
select: %{
|
||||||
^name1 => type(^object1.id, :integer),
|
^name1 => type(^object1.id, :integer),
|
||||||
|
@ -109,6 +110,19 @@ defmodule Philomena.Notifications.Creator do
|
||||||
}
|
}
|
||||||
end
|
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
|
defp create_notification(query, notification, name) do
|
||||||
{count, nil} =
|
{count, nil} =
|
||||||
Repo.insert_all(
|
Repo.insert_all(
|
||||||
|
|
|
@ -121,14 +121,12 @@ defmodule Philomena.Posts do
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform_notify(post_id) do
|
def perform_notify(post_id) do
|
||||||
post = get_post!(post_id)
|
post =
|
||||||
|
post_id
|
||||||
|
|> get_post!()
|
||||||
|
|> Repo.preload([:user, :topic])
|
||||||
|
|
||||||
topic =
|
Notifications.create_forum_post_notification(post.user, post.topic, post)
|
||||||
post
|
|
||||||
|> Repo.preload(:topic)
|
|
||||||
|> Map.fetch!(:topic)
|
|
||||||
|
|
||||||
Notifications.create_forum_post_notification(topic, post)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -91,9 +91,12 @@ defmodule Philomena.Topics do
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform_notify([topic_id, _post_id]) do
|
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
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
Loading…
Reference in a new issue