summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarie <github@yuugi.dev>2024-10-02 19:16:54 +0200
committerMarie <github@yuugi.dev>2024-10-02 19:16:54 +0200
commit34cbf552398e12fc96c249e8d83ff4356847e9a5 (patch)
treeda1b249d5d85934bf98563ca6ad16bca8587dce9
parentMerge branch 'develop' into upd/instancesponsor (diff)
downloadsharkey-34cbf552398e12fc96c249e8d83ff4356847e9a5.tar.gz
sharkey-34cbf552398e12fc96c249e8d83ff4356847e9a5.tar.bz2
sharkey-34cbf552398e12fc96c249e8d83ff4356847e9a5.zip
upd: apply suggestion
-rw-r--r--packages/backend/src/server/api/endpoints/sponsors.ts91
1 files changed, 53 insertions, 38 deletions
diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts
index 34a3ed70dd..27348e2d82 100644
--- a/packages/backend/src/server/api/endpoints/sponsors.ts
+++ b/packages/backend/src/server/api/endpoints/sponsors.ts
@@ -33,52 +33,67 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private metaService: MetaService,
) {
super(meta, paramDef, async (ps, me) => {
- let totalSponsors;
- const cachedSponsors = await this.redisClient.get('sponsors');
- const cachedInstanceSponsors = await this.redisClient.get('instanceSponsors');
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ let totalSponsors: any;
- if (!ps.forceUpdate && !ps.instance && cachedSponsors) {
- totalSponsors = JSON.parse(cachedSponsors);
- } else if (ps.instance && !ps.forceUpdate && cachedInstanceSponsors) {
- totalSponsors = JSON.parse(cachedInstanceSponsors);
- } else if (!ps.instance) {
- 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());
-
- // Merge both together into one array and make sure it only has Active subscriptions
- const allSponsors = [...sponsorsOC, ...backers].filter(sponsor => sponsor.isActive === true);
-
- // Remove possible duplicates
- totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()];
-
- await this.redisClient.set('sponsors', JSON.stringify(totalSponsors), 'EX', 3600);
- } catch (error) {
- totalSponsors = [];
+ 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);
+
+ if (!forcedUpdate && cached) {
+ return JSON.parse(cached);
}
- } else {
- try {
- const meta = await this.metaService.fetch();
- if (meta.donationUrl && !meta.donationUrl.includes('opencollective.com')) {
- totalSponsors = [];
- } else if (meta.donationUrl) {
- const backers = await fetch(`${meta.donationUrl}/members/users.json`).then((response) => response.json());
+ try {
+ const result = await fetch_cb();
+ await this.redisClient.set(key, JSON.stringify(totalSponsors), '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) {
+ 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 {
+ return [];
+ }
+ } catch (error) {
+ return [];
+ }
+ }) };
+ } else {
+ return { sponsor_data: await maybeCached('sponsors', ps.forceUpdate, async () => {
+ 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());
+
// 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);
-
+ const allSponsors = [...sponsorsOC, ...backers].filter(sponsor => sponsor.isActive === true);
+
// Remove possible duplicates
totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()];
-
- await this.redisClient.set('instanceSponsors', JSON.stringify(totalSponsors), 'EX', 3600);
- } else {
- totalSponsors = [];
+
+ await this.redisClient.set('sponsors', JSON.stringify(totalSponsors), 'EX', 3600);
+ return totalSponsors;
+ } catch (error) {
+ return [];
}
- } catch (error) {
- totalSponsors = [];
- }
+ }) };
}
- return { sponsor_data: totalSponsors };
});
}
}