feat: Support after param in messages end point (#6848)
Adds support to `after` param while fetching messages Fixes: https://linear.app/chatwoot/issue/CW-1475/support-after-param-in-messages-end-point
This commit is contained in:
@@ -22,11 +22,29 @@ class MessageFinder
|
|||||||
|
|
||||||
def current_messages
|
def current_messages
|
||||||
if @params[:after].present? && @params[:before].present?
|
if @params[:after].present? && @params[:before].present?
|
||||||
messages.reorder('created_at asc').where('id >= ? AND id < ?', @params[:after].to_i, @params[:before].to_i).limit(1000)
|
messages_between(@params[:after].to_i, @params[:before].to_i)
|
||||||
elsif @params[:before].present?
|
elsif @params[:before].present?
|
||||||
messages.reorder('created_at desc').where('id < ?', @params[:before].to_i).limit(20).reverse
|
messages_before(@params[:before].to_i)
|
||||||
|
elsif @params[:after].present?
|
||||||
|
messages_after(@params[:after].to_i)
|
||||||
else
|
else
|
||||||
messages.reorder('created_at desc').limit(20).reverse
|
messages_latest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def messages_after(after_id)
|
||||||
|
messages.reorder('created_at asc').where('id > ?', after_id).limit(100)
|
||||||
|
end
|
||||||
|
|
||||||
|
def messages_before(before_id)
|
||||||
|
messages.reorder('created_at desc').where('id < ?', before_id).limit(20).reverse
|
||||||
|
end
|
||||||
|
|
||||||
|
def messages_between(after_id, before_id)
|
||||||
|
messages.reorder('created_at asc').where('id >= ? AND id < ?', after_id, before_id).limit(1000)
|
||||||
|
end
|
||||||
|
|
||||||
|
def messages_latest
|
||||||
|
messages.reorder('created_at desc').limit(20).reverse
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -53,10 +53,25 @@ describe ::MessageFinder do
|
|||||||
|
|
||||||
it 'filter conversations by status' do
|
it 'filter conversations by status' do
|
||||||
result = message_finder.perform
|
result = message_finder.perform
|
||||||
expect(result.count).to be 6
|
expect(result.count).to be 5
|
||||||
expect(result.first.id).to be conversation.messages.first.id
|
expect(result.first.id).to be conversation.messages.second.id
|
||||||
expect(result.last.message_type).to eq 'outgoing'
|
expect(result.last.message_type).to eq 'outgoing'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'with after and before attribute' do
|
||||||
|
let(:params) do
|
||||||
|
{
|
||||||
|
after: conversation.messages.first.id,
|
||||||
|
before: conversation.messages.last.id
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'filter conversations by status' do
|
||||||
|
result = message_finder.perform
|
||||||
|
expect(result.count).to be 5
|
||||||
|
expect(result.last.id).to be conversation.messages[-2].id
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user