mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-17 11:04:22 +01:00
Remove background queueing for notification broadcasts
This commit is contained in:
parent
9538575c97
commit
183a99bc4f
9 changed files with 19 additions and 79 deletions
|
@ -15,7 +15,6 @@ defmodule Philomena.Comments do
|
||||||
alias Philomena.Images.Image
|
alias Philomena.Images.Image
|
||||||
alias Philomena.Images
|
alias Philomena.Images
|
||||||
alias Philomena.Notifications
|
alias Philomena.Notifications
|
||||||
alias Philomena.NotificationWorker
|
|
||||||
alias Philomena.Versions
|
alias Philomena.Versions
|
||||||
alias Philomena.Reports
|
alias Philomena.Reports
|
||||||
|
|
||||||
|
@ -63,21 +62,13 @@ defmodule Philomena.Comments do
|
||||||
|> Multi.one(:image, image_lock_query)
|
|> Multi.one(:image, image_lock_query)
|
||||||
|> Multi.insert(:comment, comment)
|
|> Multi.insert(:comment, comment)
|
||||||
|> Multi.update_all(:update_image, image_query, inc: [comments_count: 1])
|
|> Multi.update_all(:update_image, image_query, inc: [comments_count: 1])
|
||||||
|
|> Multi.run(:notification, ¬ify_comment/2)
|
||||||
|> Images.maybe_subscribe_on(:image, attribution[:user], :watch_on_reply)
|
|> Images.maybe_subscribe_on(:image, attribution[:user], :watch_on_reply)
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_comment(comment) do
|
defp notify_comment(_repo, %{image: image, comment: comment}) do
|
||||||
Exq.enqueue(Exq, "notifications", NotificationWorker, ["Comments", comment.id])
|
Notifications.create_image_comment_notification(comment.user, image, comment)
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -177,7 +168,6 @@ defmodule Philomena.Comments do
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, %{comment: comment, reports: {_count, reports}}} ->
|
{:ok, %{comment: comment, reports: {_count, reports}}} ->
|
||||||
notify_comment(comment)
|
|
||||||
UserStatistics.inc_stat(comment.user, :comments_posted)
|
UserStatistics.inc_stat(comment.user, :comments_posted)
|
||||||
Reports.reindex_reports(reports)
|
Reports.reindex_reports(reports)
|
||||||
reindex_comment(comment)
|
reindex_comment(comment)
|
||||||
|
|
|
@ -14,7 +14,6 @@ defmodule Philomena.Galleries do
|
||||||
alias Philomena.IndexWorker
|
alias Philomena.IndexWorker
|
||||||
alias Philomena.GalleryReorderWorker
|
alias Philomena.GalleryReorderWorker
|
||||||
alias Philomena.Notifications
|
alias Philomena.Notifications
|
||||||
alias Philomena.NotificationWorker
|
|
||||||
alias Philomena.Images
|
alias Philomena.Images
|
||||||
|
|
||||||
use Philomena.Subscriptions,
|
use Philomena.Subscriptions,
|
||||||
|
@ -163,7 +162,7 @@ defmodule Philomena.Galleries do
|
||||||
|
|
||||||
def add_image_to_gallery(gallery, image) do
|
def add_image_to_gallery(gallery, image) do
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.run(:lock, fn repo, %{} ->
|
|> Multi.run(:gallery, fn repo, %{} ->
|
||||||
gallery =
|
gallery =
|
||||||
Gallery
|
Gallery
|
||||||
|> where(id: ^gallery.id)
|
|> where(id: ^gallery.id)
|
||||||
|
@ -179,7 +178,7 @@ defmodule Philomena.Galleries do
|
||||||
|> Interaction.changeset(%{"image_id" => image.id, "position" => position})
|
|> Interaction.changeset(%{"image_id" => image.id, "position" => position})
|
||||||
|> repo.insert()
|
|> repo.insert()
|
||||||
end)
|
end)
|
||||||
|> Multi.run(:gallery, fn repo, %{} ->
|
|> Multi.run(:image_count, fn repo, %{} ->
|
||||||
now = DateTime.utc_now()
|
now = DateTime.utc_now()
|
||||||
|
|
||||||
{count, nil} =
|
{count, nil} =
|
||||||
|
@ -189,11 +188,11 @@ defmodule Philomena.Galleries do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|
|> Multi.run(:notification, ¬ify_gallery/2)
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, result} ->
|
{:ok, result} ->
|
||||||
Images.reindex_image(image)
|
Images.reindex_image(image)
|
||||||
notify_gallery(gallery, image)
|
|
||||||
reindex_gallery(gallery)
|
reindex_gallery(gallery)
|
||||||
|
|
||||||
{:ok, result}
|
{:ok, result}
|
||||||
|
@ -205,7 +204,7 @@ defmodule Philomena.Galleries do
|
||||||
|
|
||||||
def remove_image_from_gallery(gallery, image) do
|
def remove_image_from_gallery(gallery, image) do
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.run(:lock, fn repo, %{} ->
|
|> Multi.run(:gallery, fn repo, %{} ->
|
||||||
gallery =
|
gallery =
|
||||||
Gallery
|
Gallery
|
||||||
|> where(id: ^gallery.id)
|
|> where(id: ^gallery.id)
|
||||||
|
@ -222,7 +221,7 @@ defmodule Philomena.Galleries do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|> Multi.run(:gallery, fn repo, %{interaction: interaction_count} ->
|
|> Multi.run(:image_count, fn repo, %{interaction: interaction_count} ->
|
||||||
now = DateTime.utc_now()
|
now = DateTime.utc_now()
|
||||||
|
|
||||||
{count, nil} =
|
{count, nil} =
|
||||||
|
@ -245,22 +244,16 @@ defmodule Philomena.Galleries do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp notify_gallery(_repo, %{gallery: gallery}) do
|
||||||
|
Notifications.create_gallery_image_notification(gallery)
|
||||||
|
end
|
||||||
|
|
||||||
defp last_position(gallery_id) do
|
defp last_position(gallery_id) do
|
||||||
Interaction
|
Interaction
|
||||||
|> where(gallery_id: ^gallery_id)
|
|> where(gallery_id: ^gallery_id)
|
||||||
|> Repo.aggregate(:max, :position)
|
|> Repo.aggregate(:max, :position)
|
||||||
end
|
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
|
def reorder_gallery(gallery, image_ids) do
|
||||||
Exq.enqueue(Exq, "indexing", GalleryReorderWorker, [gallery.id, image_ids])
|
Exq.enqueue(Exq, "indexing", GalleryReorderWorker, [gallery.id, image_ids])
|
||||||
end
|
end
|
||||||
|
|
|
@ -24,7 +24,6 @@ defmodule Philomena.Images do
|
||||||
alias Philomena.SourceChanges.SourceChange
|
alias Philomena.SourceChanges.SourceChange
|
||||||
alias Philomena.Notifications.ImageCommentNotification
|
alias Philomena.Notifications.ImageCommentNotification
|
||||||
alias Philomena.Notifications.ImageMergeNotification
|
alias Philomena.Notifications.ImageMergeNotification
|
||||||
alias Philomena.NotificationWorker
|
|
||||||
alias Philomena.TagChanges.Limits
|
alias Philomena.TagChanges.Limits
|
||||||
alias Philomena.TagChanges.TagChange
|
alias Philomena.TagChanges.TagChange
|
||||||
alias Philomena.Tags
|
alias Philomena.Tags
|
||||||
|
@ -602,13 +601,13 @@ defmodule Philomena.Images do
|
||||||
|> Multi.run(:migrate_interactions, fn _, %{} ->
|
|> Multi.run(:migrate_interactions, fn _, %{} ->
|
||||||
{:ok, Interactions.migrate_interactions(image, duplicate_of_image)}
|
{:ok, Interactions.migrate_interactions(image, duplicate_of_image)}
|
||||||
end)
|
end)
|
||||||
|
|> Multi.run(:notification, ¬ify_merge(&1, &2, image, duplicate_of_image))
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> process_after_hide()
|
|> process_after_hide()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, result} ->
|
{:ok, result} ->
|
||||||
reindex_image(duplicate_of_image)
|
reindex_image(duplicate_of_image)
|
||||||
Comments.reindex_comments(duplicate_of_image)
|
Comments.reindex_comments(duplicate_of_image)
|
||||||
notify_merge(image, duplicate_of_image)
|
|
||||||
|
|
||||||
{:ok, result}
|
{:ok, result}
|
||||||
|
|
||||||
|
@ -954,14 +953,7 @@ defmodule Philomena.Images do
|
||||||
|> Repo.update()
|
|> Repo.update()
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_merge(source, target) do
|
defp notify_merge(_repo, _changes, 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)
|
|
||||||
|
|
||||||
Notifications.create_image_merge_notification(target, source)
|
Notifications.create_image_merge_notification(target, source)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,6 @@ defmodule Philomena.Posts do
|
||||||
alias Philomena.IndexWorker
|
alias Philomena.IndexWorker
|
||||||
alias Philomena.Forums.Forum
|
alias Philomena.Forums.Forum
|
||||||
alias Philomena.Notifications
|
alias Philomena.Notifications
|
||||||
alias Philomena.NotificationWorker
|
|
||||||
alias Philomena.Versions
|
alias Philomena.Versions
|
||||||
alias Philomena.Reports
|
alias Philomena.Reports
|
||||||
|
|
||||||
|
@ -93,6 +92,7 @@ defmodule Philomena.Posts do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|
|> Multi.run(:notification, ¬ify_post/2)
|
||||||
|> Topics.maybe_subscribe_on(:topic, attributes[:user], :watch_on_reply)
|
|> Topics.maybe_subscribe_on(:topic, attributes[:user], :watch_on_reply)
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
|
@ -106,8 +106,8 @@ defmodule Philomena.Posts do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_post(post) do
|
defp notify_post(_repo, %{post: post, topic: topic}) do
|
||||||
Exq.enqueue(Exq, "notifications", NotificationWorker, ["Posts", post.id])
|
Notifications.create_forum_post_notification(post.user, topic, post)
|
||||||
end
|
end
|
||||||
|
|
||||||
def report_non_approved(%Post{approved: true}), do: false
|
def report_non_approved(%Post{approved: true}), do: false
|
||||||
|
@ -120,15 +120,6 @@ defmodule Philomena.Posts do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform_notify(post_id) do
|
|
||||||
post =
|
|
||||||
post_id
|
|
||||||
|> get_post!()
|
|
||||||
|> Repo.preload([:user, :topic])
|
|
||||||
|
|
||||||
Notifications.create_forum_post_notification(post.user, post.topic, post)
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Updates a post.
|
Updates a post.
|
||||||
|
|
||||||
|
@ -241,7 +232,6 @@ defmodule Philomena.Posts do
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, %{post: post, reports: {_count, reports}}} ->
|
{:ok, %{post: post, reports: {_count, reports}}} ->
|
||||||
notify_post(post)
|
|
||||||
UserStatistics.inc_stat(post.user, :forum_posts)
|
UserStatistics.inc_stat(post.user, :forum_posts)
|
||||||
Reports.reindex_reports(reports)
|
Reports.reindex_reports(reports)
|
||||||
reindex_post(post)
|
reindex_post(post)
|
||||||
|
|
|
@ -11,7 +11,6 @@ defmodule Philomena.Topics do
|
||||||
alias Philomena.Forums.Forum
|
alias Philomena.Forums.Forum
|
||||||
alias Philomena.Posts
|
alias Philomena.Posts
|
||||||
alias Philomena.Notifications
|
alias Philomena.Notifications
|
||||||
alias Philomena.NotificationWorker
|
|
||||||
|
|
||||||
use Philomena.Subscriptions,
|
use Philomena.Subscriptions,
|
||||||
on_delete: :clear_topic_notification,
|
on_delete: :clear_topic_notification,
|
||||||
|
@ -73,6 +72,7 @@ defmodule Philomena.Topics do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|
|> Multi.run(:notification, ¬ify_topic/2)
|
||||||
|> maybe_subscribe_on(:topic, attribution[:user], :watch_on_new_topic)
|
|> maybe_subscribe_on(:topic, attribution[:user], :watch_on_new_topic)
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
|
@ -87,16 +87,7 @@ defmodule Philomena.Topics do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_topic(topic, post) do
|
defp notify_topic(_repo, %{topic: topic}) do
|
||||||
Exq.enqueue(Exq, "notifications", NotificationWorker, ["Topics", [topic.id, post.id]])
|
|
||||||
end
|
|
||||||
|
|
||||||
def perform_notify([topic_id, _post_id]) do
|
|
||||||
topic =
|
|
||||||
topic_id
|
|
||||||
|> get_topic!()
|
|
||||||
|> Repo.preload(:user)
|
|
||||||
|
|
||||||
Notifications.create_forum_topic_notification(topic.user, topic)
|
Notifications.create_forum_topic_notification(topic.user, topic)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
defmodule Philomena.NotificationWorker do
|
|
||||||
@modules %{
|
|
||||||
"Comments" => Philomena.Comments,
|
|
||||||
"Galleries" => Philomena.Galleries,
|
|
||||||
"Images" => Philomena.Images,
|
|
||||||
"Posts" => Philomena.Posts,
|
|
||||||
"Topics" => Philomena.Topics
|
|
||||||
}
|
|
||||||
|
|
||||||
def perform(module, args) do
|
|
||||||
@modules[module].perform_notify(args)
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -82,7 +82,6 @@ defmodule PhilomenaWeb.Image.CommentController do
|
||||||
Images.reindex_image(conn.assigns.image)
|
Images.reindex_image(conn.assigns.image)
|
||||||
|
|
||||||
if comment.approved do
|
if comment.approved do
|
||||||
Comments.notify_comment(comment)
|
|
||||||
UserStatistics.inc_stat(conn.assigns.current_user, :comments_posted)
|
UserStatistics.inc_stat(conn.assigns.current_user, :comments_posted)
|
||||||
else
|
else
|
||||||
Comments.report_non_approved(comment)
|
Comments.report_non_approved(comment)
|
||||||
|
|
|
@ -36,7 +36,6 @@ defmodule PhilomenaWeb.Topic.PostController do
|
||||||
case Posts.create_post(topic, attributes, post_params) do
|
case Posts.create_post(topic, attributes, post_params) do
|
||||||
{:ok, %{post: post}} ->
|
{:ok, %{post: post}} ->
|
||||||
if post.approved do
|
if post.approved do
|
||||||
Posts.notify_post(post)
|
|
||||||
UserStatistics.inc_stat(conn.assigns.current_user, :forum_posts)
|
UserStatistics.inc_stat(conn.assigns.current_user, :forum_posts)
|
||||||
else
|
else
|
||||||
Posts.report_non_approved(post)
|
Posts.report_non_approved(post)
|
||||||
|
|
|
@ -111,7 +111,6 @@ defmodule PhilomenaWeb.TopicController do
|
||||||
case Topics.create_topic(forum, attributes, topic_params) do
|
case Topics.create_topic(forum, attributes, topic_params) do
|
||||||
{:ok, %{topic: topic}} ->
|
{:ok, %{topic: topic}} ->
|
||||||
post = hd(topic.posts)
|
post = hd(topic.posts)
|
||||||
Topics.notify_topic(topic, post)
|
|
||||||
|
|
||||||
if forum.access_level == "normal" do
|
if forum.access_level == "normal" do
|
||||||
PhilomenaWeb.Endpoint.broadcast!(
|
PhilomenaWeb.Endpoint.broadcast!(
|
||||||
|
|
Loading…
Reference in a new issue