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
|
|
|
|
@conversation.messages.includes(:attachment, user: { avatar_attachment: :blob })
|
|
|
|
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
|
|
|
|
if @params[:before].present?
|
2019-10-29 07:20:54 +00:00
|
|
|
messages.reorder('created_at desc').where('id < ?', @params[:before]).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
|