mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 02:09:07 +03:00
feat: delete notifications
This commit is contained in:
parent
cd811195f1
commit
c6472013a3
@ -31,7 +31,13 @@ export interface ServerToClientEvents {
|
||||
/**
|
||||
* Chat
|
||||
*/
|
||||
'chat:message': (data: { content: string; time: string; from: string; to: string; notificationId: string }) => void;
|
||||
'chat:message': (data: {
|
||||
content: string;
|
||||
time: string;
|
||||
from: string;
|
||||
to: string;
|
||||
notificationId?: string | undefined;
|
||||
}) => void;
|
||||
}
|
||||
|
||||
export interface ClientToServerEvents {
|
||||
@ -45,6 +51,11 @@ export interface ClientToServerEvents {
|
||||
* Chat
|
||||
*/
|
||||
'chat:message': (data: { content: string; to: string }) => void;
|
||||
|
||||
/**
|
||||
* Notification
|
||||
*/
|
||||
'notification:read': (data: { userId: string; ids: string[] }) => void;
|
||||
}
|
||||
|
||||
export interface InterServerEvents {}
|
||||
@ -130,11 +141,17 @@ class WsService {
|
||||
|
||||
await this.messageStore.add(userId, to, message);
|
||||
|
||||
const notification = await this.notificationStore.add(to, `You received a new private message`, 'MESSAGE');
|
||||
this.io.to(to).emit('chat:message', { ...message, notificationId: notification.id });
|
||||
if (userId !== to) {
|
||||
const notification = await this.notificationStore.add(to, `You received a new private message`, 'MESSAGE');
|
||||
this.io.to(to).emit('chat:message', { ...message, notificationId: notification.id });
|
||||
} else {
|
||||
this.io.to(to).emit('chat:message', { ...message });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('notification:read', data => this.notificationStore.del(data.userId, data.ids));
|
||||
|
||||
/**
|
||||
* Destroy session on user logout request
|
||||
*/
|
||||
|
||||
@ -50,6 +50,14 @@ class RedisNotificationStore {
|
||||
const notification = (await this.redisClient.hgetall(key)) as NotificationType;
|
||||
return notification;
|
||||
}
|
||||
|
||||
public async del(userId: string, keys: string[]): Promise<number> {
|
||||
const k = keys.map(v => `notification:${v}`);
|
||||
const count = await this.redisClient.zrem(`user:${userId}`, k);
|
||||
await this.redisClient.del(k);
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
export default RedisNotificationStore;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user