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..df3e40fbd4 --- /dev/null +++ b/packages/backend/src/core/SponsorsService.ts @@ -0,0 +1,88 @@ +/* + * 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 99414e739a..2a8a461a8f 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -3,14 +3,13 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { Inject, Injectable } from '@nestjs/common'; -import * as Redis from 'ioredis'; +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 = { tags: ['meta'], - description: 'Get Sharkey Sponsors', + description: 'Get Sharkey Sponsors or Instance Sponsors', requireCredential: false, requireCredentialPrivateMode: false, @@ -20,6 +19,7 @@ export const paramDef = { type: 'object', properties: { forceUpdate: { type: 'boolean', default: false }, + instance: { type: 'boolean', default: false }, }, required: [], } as const; @@ -27,31 +27,14 @@ 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 sponsorsService: SponsorsService, ) { super(meta, paramDef, async (ps, me) => { - let totalSponsors; - const cachedSponsors = await this.redisClient.get('sponsors'); - - if (!ps.forceUpdate && cachedSponsors) { - totalSponsors = JSON.parse(cachedSponsors); + if (ps.instance) { + return { sponsor_data: await this.sponsorsService.instanceSponsors(ps.forceUpdate) }; } else { - 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 = []; - } + return { sponsor_data: await this.sponsorsService.sharkeySponsors(ps.forceUpdate) }; } - return { sponsor_data: totalSponsors }; }); } } 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 + + +
+ + + + {{ sponsor.name }} + + +
+
+
@@ -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 c2134a49e1..20e3a6e545 100644 --- a/packages/misskey-js/src/autogen/apiClientJSDoc.ts +++ b/packages/misskey-js/src/autogen/apiClientJSDoc.ts @@ -4269,7 +4269,7 @@ declare module '../api.js' { ): Promise>; /** - * Get Sharkey 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 6f8e0ca56b..94b77efb66 100644 --- a/packages/misskey-js/src/autogen/types.ts +++ b/packages/misskey-js/src/autogen/types.ts @@ -3689,7 +3689,7 @@ export type paths = { '/sponsors': { /** * sponsors - * @description Get Sharkey Sponsors + * @description Get Sharkey Sponsors or Instance Sponsors * * **Credential required**: *No* */ @@ -28079,7 +28079,7 @@ export type operations = { }; /** * sponsors - * @description Get Sharkey Sponsors + * @description Get Sharkey Sponsors or Instance Sponsors * * **Credential required**: *No* */ @@ -28089,6 +28089,8 @@ export type operations = { 'application/json': { /** @default false */ forceUpdate?: boolean; + /** @default false */ + instance?: boolean; }; }; };