11package com.getcode.network
22
3- import androidx.paging.PagingConfig
43import androidx.paging.PagingData
54import androidx.paging.PagingSource
65import com.getcode.db.AppDatabase
@@ -12,17 +11,16 @@ import com.getcode.mapper.ConversationMessageMapper
1211import com.getcode.model.Cursor
1312import com.getcode.model.ID
1413import com.getcode.model.MessageStatus
15- import com.getcode.model.chat.Chat
1614import com.getcode.model.chat.ChatMember
1715import com.getcode.model.chat.ChatMessage
16+ import com.getcode.model.chat.ConversationEntity
1817import com.getcode.model.chat.Identity
1918import com.getcode.model.chat.Platform
2019import com.getcode.model.chat.Title
2120import com.getcode.model.chat.isConversation
2221import com.getcode.model.chat.selfId
2322import com.getcode.network.client.Client
2423import com.getcode.network.client.advancePointer
25- import com.getcode.network.client.fetchChats
2624import com.getcode.network.client.fetchMessagesFor
2725import com.getcode.network.client.fetchV2Chats
2826import com.getcode.network.repository.encodeBase64
@@ -45,14 +43,14 @@ import javax.inject.Singleton
4543@Singleton
4644class ChatHistoryController @Inject constructor(
4745 private val client : Client ,
48- private val tipController : TipController ,
46+ private val twitterUserController : TwitterUserController ,
4947 private val conversationMapper : ConversationMapper ,
5048 private val conversationMessageMapper : ConversationMessageMapper ,
5149) : CoroutineScope by CoroutineScope(Dispatchers .IO ) {
5250
53- private val chatEntries = MutableStateFlow <List <Chat >? > (null )
51+ private val chatEntries = MutableStateFlow <List <ConversationEntity >? > (null )
5452
55- val chats: StateFlow <List <Chat >? >
53+ val chats: StateFlow <List <ConversationEntity >? >
5654 get() = chatEntries
5755 .map { it?.filter { entry -> entry.isConversation } }
5856 .stateIn(this , SharingStarted .Eagerly , emptyList())
@@ -69,7 +67,7 @@ class ChatHistoryController @Inject constructor(
6967 chatFlows.clear()
7068 }
7169
72- fun updateChatWithMessages (chat : Chat , messages : List <ChatMessage >) {
70+ fun updateChatWithMessages (chat : ConversationEntity , messages : List <ChatMessage >) {
7371 val updatedMessages = (chat.messages + messages).distinctBy { it.id }
7472 val updatedChat = chat.copy(messages = updatedMessages)
7573 val chats = chatEntries.value?.map {
@@ -90,10 +88,10 @@ class ChatHistoryController @Inject constructor(
9088
9189 private fun owner (): KeyPair ? = SessionManager .getKeyPair()
9290
93- suspend fun fetchChats (update : Boolean = false) {
91+ suspend fun fetch (update : Boolean = false) {
9492 if (loadingMessages) return
9593
96- val updatedWithMessages = mutableListOf<Chat >()
94+ val updatedWithMessages = mutableListOf<ConversationEntity >()
9795 val containers = fetchChatsWithoutMessages()
9896 trace(message = " Fetched ${containers.count()} chats" , type = TraceType .Silent )
9997
@@ -122,12 +120,12 @@ class ChatHistoryController @Inject constructor(
122120 chatEntries.value = updatedWithMessages.sortedByDescending { it.lastMessageMillis }
123121 }
124122
125- fun addChat (chat : Chat ) {
123+ fun addChat (chat : ConversationEntity ) {
126124 chatEntries.value = (chatEntries.value.orEmpty() + chat)
127125 .sortedByDescending { it.lastMessageMillis }
128126 }
129127
130- fun findChat (predicate : (Chat ) -> Boolean ): Chat ? {
128+ fun findChat (predicate : (ConversationEntity ) -> Boolean ): ConversationEntity ? {
131129 return chatEntries.value?.firstOrNull(predicate)
132130 }
133131
@@ -144,7 +142,7 @@ class ChatHistoryController @Inject constructor(
144142 }
145143 }
146144
147- private suspend fun fetchLatestMessageForChat (chat : Chat ): Result <ChatMessage ?> {
145+ private suspend fun fetchLatestMessageForChat (chat : ConversationEntity ): Result <ChatMessage ?> {
148146 val encodedId = chat.id.toByteArray().encodeBase64()
149147 Timber .d(" fetching last message for $encodedId " )
150148 val owner = owner() ? : return Result .success(null )
@@ -169,7 +167,7 @@ class ChatHistoryController @Inject constructor(
169167 }.map { it.getOrNull(0 ) }
170168 }
171169
172- private suspend fun fetchChatsWithoutMessages (): List <Chat > {
170+ private suspend fun fetchChatsWithoutMessages (): List <ConversationEntity > {
173171 val owner = owner() ? : return emptyList()
174172 val result = client.fetchV2Chats(owner)
175173 .map { chats ->
@@ -201,14 +199,14 @@ class ChatHistoryController @Inject constructor(
201199 return result.getOrNull().orEmpty()
202200 }
203201
204- private suspend fun fetchMemberImages (chat : Chat ): List <ChatMember > {
202+ private suspend fun fetchMemberImages (chat : ConversationEntity ): List <ChatMember > {
205203 return chat.members
206204 .map { member ->
207205 if (member.isSelf) return @map member
208206 if (member.identity == null ) return @map member
209207 if (member.identity.imageUrl != null ) return @map member
210208 val metadata = runCatching {
211- tipController.fetch (member.identity.username)
209+ twitterUserController.fetchUser (member.identity.username)
212210 }.getOrNull() ? : return @map member
213211
214212 member.copy(
0 commit comments