Add fail safe for sponsors endpoint in case of not being able to connect

This commit is contained in:
Marie 2024-02-21 18:59:31 +00:00 committed by Amelia Yukii
parent a1a3eec262
commit 349f846f32
2 changed files with 30 additions and 23 deletions

View file

@ -1,20 +1,20 @@
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import * as Redis from 'ioredis';
import { Endpoint } from '@/server/api/endpoint-base.js'; import { Endpoint } from '@/server/api/endpoint-base.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import * as Redis from 'ioredis';
export const meta = { export const meta = {
tags: ["meta"], tags: ['meta'],
description: "Get Sharkey GH Sponsors", description: 'Get Sharkey GH Sponsors',
requireCredential: false, requireCredential: false,
requireCredentialPrivateMode: false, requireCredentialPrivateMode: false,
} as const; } as const;
export const paramDef = { export const paramDef = {
type: "object", type: 'object',
properties: { properties: {
forceUpdate: { type: "boolean", default: false }, forceUpdate: { type: 'boolean', default: false },
}, },
required: [], required: [],
} as const; } as const;
@ -26,7 +26,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
let sponsors; let sponsors;
const cachedSponsors = await this.redisClient.get("sponsors"); const cachedSponsors = await this.redisClient.get('sponsors');
if (!ps.forceUpdate && cachedSponsors) { if (!ps.forceUpdate && cachedSponsors) {
sponsors = JSON.parse(cachedSponsors); sponsors = JSON.parse(cachedSponsors);
} else { } else {
@ -36,9 +36,16 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
return ctrl.signal; return ctrl.signal;
}; };
sponsors = await fetch("https://kaifa.ch/transfem-sponsors.json", { signal: AbortSignal.timeout(2000) }) try {
sponsors = await fetch('https://kaifa.ch/transfem-sponsors.json', { signal: AbortSignal.timeout(2000) })
.then((response) => response.json()); .then((response) => response.json());
await this.redisClient.set("sponsors", JSON.stringify(sponsors), "EX", 3600);
await this.redisClient.set('sponsors', JSON.stringify(sponsors), 'EX', 3600);
} catch (error) {
sponsors = {
sponsors: [],
};
}
} }
return { sponsor_data: sponsors['sponsors'] }; return { sponsor_data: sponsors['sponsors'] };
}); });

View file

@ -121,7 +121,7 @@ SPDX-License-Identifier: AGPL-3.0-only
</a> </a>
</div> </div>
</FormSection> </FormSection>
<FormSection> <FormSection v-if="sponsors[0].length > 0">
<template #label>Our lovely Sponsors</template> <template #label>Our lovely Sponsors</template>
<div :class="$style.contributors"> <div :class="$style.contributors">
<span <span