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:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user