From 22331fb6268344c3987b4dade3e08ab1aef503ed Mon Sep 17 00:00:00 2001 From: Pranav Date: Fri, 7 Mar 2025 13:56:10 -0800 Subject: [PATCH] fix: Paginate attachments API (#11044) There are attachments with over 1000 attachments (unusual) in production, and some of them timeout. This PR would limit the number of attachments to 100 (which is sufficient for viewing the files in the gallery, pagination on the UI can be added later). --- .../api/v1/accounts/conversations_controller.rb | 11 +++++++++++ .../accounts/conversations/attachments.json.jbuilder | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/app/controllers/api/v1/accounts/conversations_controller.rb b/app/controllers/api/v1/accounts/conversations_controller.rb index 2cd5281ff..138c2bd68 100644 --- a/app/controllers/api/v1/accounts/conversations_controller.rb +++ b/app/controllers/api/v1/accounts/conversations_controller.rb @@ -6,6 +6,8 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro before_action :conversation, except: [:index, :meta, :search, :create, :filter] before_action :inbox, :contact, :contact_inbox, only: [:create] + ATTACHMENT_RESULTS_PER_PAGE = 100 + def index result = conversation_finder.perform @conversations = result[:conversations] @@ -24,7 +26,12 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro end def attachments + @attachments_count = @conversation.attachments.count @attachments = @conversation.attachments + .includes(:message) + .order(created_at: :desc) + .page(attachment_params[:page]) + .per(ATTACHMENT_RESULTS_PER_PAGE) end def show; end @@ -124,6 +131,10 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro params.permit(:priority) end + def attachment_params + params.permit(:page) + end + def update_last_seen_on_conversation(last_seen_at, update_assignee) # rubocop:disable Rails/SkipsModelValidations @conversation.update_column(:agent_last_seen_at, last_seen_at) diff --git a/app/views/api/v1/accounts/conversations/attachments.json.jbuilder b/app/views/api/v1/accounts/conversations/attachments.json.jbuilder index 068462d54..167b18390 100644 --- a/app/views/api/v1/accounts/conversations/attachments.json.jbuilder +++ b/app/views/api/v1/accounts/conversations/attachments.json.jbuilder @@ -1,3 +1,7 @@ +json.meta do + json.total_count @attachments_count +end + json.payload @attachments do |attachment| json.message_id attachment.push_event_data[:message_id] json.thumb_url attachment.push_event_data[:thumb_url]