Skip to content

Commit

Permalink
feat: add reader id for presence
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <[email protected]>
  • Loading branch information
Innei committed Sep 2, 2024
1 parent 19d1030 commit 33c48f7
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 53 deletions.
34 changes: 24 additions & 10 deletions apps/core/src/modules/activity/activity.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IpLocation, IpRecord } from '~/common/decorators/ip.decorator'
import { CollectionRefTypes } from '~/constants/db.constant'
import { PagerDto } from '~/shared/dto/pager.dto'

import { ReaderService } from '../reader/reader.service'
import { Activity } from './activity.constant'
import { ActivityService } from './activity.service'
import {
Expand All @@ -24,7 +25,10 @@ import { GetPresenceQueryDto, UpdatePresenceDto } from './dtos/presence.dto'

@ApiController('/activity')
export class ActivityController {
constructor(private readonly service: ActivityService) {}
constructor(
private readonly service: ActivityService,
private readonly readerService: ReaderService,
) {}

@Post('/like')
async thumbsUpArticle(
Expand Down Expand Up @@ -73,16 +77,26 @@ export class ActivityController {
@HTTPDecorators.SkipLogging
@HTTPDecorators.Bypass
async getPresence(@Query() query: GetPresenceQueryDto) {
return this.service
.getRoomPresence(query.room_name)
.then((list) => {
return list.map(({ ip, ...item }) => {
const roomPresence = await this.service.getRoomPresence(query.room_name)

const readerIds = [] as string[]
for (const item of roomPresence) {
if (item.readerId) {
readerIds.push(item.readerId)
}
}
const readers = await this.readerService.findReaderInIds(readerIds)

return {
data: keyBy(
roomPresence.map(({ ip, ...item }) => {
return snakecaseKeys(item)
})
})
.then((list) => {
return keyBy(list, 'identity')
})
}),
'identity',
),

readers: keyBy(readers, '_id'),
}
}

@Delete('/:type')
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/modules/activity/activity.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface ActivityPresence {
displayName?: string

ip?: string
readerId?: string
}
2 changes: 2 additions & 0 deletions apps/core/src/modules/activity/activity.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GatewayModule } from '~/processors/gateway/gateway.module'
import { CommentModule } from '../comment/comment.module'
import { NoteModule } from '../note/note.module'
import { PostModule } from '../post/post.module'
import { ReaderModule } from '../reader/reader.module'
import { ActivityController } from './activity.controller'
import { ActivityService } from './activity.service'

Expand All @@ -18,6 +19,7 @@ import { ActivityService } from './activity.service'

forwardRef(() => PostModule),
forwardRef(() => NoteModule),
ReaderModule,
],
})
export class ActivityModule {}
2 changes: 1 addition & 1 deletion apps/core/src/modules/activity/activity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class ActivityService implements OnModuleInit, OnModuleDestroy {
operationTime: data.ts,
updatedAt: Date.now(),
connectedAt: +new Date(socket.handshake.time),

readerId: data.readerId,
ip,
}

Expand Down
13 changes: 12 additions & 1 deletion apps/core/src/modules/activity/dtos/presence.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { IsNumber, IsOptional, IsString, MaxLength, Min } from 'class-validator'
import {
IsMongoId,
IsNumber,
IsOptional,
IsString,
MaxLength,
Min,
} from 'class-validator'

export class UpdatePresenceDto {
@IsString()
Expand All @@ -24,6 +31,10 @@ export class UpdatePresenceDto {
@IsString()
@MaxLength(30)
sid: string

@IsMongoId()
@IsOptional()
readerId?: string
}

export class GetPresenceQueryDto {
Expand Down
2 changes: 1 addition & 1 deletion apps/core/src/modules/reader/reader.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ReaderService } from './reader.service'

@ApiController('readers')
@Auth()
export class ReaderController {
export class ReaderAuthController {
constructor(private readonly readerService: ReaderService) {}
@Get('/')
async find() {
Expand Down
4 changes: 2 additions & 2 deletions apps/core/src/modules/reader/reader.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module } from '@nestjs/common'

import { ReaderController } from './reader.controller'
import { ReaderAuthController } from './reader.controller'
import { ReaderService } from './reader.service'

@Module({
controllers: [ReaderController],
controllers: [ReaderAuthController],
providers: [ReaderService],
exports: [ReaderService],
})
Expand Down
95 changes: 58 additions & 37 deletions apps/core/src/modules/reader/reader.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Document } from 'mongodb'
import { Types } from 'mongoose'

import { Injectable } from '@nestjs/common'
Expand All @@ -9,52 +10,63 @@ import { AUTH_JS_USER_COLLECTION } from '../auth/auth.constant'
@Injectable()
export class ReaderService {
constructor(private readonly databaseService: DatabaseService) {}
find() {
return this.databaseService.db
.collection(AUTH_JS_USER_COLLECTION)
.aggregate([
{
$lookup: {
from: 'accounts',
localField: '_id',
foreignField: 'userId',
as: 'account',
},
},
{
// flat account array
$unwind: '$account',

private buildQueryPipeline(where?: Record<string, any>): Document[] {
const basePipeline: Document[] = [
{
$lookup: {
from: 'accounts',
localField: '_id',
foreignField: 'userId',
as: 'account',
},
},
{
// flat account array
$unwind: '$account',
},

{
$project: {
{
$project: {
_id: 1,
email: 1,
isOwner: 1,
image: 1,
name: 1,
account: {
_id: 1,
email: 1,
isOwner: 1,
image: 1,
name: 1,
account: {
_id: 1,
type: 1,
provider: 1,
},
type: 1,
provider: 1,
},
},
},

// account field flat to root level
{
$replaceRoot: {
newRoot: {
$mergeObjects: ['$account', '$$ROOT'],
},
// account field flat to root level
{
$replaceRoot: {
newRoot: {
$mergeObjects: ['$account', '$$ROOT'],
},
},
{
$project: {
account: 0,
},
},
{
$project: {
account: 0,
},
])
},
]

if (where) {
basePipeline.push({
$match: where,
})
}
return basePipeline
}
find() {
return this.databaseService.db
.collection(AUTH_JS_USER_COLLECTION)
.aggregate(this.buildQueryPipeline())
.toArray()
}
async updateAsOwner(id: string) {
Expand All @@ -67,4 +79,13 @@ export class ReaderService {
.collection(AUTH_JS_USER_COLLECTION)
.updateOne({ _id: new Types.ObjectId(id) }, { $set: { isOwner: false } })
}
async findReaderInIds(ids: string[]) {
return this.databaseService.db
.collection(AUTH_JS_USER_COLLECTION)
.aggregate(
this.buildQueryPipeline({
_id: { $in: ids.map((id) => new Types.ObjectId(id)) },
}),
)
}
}
4 changes: 3 additions & 1 deletion packages/api-client/controllers/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class ActivityController<ResponseWrapper> implements IController {
sid,
ts,
displayName,
readerId,
}: {
roomName: string
position: number
Expand All @@ -75,6 +76,7 @@ export class ActivityController<ResponseWrapper> implements IController {

displayName?: string
ts?: number
readerId?: string
}) {
return this.proxy.presence.update.post({
data: {
Expand All @@ -83,7 +85,7 @@ export class ActivityController<ResponseWrapper> implements IController {
ts: ts || Date.now(),
roomName,
sid,

readerId,
displayName,
},
})
Expand Down

0 comments on commit 33c48f7

Please sign in to comment.