From 2d31c189d89900cf4ce48b85cf3ef0fccd4aa9f3 Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Sat, 14 Oct 2023 01:59:09 +0200 Subject: [PATCH] add: sponsors --- .../backend/src/server/api/EndpointsModule.ts | 4 ++ packages/backend/src/server/api/endpoints.ts | 2 + .../src/server/api/endpoints/sponsors.ts | 46 +++++++++++++++++++ packages/frontend/src/pages/about-sharkey.vue | 18 ++++++++ packages/misskey-js/src/api.types.ts | 3 ++ 5 files changed, 73 insertions(+) create mode 100644 packages/backend/src/server/api/endpoints/sponsors.ts diff --git a/packages/backend/src/server/api/EndpointsModule.ts b/packages/backend/src/server/api/EndpointsModule.ts index e5c6c310f..9aa75b0af 100644 --- a/packages/backend/src/server/api/EndpointsModule.ts +++ b/packages/backend/src/server/api/EndpointsModule.ts @@ -354,6 +354,7 @@ import * as ep___users_achievements from './endpoints/users/achievements.js'; import * as ep___users_updateMemo from './endpoints/users/update-memo.js'; import * as ep___fetchRss from './endpoints/fetch-rss.js'; import * as ep___retention from './endpoints/retention.js'; +import * as ep___sponsors from './endpoints/sponsors.js'; import { GetterService } from './GetterService.js'; import { ApiLoggerService } from './ApiLoggerService.js'; import type { Provider } from '@nestjs/common'; @@ -706,6 +707,7 @@ const $users_achievements: Provider = { provide: 'ep:users/achievements', useCla const $users_updateMemo: Provider = { provide: 'ep:users/update-memo', useClass: ep___users_updateMemo.default }; const $fetchRss: Provider = { provide: 'ep:fetch-rss', useClass: ep___fetchRss.default }; const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention.default }; +const $sponsors: Provider = { provide: 'ep:sponsors', useClass: ep___sponsors.default }; @Module({ imports: [ @@ -1062,6 +1064,7 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention $users_updateMemo, $fetchRss, $retention, + $sponsors, ], exports: [ $admin_meta, @@ -1409,6 +1412,7 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention $users_updateMemo, $fetchRss, $retention, + $sponsors, ], }) export class EndpointsModule {} diff --git a/packages/backend/src/server/api/endpoints.ts b/packages/backend/src/server/api/endpoints.ts index 548367d12..d7f5611f5 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -354,6 +354,7 @@ import * as ep___users_achievements from './endpoints/users/achievements.js'; import * as ep___users_updateMemo from './endpoints/users/update-memo.js'; import * as ep___fetchRss from './endpoints/fetch-rss.js'; import * as ep___retention from './endpoints/retention.js'; +import * as ep___sponsors from './endpoints/sponsors.js'; const eps = [ ['admin/meta', ep___admin_meta], @@ -704,6 +705,7 @@ const eps = [ ['users/update-memo', ep___users_updateMemo], ['fetch-rss', ep___fetchRss], ['retention', ep___retention], + ['sponsors', ep___sponsors], ]; export interface IEndpointMeta { diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts new file mode 100644 index 000000000..6923fdb41 --- /dev/null +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -0,0 +1,46 @@ +import { Inject, Injectable } from '@nestjs/common'; +import { Endpoint } from '@/server/api/endpoint-base.js'; +import { DI } from '@/di-symbols.js'; +import * as Redis from 'ioredis'; + +export const meta = { + tags: ["meta"], + description: "Get Sharkey GH Sponsors", + + requireCredential: false, + requireCredentialPrivateMode: false, +} as const; + +export const paramDef = { + type: "object", + properties: { + forceUpdate: { type: "boolean", default: false }, + }, + required: [], +} as const; + +@Injectable() +export default class extends Endpoint { // eslint-disable-line import/no-default-export + constructor( + @Inject(DI.redis) private redisClient: Redis.Redis, + ) { + super(meta, paramDef, async (ps, me) => { + let sponsors; + const cachedSponsors = await this.redisClient.get("sponsors"); + if (!ps.forceUpdate && cachedSponsors) { + sponsors = JSON.parse(cachedSponsors); + } else { + AbortSignal.timeout ??= function timeout(ms) { + const ctrl = new AbortController(); + setTimeout(() => ctrl.abort(), ms); + return ctrl.signal; + }; + + sponsors = await fetch("https://kaifa.ch/transfem-sponsors.json",{ signal: AbortSignal.timeout(2000) }) + .then((response) => response.json()); + await this.redisClient.set("sponsors", JSON.stringify(sponsors), "EX", 3600); + } + return { sponsor_data: sponsors['sponsors'] }; + }) + } +} diff --git a/packages/frontend/src/pages/about-sharkey.vue b/packages/frontend/src/pages/about-sharkey.vue index 85047584d..9083d85e7 100644 --- a/packages/frontend/src/pages/about-sharkey.vue +++ b/packages/frontend/src/pages/about-sharkey.vue @@ -84,6 +84,21 @@ SPDX-License-Identifier: AGPL-3.0-only + + +
+ + + + {{ sponsor.details.name }} + + +
+
@@ -110,8 +125,11 @@ let thereIsTreasure = $ref($i && !claimedAchievements.includes('foundTreasure')) let easterEggReady = false; let easterEggEmojis = $ref([]); let easterEggEngine = $ref(null); +let sponsors = $ref([]); const containerEl = $shallowRef(); +await os.api('sponsors', { forceUpdate: true }).then((res) => sponsors.push(res.sponsor_data)); + function iconLoaded() { const emojis = defaultStore.state.reactions; const containerWidth = containerEl.offsetWidth; diff --git a/packages/misskey-js/src/api.types.ts b/packages/misskey-js/src/api.types.ts index 0b2961f2b..a93bda020 100644 --- a/packages/misskey-js/src/api.types.ts +++ b/packages/misskey-js/src/api.types.ts @@ -583,6 +583,9 @@ export type Endpoints = { // reset-password 'reset-password': { req: { token: string; password: string; }; res: null; }; + // sponsors + 'sponsors': { req: { forceUpdate: boolean; }; res: null; }; + // room 'room/show': { req: TODO; res: TODO; }; 'room/update': { req: TODO; res: TODO; };