Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Aug 6, 2024
2 parents 549a810 + c173162 commit 773991c
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 202 deletions.
16 changes: 3 additions & 13 deletions lib/philomena/comments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule Philomena.Comments do
alias Philomena.Images.Image
alias Philomena.Images
alias Philomena.Notifications
alias Philomena.NotificationWorker
alias Philomena.Versions
alias Philomena.Reports
alias Philomena.Users.User
Expand Down Expand Up @@ -65,21 +64,13 @@ defmodule Philomena.Comments do
|> Multi.one(:image, image_lock_query)
|> Multi.insert(:comment, comment)
|> Multi.update_all(:update_image, image_query, inc: [comments_count: 1])
|> Multi.run(:notification, &notify_comment/2)
|> Images.maybe_subscribe_on(:image, attribution[:user], :watch_on_reply)
|> Repo.transaction()
end

def notify_comment(comment) do
Exq.enqueue(Exq, "notifications", NotificationWorker, ["Comments", comment.id])
end

def perform_notify(comment_id) do
comment =
comment_id
|> get_comment!()
|> Repo.preload([:user, :image])

Notifications.create_image_comment_notification(comment.user, comment.image, comment)
defp notify_comment(_repo, %{image: image, comment: comment}) do
Notifications.create_image_comment_notification(comment.user, image, comment)
end

@doc """
Expand Down Expand Up @@ -208,7 +199,6 @@ defmodule Philomena.Comments do
|> Repo.transaction()
|> case do
{:ok, %{comment: comment, reports: {_count, reports}}} ->
notify_comment(comment)
UserStatistics.inc_stat(comment.user, :comments_posted)
Reports.reindex_reports(reports)
reindex_comment(comment)
Expand Down
25 changes: 9 additions & 16 deletions lib/philomena/galleries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule Philomena.Galleries do
alias Philomena.IndexWorker
alias Philomena.GalleryReorderWorker
alias Philomena.Notifications
alias Philomena.NotificationWorker
alias Philomena.Images

use Philomena.Subscriptions,
Expand Down Expand Up @@ -163,7 +162,7 @@ defmodule Philomena.Galleries do

def add_image_to_gallery(gallery, image) do
Multi.new()
|> Multi.run(:lock, fn repo, %{} ->
|> Multi.run(:gallery, fn repo, %{} ->
gallery =
Gallery
|> where(id: ^gallery.id)
Expand All @@ -179,7 +178,7 @@ defmodule Philomena.Galleries do
|> Interaction.changeset(%{"image_id" => image.id, "position" => position})
|> repo.insert()
end)
|> Multi.run(:gallery, fn repo, %{} ->
|> Multi.run(:image_count, fn repo, %{} ->
now = DateTime.utc_now()

{count, nil} =
Expand All @@ -189,11 +188,11 @@ defmodule Philomena.Galleries do

{:ok, count}
end)
|> Multi.run(:notification, &notify_gallery/2)
|> Repo.transaction()
|> case do
{:ok, result} ->
Images.reindex_image(image)
notify_gallery(gallery, image)
reindex_gallery(gallery)

{:ok, result}
Expand All @@ -205,7 +204,7 @@ defmodule Philomena.Galleries do

def remove_image_from_gallery(gallery, image) do
Multi.new()
|> Multi.run(:lock, fn repo, %{} ->
|> Multi.run(:gallery, fn repo, %{} ->
gallery =
Gallery
|> where(id: ^gallery.id)
Expand All @@ -222,7 +221,7 @@ defmodule Philomena.Galleries do

{:ok, count}
end)
|> Multi.run(:gallery, fn repo, %{interaction: interaction_count} ->
|> Multi.run(:image_count, fn repo, %{interaction: interaction_count} ->
now = DateTime.utc_now()

{count, nil} =
Expand All @@ -245,22 +244,16 @@ defmodule Philomena.Galleries do
end
end

defp notify_gallery(_repo, %{gallery: gallery}) do
Notifications.create_gallery_image_notification(gallery)
end

defp last_position(gallery_id) do
Interaction
|> where(gallery_id: ^gallery_id)
|> Repo.aggregate(:max, :position)
end

def notify_gallery(gallery, image) do
Exq.enqueue(Exq, "notifications", NotificationWorker, ["Galleries", [gallery.id, image.id]])
end

def perform_notify([gallery_id, _image_id]) do
gallery = get_gallery!(gallery_id)

Notifications.create_gallery_image_notification(gallery)
end

def reorder_gallery(gallery, image_ids) do
Exq.enqueue(Exq, "indexing", GalleryReorderWorker, [gallery.id, image_ids])
end
Expand Down
12 changes: 2 additions & 10 deletions lib/philomena/images.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ defmodule Philomena.Images do
alias Philomena.SourceChanges.SourceChange
alias Philomena.Notifications.ImageCommentNotification
alias Philomena.Notifications.ImageMergeNotification
alias Philomena.NotificationWorker
alias Philomena.TagChanges.Limits
alias Philomena.TagChanges.TagChange
alias Philomena.Tags
Expand Down Expand Up @@ -622,13 +621,13 @@ defmodule Philomena.Images do
|> Multi.run(:migrate_interactions, fn _, %{} ->
{:ok, Interactions.migrate_interactions(image, duplicate_of_image)}
end)
|> Multi.run(:notification, &notify_merge(&1, &2, image, duplicate_of_image))
|> Repo.transaction()
|> process_after_hide()
|> case do
{:ok, result} ->
reindex_image(duplicate_of_image)
Comments.reindex_comments(duplicate_of_image)
notify_merge(image, duplicate_of_image)

{:ok, result}

Expand Down Expand Up @@ -974,14 +973,7 @@ defmodule Philomena.Images do
|> Repo.update()
end

def notify_merge(source, target) do
Exq.enqueue(Exq, "notifications", NotificationWorker, ["Images", [source.id, target.id]])
end

def perform_notify([source_id, target_id]) do
source = get_image!(source_id)
target = get_image!(target_id)

defp notify_merge(_repo, _changes, source, target) do
Notifications.create_image_merge_notification(target, source)
end

Expand Down
106 changes: 58 additions & 48 deletions lib/philomena/notifications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Philomena.Notifications do
"""

