Skip to content

Commit

Permalink
feat(revision): include author details in metadata dto
Browse files Browse the repository at this point in the history
Signed-off-by: David Mehren <[email protected]>
  • Loading branch information
davidmehren committed Mar 7, 2022
1 parent a9f2773 commit 6f1bdcb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/revisions/revision-metadata.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
29 changes: 28 additions & 1 deletion src/revisions/revisions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
(
Expand Down

0 comments on commit 6f1bdcb

Please sign in to comment.