From 5e833c558ee3d6b9296fc64d741271556b0e0d62 Mon Sep 17 00:00:00 2001 From: Jonathan Barrow Date: Tue, 3 Oct 2023 12:59:00 -0400 Subject: [PATCH] make WWP always return unique users between communities --- src/services/api/routes/topics.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/services/api/routes/topics.ts b/src/services/api/routes/topics.ts index 6f51d11..5869fa5 100644 --- a/src/services/api/routes/topics.ts +++ b/src/services/api/routes/topics.ts @@ -72,6 +72,8 @@ async function generateTopicsData(communities: HydratedCommunityDocument[]): Pro topic: WWPTopic; }[] = []; + const seenPeople: number[] = []; + for (let i = 0; i < communities.length; i++) { const community = communities[i]; @@ -114,7 +116,7 @@ async function generateTopicsData(communities: HydratedCommunityDocument[]): Pro } }); - const people = await getCommunityPeople(community); + const people = await getCommunityPeople(community, seenPeople); for (const person of people) { const post = Post.hydrate(person.post).json({ @@ -133,6 +135,8 @@ async function generateTopicsData(communities: HydratedCommunityDocument[]): Pro ] } }); + + seenPeople.push(person._id); } topics.push({ @@ -149,7 +153,7 @@ async function generateTopicsData(communities: HydratedCommunityDocument[]): Pro }; } -async function getCommunityPeople(community: HydratedCommunityDocument, hours = 24): Promise<{ _id: number; post: IPost }[]> { +async function getCommunityPeople(community: HydratedCommunityDocument, seenPeople: number[], hours = 24): Promise<{ _id: number; post: IPost }[]> { const now = new Date(); const last24Hours = new Date(now.getTime() - hours * 60 * 60 * 1000); const people = await Post.aggregate<{ _id: number; post: IPost }>([ @@ -163,7 +167,13 @@ async function getCommunityPeople(community: HydratedCommunityDocument, hours = }, message_to_pid: null, parent: null, - removed: false + removed: false, + pid: { + // * Exclude people we have seen in other communities. + // * This increases generation time, but ensures the + // * max number of slots we can fill end up getting used + $nin: seenPeople + } } }, { @@ -192,7 +202,7 @@ async function getCommunityPeople(community: HydratedCommunityDocument, hours = // * Double the search range each time to get // * exponentially more posts. This speeds up // * the search at the cost of using older posts - return getCommunityPeople(community, hours * 2); + return getCommunityPeople(community, seenPeople, hours * 2); } return people;