@@ -12,6 +12,8 @@ import com.getcode.model.Kin
1212import com.getcode.utils.base58
1313import kotlinx.coroutines.flow.Flow
1414import xyz.flipchat.services.domain.model.chat.Conversation
15+ import xyz.flipchat.services.domain.model.chat.ConversationMessage
16+ import xyz.flipchat.services.domain.model.chat.ConversationWithMembers
1517import xyz.flipchat.services.domain.model.chat.ConversationWithMembersAndLastMessage
1618import xyz.flipchat.services.domain.model.chat.ConversationWithMembersAndLastPointers
1719
@@ -28,92 +30,40 @@ interface ConversationDao {
2830 suspend fun upsertConversations (vararg conversation : Conversation )
2931
3032 @RewriteQueriesToDropUnusedColumns
31- @Transaction
3233 @Query(
3334 """
34- SELECT
35- -- Conversation fields
36- c.idBase58 AS idBase58,
37- c.ownerIdBase58 AS ownerIdBase58,
38- c.title AS title,
39- c.imageUri AS imageUri,
40- c.canMute AS canMute,
41- c.unreadCount AS unreadCount,
42- c.hasMoreUnread AS hasMoreUnread,
43- c.isOpen AS isOpen,
44- c.roomNumber AS roomNumber,
45- c.lastActivity AS lastActivity,
46-
47- -- Last message fields
48- lm.idBase58 AS lastMessageIdBase58,
49- lm.dateMillis AS lastMessageDateMillis,
50- lm.senderIdBase58 AS lastMessageSenderIdBase58,
51- lm.type AS lastMessageType,
52- lm.content AS lastMessageContent,
53-
54- -- Member fields
55- cm.memberIdBase58 AS memberIdBase58,
56- cm.memberName AS memberName,
57- cm.imageUri AS imageUri,
58- cm.isHost AS isHost,
59- cm.isMuted AS isMuted,
60- cm.isFullMember AS isFullMember,
61- cm.isBlocked AS isBlocked
62-
63- FROM conversations AS c
64-
65- -- Join to fetch all members for each conversation
66- LEFT JOIN members AS cm
67- ON cm.conversationIdBase58 = c.idBase58
68-
69- -- Subquery to find the most recent message for each conversation
70- LEFT JOIN (
71- SELECT
72- conversationIdBase58,
73- MAX(dateMillis) AS maxDateMillis
74- FROM
75- messages
76-
77- GROUP BY
78- conversationIdBase58
79- ) AS latestMessage
80- ON
81- c.idBase58 = latestMessage.conversationIdBase58
82-
83- -- Subquery to find the most recent message for ordering, filtering by specific types
84- LEFT JOIN (
85- SELECT
86- conversationIdBase58,
87- MAX(dateMillis) AS maxDateMillis
88- FROM
89- messages
90- WHERE
91- type IN (1, 8)
92- GROUP BY
93- conversationIdBase58
94- ) AS messageForOrder
95- ON
96- c.idBase58 = messageForOrder.conversationIdBase58
97-
98- -- Join to get details for the latest message
99- LEFT JOIN messages AS lm
100- ON
101- latestMessage.conversationIdBase58 = lm.conversationIdBase58
102- AND latestMessage.maxDateMillis = lm.dateMillis
103- WHERE type IN (1,8)
104-
105- GROUP BY c.idBase58
106-
107- ORDER BY
108- messageForOrder.maxDateMillis DESC
109-
110- LIMIT :limit OFFSET :offset
111- """
35+ SELECT * FROM conversations
36+ WHERE roomNumber > 0
37+ ORDER BY lastActivity DESC
38+ LIMIT :limit OFFSET :offset
39+ """
11240 )
113- suspend fun getPagedConversations (
114- limit : Int ,
115- offset : Int
116- ): List <ConversationWithMembersAndLastMessage >
41+ suspend fun getPagedConversationsWithMembers (limit : Int , offset : Int ): List <ConversationWithMembers >
42+
43+ suspend fun getPagedConversations (limit : Int , offset : Int ): List <ConversationWithMembersAndLastMessage > {
44+ return getPagedConversationsWithMembers(limit, offset)
45+ .map {
46+ val lastMessage = getLastMessage(it.conversation.id)
47+ ConversationWithMembersAndLastMessage (
48+ conversation = it.conversation,
49+ members = it.members,
50+ lastMessage = lastMessage
51+ )
52+ }
53+ }
54+
55+
56+ @Query("""
57+ SELECT *
58+ FROM messages
59+ WHERE conversationIdBase58 = :id AND type IN (1,8)
60+ ORDER BY dateMillis DESC
61+ LIMIT 1
62+ """ )
63+ suspend fun getLastMessage (id : String ): ConversationMessage ?
64+ suspend fun getLastMessage (id : ID ): ConversationMessage ? {
65+ return getLastMessage(id.base58)
66+ }
11767
11868
11969
0 commit comments