wip
This commit is contained in:
@@ -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 {}
|
||||
|
||||
18
src/index.ts
18
src/index.ts
@@ -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' }))
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user