upd: apply suggestion

This commit is contained in:
Marie 2024-10-02 19:16:54 +02:00
parent e2010caaff
commit 34cbf55239
No known key found for this signature in database
GPG key ID: 7ADF6C9CD9A28555

View file

@ -33,52 +33,67 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private metaService: MetaService, private metaService: MetaService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
let totalSponsors; // eslint-disable-next-line @typescript-eslint/no-explicit-any
const cachedSponsors = await this.redisClient.get('sponsors'); let totalSponsors: any;
const cachedInstanceSponsors = await this.redisClient.get('instanceSponsors');
if (!ps.forceUpdate && !ps.instance && cachedSponsors) { const maybeCached = async (key: string, forcedUpdate: boolean, fetch_cb: () => void) => {
totalSponsors = JSON.parse(cachedSponsors); // get Key first before doing the if statement as it can be defined as either string or null
} else if (ps.instance && !ps.forceUpdate && cachedInstanceSponsors) { const cached = await this.redisClient.get(key);
totalSponsors = JSON.parse(cachedInstanceSponsors);
} else if (!ps.instance) { if (!forcedUpdate && cached) {
try { return JSON.parse(cached);
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 = [];
} }
} else {
try { try {
const meta = await this.metaService.fetch(); const result = await fetch_cb();
if (meta.donationUrl && !meta.donationUrl.includes('opencollective.com')) { await this.redisClient.set(key, JSON.stringify(totalSponsors), 'EX', 3600);
totalSponsors = []; return result;
} else if (meta.donationUrl) { } catch (e) { return []; }
const backers = await fetch(`${meta.donationUrl}/members/users.json`).then((response) => response.json()); };
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 // 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 // Remove possible duplicates
totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()]; totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()];
await this.redisClient.set('instanceSponsors', JSON.stringify(totalSponsors), 'EX', 3600); await this.redisClient.set('sponsors', JSON.stringify(totalSponsors), 'EX', 3600);
} else { return totalSponsors;
totalSponsors = []; } catch (error) {
return [];
} }
} catch (error) { }) };
totalSponsors = [];
}
} }
return { sponsor_data: totalSponsors };
}); });
} }
} }