mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-15 07:34:42 -04:00
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:
parent
52b5dc1929
commit
1ac4258d07
2 changed files with 33 additions and 6 deletions
|
@ -163,4 +163,26 @@ describe('RevisionsService', () => {
|
||||||
expect(revisions).toEqual(updatedRevisions);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -109,16 +109,21 @@ export class RevisionsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRevisionUserInfo(revision: Revision): Promise<RevisionUserInfo> {
|
async getRevisionUserInfo(revision: Revision): Promise<RevisionUserInfo> {
|
||||||
const users = await Promise.all(
|
// get a deduplicated list of all authors
|
||||||
(
|
let authors = await Promise.all(
|
||||||
await revision.edits
|
(await revision.edits).map(async (edit) => await edit.author),
|
||||||
).map(async (edit) => await (await edit.author).user),
|
|
||||||
);
|
);
|
||||||
|
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 usernames = users.flatMap((user) => (user ? [user.username] : []));
|
||||||
const anonymousUserCount = users.filter((user) => user === null).length;
|
|
||||||
return {
|
return {
|
||||||
usernames: usernames,
|
usernames: usernames,
|
||||||
anonymousUserCount: anonymousUserCount,
|
anonymousUserCount: users.length - usernames.length,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue