2019-08-14 09:48:44 +00:00
|
|
|
class MessageFinder
|
|
|
|
def initialize(conversation, params)
|
|
|
|
@conversation = conversation
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
current_messages
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-15 11:29:12 +00:00
|
|
|
def conversation_messages
|
2020-11-10 09:55:26 +00:00
|
|
|
@conversation.messages.includes(:attachments, :sender, sender: { avatar_attachment: [:blob] })
|
2019-12-15 11:29:12 +00:00
|
|
|
end
|
|
|
|
|
2019-10-29 07:20:54 +00:00
|
|
|
def messages
|
2019-12-15 11:29:12 +00:00
|
|
|
return conversation_messages if @params[:filter_internal_messages].blank?
|
2019-10-29 07:20:54 +00:00
|
|
|
|
2019-12-15 11:29:12 +00:00
|
|
|
conversation_messages.where.not('private = ? OR message_type = ?', true, 2)
|
2019-10-29 07:20:54 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def current_messages
|
2022-11-16 16:11:48 +00:00
|
|
|
if @params[:after].present?
|
|
|
|
messages.reorder('created_at asc').where('id >= ?', @params[:before].to_i).limit(20)
|
|
|
|
elsif @params[:before].present?
|
2022-05-06 09:20:55 +00:00
|
|
|
messages.reorder('created_at desc').where('id < ?', @params[:before].to_i).limit(20).reverse
|
2019-08-14 09:48:44 +00:00
|
|
|
else
|
2019-10-29 07:20:54 +00:00
|
|
|
messages.reorder('created_at desc').limit(20).reverse
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|