Skip to content

Commit

Permalink
feat: chat-room friendship group chat notification
Browse files Browse the repository at this point in the history
  • Loading branch information
QuarkGluonPlasma committed Aug 2, 2024
1 parent 8be4bc3 commit 35a2ffd
Show file tree
Hide file tree
Showing 46 changed files with 1,545 additions and 191 deletions.
19 changes: 19 additions & 0 deletions chat-room-backend/.vscode/launch.json
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"
}
]
}
6 changes: 5 additions & 1 deletion chat-room-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
"@nestjs/jwt": "^10.2.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/platform-socket.io": "^10.3.10",
"@nestjs/websockets": "^10.3.10",
"@prisma/client": "^5.12.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"minio": "^8.0.1",
"nodemailer": "^6.9.13",
"redis": "^4.6.13",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"socket.io": "^4.7.5"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
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;
11 changes: 11 additions & 0 deletions chat-room-backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,14 @@ model UserChatroom {
@@id([userId, chatroomId])
}

model ChatHistory {
id Int @id @default(autoincrement())
content String @db.VarChar(500)
//聊天记录类型 text:0、image:1、file:2
type Int
chatroomId Int
senderId Int
createTime DateTime @default(now())
updateTime DateTime @updatedAt
}
6 changes: 6 additions & 0 deletions chat-room-backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { APP_GUARD } from '@nestjs/core';
import { AuthGuard } from './auth.guard';
import { FriendshipModule } from './friendship/friendship.module';
import { ChatroomModule } from './chatroom/chatroom.module';
import { MinioModule } from './minio/minio.module';
import { ChatModule } from './chat/chat.module';
import { ChatHistoryModule } from './chat-history/chat-history.module';

@Module({
imports: [
Expand All @@ -31,6 +34,9 @@ import { ChatroomModule } from './chatroom/chatroom.module';
}),
FriendshipModule,
ChatroomModule,
MinioModule,
ChatModule,
ChatHistoryModule,
],
controllers: [AppController],
providers: [
Expand Down
12 changes: 12 additions & 0 deletions chat-room-backend/src/chat-history/chat-history.controller.ts
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);
}
}
10 changes: 10 additions & 0 deletions chat-room-backend/src/chat-history/chat-history.module.ts
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 chat-room-backend/src/chat-history/chat-history.service.ts
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
});
}

}
60 changes: 60 additions & 0 deletions chat-room-backend/src/chat/chat.gateway.ts
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
});
}
}
10 changes: 10 additions & 0 deletions chat-room-backend/src/chat/chat.module.ts
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 {}
4 changes: 4 additions & 0 deletions chat-room-backend/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class ChatService {}
4 changes: 2 additions & 2 deletions chat-room-backend/src/chatroom/chatroom.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export class ChatroomController {
}

@Get('list')
async list(@UserInfo('userId') userId: number) {
async list(@UserInfo('userId') userId: number, @Query('name') name: string) {
if(!userId) {
throw new BadRequestException('userId 不能为空')
}
return this.chatroomService.list(userId);
return this.chatroomService.list(userId, name);
}

@Get('members')
Expand Down
5 changes: 4 additions & 1 deletion chat-room-backend/src/chatroom/chatroom.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ChatroomService {
return '创建成功'
}

async list(userId: number) {
async list(userId: number, name: string) {
const chatroomIds = await this.prismaService.userChatroom.findMany({
where: {
userId
Expand All @@ -62,6 +62,9 @@ export class ChatroomService {
where: {
id: {
in: chatroomIds.map(item => item.chatroomId)
},
name: {
contains: name
}
},
select: {
Expand Down
4 changes: 2 additions & 2 deletions chat-room-backend/src/friendship/dto/friend-add.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { IsNotEmpty } from "class-validator";
export class FriendAddDto {

@IsNotEmpty({
message: "添加的好友 id 不能为空"
message: "添加好友的 username 不能为空"
})
friendId: number;
username: string;

reason: string;
}
4 changes: 2 additions & 2 deletions chat-room-backend/src/friendship/friendship.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class FriendshipController {
}

@Get('list')
async friendship(@UserInfo('userId') userId: number) {
return this.friendshipService.getFriendship(userId);
async friendship(@UserInfo('userId') userId: number, @Query('name') name: string) {
return this.friendshipService.getFriendship(userId, name);
}

@Get('remove/:id')
Expand Down
Loading

0 comments on commit 35a2ffd

Please sign in to comment.