server: improve message notify

This commit is contained in:
fantasticit 2022-07-30 14:08:31 +08:00
parent 837643b641
commit ec9d33d3b1
6 changed files with 38 additions and 8 deletions

View File

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

View File

@ -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,
});
})
);

View File

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

View File

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

View File

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

View File

@ -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,
});
}