diff options
| author | Marie <github@yuugi.dev> | 2024-10-08 18:07:58 +0000 |
|---|---|---|
| committer | Marie <github@yuugi.dev> | 2024-10-08 18:07:58 +0000 |
| commit | 72a0f16b384a06bef6b20f73bf3dbb388580310d (patch) | |
| tree | 7e7399797220b51d745605848159195eca802ca4 /packages/backend/src/core | |
| parent | merge: Count CW text in the character limit (resolves #724) (!667) (diff) | |
| parent | chore: change typing, remove unusued imports (diff) | |
| download | sharkey-72a0f16b384a06bef6b20f73bf3dbb388580310d.tar.gz sharkey-72a0f16b384a06bef6b20f73bf3dbb388580310d.tar.bz2 sharkey-72a0f16b384a06bef6b20f73bf3dbb388580310d.zip | |
merge: Show instance sponsors if OC is set as donation url (!642)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/642
Approved-by: Hazelnoot <acomputerdog@gmail.com>
Approved-by: Julia <julia@insertdomain.name>
Diffstat (limited to 'packages/backend/src/core')
| -rw-r--r-- | packages/backend/src/core/CoreModule.ts | 11 | ||||
| -rw-r--r-- | packages/backend/src/core/SponsorsService.ts | 88 |
2 files changed, 99 insertions, 0 deletions
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<void[]>; + + constructor( + @Inject(DI.redis) + private redisClient: Redis.Redis, + + private metaService: MetaService, + ) { + this.cache = new RedisKVCache<void[]>(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(); + } +} |