Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update seen by id field in message threads #156

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ fun MessagesContent(modifier: Modifier) {
state.newMessagesToAppend,
state.threadMembers,
state.currentUserId,
state.thread,
loadMore = { viewModel.loadMore() }
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,22 @@ class MessagesViewModel @Inject constructor(
newMessagesToAppend = appendedMessages,
messagesByDate = newMessages.groupMessagesByDate()
)
markMessagesAsSeen(filter)
markMessagesAsSeen()
}
}
}
}

private fun markMessagesAsSeen(messages: List<ApiThreadMessage>) =
private fun markMessagesAsSeen() =
viewModelScope.launch(appDispatcher.IO) {
try {
val unreadMessages = messages.distinct()
.filter { !it.seen_by.contains(state.value.currentUserId) }
.map { it.id }

if (unreadMessages.isNotEmpty()) {
messagesRepository.markMessagesAsSeen(
threadId,
unreadMessages,
state.value.currentUserId
)
val thread = state.value.thread ?: return@launch
val userId = state.value.currentUserId
if (thread.seen_by_ids.contains(userId)) {
return@launch
}

messagesRepository.markMessagesAsSeen(threadId, userId)
kaushiksaliya marked this conversation as resolved.
Show resolved Hide resolved
} catch (e: Exception) {
Timber.e(e, "Error marking messages as seen")
}
Expand Down Expand Up @@ -140,7 +136,6 @@ class MessagesViewModel @Inject constructor(
error = null
)
)
markMessagesAsSeen(newMessages)
loadingData = false
} catch (e: Exception) {
Timber.e(e, "Error loading messages")
Expand Down Expand Up @@ -180,7 +175,7 @@ class MessagesViewModel @Inject constructor(
selectedMember = members.filter { it.user.id != state.value.currentUserId }
)
)

markMessagesAsSeen()
if (messagesJob == null) listenMessages()
}
}
Expand Down Expand Up @@ -312,7 +307,10 @@ class MessagesViewModel @Inject constructor(
val newMessage = messagesRepository.generateMessage(message, userId, threadId)
val newMessages = state.value.newMessagesToAppend.toMutableList()
newMessages.add(newMessage)
_state.emit(_state.value.copy(newMessagesToAppend = newMessages))

val thread = state.value.thread?.copy(seen_by_ids = emptyList())
_state.emit(_state.value.copy(newMessagesToAppend = newMessages, thread = thread))
cp-radhika-s marked this conversation as resolved.
Show resolved Hide resolved

messagesRepository.sendMessage(newMessage)
} catch (e: Exception) {
Timber.e(e, "Failed to send message")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.canopas.yourspace.R
import com.canopas.yourspace.data.models.messages.ApiThread
import com.canopas.yourspace.data.models.messages.ApiThreadMessage
import com.canopas.yourspace.data.models.user.UserInfo
import com.canopas.yourspace.domain.utils.formattedMessageDateHeader
Expand All @@ -55,6 +56,7 @@ fun ColumnScope.MessageList(
newMessagesToAppend: List<ApiThreadMessage>,
members: List<UserInfo>,
currentUserId: String,
thread: ApiThread?,
loadMore: () -> Unit
) {
val reachedBottom by remember {
Expand Down Expand Up @@ -98,11 +100,15 @@ fun ColumnScope.MessageList(
itemsIndexed(messages, key = { _, item -> item.id }) { index, message ->
val by = members.firstOrNull { it.user.id == message.sender_id }

val seenBy =
members.filter { message.seen_by.contains(it.user.id) && it.user.id != currentUserId }
val seenBy = if (thread != null) {
members.filter { thread.seen_by_ids.contains(it.user.id) && it.user.id != currentUserId }
} else {
emptyList()
}

val myLatestMsg =
messages.firstOrNull { it.sender_id == currentUserId }?.id == message.id

MessageContent(
previousMessage = if (index < messages.size - 1) messages[index + 1] else null,
nextMessage = if (index > 0 && index < messages.size - 1) messages[index - 1] else null,
Expand Down Expand Up @@ -231,6 +237,11 @@ fun MessageBubble(
} else {
AppTheme.colorScheme.containerLow
}
val messageTextColor = if (isSender) {
AppTheme.colorScheme.textInversePrimary
} else {
AppTheme.colorScheme.textPrimary
}

val shape = if (isSender) {
RoundedCornerShape(
Expand Down Expand Up @@ -278,7 +289,7 @@ fun MessageBubble(
}
Text(
text = message,
style = AppTheme.appTypography.subTitle3.copy(color = AppTheme.colorScheme.textInversePrimary)
style = AppTheme.appTypography.subTitle3.copy(color = messageTextColor)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private fun LazyItemScope.ThreadItem(
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically
) {
val hasUnreadMsg = message?.seen_by?.contains(currentUser?.id) == false
val hasUnreadMsg = !threadInfo.thread.seen_by_ids.contains(currentUser?.id)
Text(
text = message?.createdAtMs?.formattedMessageTimeString(
LocalContext.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ data class ApiThread(
val id: String = UUID.randomUUID().toString(),
val admin_id: String = "",
val space_id: String = "",
val last_message:String = "",
kaushiksaliya marked this conversation as resolved.
Show resolved Hide resolved
val member_ids: List<String> = emptyList(),
val seen_by_ids: List<String> = emptyList(),
val archived_for: Map<String, Long> = emptyMap<String, Long>(),
val created_at: Long = System.currentTimeMillis()
val created_at: Long = System.currentTimeMillis(),
val last_message_at: Date? = null,
) {
@get:Exclude
val isGroup: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ class MessagesRepository @Inject constructor(
fun getLatestMessages(threadId: String, limit: Int) =
apiMessagesService.getLatestMessages(threadId, limit)

suspend fun markMessagesAsSeen(
threadId: String,
messageIds: List<String>,
currentUserId: String
) {
apiMessagesService.markMessagesAsSeen(threadId, messageIds, currentUserId)
suspend fun markMessagesAsSeen(threadId: String, currentUserId: String) {
apiMessagesService.markMessagesAsSeen(threadId, currentUserId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ class ApiMessagesService @Inject constructor(
id = docRef.id,
thread_id = threadId,
sender_id = senderId,
message = message,
seen_by = listOf(senderId)
message = message
)
docRef.set(threadMessage).await()
}
Expand All @@ -105,8 +104,7 @@ class ApiMessagesService @Inject constructor(
id = docRef.id,
thread_id = threadId,
sender_id = senderId,
message = message,
seen_by = listOf(senderId)
message = message
)
}

Expand All @@ -115,16 +113,8 @@ class ApiMessagesService @Inject constructor(
docRef.set(message).await()
}

suspend fun markMessagesAsSeen(threadId: String, messageIds: List<String>, userId: String) {
db.runBatch { batch ->
messageIds.forEach { id ->
batch.update(
threadMessagesRef(threadId).document(id),
"seen_by",
FieldValue.arrayUnion(userId)
)
}
}.await()
suspend fun markMessagesAsSeen(threadId: String, userId: String) {
threadRef.document(threadId).update("seen_by_ids", FieldValue.arrayUnion(userId)).await()
}

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down
Loading