NotesService: Implement getAuthorUsers

This reimplements logic to get all Users that ever edited a note
and fixes the empty `editedBy` property of `toNoteMetadataDto`
introduced in 81cc092e.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-05-24 21:58:44 +02:00
parent 6abcb686ca
commit 5846ca75a9
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
3 changed files with 51 additions and 2 deletions

View file

@ -41,6 +41,7 @@ export class NotesService {
private readonly logger: ConsoleLoggerService,
@InjectRepository(Note) private noteRepository: Repository<Note>,
@InjectRepository(Tag) private tagRepository: Repository<Tag>,
@InjectRepository(User) private userRepository: Repository<User>,
@Inject(UsersService) private usersService: UsersService,
@Inject(GroupsService) private groupsService: GroupsService,
@Inject(forwardRef(() => RevisionsService))
@ -188,6 +189,22 @@ export class NotesService {
return note;
}
/**
* @async
* Get all users that ever appeared as an author for the given note
* @param note The note to search authors for
*/
async getAuthorUsers(note: Note): Promise<User[]> {
return await this.userRepository
.createQueryBuilder('user')
.innerJoin('user.authors', 'author')
.innerJoin('author.authorships', 'authorship')
.innerJoin('authorship.revisions', 'revision')
.innerJoin('revision.note', 'note')
.where('note.id = :id', { id: note.id })
.getMany();
}
/**
* Check if the provided note id or alias is not forbidden
* @param noteIdOrAlias - the alias or id in question
@ -358,7 +375,7 @@ export class NotesService {
title: note.title ?? '',
createTime: (await this.getFirstRevision(note)).createdAt,
description: note.description ?? '',
editedBy: [], // TODO temporary placeholder,
editedBy: (await this.getAuthorUsers(note)).map((user) => user.userName),
permissions: this.toNotePermissionsDto(note),
tags: this.toTagList(note),
updateTime: (await this.getLatestRevision(note)).createdAt,