Fully async chat message notifications

This commit is contained in:
Roman Rizzi 2022-12-30 18:32:46 -03:00
parent 702f27e6ee
commit db2ea40975
No known key found for this signature in database
GPG Key ID: 64024A71CE7330D3
4 changed files with 94 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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__)

View File

@ -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