feat: delete notifications

This commit is contained in:
Sergey Yarkov 2022-02-28 21:39:34 +03:00
parent cd811195f1
commit c6472013a3
2 changed files with 28 additions and 3 deletions

View File

@ -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);
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
*/

View File

@ -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;