diff options
| author | Julia <julia@insertdomain.name> | 2024-09-23 19:33:41 +0000 |
|---|---|---|
| committer | Julia <julia@insertdomain.name> | 2024-09-23 19:33:41 +0000 |
| commit | 674fd138070d3db01a87dd13cc2352d809281945 (patch) | |
| tree | e9ef6ad1bcc5a534f75d77db6a6844e1515801b4 /packages/backend/src/server | |
| parent | merge: Bump version (!620) (diff) | |
| parent | Bump version (diff) | |
| download | sharkey-674fd138070d3db01a87dd13cc2352d809281945.tar.gz sharkey-674fd138070d3db01a87dd13cc2352d809281945.tar.bz2 sharkey-674fd138070d3db01a87dd13cc2352d809281945.zip | |
merge: Bump version (!635)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/635
Diffstat (limited to 'packages/backend/src/server')
| -rw-r--r-- | packages/backend/src/server/api/ApiCallService.ts | 18 | ||||
| -rw-r--r-- | packages/backend/src/server/api/RateLimiterService.ts | 32 | ||||
| -rw-r--r-- | packages/backend/src/server/api/endpoints/sponsors.ts | 33 |
3 files changed, 36 insertions, 47 deletions
diff --git a/packages/backend/src/server/api/ApiCallService.ts b/packages/backend/src/server/api/ApiCallService.ts index 47f64f6609..808795fdac 100644 --- a/packages/backend/src/server/api/ApiCallService.ts +++ b/packages/backend/src/server/api/ApiCallService.ts @@ -64,15 +64,6 @@ export class ApiCallService implements OnApplicationShutdown { let statusCode = err.httpStatusCode; if (err.httpStatusCode === 401) { reply.header('WWW-Authenticate', 'Bearer realm="Misskey"'); - } else if (err.kind === 'client') { - reply.header('WWW-Authenticate', `Bearer realm="Misskey", error="invalid_request", error_description="${err.message}"`); - statusCode = statusCode ?? 400; - } else if (err.kind === 'permission') { - // (ROLE_PERMISSION_DENIEDは関係ない) - if (err.code === 'PERMISSION_DENIED') { - reply.header('WWW-Authenticate', `Bearer realm="Misskey", error="insufficient_scope", error_description="${err.message}"`); - } - statusCode = statusCode ?? 403; } else if (err.code === 'RATE_LIMIT_EXCEEDED') { const info: unknown = err.info; const unixEpochInSeconds = Date.now(); @@ -83,6 +74,15 @@ export class ApiCallService implements OnApplicationShutdown { } else { this.logger.warn(`rate limit information has unexpected type ${typeof(err.info?.reset)}`); } + } else if (err.kind === 'client') { + reply.header('WWW-Authenticate', `Bearer realm="Misskey", error="invalid_request", error_description="${err.message}"`); + statusCode = statusCode ?? 400; + } else if (err.kind === 'permission') { + // (ROLE_PERMISSION_DENIEDは関係ない) + if (err.code === 'PERMISSION_DENIED') { + reply.header('WWW-Authenticate', `Bearer realm="Misskey", error="insufficient_scope", error_description="${err.message}"`); + } + statusCode = statusCode ?? 403; } else if (!statusCode) { statusCode = 500; } diff --git a/packages/backend/src/server/api/RateLimiterService.ts b/packages/backend/src/server/api/RateLimiterService.ts index e94160a657..e9afb9d05a 100644 --- a/packages/backend/src/server/api/RateLimiterService.ts +++ b/packages/backend/src/server/api/RateLimiterService.ts @@ -32,18 +32,11 @@ export class RateLimiterService { @bindThis public limit(limitation: IEndpointMeta['limit'] & { key: NonNullable<string> }, actor: string, factor = 1) { - { - if (this.disabled) { - return Promise.resolve(); - } - - // those lines with the "wrong" brace style / indentation are - // done that way so that the *other* lines stay identical to - // Misskey, simplifying merges + return new Promise<void>((ok, reject) => { + if (this.disabled) ok(); // Short-term limit - // eslint-disable-next-line brace-style - const minP = () => { return new Promise<void>((ok, reject) => { + const minP = (): void => { const minIntervalLimiter = new Limiter({ id: `${actor}:${limitation.key}:min`, duration: limitation.minInterval! * factor, @@ -62,18 +55,16 @@ export class RateLimiterService { return reject({ code: 'BRIEF_REQUEST_INTERVAL', info }); } else { if (hasLongTermLimit) { - return maxP().then(ok, reject); + return maxP(); } else { return ok(); } } }); - // eslint-disable-next-line brace-style - }); }; + }; // Long term limit - // eslint-disable-next-line brace-style - const maxP = () => { return new Promise<void>((ok, reject) => { + const maxP = (): void => { const limiter = new Limiter({ id: `${actor}:${limitation.key}`, duration: limitation.duration! * factor, @@ -94,8 +85,7 @@ export class RateLimiterService { return ok(); } }); - // eslint-disable-next-line brace-style - }); }; + }; const hasShortTermLimit = typeof limitation.minInterval === 'number'; @@ -104,12 +94,12 @@ export class RateLimiterService { typeof limitation.max === 'number'; if (hasShortTermLimit) { - return minP(); + minP(); } else if (hasLongTermLimit) { - return maxP(); + maxP(); } else { - return Promise.resolve(); + ok(); } - } + }); } } diff --git a/packages/backend/src/server/api/endpoints/sponsors.ts b/packages/backend/src/server/api/endpoints/sponsors.ts index b6ccb9b2f9..99414e739a 100644 --- a/packages/backend/src/server/api/endpoints/sponsors.ts +++ b/packages/backend/src/server/api/endpoints/sponsors.ts @@ -10,7 +10,7 @@ import { DI } from '@/di-symbols.js'; export const meta = { tags: ['meta'], - description: 'Get Sharkey GH Sponsors', + description: 'Get Sharkey Sponsors', requireCredential: false, requireCredentialPrivateMode: false, @@ -30,29 +30,28 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint- @Inject(DI.redis) private redisClient: Redis.Redis, ) { super(meta, paramDef, async (ps, me) => { - let sponsors; - const cachedSponsors = await this.redisClient.get('sponsors'); + let totalSponsors; + const cachedSponsors = await this.redisClient.get('sponsors'); + if (!ps.forceUpdate && cachedSponsors) { - sponsors = JSON.parse(cachedSponsors); + totalSponsors = JSON.parse(cachedSponsors); } else { - AbortSignal.timeout ??= function timeout(ms) { - const ctrl = new AbortController(); - setTimeout(() => ctrl.abort(), ms); - return ctrl.signal; - }; - try { - sponsors = await fetch('https://kaifa.ch/transfem-sponsors.json', { signal: AbortSignal.timeout(2000) }) - .then((response) => response.json()); + 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(sponsors), 'EX', 3600); + await this.redisClient.set('sponsors', JSON.stringify(totalSponsors), 'EX', 3600); } catch (error) { - sponsors = { - sponsors: [], - }; + totalSponsors = []; } } - return { sponsor_data: sponsors['sponsors'] }; + return { sponsor_data: totalSponsors }; }); } } |