diff --git a/packages/server/src/services/document.service.ts b/packages/server/src/services/document.service.ts index 8a3e2303..108cc399 100644 --- a/packages/server/src/services/document.service.ts +++ b/packages/server/src/services/document.service.ts @@ -592,24 +592,14 @@ export class DocumentService { } /** - * 获取用户最近更新的10篇文档 + * 获取用户最近访问的10篇文档 * @param user * @param dto * @returns */ public async getRecentDocuments(user: OutUser) { - const query = await this.documentAuthorityRepo - .createQueryBuilder('documentAuth') - .where('documentAuth.userId=:userId') - .andWhere('documentAuth.readable=:readable') - .setParameter('userId', user.id) - .setParameter('readable', 1); + const documentIds = await this.viewService.getUserRecentVisitedDocuments(user.id); - query.orderBy('documentAuth.updatedAt', 'DESC'); - - query.take(20); - - const documentIds = await (await query.getMany()).map((docAuth) => docAuth.documentId); const documents = await this.documentRepo.findByIds(documentIds, { order: { updatedAt: 'DESC' } }); const docs = documents.filter((doc) => !doc.isWikiHome).map((doc) => instanceToPlain(doc)); diff --git a/packages/server/src/services/view.service.ts b/packages/server/src/services/view.service.ts index ea49cb4b..a4bae09c 100644 --- a/packages/server/src/services/view.service.ts +++ b/packages/server/src/services/view.service.ts @@ -2,7 +2,7 @@ import { ViewEntity } from '@entities/view.entity'; import { parseUserAgent } from '@helpers/ua.helper'; import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { IPagination } from '@think/domains'; +import { IDocument, IPagination, IUser } from '@think/domains'; import { Repository } from 'typeorm'; @Injectable() @@ -54,4 +54,12 @@ export class ViewService { return { data, total }; } + + async getUserRecentVisitedDocuments(userId: IUser['id']): Promise> { + const [ret] = await this.viewRepo.findAndCount({ + where: { userId }, + take: 20, + }); + return ret.map((item) => item.documentId); + } }