From ec9d33d3b13f614b8df971fac4e9daa231c1cf91 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sat, 30 Jul 2022 14:08:31 +0800 Subject: [PATCH] server: improve message notify --- packages/server/src/constants/index.ts | 1 + .../server/src/services/comment.service.ts | 3 ++ .../server/src/services/document.service.ts | 4 +++ .../server/src/services/message.service.ts | 32 ++++++++++++++----- .../src/services/organization.service.ts | 3 ++ packages/server/src/services/wiki.service.ts | 3 ++ 6 files changed, 38 insertions(+), 8 deletions(-) diff --git a/packages/server/src/constants/index.ts b/packages/server/src/constants/index.ts index 7fc75818..8fede7e3 100644 --- a/packages/server/src/constants/index.ts +++ b/packages/server/src/constants/index.ts @@ -2,4 +2,5 @@ export enum RedisDBEnum { documentVersion = 0, view = 1, verify = 2, + message = 3, } diff --git a/packages/server/src/services/comment.service.ts b/packages/server/src/services/comment.service.ts index 7512a84d..bd2e8ce9 100644 --- a/packages/server/src/services/comment.service.ts +++ b/packages/server/src/services/comment.service.ts @@ -97,6 +97,7 @@ export class CommentService { wikiId: doc.wikiId, documentId: doc.id, }), + uniqueId: ret.id, }); }) ); @@ -206,6 +207,7 @@ export class CommentService { wikiId: doc.wikiId, documentId: doc.id, }), + uniqueId: newData.id, }); }) ); @@ -234,6 +236,7 @@ export class CommentService { wikiId: doc.wikiId, documentId: doc.id, }), + uniqueId: data.id, }); }) ); diff --git a/packages/server/src/services/document.service.ts b/packages/server/src/services/document.service.ts index 797e802d..16a94d8d 100644 --- a/packages/server/src/services/document.service.ts +++ b/packages/server/src/services/document.service.ts @@ -136,6 +136,7 @@ export class DocumentService { wikiId: doc.wikiId, documentId: doc.id, }), + uniqueId: doc.id, }); } @@ -174,6 +175,7 @@ export class DocumentService { wikiId: doc.wikiId, documentId: doc.id, }), + uniqueId: doc.id, }); } @@ -212,6 +214,7 @@ export class DocumentService { wikiId: doc.wikiId, documentId: doc.id, }), + uniqueId: doc.id, }); } @@ -751,6 +754,7 @@ export class DocumentService { wikiId: doc.wikiId, documentId: doc.id, }), + uniqueId: doc.id, }); }) .filter(Boolean) diff --git a/packages/server/src/services/message.service.ts b/packages/server/src/services/message.service.ts index fbd1a11e..ab2acdd3 100644 --- a/packages/server/src/services/message.service.ts +++ b/packages/server/src/services/message.service.ts @@ -3,13 +3,30 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { IUser } from '@think/domains'; import { Repository } from 'typeorm'; +import Redis from 'ioredis'; +import { buildRedis } from '@helpers/redis.helper'; +import { RedisDBEnum } from '@constants/*'; +import { uniqueId } from 'lodash'; @Injectable() export class MessageService { + private redis: Redis; + constructor( @InjectRepository(MessageEntity) private readonly messageRepo: Repository - ) {} + ) { + this.buildRedis(); + } + + private async buildRedis() { + try { + this.redis = await buildRedis(RedisDBEnum.message); + console.log('[think] 消息服务启动成功'); + } catch (e) { + console.error(`[think] 消息服务启动错误: "${e.message}"`); + } + } /** * 新消息通知 @@ -17,16 +34,15 @@ export class MessageService { * @param msg * @returns */ - async notify(userId: IUser['id'], msg) { - const data = { userId, ...msg }; - const mayBeNotified = await this.messageRepo.findOne(data); + async notify(userId: IUser['id'], msg: { title: string; message: string; url: string; uniqueId: string | number }) { + const key = `message-${userId}-${uniqueId}`; - if (mayBeNotified) { - if (!mayBeNotified.read) { - return; - } + if (await this.redis.get(key)) { + return; } + const data = { userId, ...msg }; + await this.redis.set(key, Date.now(), 'EX', 5 * 60); const res = await this.messageRepo.create(data); const ret = await this.messageRepo.save(res); return ret; diff --git a/packages/server/src/services/organization.service.ts b/packages/server/src/services/organization.service.ts index 178f62ab..8dc5c164 100644 --- a/packages/server/src/services/organization.service.ts +++ b/packages/server/src/services/organization.service.ts @@ -218,6 +218,7 @@ export class OrganizationService { url: buildMessageURL('toOrganization')({ organizationId: organization.id, }), + uniqueId: organization.id, }); return ret; @@ -256,6 +257,7 @@ export class OrganizationService { url: buildMessageURL('toOrganization')({ organizationId: organization.id, }), + uniqueId: organization.id, }); return ret; @@ -294,6 +296,7 @@ export class OrganizationService { url: buildMessageURL('toOrganization')({ organizationId: organization.id, }), + uniqueId: organization.id, }); return ret; diff --git a/packages/server/src/services/wiki.service.ts b/packages/server/src/services/wiki.service.ts index 7c449ae3..ecef94a2 100644 --- a/packages/server/src/services/wiki.service.ts +++ b/packages/server/src/services/wiki.service.ts @@ -118,6 +118,7 @@ export class WikiService { organizationId: wiki.organizationId, wikiId: wiki.id, }), + uniqueId: wiki.id, }); } @@ -165,6 +166,7 @@ export class WikiService { organizationId: wiki.organizationId, wikiId: wiki.id, }), + uniqueId: wiki.id, }); } @@ -212,6 +214,7 @@ export class WikiService { organizationId: wiki.organizationId, wikiId: wiki.id, }), + uniqueId: wiki.id, }); }