summary refs log tree commit diff
path: root/api/src/routes/discoverable-guilds.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/routes/discoverable-guilds.ts')
-rw-r--r--api/src/routes/discoverable-guilds.ts26
1 files changed, 20 insertions, 6 deletions
diff --git a/api/src/routes/discoverable-guilds.ts b/api/src/routes/discoverable-guilds.ts
index 1cf56f84..0aa2baa9 100644
--- a/api/src/routes/discoverable-guilds.ts
+++ b/api/src/routes/discoverable-guilds.ts
@@ -6,15 +6,29 @@ import { route } from "@fosscord/api";
 const router = Router();
 
 router.get("/", route({}), async (req: Request, res: Response) => {
-	const { limit } = req.params;
-	var showAllGuilds = Config.get().guild.showAllGuildsInDiscovery;
+	const { offset, limit, categories } = req.query;
+	var showAllGuilds = Config.get().guild.discovery.showAllGuilds;
+	var configLimit = Config.get().guild.discovery.limit;
 	// ! this only works using SQL querys
 	// TODO: implement this with default typeorm query
 	// const guilds = await Guild.find({ where: { features: "DISCOVERABLE" } }); //, take: Math.abs(Number(limit)) });
-	const guilds = showAllGuilds
-		? await Guild.find({ take: Math.abs(Number(limit || 20)) })
-		: await Guild.find({ where: `"features" LIKE '%COMMUNITY%'`, take: Math.abs(Number(limit || 20)) });
-	res.send({ guilds: guilds });
+	let guilds;
+	if (categories == undefined) {
+		guilds = showAllGuilds
+			? await Guild.find({ take: Math.abs(Number(limit || configLimit)) })
+			: await Guild.find({ where: `"features" LIKE '%DISCOVERABLE%'`, take: Math.abs(Number(limit || configLimit)) });
+	} else {
+		guilds = showAllGuilds
+				? await Guild.find({ where: `"primary_category_id" = ${categories}`, take: Math.abs(Number(limit || configLimit)) })
+				: await Guild.find({
+						where: `"primary_category_id" = ${categories} AND "features" LIKE '%DISCOVERABLE%'`,
+						take: Math.abs(Number(limit || configLimit))
+				  });
+	}
+
+	const total = guilds ? guilds.length : undefined;
+
+	res.send({ total: total, guilds: guilds, offset: Number(offset || Config.get().guild.discovery.offset), limit: Number(limit || configLimit) });
 });
 
 export default router;