Merge pull request #140 from fantasticit/fix/13x

server: improve message notify
This commit is contained in:
fantasticit 2022-07-30 14:09:29 +08:00 committed by GitHub
commit 909c266f06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 10 deletions

View File

@ -2,4 +2,5 @@ export enum RedisDBEnum {
documentVersion = 0, documentVersion = 0,
view = 1, view = 1,
verify = 2, verify = 2,
message = 3,
} }

View File

@ -14,7 +14,9 @@ import helmet from 'helmet';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule, {
logger: ['warn', 'error'],
});
const config = app.get(ConfigService); const config = app.get(ConfigService);
const port = config.get('server.port') || 5002; const port = config.get('server.port') || 5002;

View File

@ -97,6 +97,7 @@ export class CommentService {
wikiId: doc.wikiId, wikiId: doc.wikiId,
documentId: doc.id, documentId: doc.id,
}), }),
uniqueId: ret.id,
}); });
}) })
); );
@ -206,6 +207,7 @@ export class CommentService {
wikiId: doc.wikiId, wikiId: doc.wikiId,
documentId: doc.id, documentId: doc.id,
}), }),
uniqueId: newData.id,
}); });
}) })
); );
@ -234,6 +236,7 @@ export class CommentService {
wikiId: doc.wikiId, wikiId: doc.wikiId,
documentId: doc.id, documentId: doc.id,
}), }),
uniqueId: data.id,
}); });
}) })
); );

View File

@ -136,6 +136,7 @@ export class DocumentService {
wikiId: doc.wikiId, wikiId: doc.wikiId,
documentId: doc.id, documentId: doc.id,
}), }),
uniqueId: doc.id,
}); });
} }
@ -174,6 +175,7 @@ export class DocumentService {
wikiId: doc.wikiId, wikiId: doc.wikiId,
documentId: doc.id, documentId: doc.id,
}), }),
uniqueId: doc.id,
}); });
} }
@ -212,6 +214,7 @@ export class DocumentService {
wikiId: doc.wikiId, wikiId: doc.wikiId,
documentId: doc.id, documentId: doc.id,
}), }),
uniqueId: doc.id,
}); });
} }
@ -751,6 +754,7 @@ export class DocumentService {
wikiId: doc.wikiId, wikiId: doc.wikiId,
documentId: doc.id, documentId: doc.id,
}), }),
uniqueId: doc.id,
}); });
}) })
.filter(Boolean) .filter(Boolean)

View File

@ -3,13 +3,30 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { IUser } from '@think/domains'; import { IUser } from '@think/domains';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import Redis from 'ioredis';
import { buildRedis } from '@helpers/redis.helper';
import { RedisDBEnum } from '@constants/*';
import { uniqueId } from 'lodash';
@Injectable() @Injectable()
export class MessageService { export class MessageService {
private redis: Redis;
constructor( constructor(
@InjectRepository(MessageEntity) @InjectRepository(MessageEntity)
private readonly messageRepo: Repository<MessageEntity> private readonly messageRepo: Repository<MessageEntity>
) {} ) {
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 * @param msg
* @returns * @returns
*/ */
async notify(userId: IUser['id'], msg) { async notify(userId: IUser['id'], msg: { title: string; message: string; url: string; uniqueId: string | number }) {
const data = { userId, ...msg }; const key = `message-${userId}-${uniqueId}`;
const mayBeNotified = await this.messageRepo.findOne(data);
if (mayBeNotified) { if (await this.redis.get(key)) {
if (!mayBeNotified.read) {
return; return;
} }
}
const data = { userId, ...msg };
await this.redis.set(key, Date.now(), 'EX', 5 * 60);
const res = await this.messageRepo.create(data); const res = await this.messageRepo.create(data);
const ret = await this.messageRepo.save(res); const ret = await this.messageRepo.save(res);
return ret; return ret;

View File

@ -218,6 +218,7 @@ export class OrganizationService {
url: buildMessageURL('toOrganization')({ url: buildMessageURL('toOrganization')({
organizationId: organization.id, organizationId: organization.id,
}), }),
uniqueId: organization.id,
}); });
return ret; return ret;
@ -256,6 +257,7 @@ export class OrganizationService {
url: buildMessageURL('toOrganization')({ url: buildMessageURL('toOrganization')({
organizationId: organization.id, organizationId: organization.id,
}), }),
uniqueId: organization.id,
}); });
return ret; return ret;
@ -294,6 +296,7 @@ export class OrganizationService {
url: buildMessageURL('toOrganization')({ url: buildMessageURL('toOrganization')({
organizationId: organization.id, organizationId: organization.id,
}), }),
uniqueId: organization.id,
}); });
return ret; return ret;

View File

@ -60,7 +60,7 @@ export class SystemService {
: await this.systemRepo.create(emailConfig); : await this.systemRepo.create(emailConfig);
await this.systemRepo.save(newConfig); await this.systemRepo.save(newConfig);
console.log('[think] 已载入文件配置', newConfig); console.log('[think] 已载入文件配置', JSON.stringify(newConfig, null, 2));
} }
/** /**

View File

@ -118,6 +118,7 @@ export class WikiService {
organizationId: wiki.organizationId, organizationId: wiki.organizationId,
wikiId: wiki.id, wikiId: wiki.id,
}), }),
uniqueId: wiki.id,
}); });
} }
@ -165,6 +166,7 @@ export class WikiService {
organizationId: wiki.organizationId, organizationId: wiki.organizationId,
wikiId: wiki.id, wikiId: wiki.id,
}), }),
uniqueId: wiki.id,
}); });
} }
@ -212,6 +214,7 @@ export class WikiService {
organizationId: wiki.organizationId, organizationId: wiki.organizationId,
wikiId: wiki.id, wikiId: wiki.id,
}), }),
uniqueId: wiki.id,
}); });
} }