diff --git a/src/revisions/revisions.service.spec.ts b/src/revisions/revisions.service.spec.ts index 3ee94ed1a..a3ec0a9aa 100644 --- a/src/revisions/revisions.service.spec.ts +++ b/src/revisions/revisions.service.spec.ts @@ -163,4 +163,26 @@ describe('RevisionsService', () => { expect(revisions).toEqual(updatedRevisions); }); }); + + describe('getRevisionUserInfo', () => { + it('counts users correctly', async () => { + const user = User.create('test', 'test') as User; + const author = Author.create(123) as Author; + author.user = Promise.resolve(user); + const anonAuthor = Author.create(123) as Author; + const anonAuthor2 = Author.create(123) as Author; + const edits = [Edit.create(author, 12, 15) as Edit]; + edits.push(Edit.create(author, 16, 18) as Edit); + edits.push(Edit.create(author, 29, 20) as Edit); + edits.push(Edit.create(anonAuthor, 29, 20) as Edit); + edits.push(Edit.create(anonAuthor, 29, 20) as Edit); + edits.push(Edit.create(anonAuthor2, 29, 20) as Edit); + const revision = Revision.create('', '', {} as Note) as Revision; + revision.edits = Promise.resolve(edits); + + const userInfo = await service.getRevisionUserInfo(revision); + expect(userInfo.usernames.length).toEqual(1); + expect(userInfo.anonymousUserCount).toEqual(2); + }); + }); }); diff --git a/src/revisions/revisions.service.ts b/src/revisions/revisions.service.ts index 16517f3da..73b542179 100644 --- a/src/revisions/revisions.service.ts +++ b/src/revisions/revisions.service.ts @@ -109,16 +109,21 @@ export class RevisionsService { } async getRevisionUserInfo(revision: Revision): Promise { - const users = await Promise.all( - ( - await revision.edits - ).map(async (edit) => await (await edit.author).user), + // get a deduplicated list of all authors + let authors = await Promise.all( + (await revision.edits).map(async (edit) => await edit.author), ); + authors = [...new Set(authors)]; // remove duplicates with Set + + // retrieve user objects of the authors + const users = await Promise.all( + authors.map(async (author) => await author.user), + ); + // collect usernames of the users const usernames = users.flatMap((user) => (user ? [user.username] : [])); - const anonymousUserCount = users.filter((user) => user === null).length; return { usernames: usernames, - anonymousUserCount: anonymousUserCount, + anonymousUserCount: users.length - usernames.length, }; }