@@ -7,11 +7,13 @@ import com.getcode.ed25519.Ed25519.KeyPair
77import com.getcode.manager.SessionManager
88import com.getcode.model.Chat
99import com.getcode.model.ChatMessage
10+ import com.getcode.model.Cursor
1011import com.getcode.model.HistoricalTransaction
1112import com.getcode.model.ID
1213import com.getcode.network.client.Client
1314import com.getcode.network.client.fetchChats
1415import com.getcode.network.client.fetchMessagesFor
16+ import com.getcode.network.client.setMuted
1517import com.getcode.network.repository.TransactionRepository
1618import com.getcode.network.repository.encodeBase64
1719import com.getcode.network.source.ChatMessagePagingSource
@@ -25,6 +27,7 @@ import kotlinx.coroutines.flow.catch
2527import kotlinx.coroutines.flow.collect
2628import kotlinx.coroutines.flow.filterNotNull
2729import kotlinx.coroutines.flow.map
30+ import kotlinx.coroutines.flow.update
2831import kotlinx.coroutines.reactive.asFlow
2932import okhttp3.internal.toImmutableList
3033import timber.log.Timber
@@ -46,7 +49,6 @@ class HistoryController @Inject constructor(
4649 .sortedByDescending { it.date }
4750 .toImmutableList()
4851
49-
5052 private val _chats = MutableStateFlow <List <Chat >? > (null )
5153 val chats: StateFlow <List <Chat >? >
5254 get() = _chats .asStateFlow()
@@ -93,7 +95,7 @@ class HistoryController @Inject constructor(
9395 Timber .d(" chats fetched = ${containers.count()} " )
9496 _chats .value = containers
9597
96- val updatedWithMessages= mutableListOf<Chat >()
98+ val updatedWithMessages = mutableListOf<Chat >()
9799 containers.onEach { chat ->
98100 val result = fetchLatestMessageForChat(chat.id)
99101 result.onSuccess { message ->
@@ -108,6 +110,37 @@ class HistoryController @Inject constructor(
108110 _chats .value = updatedWithMessages.sortedByDescending { it.lastMessageMillis }
109111 }
110112
113+ suspend fun setMuted (chatId : ID , muted : Boolean ): Result <Boolean > {
114+ val owner = owner() ? : return Result .failure(Throwable (" No owner detected" ))
115+
116+ _chats .update {
117+ it?.toMutableList()?.apply chats@{
118+ indexOfFirst { chat -> chat.id == chatId }
119+ .takeIf { index -> index >= 0 }
120+ ?.let { index ->
121+ val chat = this [index]
122+ Timber .d(" changing mute state for chat locally" )
123+ this [index] = chat.copy(isMuted = muted)
124+ }
125+ }?.toList()
126+ }
127+
128+ return client.setMuted(owner, chatId, muted)
129+ }
130+
131+ suspend fun fetchMessagesForChat (
132+ id : List <Byte >,
133+ cursor : Cursor ? = null,
134+ limit : Int? = null
135+ ): Result <ChatMessage ?> {
136+ val encodedId = id.toByteArray().encodeBase64()
137+ val owner = owner() ? : return Result .success(null )
138+ return client.fetchMessagesFor(owner, id, cursor, limit)
139+ .onFailure {
140+ Timber .e(t = it, " Failed to fetch messages for $encodedId ." )
141+ }.map { it.getOrNull(0 ) }
142+ }
143+
111144 private suspend fun fetchLatestMessageForChat (id : List <Byte >): Result <ChatMessage ?> {
112145 val encodedId = id.toByteArray().encodeBase64()
113146 Timber .d(" fetching messages for $encodedId " )
0 commit comments