chore: Refactor MarkMessagesAsReadJob based on conversation id and time stamp (#8217)

- Mark all messages as read by providing the conversation ID and timestamp.
- For Instagram, ensure all previous messages that weren't marked as failed are now marked as read. This is because the read events are only triggered for the most recent message and not for any previous ones.
This commit is contained in:
Muhsin Keloth
2023-10-28 03:51:39 +05:30
committed by GitHub
parent 61e03fa33a
commit 24fbab94c3
8 changed files with 36 additions and 24 deletions

View File

@@ -28,7 +28,7 @@ class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController
conversation.contact_last_seen_at = DateTime.now.utc
conversation.save!
::Conversations::MarkMessagesAsReadJob.perform_later(conversation)
::Conversations::MarkMessagesAsReadJob.perform_later(conversation.id, conversation.contact_last_seen_at)
head :ok
end

View File

@@ -23,7 +23,7 @@ class Public::Api::V1::Inboxes::ConversationsController < Public::Api::V1::Inbox
def update_last_seen
@conversation.contact_last_seen_at = DateTime.now.utc
@conversation.save!
::Conversations::MarkMessagesAsReadJob.perform_later(@conversation)
::Conversations::MarkMessagesAsReadJob.perform_later(@conversation.id, @conversation.contact_last_seen_at)
head :ok
end

View File

@@ -1,12 +1,15 @@
class Conversations::MarkMessagesAsReadJob < ApplicationJob
queue_as :low
def perform(conversation)
def perform(conversation_id, timestamp)
conversation = Conversation.find_by(id: conversation_id)
return unless conversation
# Mark every message created before the user's viewing time as read.
conversation.messages.where(status: %w[sent delivered])
.where.not(message_type: 'incoming')
.where('created_at <= ?',
conversation.contact_last_seen_at).find_each do |message|
.where('messages.created_at <= ?', timestamp).find_each do |message|
message.update!(status: 'read')
end
end

View File

@@ -4,12 +4,7 @@ class Instagram::ReadStatusService
def perform
return if instagram_channel.blank?
process_status if message.present?
end
def process_status
@message.status = 'read'
@message.save!
::Conversations::MarkMessagesAsReadJob.perform_later(message.conversation.id, message.created_at) if message.present?
end
def instagram_id