-
Notifications
You must be signed in to change notification settings - Fork 0
NCTO-155-make-notifications-system #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Notgoyome
wants to merge
12
commits into
main
Choose a base branch
from
NCTO-155-make-notifications-system
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b8b0f8d
[BACKEND] [FEATURE] Implement notifications system
Notgoyome 213357a
[BACKEND] [FIX] Remove unused markAllAsRead endpoint from notificatio…
Notgoyome 02501dd
[BACKEND] [REFactor] Enhance WebSocket connection handling and authen…
Notgoyome 5f96e3d
[FIX] Improve error handling in markAsRea
Notgoyome d7e7aa3
[BACKEND] [FIX] Handle WebSocket upgrade
Notgoyome 722d570
[BACKEND] [FIX] Enhance raw data handling in WebSocket authentication
Notgoyome 7eb415e
[MAIN][FIX]: lint error
Notgoyome d72ca5c
[BACKEND] [FIX] ws upgrade handler
Notgoyome 225b7a7
[BACKEND] [FIX] Ensure notification retrieval respects maximum limit …
Notgoyome 353658c
[BACKEND] [REFACT] Rename setupWebSocketServer to setupCollaborationS…
Notgoyome f4ba8da
[BACKEND][FIX] Convert notification ID to string in payload
Notgoyome 7724251
[BACKEND][REFACTO] Change notification type from string to enum
Notgoyome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
prisma/migrations/20260222071209_add_notifications/migration.sql
This file contains hidden or 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,21 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "Notification" ( | ||
| "id" SERIAL NOT NULL, | ||
| "userId" INTEGER NOT NULL, | ||
| "title" TEXT NOT NULL, | ||
| "message" TEXT NOT NULL, | ||
| "type" TEXT NOT NULL, | ||
| "read" BOOLEAN NOT NULL DEFAULT false, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
|
||
| CONSTRAINT "Notification_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "Notification_userId_createdAt_idx" ON "Notification"("userId", "createdAt"); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "Notification_userId_read_idx" ON "Notification"("userId", "read"); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "Notification" ADD CONSTRAINT "Notification_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains hidden or 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 @@ | ||
| enum NotificationType { | ||
| INFO | ||
| WARNING | ||
| } | ||
|
|
||
| model Notification { | ||
| id Int @id @default(autoincrement()) | ||
| userId Int | ||
| title String | ||
| message String | ||
| type NotificationType | ||
| read Boolean @default(false) | ||
| createdAt DateTime @default(now()) | ||
|
|
||
| user User @relation(fields: [userId], references: [id], onDelete: Cascade) | ||
|
|
||
| @@index([userId, createdAt]) | ||
| @@index([userId, read]) | ||
| } |
This file contains hidden or 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 hidden or 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,25 +1,27 @@ | ||
| import { Module } from "@nestjs/common"; | ||
| import { ConfigModule } from "@nestjs/config"; | ||
| import { S3Module } from "@s3/s3.module"; | ||
| import { UserModule } from "@user/user.module"; | ||
| import { ProjectModule } from "@projects/project.module"; | ||
| import { WorkSessionModule } from "./routes/work-session/work-session.module"; | ||
| import { PrismaModule } from "@prisma/prisma.module"; | ||
| import { AuthModule } from "@auth/auth.module"; | ||
| import { ScheduleModule } from "@nestjs/schedule"; | ||
| import { TasksModule } from "./tasks/tasks.module"; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| ConfigModule.forRoot({ isGlobal: true }), | ||
| ScheduleModule.forRoot(), | ||
| PrismaModule, | ||
| AuthModule, | ||
| S3Module, | ||
| UserModule, | ||
| ProjectModule, | ||
| WorkSessionModule, | ||
| TasksModule | ||
| ] | ||
| }) | ||
| export class AppModule {} | ||
| import { Module } from "@nestjs/common"; | ||
| import { ConfigModule } from "@nestjs/config"; | ||
| import { S3Module } from "@s3/s3.module"; | ||
| import { UserModule } from "@user/user.module"; | ||
| import { ProjectModule } from "@projects/project.module"; | ||
| import { WorkSessionModule } from "./routes/work-session/work-session.module"; | ||
| import { PrismaModule } from "@prisma/prisma.module"; | ||
| import { AuthModule } from "@auth/auth.module"; | ||
| import { ScheduleModule } from "@nestjs/schedule"; | ||
| import { TasksModule } from "./tasks/tasks.module"; | ||
| import { NotificationsModule } from "./notifications/notifications.module"; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| ConfigModule.forRoot({ isGlobal: true }), | ||
| ScheduleModule.forRoot(), | ||
| PrismaModule, | ||
| AuthModule, | ||
| S3Module, | ||
| UserModule, | ||
| ProjectModule, | ||
| WorkSessionModule, | ||
| TasksModule, | ||
| NotificationsModule, | ||
| ] | ||
| }) | ||
| export class AppModule {} |
This file contains hidden or 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,133 +1,139 @@ | ||
| import { WebSocket, WebSocketServer } from "ws"; | ||
| import http from "http"; | ||
| import * as map from "lib0/map"; | ||
|
|
||
| const WS_READY_STATE_CONNECTING = 0; | ||
| const WS_READY_STATE_OPEN = 1; | ||
| const PING_TIMEOUT = 30000; | ||
|
|
||
| const topics = new Map<string, Set<WebSocket>>(); | ||
|
|
||
| interface WSMessage { | ||
| type: "subscribe" | "unsubscribe" | "publish" | "ping" | "pong"; | ||
| topics?: string[]; | ||
| topic?: string; | ||
| clients?: number; | ||
| } | ||
|
|
||
| export let wss: WebSocketServer; | ||
|
|
||
| export const setupWebSocketServer = (server: http.Server): void => { | ||
| wss = new WebSocketServer({ noServer: true }); | ||
|
|
||
| const send = (conn: WebSocket, message: WSMessage): void => { | ||
| if ( | ||
| conn.readyState !== WS_READY_STATE_CONNECTING && | ||
| conn.readyState !== WS_READY_STATE_OPEN | ||
| ) { | ||
| conn.close(); | ||
| } | ||
| try { | ||
| conn.send(JSON.stringify(message)); | ||
| } catch { | ||
| conn.close(); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| const onConnection = (conn: WebSocket): void => { | ||
| const subscribedTopics = new Set<string>(); | ||
| let closed = false; | ||
| let pongReceived = true; | ||
| const pingInterval = setInterval(() => { | ||
| if (!pongReceived) { | ||
| conn.close(); | ||
| clearInterval(pingInterval); | ||
| } else { | ||
| pongReceived = false; | ||
| try { | ||
| conn.ping(); | ||
| } catch { | ||
| conn.close(); | ||
| } | ||
| } | ||
| }, PING_TIMEOUT); | ||
|
|
||
| conn.on("pong", () => { | ||
| pongReceived = true; | ||
| }); | ||
|
|
||
| conn.on("close", () => { | ||
| subscribedTopics.forEach((topicName) => { | ||
| const subs = topics.get(topicName) || new Set(); | ||
| subs.delete(conn); | ||
| if (subs.size === 0) { | ||
| topics.delete(topicName); | ||
| } | ||
| }); | ||
| subscribedTopics.clear(); | ||
| closed = true; | ||
| }); | ||
|
|
||
| conn.on("message", (raw: import("ws").RawData): void => { | ||
| let message: WSMessage; | ||
|
|
||
| try { | ||
| const parsed = JSON.parse( | ||
| typeof raw === "string" ? raw : raw.toString() | ||
| ); | ||
| message = parsed as WSMessage; | ||
| } catch { | ||
| conn.close(); | ||
| return; | ||
| } | ||
|
|
||
| if (message && message.type && !closed) { | ||
| switch (message.type) { | ||
| case "subscribe": | ||
| (message.topics || []).forEach((topicName: string) => { | ||
| if (typeof topicName === "string") { | ||
| const topic = map.setIfUndefined( | ||
| topics, | ||
| topicName, | ||
| () => new Set<WebSocket>() | ||
| ); | ||
| topic.add(conn); | ||
| subscribedTopics.add(topicName); | ||
| } | ||
| }); | ||
| break; | ||
| case "unsubscribe": | ||
| (message.topics || []).forEach((topicName: string) => { | ||
| const subs = topics.get(topicName); | ||
| if (subs) { | ||
| subs.delete(conn); | ||
| } | ||
| }); | ||
| break; | ||
| case "publish": | ||
| if (message.topic) { | ||
| const receivers = topics.get(message.topic); | ||
| if (receivers) { | ||
| message.clients = receivers.size; | ||
| receivers.forEach((receiver) => send(receiver, message)); | ||
| } | ||
| } | ||
| break; | ||
| case "ping": | ||
| send(conn, { type: "pong" } as WSMessage); | ||
| break; | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| wss.on("connection", onConnection); | ||
|
|
||
| server.on("upgrade", (request, socket, head) => { | ||
| const handleAuth = (ws: WebSocket): void => { | ||
| wss.emit("connection", ws, request); | ||
| }; | ||
| wss.handleUpgrade(request, socket, head, handleAuth); | ||
| }); | ||
| }; | ||
| import { WebSocket, WebSocketServer } from "ws"; | ||
| import http from "http"; | ||
| import * as map from "lib0/map"; | ||
|
|
||
| const WS_READY_STATE_CONNECTING = 0; | ||
| const WS_READY_STATE_OPEN = 1; | ||
| const PING_TIMEOUT = 30000; | ||
|
|
||
| const topics = new Map<string, Set<WebSocket>>(); | ||
|
|
||
| interface WSMessage { | ||
| type: "subscribe" | "unsubscribe" | "publish" | "ping" | "pong"; | ||
| topics?: string[]; | ||
| topic?: string; | ||
| clients?: number; | ||
| } | ||
|
|
||
| export let wss: WebSocketServer; | ||
| type UpgradeRequest = http.IncomingMessage & { _wsHandled?: boolean }; | ||
|
|
||
| export const setupCollaborationSocket = (server: http.Server): void => { | ||
| wss = new WebSocketServer({ noServer: true }); | ||
|
|
||
| const send = (conn: WebSocket, message: WSMessage): void => { | ||
| if ( | ||
| conn.readyState !== WS_READY_STATE_CONNECTING && | ||
| conn.readyState !== WS_READY_STATE_OPEN | ||
| ) { | ||
| conn.close(); | ||
| } | ||
| try { | ||
| conn.send(JSON.stringify(message)); | ||
| } catch { | ||
| conn.close(); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| const onConnection = (conn: WebSocket): void => { | ||
| const subscribedTopics = new Set<string>(); | ||
| let closed = false; | ||
| let pongReceived = true; | ||
| const pingInterval = setInterval(() => { | ||
| if (!pongReceived) { | ||
| conn.close(); | ||
| clearInterval(pingInterval); | ||
| } else { | ||
| pongReceived = false; | ||
| try { | ||
| conn.ping(); | ||
| } catch { | ||
| conn.close(); | ||
| } | ||
| } | ||
| }, PING_TIMEOUT); | ||
|
|
||
| conn.on("pong", () => { | ||
| pongReceived = true; | ||
| }); | ||
|
|
||
| conn.on("close", () => { | ||
| subscribedTopics.forEach((topicName) => { | ||
| const subs = topics.get(topicName) || new Set(); | ||
| subs.delete(conn); | ||
| if (subs.size === 0) { | ||
| topics.delete(topicName); | ||
| } | ||
| }); | ||
| subscribedTopics.clear(); | ||
| closed = true; | ||
| }); | ||
|
|
||
| conn.on("message", (raw: import("ws").RawData): void => { | ||
| let message: WSMessage; | ||
|
|
||
| try { | ||
| const parsed = JSON.parse( | ||
| typeof raw === "string" ? raw : raw.toString() | ||
| ); | ||
| message = parsed as WSMessage; | ||
| } catch { | ||
| conn.close(); | ||
| return; | ||
| } | ||
|
|
||
| if (message && message.type && !closed) { | ||
| switch (message.type) { | ||
| case "subscribe": | ||
| (message.topics || []).forEach((topicName: string) => { | ||
| if (typeof topicName === "string") { | ||
| const topic = map.setIfUndefined( | ||
| topics, | ||
| topicName, | ||
| () => new Set<WebSocket>() | ||
| ); | ||
| topic.add(conn); | ||
| subscribedTopics.add(topicName); | ||
| } | ||
| }); | ||
| break; | ||
| case "unsubscribe": | ||
| (message.topics || []).forEach((topicName: string) => { | ||
| const subs = topics.get(topicName); | ||
| if (subs) { | ||
| subs.delete(conn); | ||
| } | ||
| }); | ||
| break; | ||
| case "publish": | ||
| if (message.topic) { | ||
| const receivers = topics.get(message.topic); | ||
| if (receivers) { | ||
| message.clients = receivers.size; | ||
| receivers.forEach((receiver) => send(receiver, message)); | ||
| } | ||
| } | ||
| break; | ||
| case "ping": | ||
| send(conn, { type: "pong" } as WSMessage); | ||
| break; | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| wss.on("connection", onConnection); | ||
|
|
||
| server.on("upgrade", (request: UpgradeRequest, socket, head) => { | ||
| const url = request.url ?? ""; | ||
| if (!url.startsWith("/socket/webrtc")) { | ||
| return; | ||
| } | ||
| request._wsHandled = true; | ||
| const handleAuth = (ws: WebSocket): void => { | ||
| wss.emit("connection", ws, request); | ||
| }; | ||
| wss.handleUpgrade(request, socket, head, handleAuth); | ||
| }); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.