Basic gif integration manager, port tenor search to that

This commit is contained in:
Rory& committed 2026-07-08 01:20:38 +02:00
1 parent 52f2e814f9
commit ee57b28c32
11 files changed
+164 -23

No files matched your search

+2
View File
@@ -29,6 +29,7 @@ import { BcryptWorkerPool } from "../util/util/workers/bcrypt/BcryptWorkerPool";
import { Authentication, CORS, ImageProxy, BodyParser, ErrorHandler, initRateLimits, initTranslation } from "./middlewares";
import { initInstance } from "./util/handlers/Instance";
import { route, addPendingPoll } from "./util";
import { GifProviderManager } from "@spacebar/util/util/integrations/gifProviders/GifProviderManager";
const ASSETS_FOLDER = path.join(__dirname, "..", "..", "assets");
const PUBLIC_ASSETS_FOLDER = path.join(ASSETS_FOLDER, "public");
@@ -64,6 +65,7 @@ export class SpacebarServer extends Server {
await initInstance();
WebAuthn.init();
// await BcryptWorkerPool.Init(8); // TODO: make configurable
await GifProviderManager.init();
const logRequests = process.env["LOG_REQUESTS"] != undefined;
if (logRequests) {
+13 -16
View File
@@ -1,6 +1,6 @@
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors
Copyright (C) 2026 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
@@ -17,9 +17,9 @@
*/
import { route } from "@spacebar/api/util/handlers/route";
import { getGifApiKey, parseGifResult } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { TenorGif, TenorMediaTypes } from "@spacebar/schemas";
import { GifMediaTypes } from "@spacebar/schemas";
import { GifProviderManager } from "@spacebar/util/util/integrations/gifProviders/GifProviderManager";
const router = Router({ mergeParams: true });
@@ -35,33 +35,30 @@ router.get(
media_format: {
type: "string",
description: "Media format",
values: Object.keys(TenorMediaTypes).filter((key) => isNaN(Number(key))),
values: Object.keys(GifMediaTypes).filter((key) => isNaN(Number(key))),
},
locale: {
type: "string",
description: "Locale",
},
provider: {
type: "string",
description: "Provider to use",
},
},
responses: {
200: {
body: "TenorGifsResponse",
body: "GifsResponse",
},
},
}),
async (req: Request, res: Response) => {
// TODO: Custom providers
const { q, media_format, locale } = req.query;
const { provider } = req.query;
const apiKey = getGifApiKey();
const impl = GifProviderManager.getProvider(provider as string);
const result = await impl.search(req.query as typeof impl.search.arguments);
const response = await fetch(`https://g.tenor.com/v1/search?q=${q}&media_format=${media_format}&locale=${locale}&key=${apiKey}`, {
method: "get",
headers: { "Content-Type": "application/json" },
});
const { results } = (await response.json()) as { results: TenorGif[] };
res.json(results.map(parseGifResult)).status(200);
res.json(result).status(200);
},
);
+2 -2
View File
@@ -19,7 +19,7 @@
import { route } from "@spacebar/api/util/handlers/route";
import { getGifApiKey, parseGifResult } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { TenorGif, TenorMediaTypes } from "@spacebar/schemas";
import { TenorGif, GifMediaTypes } from "@spacebar/schemas";
const router = Router({ mergeParams: true });
@@ -30,7 +30,7 @@ router.get(
media_format: {
type: "string",
description: "Media format",
values: Object.keys(TenorMediaTypes).filter((key) => isNaN(Number(key))),
values: Object.keys(GifMediaTypes).filter((key) => isNaN(Number(key))),
},
locale: {
type: "string",