-
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.
feat :: prepare qna type chatting - db spec changes
- Loading branch information
Showing
22 changed files
with
325 additions
and
78 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
13 changes: 8 additions & 5 deletions
13
src/main/kotlin/com/teamapi/palette/dto/chat/ChatResponse.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,13 +1,16 @@ | ||
package com.teamapi.palette.dto.chat | ||
|
||
import java.time.LocalDateTime | ||
import com.teamapi.palette.entity.chat.PromptData | ||
import com.teamapi.palette.entity.consts.ChatState | ||
import java.time.ZonedDateTime | ||
|
||
data class ChatResponse( | ||
val id: Long?, | ||
val message: String, | ||
val datetime: LocalDateTime, | ||
val id: String?, | ||
val message: String?, | ||
val resource: ChatState, | ||
val datetime: ZonedDateTime, | ||
val roomId: Long, | ||
val userId: Long, | ||
val isAi: Boolean, | ||
val resource: String | ||
val data: PromptData?, | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
package com.teamapi.palette.entity.chat | ||
|
||
import com.teamapi.palette.dto.chat.ChatResponse | ||
import org.springframework.data.annotation.Id | ||
import com.teamapi.palette.entity.consts.ChatState | ||
import com.teamapi.palette.repository.mongo.MongoDatabases | ||
import org.bson.codecs.pojo.annotations.BsonId | ||
import org.bson.types.ObjectId | ||
import org.springframework.data.mongodb.core.mapping.Document | ||
import org.springframework.data.relational.core.mapping.Column | ||
import java.time.LocalDateTime | ||
import java.time.ZonedDateTime | ||
|
||
@Document("chats") | ||
@Document(MongoDatabases.CHAT) | ||
data class Chat( | ||
@Id | ||
val id: Long? = null, | ||
val message: String, | ||
val resource: String = "CHAT", | ||
val datetime: LocalDateTime, | ||
@Column("room_id") | ||
@BsonId | ||
val id: ObjectId = ObjectId.get(), | ||
val datetime: ZonedDateTime, // MUST-INCLUDED | ||
val resource: ChatState = ChatState.CHAT, | ||
|
||
// default property | ||
val message: String? = null, | ||
val roomId: Long, | ||
@Column("user_id") | ||
val userId: Long, | ||
@Column("is_ai") | ||
val isAi: Boolean | ||
val isAi: Boolean, | ||
|
||
// additional prompt data | ||
val data: PromptData? = null | ||
) { | ||
fun toDto() = ChatResponse(id, message, datetime, roomId, userId, isAi, resource) | ||
fun toDto() = ChatResponse(id.toString(), message, resource, datetime, roomId, userId, isAi, data) | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/com/teamapi/palette/entity/chat/PromptData.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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.teamapi.palette.entity.chat | ||
|
||
import com.teamapi.palette.entity.consts.PromptType | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
sealed class PromptData(val type: PromptType) { | ||
@Serializable | ||
data class Selectable(val choice: List<String>) : PromptData(PromptType.SELECTABLE) | ||
@Serializable | ||
data class Grid(val xSize: Int, val ySize: Int) : PromptData(PromptType.GRID) | ||
@Serializable | ||
data object UserInput : PromptData(PromptType.USER_INPUT) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/teamapi/palette/entity/consts/ChatState.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.teamapi.palette.entity.consts | ||
|
||
enum class ChatState { | ||
CHAT, | ||
IMAGE, | ||
PROMPT_USER_INPUT, | ||
PROMPT_SELECT, | ||
PROMPT_GRID, | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/teamapi/palette/entity/consts/PromptType.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.teamapi.palette.entity.consts | ||
|
||
enum class PromptType { | ||
USER_INPUT, | ||
SELECTABLE, | ||
GRID | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/teamapi/palette/repository/chat/ChatQueryRepository.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.teamapi.palette.repository.chat | ||
|
||
import com.teamapi.palette.dto.chat.ChatResponse | ||
import org.springframework.data.domain.Pageable | ||
import java.time.ZonedDateTime | ||
|
||
interface ChatQueryRepository { | ||
suspend fun getImagesByUserId(userId: Long, pageable: Pageable): List<String> | ||
suspend fun getMessageByRoomId(roomId: Long, offset: ZonedDateTime, size: Long): List<ChatResponse> | ||
suspend fun getLatestMessageMapById(roomIds: List<Long>): Map<Long, String?> | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/kotlin/com/teamapi/palette/repository/chat/ChatQueryRepositoryImpl.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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.teamapi.palette.repository.chat | ||
|
||
import com.mongodb.client.model.Aggregates | ||
import com.mongodb.client.model.Projections | ||
import com.mongodb.kotlin.client.coroutine.MongoDatabase | ||
import com.teamapi.palette.dto.chat.ChatResponse | ||
import com.teamapi.palette.entity.chat.Chat | ||
import com.teamapi.palette.entity.consts.ChatState | ||
import com.teamapi.palette.repository.mongo.* | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.toList | ||
import org.bson.codecs.pojo.annotations.BsonId | ||
import org.springframework.data.domain.Pageable | ||
import java.time.LocalDateTime | ||
import java.time.ZonedDateTime | ||
|
||
class ChatQueryRepositoryImpl( | ||
private val mongoDb: MongoDatabase | ||
) : ChatQueryRepository { | ||
// used internally | ||
internal data class ImageFoundResult(val message: String) | ||
internal data class LastMessageResult(@BsonId val id: Long, val lastMessage: String?) | ||
|
||
override suspend fun getImagesByUserId(userId: Long, pageable: Pageable): List<String> { | ||
val collection = mongoDb.getCollection<Chat>(MongoDatabases.CHAT) | ||
|
||
return collection | ||
.find<ImageFoundResult>((Chat::userId eq userId) and (Chat::resource eq ChatState.IMAGE)) | ||
.sort(Chat::datetime.desc()) | ||
.skip(pageable.offset.toInt()) | ||
.limit(pageable.pageSize) | ||
.projection(Projections.include(Chat::message.name)) | ||
.map { it.message } | ||
.toList() | ||
} | ||
|
||
override suspend fun getMessageByRoomId(roomId: Long, offset: ZonedDateTime, size: Long): List<ChatResponse> { | ||
val collection = mongoDb.getCollection<Chat>(MongoDatabases.CHAT) | ||
|
||
return collection | ||
.find(Chat::datetime lt offset and (Chat::roomId eq roomId)) | ||
.sort(Chat::datetime.desc()) | ||
.limit(size.toInt()) | ||
.toList() | ||
.map { it.toDto() } | ||
} | ||
|
||
override suspend fun getLatestMessageMapById(roomIds: List<Long>): Map<Long, String?> { | ||
val collection = mongoDb.getCollection<Chat>(MongoDatabases.CHAT) | ||
|
||
return collection.aggregate<LastMessageResult>( | ||
Aggregates.match((Chat::roomId `in` roomIds) and (Chat::resource ne ChatState.IMAGE)), | ||
Aggregates.group(Chat::roomId.literal, Chat::message.getLastAs("lastMessage")), | ||
).toList().associate { it.id to it.lastMessage } | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/teamapi/palette/repository/mongo/MongoDatabases.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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.teamapi.palette.repository.mongo | ||
|
||
data object MongoDatabases { | ||
const val CHAT = "chats" | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/com/teamapi/palette/repository/mongo/MongoQueryHelpers.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.teamapi.palette.repository.mongo | ||
|
||
import com.mongodb.client.model.Accumulators | ||
import com.mongodb.client.model.BsonField | ||
import com.mongodb.client.model.Filters | ||
import com.mongodb.client.model.Sorts | ||
import com.mongodb.kotlin.client.coroutine.MongoCollection | ||
import org.bson.conversions.Bson | ||
import org.springframework.data.mapping.toDotPath | ||
import kotlin.reflect.KProperty1 | ||
|
||
infix fun <R, T : Any?> KProperty1<R, T>.eq(other: T?): Bson = Filters.eq(toDotPath(), other) | ||
infix fun <R, T> KProperty1<R, T>.ne(other: T?): Bson = Filters.ne(toDotPath(), other) | ||
infix fun <R, T : Any> KProperty1<R, T>.lt(other: T): Bson = Filters.lt(toDotPath(), other) | ||
infix fun <R, T : Any> KProperty1<R, T>.lte(other: T): Bson = Filters.lte(toDotPath(), other) | ||
infix fun <R, T : Any> KProperty1<R, T>.gt(other: T): Bson = Filters.gt(toDotPath(), other) | ||
infix fun <R, T : Any> KProperty1<R, T>.gte(other: T): Bson = Filters.gte(toDotPath(), other) | ||
infix fun <R, T : Any?> KProperty1<R, T>.`in`(others: List<T>): Bson = Filters.`in`(toDotPath(), others) | ||
|
||
//fun <T : Any> MongoCollection<T>.aggregate(vararg pipeline: Bson) = aggregate(pipeline.toList()) | ||
inline fun <reified T : Any> MongoCollection<*>.aggregate(vararg pipeline: Bson) = aggregate<T>(pipeline.toList()) | ||
|
||
infix fun Bson.and(bson: Bson) = Filters.and(this, bson) | ||
|
||
fun KProperty1<*, *>.getFirstAs(name: String): BsonField = Accumulators.first(name, literal) | ||
fun KProperty1<*, *>.getLastAs(name: String): BsonField = Accumulators.last(name, literal) | ||
val KProperty1<*, *>.literal: String get() = "\$${toDotPath()}" | ||
|
||
fun KProperty1<*, *>.asc() = Sorts.ascending(name) | ||
fun KProperty1<*, *>.desc() = Sorts.descending(name) |
Oops, something went wrong.