forked from QuarkGluonPlasma/nestjs-course-code
-
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: chat-room friendship group chat notification
- Loading branch information
1 parent
8be4bc3
commit 35a2ffd
Showing
46 changed files
with
1,545 additions
and
191 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "debug nest", | ||
"request": "launch", | ||
"runtimeExecutable": "npm", | ||
"runtimeArgs": [ | ||
"run", | ||
"start:dev" | ||
], | ||
"console": "integratedTerminal", | ||
"type": "node" | ||
} | ||
] | ||
} |
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
12 changes: 12 additions & 0 deletions
12
chat-room-backend/prisma/migrations/20240802135008_chat_history/migration.sql
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,12 @@ | ||
-- CreateTable | ||
CREATE TABLE `ChatHistory` ( | ||
`id` INTEGER NOT NULL AUTO_INCREMENT, | ||
`content` VARCHAR(500) NOT NULL, | ||
`type` INTEGER NOT NULL, | ||
`chatroomId` INTEGER NOT NULL, | ||
`senderId` INTEGER NOT NULL, | ||
`createTime` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), | ||
`updateTime` DATETIME(3) NOT NULL, | ||
|
||
PRIMARY KEY (`id`) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
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
12 changes: 12 additions & 0 deletions
12
chat-room-backend/src/chat-history/chat-history.controller.ts
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,12 @@ | ||
import { Controller, Get, Query, Res } from '@nestjs/common'; | ||
import { ChatHistoryService } from './chat-history.service'; | ||
|
||
@Controller('chat-history') | ||
export class ChatHistoryController { | ||
constructor(private readonly chatHistoryService: ChatHistoryService) {} | ||
|
||
@Get('list') | ||
async list(@Query('chatroomId') chatroomId: string) { | ||
return this.chatHistoryService.list(+chatroomId); | ||
} | ||
} |
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,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ChatHistoryService } from './chat-history.service'; | ||
import { ChatHistoryController } from './chat-history.controller'; | ||
|
||
@Module({ | ||
controllers: [ChatHistoryController], | ||
providers: [ChatHistoryService], | ||
exports: [ChatHistoryService], | ||
}) | ||
export class ChatHistoryModule {} |
26 changes: 26 additions & 0 deletions
26
chat-room-backend/src/chat-history/chat-history.service.ts
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,26 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { ChatHistory } from '@prisma/client'; | ||
import { PrismaService } from 'src/prisma/prisma.service'; | ||
|
||
export type HistoryDto = Pick<ChatHistory, 'chatroomId' | 'senderId' | 'type' | 'content'>; | ||
|
||
@Injectable() | ||
export class ChatHistoryService { | ||
@Inject(PrismaService) | ||
private prismaService: PrismaService; | ||
|
||
async list(chatroomId: number) { | ||
return this.prismaService.chatHistory.findMany({ | ||
where: { | ||
chatroomId | ||
} | ||
}); | ||
} | ||
|
||
async add(chatroomId: number, history: HistoryDto) { | ||
return this.prismaService.chatHistory.create({ | ||
data: history | ||
}); | ||
} | ||
|
||
} |
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,60 @@ | ||
import { ChatHistory } from '@prisma/client'; | ||
import { MessageBody, SubscribeMessage, WebSocketGateway, WebSocketServer } from '@nestjs/websockets'; | ||
import { ChatService } from './chat.service'; | ||
import { Server, Socket } from 'socket.io'; | ||
import { ChatHistoryService } from 'src/chat-history/chat-history.service'; | ||
import { Inject } from '@nestjs/common'; | ||
|
||
interface JoinRoomPayload { | ||
chatroomId: number | ||
userId: number | ||
} | ||
|
||
interface SendMessagePayload { | ||
sendUserId: number; | ||
chatroomId: number; | ||
message: { | ||
type: 'text' | 'image', | ||
content: string | ||
} | ||
} | ||
|
||
@WebSocketGateway({cors: { origin: '*' }}) | ||
export class ChatGateway { | ||
constructor(private readonly chatService: ChatService) {} | ||
|
||
@WebSocketServer() server: Server; | ||
|
||
@SubscribeMessage('joinRoom') | ||
joinRoom(client: Socket, payload: JoinRoomPayload): void { | ||
const roomName = payload.chatroomId.toString(); | ||
|
||
client.join(roomName) | ||
|
||
this.server.to(roomName).emit('message', { | ||
type: 'joinRoom', | ||
userId: payload.userId | ||
}); | ||
} | ||
|
||
@Inject(ChatHistoryService) | ||
private chatHistoryService: ChatHistoryService | ||
|
||
@SubscribeMessage('sendMessage') | ||
async sendMessage(@MessageBody() payload: SendMessagePayload) { | ||
const roomName = payload.chatroomId.toString(); | ||
|
||
await this.chatHistoryService.add(payload.chatroomId, { | ||
content: payload.message.content, | ||
type: payload.message.type === 'image' ? 1 : 0, | ||
chatroomId: payload.chatroomId, | ||
senderId: payload.sendUserId | ||
}); | ||
|
||
this.server.to(roomName).emit('message', { | ||
type: 'sendMessage', | ||
userId: payload.sendUserId, | ||
message: payload.message | ||
}); | ||
} | ||
} |
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,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ChatService } from './chat.service'; | ||
import { ChatGateway } from './chat.gateway'; | ||
import { ChatHistoryModule } from 'src/chat-history/chat-history.module'; | ||
|
||
@Module({ | ||
imports: [ChatHistoryModule], | ||
providers: [ChatGateway, ChatService], | ||
}) | ||
export class ChatModule {} |
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,4 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class ChatService {} |
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
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
Oops, something went wrong.