Fixes https://linear.app/chatwoot/issue/CW-6358/handling-of-nil-conversation-in-the-readstatusservice Improve handling of nil conversation in the `ReadStatusService` to prevent potential errors. Ensure that the conversation is checked before performing updates to message status. This change fixes the below error. ``` NoMethodError: undefined method 'conversations' for nil (NoMethodError) channel.inbox.contact_inboxes.find_by(source_id: tt_conversation_id).conversations.first ^^^^^^^^^^^^^^ from app/services/tiktok/messaging_helpers.rb:29:in 'Tiktok::MessagingHelpers#find_conversation' from app/services/tiktok/read_status_service.rb:13:in 'Tiktok::ReadStatusService#conversation' from app/services/tiktok/read_status_service.rb:9:in 'Tiktok::ReadStatusService#perform' from app/jobs/webhooks/tiktok_events_job.rb:67:in 'Webhooks::TiktokEventsJob#im_mark_read_msg' from app/jobs/webhooks/tiktok_events_job.rb:31:in 'Webhooks::TiktokEventsJob#process_event' from app/jobs/webhooks/tiktok_events_job.rb:15:in 'block in Webhooks::TiktokEventsJob#perform' from app/jobs/mutex_application_job.rb:23:in 'MutexApplicationJob#with_lock' from app/jobs/webhooks/tiktok_events_job.rb:14:in 'Webhooks::TiktokEventsJob#perform' from activejob (7.1.5.2) lib/active_job/execution.rb:68:in 'block in ActiveJob::Execution#_perform_job' from activesupport (7.1.5.2) lib/active_support/callbacks.rb:121:in 'block in ActiveSupport::Callbacks#run_callbacks' from i18n (1.14.7) lib/i18n.rb:353:in 'I18n::Base#with_locale' from activejob (7.1.5.2) lib/active_job/translation.rb:9:in 'block (2 levels) in <module:Translation>' from activesupport (7.1.5.2) lib/active_support/callbacks.rb:130:in 'BasicObject#instance_exec' from activesupport (7.1.5.2) lib/active_support/callbacks.rb:130:in 'block in ActiveSupport::Callbacks#run_callbacks' from activesupport (7.1.5.2) lib/active_support/core_ext/time/zones.rb:65:in 'Time.use_zone' from activejob (7.1.5.2) lib/active_job/timezones.rb:9:in 'block (2 levels) in <module:Timezones>' from activesupport (7.1.5.2) lib/active_support/callbacks.rb:130:in 'BasicObject#instance_exec' from activesupport (7.1.5.2) lib/active_support/callbacks.rb:130:in 'block in ActiveSupport::Callbacks#run_callbacks' from activesupport (7.1.5.2) lib/active_support/callbacks.rb:141:in 'ActiveSupport::Callbacks#run_callbacks' from activejob (7.1.5.2) lib/active_job/execution.rb:67:in 'ActiveJob::Execution#_perform_job ``` Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
37 lines
769 B
Ruby
37 lines
769 B
Ruby
class Tiktok::ReadStatusService
|
|
include Tiktok::MessagingHelpers
|
|
|
|
pattr_initialize [:channel!, :content!]
|
|
|
|
def perform
|
|
return if channel.blank? || content.blank? || outbound_event? || conversation.blank?
|
|
|
|
::Conversations::UpdateMessageStatusJob.perform_later(conversation.id, last_read_timestamp)
|
|
end
|
|
|
|
def conversation
|
|
@conversation ||= find_conversation(channel, tt_conversation_id)
|
|
end
|
|
|
|
def tt_conversation_id
|
|
content[:conversation_id]
|
|
end
|
|
|
|
def last_read_timestamp
|
|
tt = content[:read][:last_read_timestamp]
|
|
Time.zone.at(tt.to_i / 1000).utc
|
|
end
|
|
|
|
def business_id
|
|
channel.business_id
|
|
end
|
|
|
|
def from_user_id
|
|
content[:from_user][:id]
|
|
end
|
|
|
|
def outbound_event?
|
|
business_id.to_s == from_user_id.to_s
|
|
end
|
|
end
|