-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
88 additions
and
30 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
api/prisma/migrations/20231110173333_user_status/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,13 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `isConnected` on the `User` table. All the data in the column will be lost. | ||
- Added the required column `status` to the `User` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- CreateEnum | ||
CREATE TYPE "UserStatus" AS ENUM ('OFFLINE', 'ONLINE', 'INGAME'); | ||
|
||
-- AlterTable | ||
ALTER TABLE "User" DROP COLUMN "isConnected", | ||
ADD COLUMN "status" "UserStatus" NOT NULL; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './notify.module'; | ||
export * from './notify.service'; |
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 |
---|---|---|
@@ -1,52 +1,50 @@ | ||
import { Logger, UseFilters, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common'; | ||
import { Logger, UseFilters, UseGuards } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { | ||
OnGatewayConnection, | ||
OnGatewayDisconnect, | ||
OnGatewayInit, | ||
WebSocketGateway, | ||
WebSocketServer, | ||
} from '@nestjs/websockets'; | ||
import { Server, Socket } from 'socket.io'; | ||
|
||
import { HttpExceptionTransformationFilter } from '@/utils/ws-http-exception.filter'; | ||
|
||
import { WSAuthMiddleware } from '../auth/ws/ws.middleware'; | ||
import { WsJwtGuard } from '../auth/ws/ws-jwt.guard'; | ||
import { FriendService, UserService } from '../user'; | ||
import { UserService } from '../user'; | ||
import { NotifyService } from './notify.service'; | ||
|
||
@UseGuards(WsJwtGuard) | ||
@UsePipes(new ValidationPipe()) | ||
@WebSocketGateway({ namespace: 'notify' }) | ||
@UseFilters(HttpExceptionTransformationFilter) | ||
export class NotifyGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect { | ||
@WebSocketServer() | ||
private io: Server; | ||
private logger: Logger = new Logger(NotifyGateway.name); | ||
private socketsID: Map<string, string> = new Map<string, string>(); | ||
private sockets: Map<string, Socket> = new Map<string, Socket>(); | ||
|
||
constructor( | ||
private jwtService: JwtService, | ||
private userService: UserService, | ||
private friendService: FriendService, | ||
private notifyService: NotifyService, | ||
) {} | ||
|
||
afterInit(server: Server) { | ||
const authMiddleware = WSAuthMiddleware(this.jwtService, this.userService); | ||
server.use(authMiddleware); | ||
|
||
this.notifyService.sockets = this.sockets; | ||
} | ||
|
||
async handleConnection(socket: Socket) { | ||
this.logger.log('Client connected: ' + socket.id); | ||
this.socketsID.set(socket.data.user.login, socket.id); | ||
this.sockets.set(socket.data.user.login, socket); | ||
|
||
const friends = await this.friendService.getFriendList(socket.data.user); | ||
for (const friend of friends) { | ||
const socketID = this.socketsID.get(friend.login); | ||
} | ||
await this.notifyService.online(socket.data.user); | ||
} | ||
|
||
handleDisconnect(socket: Socket) { | ||
this.socketsID.delete(socket.data.user.login); | ||
async handleDisconnect(socket: Socket) { | ||
await this.notifyService.offline(socket.data.user); | ||
|
||
this.sockets.delete(socket.data.user.login); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
import { FriendService, UserService } from '../user'; | ||
import { UserModule } from '../user'; | ||
import { NotifyGateway } from './notify.gateway'; | ||
import { NotifyService } from './notify.service'; | ||
|
||
@Module({ | ||
controllers: [], | ||
providers: [NotifyService, JwtService, UserService, FriendService], | ||
providers: [NotifyGateway, NotifyService], | ||
imports: [UserModule], | ||
exports: [NotifyService], | ||
}) | ||
export class NotifyModule {} |
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 |
---|---|---|
@@ -1 +1,36 @@ | ||
export class NotifyService {} | ||
import { Injectable } from '@nestjs/common'; | ||
import { User, UserStatus } from '@prisma/client'; | ||
import { Socket } from 'socket.io'; | ||
|
||
import { FriendService } from '../user'; | ||
|
||
@Injectable() | ||
export class NotifyService { | ||
public sockets: Map<string, Socket>; | ||
|
||
constructor(private friendService: FriendService) {} | ||
|
||
async emitToFriends(user: User, status: UserStatus) { | ||
const friends = await this.friendService.getFriendList(user, true); | ||
|
||
for (const friend of friends) { | ||
const socket = this.sockets.get(friend.login); | ||
|
||
if (socket) { | ||
socket.emit('notify', { user: user, status: status }); | ||
} | ||
} | ||
} | ||
|
||
async online(user: User) { | ||
await this.emitToFriends(user, UserStatus.ONLINE); | ||
} | ||
|
||
async offline(user: User) { | ||
await this.emitToFriends(user, UserStatus.OFFLINE); | ||
} | ||
|
||
async inGame(user: User) { | ||
await this.emitToFriends(user, UserStatus.INGAME); | ||
} | ||
} |
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