From 310480f7b48d6460728a12a847575edd350c10c5 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 18 Mar 2024 21:59:28 +0800 Subject: [PATCH] feat: ws type read cunt Signed-off-by: Innei --- .../src/constants/business-event.constant.ts | 2 ++ apps/core/src/modules/ack/ack.controller.ts | 18 +++++++++++++++++- .../src/modules/activity/activity.service.ts | 2 +- .../helper/helper.counting.service.ts | 2 +- packages/webhook/src/types.ts | 5 +++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/apps/core/src/constants/business-event.constant.ts b/apps/core/src/constants/business-event.constant.ts index 8046e5ed..790520be 100644 --- a/apps/core/src/constants/business-event.constant.ts +++ b/apps/core/src/constants/business-event.constant.ts @@ -54,6 +54,8 @@ export enum BusinessEvents { ACTIVITY_LIKE = 'ACTIVITY_LIKE', ACTIVITY_UPDATE_PRESENCE = 'ACTIVITY_UPDATE_PRESENCE', ACTIVITY_LEAVE_PRESENCE = 'ACTIVITY_LEAVE_PRESENCE', + + ARTICLE_READ_COUNT_UPDATE = 'ARTICLE_READ_COUNT_UPDATE', } export enum EventScope { diff --git a/apps/core/src/modules/ack/ack.controller.ts b/apps/core/src/modules/ack/ack.controller.ts index a7063775..3022b5f8 100644 --- a/apps/core/src/modules/ack/ack.controller.ts +++ b/apps/core/src/modules/ack/ack.controller.ts @@ -1,13 +1,16 @@ import { plainToInstance } from 'class-transformer' import { validateSync } from 'class-validator' import { FastifyReply, FastifyRequest } from 'fastify' +import type { CountModel } from '~/shared/model/count.model' import { Body, HttpCode, Inject, Post, Req, Res } from '@nestjs/common' import { ApiController } from '~/common/decorators/api-controller.decorator' import { Cookies } from '~/common/decorators/cookie.decorator' import { ExtendedValidationPipe } from '~/common/pipes/validation.pipe' +import { BusinessEvents } from '~/constants/business-event.constant' import { VALIDATION_PIPE_INJECTION } from '~/constants/system.constant' +import { WebEventsGateway } from '~/processors/gateway/web/events.gateway' import { CountingService } from '~/processors/helper/helper.counting.service' import { CacheService } from '~/processors/redis/cache.service' @@ -20,6 +23,8 @@ export class AckController { private readonly countingService: CountingService, @Inject(VALIDATION_PIPE_INJECTION) private readonly validatePipe: ExtendedValidationPipe, + + private readonly webGateway: WebEventsGateway, ) {} @Post('/') @@ -48,7 +53,18 @@ export class AckController { } const { id, type } = validPayload - await this.countingService.updateReadCount(type, id) + const doc = await this.countingService.updateReadCount(type, id) + + if ('count' in doc) + this.webGateway.broadcast(BusinessEvents.ARTICLE_READ_COUNT_UPDATE, { + count: -~( + doc as { + count: CountModel + } + ).count.read!, + id, + type, + }) return res.send() diff --git a/apps/core/src/modules/activity/activity.service.ts b/apps/core/src/modules/activity/activity.service.ts index 7c1dcb2d..588a89d3 100644 --- a/apps/core/src/modules/activity/activity.service.ts +++ b/apps/core/src/modules/activity/activity.service.ts @@ -84,7 +84,7 @@ export class ActivityService implements OnModuleInit, OnModuleDestroy { displayName, ip, } = presence - const joinedAt = roomJoinedAtMap[roomName] + const joinedAt = roomJoinedAtMap?.[roomName] if (!joinedAt) return const duration = operationTime - joinedAt diff --git a/apps/core/src/processors/helper/helper.counting.service.ts b/apps/core/src/processors/helper/helper.counting.service.ts index 562bffb4..80a715d9 100644 --- a/apps/core/src/processors/helper/helper.counting.service.ts +++ b/apps/core/src/processors/helper/helper.counting.service.ts @@ -62,7 +62,7 @@ export class CountingService { const doc = await model.findById(id) if (!doc) throw '' - await doc.updateOne({ $inc: { 'count.read': 1 } }) + await doc.updateOne({ $inc: { 'count.read': 1 } }).lean() this.logger.debug(`增加阅读计数,(${doc.title}`) return doc } diff --git a/packages/webhook/src/types.ts b/packages/webhook/src/types.ts index e753a9ce..85daffce 100644 --- a/packages/webhook/src/types.ts +++ b/packages/webhook/src/types.ts @@ -48,6 +48,11 @@ export interface EventPayloadMapping { [BusinessEvents.COMMENT_CREATE]: Omit & { ref: Id | PostModel | PageModel | NoteModel | RecentlyModel } + [BusinessEvents.ARTICLE_READ_COUNT_UPDATE]: { + count: number + type: 'post' | 'note' + id: string + } health_check: {} }