server: fix get recent documents

This commit is contained in:
fantasticit 2022-05-19 14:35:08 +08:00
parent c8f37fecf9
commit 9257e683da
2 changed files with 11 additions and 13 deletions

View File

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

View File

@ -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<Array<IDocument['id']>> {
const [ret] = await this.viewRepo.findAndCount({
where: { userId },
take: 20,
});
return ret.map((item) => item.documentId);
}
}