fix(revision-service): fix count of duplicate authors

`getRevisionUserInfo` returned an incorrect list of usernames,
as users who edited a note at multiple places appeared multiple times.

 This commit fixes this behaviour by deduplicating the author
  objects using a Set.

  Closes #2180

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-05-22 21:27:48 +02:00 committed by Philip Molares
parent 52b5dc1929
commit 1ac4258d07
2 changed files with 33 additions and 6 deletions

View file

@ -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);
});
});
});

View file

@ -109,16 +109,21 @@ export class RevisionsService {
}
async getRevisionUserInfo(revision: Revision): Promise<RevisionUserInfo> {
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,
};
}