mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 11:16:31 -05:00
feat(revision): include author details in metadata dto
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
a9f27731bd
commit
6f1bdcbaa5
2 changed files with 44 additions and 2 deletions
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsDate, IsNumber } from 'class-validator';
|
||||
import { IsDate, IsNumber, IsString } from 'class-validator';
|
||||
|
||||
import { Revision } from './revision.entity';
|
||||
|
||||
|
@ -34,4 +34,19 @@ export class RevisionMetadataDto {
|
|||
@IsNumber()
|
||||
@ApiProperty()
|
||||
length: number;
|
||||
|
||||
/**
|
||||
* List of the usernames that have contributed to this revision
|
||||
* Does not include anonymous users
|
||||
*/
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
authorUsernames: string[];
|
||||
|
||||
/**
|
||||
* Count of anonymous users that have contributed to this revision
|
||||
*/
|
||||
@IsNumber()
|
||||
@ApiProperty()
|
||||
anonymousAuthorCount: number;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,11 @@ import { RevisionMetadataDto } from './revision-metadata.dto';
|
|||
import { RevisionDto } from './revision.dto';
|
||||
import { Revision } from './revision.entity';
|
||||
|
||||
class RevisionUserInfo {
|
||||
usernames: string[];
|
||||
anonymousUserCount: number;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class RevisionsService {
|
||||
constructor(
|
||||
|
@ -103,20 +108,42 @@ export class RevisionsService {
|
|||
return revision;
|
||||
}
|
||||
|
||||
toRevisionMetadataDto(revision: Revision): RevisionMetadataDto {
|
||||
async getRevisionUserInfo(revision: Revision): Promise<RevisionUserInfo> {
|
||||
const users = await Promise.all(
|
||||
(
|
||||
await revision.edits
|
||||
).map(async (edit) => await (await edit.author).user),
|
||||
);
|
||||
const usernames = users.flatMap((user) => (user ? [user.username] : []));
|
||||
const anonymousUserCount = users.filter((user) => user === null).length;
|
||||
return {
|
||||
usernames: usernames,
|
||||
anonymousUserCount: anonymousUserCount,
|
||||
};
|
||||
}
|
||||
|
||||
async toRevisionMetadataDto(
|
||||
revision: Revision,
|
||||
): Promise<RevisionMetadataDto> {
|
||||
const revisionUserInfo = await this.getRevisionUserInfo(revision);
|
||||
return {
|
||||
id: revision.id,
|
||||
length: revision.length,
|
||||
createdAt: revision.createdAt,
|
||||
authorUsernames: revisionUserInfo.usernames,
|
||||
anonymousAuthorCount: revisionUserInfo.anonymousUserCount,
|
||||
};
|
||||
}
|
||||
|
||||
async toRevisionDto(revision: Revision): Promise<RevisionDto> {
|
||||
const revisionUserInfo = await this.getRevisionUserInfo(revision);
|
||||
return {
|
||||
id: revision.id,
|
||||
content: revision.content,
|
||||
length: revision.length,
|
||||
createdAt: revision.createdAt,
|
||||
authorUsernames: revisionUserInfo.usernames,
|
||||
anonymousAuthorCount: revisionUserInfo.anonymousUserCount,
|
||||
patch: revision.patch,
|
||||
edits: await Promise.all(
|
||||
(
|
||||
|
|
Loading…
Reference in a new issue