From 5f85f65bfcdeb083805076d64ef30aac9a18254f Mon Sep 17 00:00:00 2001 From: Jemma Poffinbarger Date: Sat, 24 Jun 2023 14:23:22 -0500 Subject: [PATCH] Updated topics endpoint to prevent sub-communities and user generated communities from being displayed --- src/services/api/routes/topics.ts | 32 +++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/services/api/routes/topics.ts b/src/services/api/routes/topics.ts index 7272598..d27401c 100644 --- a/src/services/api/routes/topics.ts +++ b/src/services/api/routes/topics.ts @@ -114,7 +114,32 @@ async function generateTopicsXML(communities: HydratedCommunityDocument[]): Prom async function calculateMostPopularCommunities(hours: number, limit: number): Promise { const now: Date = new Date(); const last24Hours: Date = new Date(now.getTime() - hours * 60 * 60 * 1000); - + if (!last24Hours) { + throw new Error('Invalid date'); + } + const validCommunities: { + _id: null; + communities: [string]; + }[] = await Community.aggregate([ + { + $match: { + type: 0, + parent: null + } + }, + { + $group: { + _id: null, + communities: { + $push: '$olive_community_id' + } + } + } + ]); + const communityIDs: [string] = validCommunities[0].communities; + if (!communityIDs) { + throw new Error('No communities found'); + } const popularCommunities: { _id: string; count: number; @@ -124,7 +149,10 @@ async function calculateMostPopularCommunities(hours: number, limit: number): Pr created_at: { $gte: last24Hours }, - message_to_pid: null + message_to_pid: null, + community_id: { + $in: communityIDs + } } }, {