feat(revision): include author details in metadata dto

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-02-13 20:25:34 +01:00
parent a9f27731bd
commit 6f1bdcbaa5
2 changed files with 44 additions and 2 deletions

View file

@ -5,7 +5,7 @@
*/ */
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer'; import { Type } from 'class-transformer';
import { IsDate, IsNumber } from 'class-validator'; import { IsDate, IsNumber, IsString } from 'class-validator';
import { Revision } from './revision.entity'; import { Revision } from './revision.entity';
@ -34,4 +34,19 @@ export class RevisionMetadataDto {
@IsNumber() @IsNumber()
@ApiProperty() @ApiProperty()
length: number; 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;
} }

View file

@ -16,6 +16,11 @@ import { RevisionMetadataDto } from './revision-metadata.dto';
import { RevisionDto } from './revision.dto'; import { RevisionDto } from './revision.dto';
import { Revision } from './revision.entity'; import { Revision } from './revision.entity';
class RevisionUserInfo {
usernames: string[];
anonymousUserCount: number;
}
@Injectable() @Injectable()
export class RevisionsService { export class RevisionsService {
constructor( constructor(
@ -103,20 +108,42 @@ export class RevisionsService {
return revision; 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 { return {
id: revision.id, id: revision.id,
length: revision.length, length: revision.length,
createdAt: revision.createdAt, createdAt: revision.createdAt,
authorUsernames: revisionUserInfo.usernames,
anonymousAuthorCount: revisionUserInfo.anonymousUserCount,
}; };
} }
async toRevisionDto(revision: Revision): Promise<RevisionDto> { async toRevisionDto(revision: Revision): Promise<RevisionDto> {
const revisionUserInfo = await this.getRevisionUserInfo(revision);
return { return {
id: revision.id, id: revision.id,
content: revision.content, content: revision.content,
length: revision.length, length: revision.length,
createdAt: revision.createdAt, createdAt: revision.createdAt,
authorUsernames: revisionUserInfo.usernames,
anonymousAuthorCount: revisionUserInfo.anonymousUserCount,
patch: revision.patch, patch: revision.patch,
edits: await Promise.all( edits: await Promise.all(
( (