fix: Incorrect contact access in conversations listing (#11797)

# Pull Request Template

## Description

This PR fixes the incorrect contact access in conversations listing API.
Cause:

- `undefined method 'conversations' for nil` error because `@contact` is
not initialized

Solution:
- Using `@contact_inbox` to access `@contact`
- `@contact_inbox` is properly set in the parent controller's
`set_contact_inbox` method

Fixes
https://linear.app/chatwoot/issue/CW-4185/incorrect-contact-access-pattern-in

## Type of change

Please delete options that are not relevant.

- [x] Bug fix (non-breaking change which fixes an issue)

## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Niranjan Patil
2025-09-22 17:05:11 +05:30
committed by GitHub
parent b5deecc9f9
commit b28c08059f
2 changed files with 12 additions and 1 deletions

View File

@@ -3,7 +3,7 @@ class Public::Api::V1::Inboxes::ConversationsController < Public::Api::V1::Inbox
before_action :set_conversation, only: [:toggle_typing, :update_last_seen, :show, :toggle_status]
def index
@conversations = @contact_inbox.hmac_verified? ? @contact.conversations : @contact_inbox.conversations
@conversations = @contact_inbox.hmac_verified? ? @contact_inbox.contact.conversations : @contact_inbox.conversations
end
def show; end

View File

@@ -16,6 +16,17 @@ RSpec.describe 'Public Inbox Contact Conversations API', type: :request do
expect(data.first['uuid']).to eq contact_inbox.conversations.first.uuid
end
it 'return the conversations when hmac_verified is true' do
contact_inbox.update(hmac_verified: true)
create(:conversation, contact: contact)
get "/public/api/v1/inboxes/#{api_channel.identifier}/contacts/#{contact_inbox.source_id}/conversations"
expect(response).to have_http_status(:success)
data = response.parsed_body
expect(data.length).to eq 1
expect(data.first['uuid']).to eq contact.conversations.first.uuid
end
it 'does not return any private or activity message' do
conversation = create(:conversation, contact_inbox: contact_inbox)
create(:message, account: conversation.account, inbox: conversation.inbox, conversation: conversation, content: 'message-1')