wip
This commit is contained in:
@@ -224,6 +224,7 @@ message ListMessageRequest {
|
|||||||
string chat_id = 2 [(validate.rules).string = {
|
string chat_id = 2 [(validate.rules).string = {
|
||||||
uuid: true,
|
uuid: true,
|
||||||
}];
|
}];
|
||||||
|
int32 user_id = 3 [(validate.rules).int32.gt = 0];
|
||||||
}
|
}
|
||||||
|
|
||||||
message ListMessageResponse {
|
message ListMessageResponse {
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ interface GetUserByIDDto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface ListUserDto {
|
interface ListUserDto {
|
||||||
page: number
|
page?: number
|
||||||
iser_ids: number[]
|
user_ids?: number[]
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CreateChatDto {
|
interface CreateChatDto {
|
||||||
@@ -72,6 +72,7 @@ interface GetMessageDto {
|
|||||||
interface ListMessageDto {
|
interface ListMessageDto {
|
||||||
page: number
|
page: number
|
||||||
chat_id: string
|
chat_id: string
|
||||||
|
user_id: number
|
||||||
}
|
}
|
||||||
|
|
||||||
interface VersionDto {}
|
interface VersionDto {}
|
||||||
|
|||||||
18
src/index.ts
18
src/index.ts
@@ -52,27 +52,31 @@ const server = Bun.serve({
|
|||||||
try {
|
try {
|
||||||
if (typeof message === 'string') {
|
if (typeof message === 'string') {
|
||||||
const o = JSON.parse(message) as WsData
|
const o = JSON.parse(message) as WsData
|
||||||
if (!o) {
|
if (!o) return
|
||||||
console.log('wrong message')
|
|
||||||
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') {
|
if (o.type === 'CREATE_MESSAGE') {
|
||||||
console.log('create')
|
|
||||||
const messageResponse = await client.createMessage({
|
const messageResponse = await client.createMessage({
|
||||||
chat_id: o.data.chat_id,
|
chat_id: o.data.chat_id,
|
||||||
user_id: ws.data.userId,
|
user_id: ws.data.userId,
|
||||||
text: o.data.text,
|
text: o.data.text,
|
||||||
})
|
})
|
||||||
|
|
||||||
server.publish(o.data.chat_id, JSON.stringify({ type: 'MESSAGE', ...messageResponse }))
|
server.publish(o.data.chat_id, JSON.stringify({ type: 'MESSAGE', ...messageResponse }))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (o.type === 'GET_MESSAGES') {
|
if (o.type === 'GET_MESSAGES') {
|
||||||
console.log('GET_MESSAGES')
|
const messages = await client.listMessage({ user_id: ws.data.userId, chat_id: o.data.chat_id, page: 1 })
|
||||||
const messages = await client.listMessage({ chat_id: o.data.chat_id, page: 1 })
|
|
||||||
server.publish(o.data.chat_id, JSON.stringify({ type: 'MESSAGES', ...messages }))
|
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) {
|
} catch (error) {
|
||||||
ws.send(JSON.stringify({ message: 'data error' }))
|
ws.send(JSON.stringify({ message: 'data error' }))
|
||||||
|
|||||||
@@ -33,15 +33,19 @@ interface CreateMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UserData {
|
interface ListUsers {
|
||||||
type: 'user'
|
type: 'GET_USERS'
|
||||||
id: number
|
data: {
|
||||||
|
page?: number
|
||||||
|
user_ids?: number[]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MessageData {
|
interface CreateChat {
|
||||||
type: 'message'
|
type: 'CREATE_CHAT'
|
||||||
id: number
|
data: {
|
||||||
text: string
|
userId: number
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type WsData = ListMessages | CreateMessage
|
export type WsData = ListMessages | CreateMessage | ListUsers | CreateChat
|
||||||
|
|||||||
Reference in New Issue
Block a user