From 1e0d372eb58542bfcfcdd397417349611a176915 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 2 Oct 2024 17:20:30 +0200 Subject: [PATCH 01/10] upd(backend): add support for instance oc urls --- .../src/server/api/endpoints/sponsors.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts index 99414e739a..33f9d03367 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -7,10 +7,11 @@ import { Inject, Injectable } from '@nestjs/common'; import * as Redis from 'ioredis'; import { Endpoint } from '@/server/api/endpoint-base.js'; import { DI } from '@/di-symbols.js'; +import { MetaService } from '@/core/MetaService.js'; export const meta = { tags: ['meta'], - description: 'Get Sharkey Sponsors', + description: 'Get Sharkey Sponsors or Instance Sponsors', requireCredential: false, requireCredentialPrivateMode: false, @@ -20,6 +21,7 @@ export const paramDef = { type: 'object', properties: { forceUpdate: { type: 'boolean', default: false }, + instance: { type: 'boolean', default: false }, }, required: [], } as const; @@ -28,14 +30,15 @@ export const paramDef = { export default class extends Endpoint { // eslint-disable-line import/no-default-export constructor( @Inject(DI.redis) private redisClient: Redis.Redis, + private metaService: MetaService, ) { super(meta, paramDef, async (ps, me) => { let totalSponsors; const cachedSponsors = await this.redisClient.get('sponsors'); - if (!ps.forceUpdate && cachedSponsors) { + if (!ps.forceUpdate && !ps.instance && cachedSponsors) { totalSponsors = JSON.parse(cachedSponsors); - } else { + } 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()); @@ -50,6 +53,25 @@ export default class extends Endpoint { // eslint- } catch (error) { totalSponsors = []; } + } 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()); + + // 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'); + + // Remove possible duplicates + totalSponsors = [...new Map(allSponsors.map(v => [v.profile, v])).values()]; + } else { + totalSponsors = []; + } + } catch (error) { + totalSponsors = []; + } } return { sponsor_data: totalSponsors }; }); From 3529042cb163dd69ca78bae1093ac5bafea5a34d Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 2 Oct 2024 18:25:06 +0200 Subject: [PATCH 02/10] fix: api returns all backers on users so check if tier exists --- packages/backend/src/server/api/endpoints/sponsors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts index 33f9d03367..3c3cdd7808 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -62,7 +62,7 @@ export default class extends Endpoint { // eslint- 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'); + 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()]; From cc1017b2af4225f64a8f989416617cd3eefa9ce3 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 2 Oct 2024 18:25:23 +0200 Subject: [PATCH 03/10] upd: add sponsors to instance information --- .../frontend/src/pages/about.overview.vue | 53 +++++++++++++++++++ .../misskey-js/src/autogen/apiClientJSDoc.ts | 2 +- packages/misskey-js/src/autogen/types.ts | 10 ++-- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/packages/frontend/src/pages/about.overview.vue b/packages/frontend/src/pages/about.overview.vue index 6fbb23265c..72acd9bfca 100644 --- a/packages/frontend/src/pages/about.overview.vue +++ b/packages/frontend/src/pages/about.overview.vue @@ -116,6 +116,22 @@ SPDX-License-Identifier: AGPL-3.0-only + + + + +
@@ -130,6 +146,7 @@ SPDX-License-Identifier: AGPL-3.0-only diff --git a/packages/misskey-js/src/autogen/apiClientJSDoc.ts b/packages/misskey-js/src/autogen/apiClientJSDoc.ts index c13485621b..06a96b4ed5 100644 --- a/packages/misskey-js/src/autogen/apiClientJSDoc.ts +++ b/packages/misskey-js/src/autogen/apiClientJSDoc.ts @@ -4258,7 +4258,7 @@ declare module '../api.js' { ): Promise>; /** - * Get Sharkey GH Sponsors + * Get Sharkey Sponsors or Instance Sponsors * * **Credential required**: *No* */ diff --git a/packages/misskey-js/src/autogen/types.ts b/packages/misskey-js/src/autogen/types.ts index 10354043fc..fdfd5a970a 100644 --- a/packages/misskey-js/src/autogen/types.ts +++ b/packages/misskey-js/src/autogen/types.ts @@ -3673,7 +3673,7 @@ export type paths = { '/sponsors': { /** * sponsors - * @description Get Sharkey GH Sponsors + * @description Get Sharkey Sponsors or Instance Sponsors * * **Credential required**: *No* */ @@ -5191,9 +5191,9 @@ export type operations = { infoImageUrl: string | null; notFoundImageUrl: string | null; iconUrl: string | null; - sidebarLogoUrl: string | null; app192IconUrl: string | null; app512IconUrl: string | null; + sidebarLogoUrl: string | null; enableEmail: boolean; enableServiceWorker: boolean; translatorAvailable: boolean; @@ -9705,9 +9705,9 @@ export type operations = { infoImageUrl?: string | null; notFoundImageUrl?: string | null; iconUrl?: string | null; - sidebarLogoUrl?: string | null; app192IconUrl?: string | null; app512IconUrl?: string | null; + sidebarLogoUrl?: string | null; backgroundImageUrl?: string | null; logoImageUrl?: string | null; name?: string | null; @@ -27990,7 +27990,7 @@ export type operations = { }; /** * sponsors - * @description Get Sharkey GH Sponsors + * @description Get Sharkey Sponsors or Instance Sponsors * * **Credential required**: *No* */ @@ -28000,6 +28000,8 @@ export type operations = { 'application/json': { /** @default false */ forceUpdate?: boolean; + /** @default false */ + instance?: boolean; }; }; }; From dcd6eee627b20c1dfa2dc38a9fbd9e38d4d878d5 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 2 Oct 2024 18:27:11 +0200 Subject: [PATCH 04/10] upd: cache instance sponsors --- packages/backend/src/server/api/endpoints/sponsors.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts index 3c3cdd7808..34a3ed70dd 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -34,10 +34,13 @@ export default class extends Endpoint { // eslint- ) { super(meta, paramDef, async (ps, me) => { let totalSponsors; - const cachedSponsors = await this.redisClient.get('sponsors'); + const cachedSponsors = await this.redisClient.get('sponsors'); + const cachedInstanceSponsors = await this.redisClient.get('instanceSponsors'); 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()); @@ -66,6 +69,8 @@ export default class extends Endpoint { // eslint- // 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 = []; } From 34cbf552398e12fc96c249e8d83ff4356847e9a5 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 2 Oct 2024 19:16:54 +0200 Subject: [PATCH 05/10] upd: apply suggestion --- .../src/server/api/endpoints/sponsors.ts | 91 +++++++++++-------- 1 file 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 { // 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()); - + 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 }; }); } } From 6b459be117e1f236c1a14499a14b56911d25994f Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 2 Oct 2024 19:27:26 +0200 Subject: [PATCH 06/10] 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()); From 9dd51bc9eb2cf8822b6286d9cd2f29c1cb8808a2 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 2 Oct 2024 19:31:46 +0200 Subject: [PATCH 07/10] upd: fix donationUrl check position --- .../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 5557e478ee..dadc6eb819 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -49,31 +49,32 @@ export default class extends Endpoint { // eslint- }; if (ps.instance) { - return { sponsor_data: await maybeCached('instanceSponsors', ps.forceUpdate, async () => { - let totalSponsors; - const meta = await this.metaService.fetch(); - - if (meta.donationUrl && !meta.donationUrl.includes('opencollective.com')) { - return []; - } else if (meta.donationUrl) { + const meta = await this.metaService.fetch(); + if (meta.donationUrl && !meta.donationUrl.includes('opencollective.com')) { + return []; + } else if (meta.donationUrl) { + return { sponsor_data: await maybeCached('instanceSponsors', ps.forceUpdate, async () => { + let totalSponsors; + const meta = await this.metaService.fetch(); + 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; } catch (error) { return []; } - } else { - return []; - } - }) }; + }) }; + } else { + return []; + } } else { return { sponsor_data: await maybeCached('sponsors', ps.forceUpdate, async () => { let totalSponsors; From f08ef28d548c78658e57fd31d1e987851f36d0e2 Mon Sep 17 00:00:00 2001 From: Marie Date: Wed, 2 Oct 2024 19:39:06 +0200 Subject: [PATCH 08/10] upd: fix broken returns and change if statement --- packages/backend/src/server/api/endpoints/sponsors.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts index dadc6eb819..cfea15ba31 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -50,9 +50,7 @@ export default class extends Endpoint { // eslint- if (ps.instance) { const meta = await this.metaService.fetch(); - if (meta.donationUrl && !meta.donationUrl.includes('opencollective.com')) { - return []; - } else if (meta.donationUrl) { + if (meta.donationUrl && meta.donationUrl.includes('opencollective.com')) { return { sponsor_data: await maybeCached('instanceSponsors', ps.forceUpdate, async () => { let totalSponsors; const meta = await this.metaService.fetch(); @@ -73,7 +71,7 @@ export default class extends Endpoint { // eslint- } }) }; } else { - return []; + return { sponsor_data: [] }; } } else { return { sponsor_data: await maybeCached('sponsors', ps.forceUpdate, async () => { From c2cc718f0378d175d34d0ed2b7e23eb054a3778b Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 3 Oct 2024 18:48:11 +0100 Subject: [PATCH 09/10] extract SponsorsService, use RedisKVCache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Please someone add types… --- packages/backend/src/core/CoreModule.ts | 11 +++ packages/backend/src/core/SponsorsService.ts | 86 +++++++++++++++++++ .../src/server/api/endpoints/sponsors.ts | 66 +------------- 3 files changed, 101 insertions(+), 62 deletions(-) create mode 100644 packages/backend/src/core/SponsorsService.ts diff --git a/packages/backend/src/core/CoreModule.ts b/packages/backend/src/core/CoreModule.ts index 7e51d3afa4..049d858189 100644 --- a/packages/backend/src/core/CoreModule.ts +++ b/packages/backend/src/core/CoreModule.ts @@ -149,6 +149,7 @@ import { ApQuestionService } from './activitypub/models/ApQuestionService.js'; import { QueueModule } from './QueueModule.js'; import { QueueService } from './QueueService.js'; import { LoggerService } from './LoggerService.js'; +import { SponsorsService } from './SponsorsService.js'; import type { Provider } from '@nestjs/common'; //#region 文字列ベースでのinjection用(循環参照対応のため) @@ -295,6 +296,8 @@ const $ApPersonService: Provider = { provide: 'ApPersonService', useExisting: Ap const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting: ApQuestionService }; //#endregion +const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: SponsorsService }; + @Module({ imports: [ QueueModule, @@ -443,6 +446,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting ApQuestionService, QueueService, + SponsorsService, + //#region 文字列ベースでのinjection用(循環参照対応のため) $LoggerService, $AbuseReportService, @@ -586,6 +591,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting $ApPersonService, $ApQuestionService, //#endregion + + $SponsorsService, ], exports: [ QueueModule, @@ -731,6 +738,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting ApQuestionService, QueueService, + SponsorsService, + //#region 文字列ベースでのinjection用(循環参照対応のため) $LoggerService, $AbuseReportService, @@ -873,6 +882,8 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting $ApPersonService, $ApQuestionService, //#endregion + + $SponsorsService, ], }) export class CoreModule { } diff --git a/packages/backend/src/core/SponsorsService.ts b/packages/backend/src/core/SponsorsService.ts new file mode 100644 index 0000000000..6846df3554 --- /dev/null +++ b/packages/backend/src/core/SponsorsService.ts @@ -0,0 +1,86 @@ +/* + * SPDX-FileCopyrightText: marie and other Sharkey contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common'; +import * as Redis from 'ioredis'; +import { DI } from '@/di-symbols.js'; +import { MetaService } from '@/core/MetaService.js'; +import { RedisKVCache } from '@/misc/cache.js'; +import { bindThis } from '@/decorators.js'; + +@Injectable() +export class SponsorsService implements OnApplicationShutdown { + private cache: RedisKVCache; + + constructor( + @Inject(DI.redis) private redisClient: Redis.Redis, + private metaService: MetaService, + ) { + this.cache = new RedisKVCache(this.redisClient, 'sponsors', { + lifetime: 1000 * 60 * 60, + memoryCacheLifetime: 1000 * 60, + fetcher: (key) => { + if (key === 'instance') return this.fetchInstanceSponsors(); + return this.fetchSharkeySponsors(); + }, + toRedisConverter: (value) => JSON.stringify(value), + fromRedisConverter: (value) => JSON.parse(value) + }); + } + + @bindThis + private async fetchInstanceSponsors() { + const meta = await this.metaService.fetch(); + + if (!(meta.donationUrl && meta.donationUrl.includes('opencollective.com'))) { + return []; + } + + 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 + return [...new Map(allSponsors.map(v => [v.profile, v])).values()]; + } catch (error) { + return []; + } + } + + @bindThis + private async fetchSharkeySponsors() { + 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 + return [...new Map(allSponsors.map(v => [v.profile, v])).values()]; + } catch (error) { + return []; + } + } + + @bindThis + public async instanceSponsors(forceUpdate: boolean) { + if (forceUpdate) this.cache.refresh('instance'); + return this.cache.fetch('instance'); + } + + @bindThis + public async sharkeySponsors(forceUpdate: boolean) { + if (forceUpdate) this.cache.refresh('sharkey'); + return this.cache.fetch('sharkey'); + } + + @bindThis + public onApplicationShutdown(signal?: string | undefined): void { + this.cache.dispose(); + } +} diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts index cfea15ba31..fb6e51fd4e 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -4,10 +4,9 @@ */ import { Inject, Injectable } from '@nestjs/common'; -import * as Redis from 'ioredis'; import { Endpoint } from '@/server/api/endpoint-base.js'; import { DI } from '@/di-symbols.js'; -import { MetaService } from '@/core/MetaService.js'; +import { SponsorsService } from '@/core/SponsorsService.js'; export const meta = { tags: ['meta'], @@ -29,70 +28,13 @@ export const paramDef = { @Injectable() export default class extends Endpoint { // eslint-disable-line import/no-default-export constructor( - @Inject(DI.redis) private redisClient: Redis.Redis, - private metaService: MetaService, + private sponsorsService: SponsorsService, ) { super(meta, paramDef, async (ps, me) => { - 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); - } - - try { - const result = await fetch_cb(); - await this.redisClient.set(key, JSON.stringify(result), 'EX', 3600); - return result; - } catch (e) { return []; } - }; - if (ps.instance) { - const meta = await this.metaService.fetch(); - if (meta.donationUrl && meta.donationUrl.includes('opencollective.com')) { - return { sponsor_data: await maybeCached('instanceSponsors', ps.forceUpdate, async () => { - let totalSponsors; - const meta = await this.metaService.fetch(); - - 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; - } catch (error) { - return []; - } - }) }; - } else { - return { sponsor_data: [] }; - } + return { sponsor_data: await this.sponsorsService.instanceSponsors(ps.forceUpdate) }; } 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()); - - // 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); - return totalSponsors; - } catch (error) { - return []; - } - }) }; + return { sponsor_data: await this.sponsorsService.sharkeySponsors(ps.forceUpdate) }; } }); } From 4d011e7fe01c9ce990162cfcb6221c8984434ed3 Mon Sep 17 00:00:00 2001 From: Marie Date: Thu, 3 Oct 2024 20:02:55 +0200 Subject: [PATCH 10/10] chore: change typing, remove unusued imports --- packages/backend/src/core/SponsorsService.ts | 10 ++++++---- packages/backend/src/server/api/endpoints/sponsors.ts | 3 +-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/core/SponsorsService.ts b/packages/backend/src/core/SponsorsService.ts index 6846df3554..df3e40fbd4 100644 --- a/packages/backend/src/core/SponsorsService.ts +++ b/packages/backend/src/core/SponsorsService.ts @@ -12,13 +12,15 @@ import { bindThis } from '@/decorators.js'; @Injectable() export class SponsorsService implements OnApplicationShutdown { - private cache: RedisKVCache; + private cache: RedisKVCache; constructor( - @Inject(DI.redis) private redisClient: Redis.Redis, + @Inject(DI.redis) + private redisClient: Redis.Redis, + private metaService: MetaService, ) { - this.cache = new RedisKVCache(this.redisClient, 'sponsors', { + this.cache = new RedisKVCache(this.redisClient, 'sponsors', { lifetime: 1000 * 60 * 60, memoryCacheLifetime: 1000 * 60, fetcher: (key) => { @@ -26,7 +28,7 @@ export class SponsorsService implements OnApplicationShutdown { return this.fetchSharkeySponsors(); }, toRedisConverter: (value) => JSON.stringify(value), - fromRedisConverter: (value) => JSON.parse(value) + fromRedisConverter: (value) => JSON.parse(value), }); } diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts index fb6e51fd4e..2a8a461a8f 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -3,9 +3,8 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { Inject, Injectable } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { Endpoint } from '@/server/api/endpoint-base.js'; -import { DI } from '@/di-symbols.js'; import { SponsorsService } from '@/core/SponsorsService.js'; export const meta = {