Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

Commit

Permalink
Add separate endpoint to fetch only messages
Browse files Browse the repository at this point in the history
mklkj committed Jan 31, 2024
1 parent e1afb0b commit b9049ff
Showing 5 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import io.github.mklkj.kommunicator.data.models.Chat
import io.github.mklkj.kommunicator.data.models.ChatCreateRequest
import io.github.mklkj.kommunicator.data.models.ChatCreateResponse
import io.github.mklkj.kommunicator.data.models.ChatDetails
import io.github.mklkj.kommunicator.data.models.Message
import io.github.mklkj.kommunicator.data.models.MessageRequest
import kotlinx.uuid.UUID

@@ -22,6 +23,9 @@ interface MessagesService {
@GET("/api/chats/{id}")
suspend fun getChat(@Path("id") id: UUID): ChatDetails

@GET("/api/chats/{chatId}/messages")
suspend fun getMessages(@Path("chatId") chatId: UUID): List<Message>

@POST("/api/chats/{chatId}/messages")
suspend fun sendMessage(
@Path("chatId") chatId: UUID,
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import io.github.mklkj.kommunicator.data.db.entity.LocalContact
import io.github.mklkj.kommunicator.data.models.Chat
import io.github.mklkj.kommunicator.data.models.ChatCreateRequest
import io.github.mklkj.kommunicator.data.models.ChatDetails
import io.github.mklkj.kommunicator.data.models.Message
import io.github.mklkj.kommunicator.data.models.MessageRequest
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emitAll
@@ -56,6 +57,10 @@ class MessagesRepository(
return messagesService.getChat(id)
}

suspend fun getMessages(chatId: UUID): List<Message> {
return messagesService.getMessages(chatId)
}

suspend fun sendMessage(chatId: UUID, message: MessageRequest) {
messagesService.sendMessage(chatId, message)
}
Original file line number Diff line number Diff line change
@@ -40,7 +40,33 @@ class ConversationViewModel(
mutableState.update { it.copy(isLoading = true) }
messagesRepository.sendMessage(chatId, message)
mutableState.update { it.copy(isLoading = false) }
loadData(chatId)

reloadMessages(chatId)
}
}

private fun reloadMessages(chatId: UUID) {
launch("reload_messages") {
runCatching { messagesRepository.getMessages(chatId) }
.onFailure { error ->
proceedError(error)
mutableState.update {
it.copy(
isLoading = false,
errorMessage = error.message,
)
}
}
.onSuccess { messages ->
mutableState.update {
it.copy(
isLoading = false,
details = it.details?.copy(
messages = messages,
)
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
package io.github.mklkj.kommunicator.data.service

import io.github.mklkj.kommunicator.data.models.Message
import io.github.mklkj.kommunicator.data.models.MessageEntity
import io.github.mklkj.kommunicator.data.repository.ChatRepository
import io.github.mklkj.kommunicator.data.repository.MessageRepository
import kotlinx.uuid.UUID
import org.koin.core.annotation.Singleton

@Singleton
class MessageService(
private val chatRepository: ChatRepository,
private val messageRepository: MessageRepository,
) {

suspend fun saveMessage(entity: MessageEntity) {
messageRepository.saveMessage(entity)
}

suspend fun getMessages(chatId: UUID): List<MessageEntity> {
suspend fun getMessages(chatId: UUID, userId: UUID): List<Message> {
val participants = chatRepository.getParticipants(chatId)
.filterNot { it.userId == userId }

return messageRepository.getMessages(chatId)
.map { message ->
Message(
id = message.id,
isUserMessage = message.userId == userId,
authorId = message.userId,
authorName = when (message.userId) {
userId -> "You"
else -> {
val participant = participants
.find { it.userId == message.userId }
participant?.customName ?: participant?.let {
"${it.userFirstName} ${it.userLastName}"
}
}
}.orEmpty(),
timestamp = message.timestamp,
content = message.content,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -48,9 +48,17 @@ fun Route.chatRoutes() {

call.respond(chat)
}
get("/{id}/messages") {
val userId = call.principalId ?: error("Invalid JWT!")
// todo: add verification whether a given user can read messages from that chat!!!
val chatId = call.parameters["id"]?.toUUIDOrNull() ?: error("Invalid chat id")

call.respond(messageService.getMessages(chatId, userId))
}
post("/{id}/messages") {
val message = call.receive<MessageRequest>()

// todo: add verification whether a given user can write to that chat!!!
messageService.saveMessage(
MessageEntity(
id = message.id,

0 comments on commit b9049ff

Please sign in to comment.