From 40c622ed95f29405521a7a4c8eb8ba104027bf48 Mon Sep 17 00:00:00 2001 From: Mazen Khalil Date: Wed, 28 Jan 2026 09:41:19 +0300 Subject: [PATCH] fix: Tiktok nil conversation handling in ReadStatusService (#13152) 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 ' 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 ' 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 --- app/services/tiktok/messaging_helpers.rb | 2 +- app/services/tiktok/read_status_service.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/tiktok/messaging_helpers.rb b/app/services/tiktok/messaging_helpers.rb index b4295c736..0f167a3e0 100644 --- a/app/services/tiktok/messaging_helpers.rb +++ b/app/services/tiktok/messaging_helpers.rb @@ -26,7 +26,7 @@ module Tiktok::MessagingHelpers end def find_conversation(channel, tt_conversation_id) - channel.inbox.contact_inboxes.find_by(source_id: tt_conversation_id).conversations.first + channel.inbox.contact_inboxes.find_by(source_id: tt_conversation_id)&.conversations&.first end def create_conversation(channel, contact_inbox, tt_conversation_id) diff --git a/app/services/tiktok/read_status_service.rb b/app/services/tiktok/read_status_service.rb index e09c6e8cf..02b255e94 100644 --- a/app/services/tiktok/read_status_service.rb +++ b/app/services/tiktok/read_status_service.rb @@ -4,9 +4,9 @@ class Tiktok::ReadStatusService pattr_initialize [:channel!, :content!] def perform - return if channel.blank? || content.blank? || outbound_event? + return if channel.blank? || content.blank? || outbound_event? || conversation.blank? - ::Conversations::UpdateMessageStatusJob.perform_later(conversation.id, last_read_timestamp) if conversation.present? + ::Conversations::UpdateMessageStatusJob.perform_later(conversation.id, last_read_timestamp) end def conversation