import Ecto.Query, warn: false
alias Philomena.Repo

alias Philomena.Channels.Subscription, as: ChannelSubscription
alias Philomena.Forums.Subscription, as: ForumSubscription
Expand Down Expand Up @@ -78,12 +79,11 @@ defmodule Philomena.Notifications do
"""
def create_channel_live_notification(channel) do
Creator.create_single(
where(ChannelSubscription, channel_id: ^channel.id),
ChannelLiveNotification,
nil,
:channel_id,
channel
Creator.broadcast_notification(
from: {ChannelSubscription, channel_id: channel.id},
into: ChannelLiveNotification,
select: [channel_id: channel.id],
unique_key: :channel_id
)
end

Expand All @@ -97,14 +97,12 @@ defmodule Philomena.Notifications do
"""
def create_forum_post_notification(user, topic, post) do
Creator.create_double(
where(TopicSubscription, topic_id: ^topic.id),
ForumPostNotification,
user,
:topic_id,
topic,
:post_id,
post
Creator.broadcast_notification(
notification_author: user,
from: {TopicSubscription, topic_id: topic.id},
into: ForumPostNotification,
select: [topic_id: topic.id, post_id: post.id],
unique_key: :topic_id
)
end

Expand All @@ -118,12 +116,12 @@ defmodule Philomena.Notifications do
"""
def create_forum_topic_notification(user, topic) do
Creator.create_single(
where(ForumSubscription, forum_id: ^topic.forum_id),
ForumTopicNotification,
user,
:topic_id,
topic
Creator.broadcast_notification(
notification_author: user,
from: {ForumSubscription, forum_id: topic.forum_id},
into: ForumTopicNotification,
select: [topic_id: topic.id],
unique_key: :topic_id
)
end

Expand All @@ -137,12 +135,11 @@ defmodule Philomena.Notifications do
"""
def create_gallery_image_notification(gallery) do
Creator.create_single(
where(GallerySubscription, gallery_id: ^gallery.id),
GalleryImageNotification,
nil,
:gallery_id,
gallery
Creator.broadcast_notification(
from: {GallerySubscription, gallery_id: gallery.id},
into: GalleryImageNotification,
select: [gallery_id: gallery.id],
unique_key: :gallery_id
)
end

Expand All @@ -156,14 +153,12 @@ defmodule Philomena.Notifications do
"""
def create_image_comment_notification(user, image, comment) do
Creator.create_double(
where(ImageSubscription, image_id: ^image.id),
ImageCommentNotification,
user,
:image_id,
image,
:comment_id,
comment
Creator.broadcast_notification(
notification_author: user,
from: {ImageSubscription, image_id: image.id},
into: ImageCommentNotification,
select: [image_id: image.id, comment_id: comment.id],
unique_key: :image_id
)
end

Expand All @@ -177,14 +172,11 @@ defmodule Philomena.Notifications do
"""
def create_image_merge_notification(target, source) do
Creator.create_double(
where(ImageSubscription, image_id: ^target.id),
ImageMergeNotification,
nil,
:target_id,
target,
:source_id,
source
Creator.broadcast_notification(
from: {ImageSubscription, image_id: target.id},
into: ImageMergeNotification,
select: [target_id: target.id, source_id: source.id],
unique_key: :target_id
)
end

Expand All @@ -201,7 +193,7 @@ defmodule Philomena.Notifications do
def clear_channel_live_notification(channel, user) do
ChannelLiveNotification
|> where(channel_id: ^channel.id)
|> Creator.clear(user)
|> delete_all_for_user(user)
end

@doc """
Expand All @@ -217,7 +209,7 @@ defmodule Philomena.Notifications do
def clear_forum_post_notification(topic, user) do
ForumPostNotification
|> where(topic_id: ^topic.id)
|> Creator.clear(user)
|> delete_all_for_user(user)
end

@doc """
Expand All @@ -233,7 +225,7 @@ defmodule Philomena.Notifications do
def clear_forum_topic_notification(topic, user) do
ForumTopicNotification
|> where(topic_id: ^topic.id)
|> Creator.clear(user)
|> delete_all_for_user(user)
end

@doc """
Expand All @@ -249,7 +241,7 @@ defmodule Philomena.Notifications do
def clear_gallery_image_notification(gallery, user) do
GalleryImageNotification
|> where(gallery_id: ^gallery.id)
|> Creator.clear(user)
|> delete_all_for_user(user)
end

@doc """
Expand All @@ -265,7 +257,7 @@ defmodule Philomena.Notifications do
def clear_image_comment_notification(image, user) do
ImageCommentNotification
|> where(image_id: ^image.id)
|> Creator.clear(user)
|> delete_all_for_user(user)
end

@doc """
Expand All @@ -281,6 +273,24 @@ defmodule Philomena.Notifications do
def clear_image_merge_notification(image, user) do
ImageMergeNotification
|> where(target_id: ^image.id)
|> Creator.clear(user)
|> delete_all_for_user(user)
end

#
# Clear all unread notifications using the given query.
#
# Returns `{:ok, count}`, where `count` is the number of affected rows.
#
defp delete_all_for_user(query, user) do
if user do
{count, nil} =
query
|> where(user_id: ^user.id)
|> Repo.delete_all()

{:ok, count}
else
{:ok, 0}
end
end
end
Loading

0 comments on commit 773991c

Please sign in to comment.