From ec9d33d3b13f614b8df971fac4e9daa231c1cf91 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sat, 30 Jul 2022 14:08:31 +0800 Subject: [PATCH 1/2] 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, }); } From 2eb88074db8823dfcb5d8c8c1dbccc1c91813c16 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sat, 30 Jul 2022 14:08:49 +0800 Subject: [PATCH 2/2] server: improve log --- packages/server/src/main.ts | 4 +++- packages/server/src/services/system.service.ts | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/server/src/main.ts b/packages/server/src/main.ts index a0e02529..74966225 100644 --- a/packages/server/src/main.ts +++ b/packages/server/src/main.ts @@ -14,7 +14,9 @@ import helmet from 'helmet'; import { AppModule } from './app.module'; async function bootstrap() { - const app = await NestFactory.create(AppModule); + const app = await NestFactory.create(AppModule, { + logger: ['warn', 'error'], + }); const config = app.get(ConfigService); const port = config.get('server.port') || 5002; diff --git a/packages/server/src/services/system.service.ts b/packages/server/src/services/system.service.ts index b9bb65db..562cbcba 100644 --- a/packages/server/src/services/system.service.ts +++ b/packages/server/src/services/system.service.ts @@ -60,7 +60,7 @@ export class SystemService { : await this.systemRepo.create(emailConfig); await this.systemRepo.save(newConfig); - console.log('[think] 已载入文件配置:', newConfig); + console.log('[think] 已载入文件配置', JSON.stringify(newConfig, null, 2)); } /**