This commit is contained in:
2026-03-01 22:00:47 +03:00
parent 6402b4cfb1
commit 0cf6865b0d
4 changed files with 27 additions and 17 deletions

View File

@@ -224,6 +224,7 @@ message ListMessageRequest {
string chat_id = 2 [(validate.rules).string = {
uuid: true,
}];
int32 user_id = 3 [(validate.rules).int32.gt = 0];
}
message ListMessageResponse {

View File

@@ -27,8 +27,8 @@ interface GetUserByIDDto {
}
interface ListUserDto {
page: number
iser_ids: number[]
page?: number
user_ids?: number[]
}
interface CreateChatDto {
@@ -72,6 +72,7 @@ interface GetMessageDto {
interface ListMessageDto {
page: number
chat_id: string
user_id: number
}
interface VersionDto {}

View File

@@ -52,27 +52,31 @@ const server = Bun.serve({
try {
if (typeof message === 'string') {
const o = JSON.parse(message) as WsData
if (!o) {
console.log('wrong message')
return
if (!o) return
if (o.type === 'CREATE_CHAT') {
const chat = await client.createChat({ users: [{ user_id: o.data.userId }, { user_id: ws.data.userId }] })
ws.send(JSON.stringify({ type: 'CHATS', ...chat }))
}
if (o.type === 'CREATE_MESSAGE') {
console.log('create')
const messageResponse = await client.createMessage({
chat_id: o.data.chat_id,
user_id: ws.data.userId,
text: o.data.text,
})
server.publish(o.data.chat_id, JSON.stringify({ type: 'MESSAGE', ...messageResponse }))
}
if (o.type === 'GET_MESSAGES') {
console.log('GET_MESSAGES')
const messages = await client.listMessage({ chat_id: o.data.chat_id, page: 1 })
const messages = await client.listMessage({ user_id: ws.data.userId, chat_id: o.data.chat_id, page: 1 })
server.publish(o.data.chat_id, JSON.stringify({ type: 'MESSAGES', ...messages }))
}
if (o.type === 'GET_USERS') {
const users = await client.listUser({})
ws.send(JSON.stringify({ type: 'USERS', ...users }))
}
}
} catch (error) {
ws.send(JSON.stringify({ message: 'data error' }))

View File

@@ -33,15 +33,19 @@ interface CreateMessage {
}
}
interface UserData {
type: 'user'
id: number
interface ListUsers {
type: 'GET_USERS'
data: {
page?: number
user_ids?: number[]
}
}
interface MessageData {
type: 'message'
id: number
text: string
interface CreateChat {
type: 'CREATE_CHAT'
data: {
userId: number
}
}
export type WsData = ListMessages | CreateMessage
export type WsData = ListMessages | CreateMessage | ListUsers | CreateChat