This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add separate endpoint to fetch only messages
Showing
5 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 26 additions & 1 deletion
27
server/src/main/kotlin/io/github/mklkj/kommunicator/data/service/MessageService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters