From 6b459be117e1f236c1a14499a14b56911d25994f Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 2 Oct 2024 19:27:26 +0200 Subject: [PATCH] upd: move totalSponsors and donationUrl check --- .../src/server/api/endpoints/sponsors.ts | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts index 27348e2d82..5557e478ee 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -33,9 +33,6 @@ export default class extends Endpoint { // eslint- private metaService: MetaService, ) { super(meta, paramDef, async (ps, me) => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - let totalSponsors: any; - const maybeCached = async (key: string, forcedUpdate: boolean, fetch_cb: () => void) => { // get Key first before doing the if statement as it can be defined as either string or null const cached = await this.redisClient.get(key); @@ -46,37 +43,41 @@ export default class extends Endpoint { // eslint- try { const result = await fetch_cb(); - await this.redisClient.set(key, JSON.stringify(totalSponsors), 'EX', 3600); + await this.redisClient.set(key, JSON.stringify(result), 'EX', 3600); return result; } catch (e) { return []; } }; if (ps.instance) { return { sponsor_data: await maybeCached('instanceSponsors', ps.forceUpdate, async () => { - try { - const meta = await this.metaService.fetch(); - if (meta.donationUrl && !meta.donationUrl.includes('opencollective.com')) { - return []; - } else if (meta.donationUrl) { + let totalSponsors; + const meta = await this.metaService.fetch(); + + if (meta.donationUrl && !meta.donationUrl.includes('opencollective.com')) { + return []; + } else if (meta.donationUrl) { + try { const backers = await fetch(`${meta.donationUrl}/members/users.json`).then((response) => response.json()); - + // Merge both together into one array and make sure it only has Active subscriptions const allSponsors = [...backers].filter(sponsor => sponsor.isActive === true && sponsor.role === 'BACKER' && sponsor.tier); - + // Remove possible duplicates totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()]; - + await this.redisClient.set('instanceSponsors', JSON.stringify(totalSponsors), 'EX', 3600); return totalSponsors; - } else { + } catch (error) { return []; } - } catch (error) { + } else { return []; } }) }; } else { return { sponsor_data: await maybeCached('sponsors', ps.forceUpdate, async () => { + let totalSponsors; + try { const backers = await fetch('https://opencollective.com/sharkey/tiers/backer/all.json').then((response) => response.json()); const sponsorsOC = await fetch('https://opencollective.com/sharkey/tiers/sponsor/all.json').then((response) => response.json());