fix: last incoming message sort (#4972)

This commit is contained in:
Tejaswini Chile
2022-07-06 00:37:43 +05:30
committed by GitHub
parent ac2adfbd57
commit b86638faac
5 changed files with 56 additions and 48 deletions

View File

@@ -11,14 +11,14 @@ module SortHandler
end
def self.last_messaged_conversations
Message.except(:order).select('DISTINCT ON (conversation_id) *').order('conversation_id, created_at DESC')
Message.except(:order).select(
'DISTINCT ON (conversation_id) conversation_id, id, created_at, message_type'
).order('conversation_id, created_at DESC')
end
def self.sort_on_last_user_message_at
where(
'grouped_conversations.message_type = 0'
).order(
'grouped_conversations.created_at ASC'
order(
'grouped_conversations.message_type', 'grouped_conversations.created_at ASC'
)
end
end

View File

@@ -72,7 +72,7 @@ class Conversation < ApplicationRecord
scope :last_user_message_at, lambda {
joins(
"INNER JOIN (#{last_messaged_conversations.to_sql}) grouped_conversations
"INNER JOIN (#{last_messaged_conversations.to_sql}) AS grouped_conversations
ON grouped_conversations.conversation_id = conversations.id"
).sort_on_last_user_message_at
}

View File

@@ -46,7 +46,7 @@ class Mention < ApplicationRecord
# Then select only latest incoming message from the conversations which doesn't have last message as outgoing
# Order by message created_at
Mention.joins(
"INNER JOIN (#{last_messaged_conversations.to_sql}) grouped_conversations
"INNER JOIN (#{last_messaged_conversations.to_sql}) AS grouped_conversations
ON grouped_conversations.conversation_id = mentions.conversation_id"
).sort_on_last_user_message_at
end