From c2cc718f0378d175d34d0ed2b7e23eb054a3778b Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 3 Oct 2024 18:48:11 +0100 Subject: [PATCH] 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) }; } }); }