diff --git a/package.json b/package.json index 1dbf602..bf7b023 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "module": "index.ts", "type": "module", "private": true, + "scripts": { + "dev": "bun --watch ./src/index.ts" + }, "devDependencies": { "@types/bcrypt": "^6.0.0", "@types/bun": "latest", diff --git a/proto/message.proto b/proto/message.proto index 7d11b9d..4688275 100644 --- a/proto/message.proto +++ b/proto/message.proto @@ -30,6 +30,7 @@ service MessageService { // User message CreateUserRequest { string email = 1; + string name = 2; } message CreateUserResponse { @@ -72,6 +73,7 @@ message User { optional string token = 2; string email = 3; optional string description = 4; + optional string name = 5; } message UserForChatResponse { @@ -86,8 +88,8 @@ message UserForChatResponse { // Chat message CreateChatRequest { - repeated UserForChat users = 1; - // int32 type_id = 2; + optional string name = 1; + repeated UserForChat users = 2; } message UserForChat { @@ -118,8 +120,7 @@ message GetChatResponse { message ListChatRequest { int32 page = 1; - repeated int32 user_ids = 2; - repeated string chat_ids = 3; + int32 user_id = 2; } message ListChatResponse { @@ -128,8 +129,9 @@ message ListChatResponse { message Chat { string id = 1; - int32 type_id = 3; - repeated UserForChatResponse users = 5; + int32 type_id = 2; + optional string name = 3; + repeated UserForChatResponse users = 4; } // Chat end @@ -183,6 +185,8 @@ message Message { optional string image = 4; optional string video = 5; optional string file = 6; + string created_at = 7; + string updated_at = 8; } // Message end diff --git a/src/grpc/client.ts b/src/grpc/client.ts index 589a3f4..1b429ed 100644 --- a/src/grpc/client.ts +++ b/src/grpc/client.ts @@ -27,6 +27,29 @@ interface ListChatDto { chat_ids?: string[] } +interface ListMessageDto { + page: number + chat_id: string +} + +interface CreateMessageDto { + chat_id: string + user_id: number + text?: string + image?: string + video?: string + file?: string +} + +interface Message { + id: number + user_id: number + text?: string + image: string + video: string + file: string +} + interface Chat { id: string type_id: number @@ -43,6 +66,8 @@ enum Services { getUserByEmail = 'getUserByEmail', listUser = 'listUser', listChat = 'listChat', + listMessage = 'listMessage', + createMessage = 'createMessage', } class GrpcClient { @@ -90,6 +115,14 @@ class GrpcClient { getChatsByUser(dto: ListChatDto) { return this.toPromise(this.messageClient, Services.listChat)(dto) } + + getMessagesByChatId(dto: ListMessageDto) { + return this.toPromise(this.messageClient, Services.listMessage)(dto) + } + + createMessage(dto: CreateMessageDto) { + return this.toPromise(this.messageClient, Services.createMessage)(dto) + } } export const grpcClient = new GrpcClient() diff --git a/src/index.ts b/src/index.ts index b45b795..6f53d0c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,7 @@ -import { parseJson } from './utils/utils.ts' import { HttpStatusCodes } from './constants.ts' -import type { WebSocketData } from './types/types.ts' +import type { WebSocketData, WsData } from './types/types.ts' import { loginRequest, wsRequest } from './handles.ts' -import { grpcClient } from './grpc/client.ts' +import { grpcClient as client } from './grpc/client.ts' const GROUP = 'group' const PORT = 3000 @@ -27,13 +26,13 @@ const server = Bun.serve({ const ipAddr = ws.remoteAddress console.log('ipAddr', ipAddr) - const chatResponse = await grpcClient.getChatsByUser({ page: 0, user_ids: [1] }) + const chatResponse = await client.getChatsByUser({ page: 0, user_ids: [1] }) chatResponse.data.forEach((el) => ws.subscribe(el.id)) console.log('chats', chatResponse.data) console.log('subscriptions', ws.subscriptions) - ws.send(JSON.stringify({ type: 'chats', ...chatResponse })) + ws.send(JSON.stringify({ type: 'CHATS', ...chatResponse })) } catch (error) { console.log(error) ws.close(1011, 'error') @@ -46,22 +45,48 @@ const server = Bun.serve({ // text: 'connected' // })) }, - message(ws, message) { + async message(ws, message) { console.log('Websocket message', message) // const result = ws.send(message); - // if (typeof message === 'string') { + try { + if (typeof message === 'string') { + const o = JSON.parse(message) as WsData + if (!o) { + console.log('wrong message') + return + } + + if (o.type === 'CREATE_MESSAGE') { + console.log('create') + const message = 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: 'CREATE_MESSAGE', ...message })) + } + + if (o.type === 'GET_MESSAGES') { + console.log('get') + const messages = await client.getMessagesByChatId({ chat_id: o.data.chat_id, page: 1 }) + server.publish(o.data.chat_id, JSON.stringify({ type: 'MESSAGES', ...messages })) + } + } + } catch (error) { + ws.send(JSON.stringify({ message: 'data error' })) + } + // const json = parseJson(message) // server.publish( // GROUP, // JSON.stringify({ // id: Bun.randomUUIDv7(), - // createdAt: new Date().toISOString(), // username: ws.data.username, // text: json.text, // }), // ) - // } // ws.send(response.arrayBuffer()); // ArrayBuffer // ws.send(new Uint8Array([1, 2, 3])); // TypedArray | DataView diff --git a/src/types/types.ts b/src/types/types.ts index fcebed8..e17045d 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -1,14 +1,24 @@ export interface WebSocketData { chatId?: string token?: string - userId?: number + userId: number } export type LoginDto = { email: string } -interface ChatData { - type: 'chat' - id: string +interface ListMessages { + type: 'GET_MESSAGES' + data: { + chat_id: string + } +} + +interface CreateMessage { + type: 'CREATE_MESSAGE' + data: { + chat_id: string + text: string + } } interface UserData { @@ -22,4 +32,4 @@ interface MessageData { text: string } -type WsData = ChatData | UserData | MessageData +export type WsData = ListMessages|CreateMessage diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 96e275f..33fd8d1 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,6 +1,6 @@ -export function parseJson(str: string) { +export function parseJson(str: string) { try { - return JSON.parse(str) + return JSON.parse(str) as T } catch (e: any) { console.error(e.message) }