diff --git a/plugins/chat/app/jobs/regular/send_message_notifications.rb b/plugins/chat/app/jobs/regular/send_message_notifications.rb new file mode 100644 index 0000000000..ed66e7b046 --- /dev/null +++ b/plugins/chat/app/jobs/regular/send_message_notifications.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Jobs + class SendMessageNotifications < ::Jobs::Base + def execute(args) + reason = args[:reason] + return if (timestamp = args[:timestamp]).blank? + + return if (message = ChatMessage.find_by(id: args[:chat_message_id])).nil? + + if reason == "new" + Chat::ChatNotifier.new(message, timestamp).notify_new + elsif reason == "edit" + Chat::ChatNotifier.new(message, timestamp).notify_edit + end + end + end +end diff --git a/plugins/chat/lib/chat_notifier.rb b/plugins/chat/lib/chat_notifier.rb index 7737f3f49d..18e5c8c03c 100644 --- a/plugins/chat/lib/chat_notifier.rb +++ b/plugins/chat/lib/chat_notifier.rb @@ -37,11 +37,21 @@ class Chat::ChatNotifier end def notify_edit(chat_message:, timestamp:) - new(chat_message, timestamp).notify_edit + Jobs.enqueue( + :send_message_notifications, + chat_message_id: chat_message.id, + timestamp: timestamp.iso8601(6), + reason: "edit" + ) end def notify_new(chat_message:, timestamp:) - new(chat_message, timestamp).notify_new + Jobs.enqueue( + :send_message_notifications, + chat_message_id: chat_message.id, + timestamp: timestamp.iso8601(6), + reason: "new" + ) end end @@ -333,7 +343,7 @@ class Chat::ChatNotifier chat_message_id: @chat_message.id, to_notify_ids_map: to_notify.as_json, already_notified_user_ids: already_notified_user_ids, - timestamp: @timestamp.iso8601(6), + timestamp: @timestamp, }, ) end @@ -344,7 +354,7 @@ class Chat::ChatNotifier { chat_message_id: @chat_message.id, except_user_ids: except, - timestamp: @timestamp.iso8601(6), + timestamp: @timestamp, }, ) end diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb index ba5e2d2917..b3b5070a19 100644 --- a/plugins/chat/plugin.rb +++ b/plugins/chat/plugin.rb @@ -198,6 +198,7 @@ after_initialize do load File.expand_path("../app/jobs/regular/chat_notify_watching.rb", __FILE__) load File.expand_path("../app/jobs/regular/update_channel_user_count.rb", __FILE__) load File.expand_path("../app/jobs/regular/delete_user_messages.rb", __FILE__) + load File.expand_path("../app/jobs/regular/send_message_notifications.rb", __FILE__) load File.expand_path("../app/jobs/scheduled/delete_old_chat_messages.rb", __FILE__) load File.expand_path("../app/jobs/scheduled/update_user_counts_for_chat_channels.rb", __FILE__) load File.expand_path("../app/jobs/scheduled/email_chat_notifications.rb", __FILE__) diff --git a/plugins/chat/spec/jobs/regular/send_message_notifications_spec.rb b/plugins/chat/spec/jobs/regular/send_message_notifications_spec.rb new file mode 100644 index 0000000000..141b9a83fa --- /dev/null +++ b/plugins/chat/spec/jobs/regular/send_message_notifications_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +RSpec.describe Jobs::SendMessageNotifications do + describe "#execute" do + context "when the message doesn't exist" do + it "does nothing" do + Chat::ChatNotifier.any_instance.expects(:notify_new).never + Chat::ChatNotifier.any_instance.expects(:notify_edit).never + + subject.execute(eason: "new", timestamp: 1.minute.ago) + end + end + + context "when there's a message" do + fab!(:chat_message) { Fabricate(:chat_message) } + + it "does nothing when the reason is invalid" do + Chat::ChatNotifier.expects(:notify_new).never + Chat::ChatNotifier.expects(:notify_edit).never + + subject.execute( + chat_message_id: chat_message.id, + reason: "invalid", + timestamp: 1.minute.ago + ) + end + + it "does nothing if there is no timestamp" do + Chat::ChatNotifier.any_instance.expects(:notify_new).never + Chat::ChatNotifier.any_instance.expects(:notify_edit).never + + subject.execute( + chat_message_id: chat_message.id, + reason: "invalid" + ) + end + + it "calls notify_new when the reason is 'new'" do + Chat::ChatNotifier.any_instance.expects(:notify_new).once + Chat::ChatNotifier.any_instance.expects(:notify_edit).never + + subject.execute( + chat_message_id: chat_message.id, + reason: "new", + timestamp: 1.minute.ago + ) + end + + it "calls notify_edit when the reason is 'edit'" do + Chat::ChatNotifier.any_instance.expects(:notify_new).never + Chat::ChatNotifier.any_instance.expects(:notify_edit).once + + subject.execute( + chat_message_id: chat_message.id, + reason: "edit", + timestamp: 1.minute.ago + ) + end + end + end +end