hippofish/packages/backend/src/server/api/endpoints/patrons.ts

38 lines
955 B
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import define from "../define.js";
2023-06-08 22:19:46 +02:00
import { redisClient } from "@/db/redis.js";
2022-11-19 08:54:50 +01:00
export const meta = {
2023-01-13 05:40:33 +01:00
tags: ["meta"],
description: "Get list of Calckey patrons from Codeberg",
2022-11-19 08:54:50 +01:00
requireCredential: false,
requireCredentialPrivateMode: false,
} as const;
export const paramDef = {
2023-01-13 05:40:33 +01:00
type: "object",
properties: {
forceUpdate: { type: "boolean", default: false },
},
2022-11-19 08:54:50 +01:00
required: [],
} as const;
export default define(meta, paramDef, async (ps) => {
2022-11-19 08:54:50 +01:00
let patrons;
2023-06-08 22:19:46 +02:00
const cachedPatrons = await redisClient.get("patrons");
if (!ps.forceUpdate && cachedPatrons) {
2023-06-08 22:19:46 +02:00
patrons = JSON.parse(cachedPatrons);
2023-06-12 06:57:20 +02:00
} else {
2023-06-08 22:19:46 +02:00
patrons = await fetch(
"https://codeberg.org/calckey/calckey/raw/branch/develop/patrons.json",
)
.then((response) => response.json())
.catch(() => {
patrons = cachedPatrons ? JSON.parse(cachedPatrons) : [];
});
2023-06-08 22:19:46 +02:00
await redisClient.set("patrons", JSON.stringify(patrons), "EX", 3600);
}
2022-11-19 08:54:50 +01:00
2023-06-08 22:19:46 +02:00
return patrons["patrons"];
2022-11-19 08:54:50 +01:00
});