feat: ws type read cunt

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-03-18 21:59:28 +08:00
parent c33d861eaa
commit 310480f7b4
5 changed files with 26 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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

View File

@@ -48,6 +48,11 @@ export interface EventPayloadMapping {
[BusinessEvents.COMMENT_CREATE]: Omit<CommentModel, 'ref'> & {
ref: Id | PostModel | PageModel | NoteModel | RecentlyModel
}
[BusinessEvents.ARTICLE_READ_COUNT_UPDATE]: {
count: number
type: 'post' | 'note'
id: string
}
health_check: {}
}