mirror of
https://github.com/spacebarchat/server.git
synced 2026-08-25 11:29:53 +00:00
Some cleanup with gifs
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -77,4 +77,21 @@ public class GifTests(ITestOutputHelper testOutputHelper, TestFixture fixture) :
|
||||
Assert.NotEqual(0, gif.Height);
|
||||
});
|
||||
}
|
||||
|
||||
[Theory, MemberData(nameof(GifProviders))]
|
||||
public async Task GetTrendingGifs(string provider) {
|
||||
var resp = await Assert.HttpSuccess(await Client.ApiHttpClient.GetAsync($"gifs/trending-gifs?provider={provider}", TestContext.Current.CancellationToken));
|
||||
var respContent = await resp.Content.ReadFromJsonAsync<List<GifItem>>(cancellationToken: TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.True(respContent!.Count > 0, "respContent.Count > 0");
|
||||
Assert.All(respContent, gif => {
|
||||
Assert.StringNotNullOrWhitespace(gif.Id);
|
||||
Assert.StringNotNullOrWhitespace(gif.GifSource);
|
||||
Assert.StringNotNullOrWhitespace(gif.Preview);
|
||||
Assert.StringNotNullOrWhitespace(gif.Source);
|
||||
Assert.StringNotNullOrWhitespace(gif.Url);
|
||||
Assert.NotEqual(0, gif.Width);
|
||||
Assert.NotEqual(0, gif.Height);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -129,6 +129,7 @@
|
||||
"@spacebar/database": "dist/database",
|
||||
"@spacebar/extensions": "dist/extensions",
|
||||
"@spacebar/gateway": "dist/gateway",
|
||||
"@spacebar/integrations": "dist/integrations",
|
||||
"@spacebar/util": "dist/util",
|
||||
"@spacebar/webrtc": "dist/webrtc",
|
||||
"@spacebar/schemas": "dist/schemas",
|
||||
|
||||
+1
-1
@@ -29,7 +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";
|
||||
import { GifProviderManager } from "@spacebar/integrations/gifs";
|
||||
|
||||
const ASSETS_FOLDER = path.join(__dirname, "..", "..", "assets");
|
||||
const PUBLIC_ASSETS_FOLDER = path.join(ASSETS_FOLDER, "public");
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
import { Request, Response, Router } from "express";
|
||||
import { route } from "@spacebar/api/util/handlers/route";
|
||||
import { GifMediaTypes } from "@spacebar/schemas";
|
||||
import { GifProviderManager } from "@spacebar/util/util/integrations/gifProviders/GifProviderManager";
|
||||
import { GifProviderManager } from "@spacebar/integrations/gifs";
|
||||
|
||||
const router = Router({ mergeParams: true });
|
||||
|
||||
@@ -53,11 +53,8 @@ router.get(
|
||||
},
|
||||
}),
|
||||
async (req: Request, res: Response) => {
|
||||
const { provider } = req.query;
|
||||
|
||||
const impl = GifProviderManager.getProvider((provider as string) ?? "tenor");
|
||||
const impl = GifProviderManager.getProvider((req.query.provider as string) ?? "tenor");
|
||||
const result = await impl.search(req.query as typeof impl.search.arguments);
|
||||
|
||||
res.json(result).status(200);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -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, GifMediaTypes } from "@spacebar/schemas";
|
||||
import { GifMediaTypes } from "@spacebar/schemas";
|
||||
import { GifProviderManager } from "@spacebar/integrations/gifs";
|
||||
|
||||
const router = Router({ mergeParams: true });
|
||||
|
||||
@@ -36,27 +36,25 @@ router.get(
|
||||
type: "string",
|
||||
description: "Locale",
|
||||
},
|
||||
limit: {
|
||||
type: "number",
|
||||
description: "Maximum number of GIFs to return",
|
||||
},
|
||||
provider: {
|
||||
type: "string",
|
||||
description: "Provider to use",
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
body: "TenorGifsResponse",
|
||||
body: "GifsResponse",
|
||||
},
|
||||
},
|
||||
}),
|
||||
async (req: Request, res: Response) => {
|
||||
// TODO: Custom providers
|
||||
const { media_format, locale } = req.query;
|
||||
|
||||
const apiKey = getGifApiKey();
|
||||
|
||||
const response = await fetch(`https://g.tenor.com/v1/trending?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);
|
||||
const provider = GifProviderManager.getProvider((req.query.provider as string) ?? "tenor");
|
||||
const results = await provider.getTrendingGifs(req.query as typeof provider.getTrendingGifs.arguments);
|
||||
res.json(results).status(200);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -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,11 +17,9 @@
|
||||
*/
|
||||
|
||||
import { route } from "@spacebar/api/util/handlers/route";
|
||||
import { getGifApiKey, parseGifResult } from "@spacebar/util";
|
||||
import { Request, Response, Router } from "express";
|
||||
import { TenorCategoriesResults, TrendingGifsResponse, TenorTrendingResults } from "@spacebar/schemas";
|
||||
import { GifProviderManager } from "@spacebar/util/util/integrations/gifProviders/GifProviderManager";
|
||||
import trendingGifs from "@spacebar/api/routes/gifs/trending-gifs";
|
||||
import { TrendingGifsResponse } from "@spacebar/schemas";
|
||||
import { GifProviderManager } from "@spacebar/integrations/gifs";
|
||||
|
||||
const router = Router({ mergeParams: true });
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
|
||||
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
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export * from "./GifProviderManager";
|
||||
export * from "./IGifProvider";
|
||||
+52
-9
@@ -17,9 +17,10 @@
|
||||
*/
|
||||
|
||||
import type { IGifProvider } from "../IGifProvider";
|
||||
import { TenorGif, GifsResponse, TenorCategoriesResults, GifTrendingCategory } from "@spacebar/schemas";
|
||||
import { Config, getGifApiKey, parseGifResult } from "@spacebar/util";
|
||||
import { GifsResponse, GifTrendingCategory, GifMediaTypes } from "@spacebar/schemas";
|
||||
import { Config } from "@spacebar/util";
|
||||
|
||||
// Tenor V1 API... Not yet shut down as of writing, but is going soon...
|
||||
export default class TenorGifProvider implements IGifProvider {
|
||||
id = "tenor";
|
||||
available = true;
|
||||
@@ -41,8 +42,8 @@ export default class TenorGifProvider implements IGifProvider {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
|
||||
const { results } = (await response.json()) as { results: TenorGif[] };
|
||||
return results.map(this.convertGifResult);
|
||||
const responseData = (await response.json()) as TenorSearchResults;
|
||||
return responseData.results.map(this.convertGifResult);
|
||||
}
|
||||
|
||||
async getTrendingCategories(query: { media_format: string; locale: string }): Promise<GifTrendingCategory[]> {
|
||||
@@ -51,9 +52,8 @@ export default class TenorGifProvider implements IGifProvider {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
|
||||
const { tags } = (await response.json()) as TenorCategoriesResults;
|
||||
|
||||
return tags.map((x) => ({
|
||||
const responseData = (await response.json()) as TenorCategoriesResults;
|
||||
return responseData.tags.map((x) => ({
|
||||
name: x.searchterm,
|
||||
src: x.image,
|
||||
})) satisfies GifTrendingCategory[];
|
||||
@@ -65,8 +65,8 @@ export default class TenorGifProvider implements IGifProvider {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
|
||||
const { results } = (await response.json()) as { results: TenorGif[] };
|
||||
return results.map(this.convertGifResult);
|
||||
const responseData = (await response.json()) as TenorTrendingResults;
|
||||
return responseData.results.map(this.convertGifResult);
|
||||
}
|
||||
|
||||
private convertGifResult(result: TenorGif) {
|
||||
@@ -82,3 +82,46 @@ export default class TenorGifProvider implements IGifProvider {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// API response types
|
||||
|
||||
type TenorGif = {
|
||||
created: number;
|
||||
hasaudio: boolean;
|
||||
id: string;
|
||||
media: { [type in keyof typeof GifMediaTypes]: TenorMedia }[];
|
||||
tags: string[];
|
||||
title: string;
|
||||
itemurl: string;
|
||||
hascaption: boolean;
|
||||
url: string;
|
||||
};
|
||||
|
||||
type TenorMedia = {
|
||||
preview: string;
|
||||
url: string;
|
||||
dims: number[];
|
||||
size: number;
|
||||
};
|
||||
|
||||
type TenorCategory = {
|
||||
searchterm: string;
|
||||
path: string;
|
||||
image: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
type TenorCategoriesResults = {
|
||||
tags: TenorCategory[];
|
||||
};
|
||||
|
||||
type TenorTrendingResults = {
|
||||
next: string;
|
||||
results: TenorGif[];
|
||||
locale: string;
|
||||
};
|
||||
|
||||
type TenorSearchResults = {
|
||||
next: string;
|
||||
results: TenorGif[];
|
||||
};
|
||||
@@ -20,6 +20,7 @@ export * from "./bots";
|
||||
export * from "./channels";
|
||||
export * from "./developers";
|
||||
export * from "./guilds";
|
||||
export * from "./integrations";
|
||||
export * from "./messages";
|
||||
export * from "./reports";
|
||||
export * from "./users";
|
||||
|
||||
@@ -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
|
||||
@@ -30,47 +30,6 @@ export enum GifMediaTypes {
|
||||
nanowebm,
|
||||
}
|
||||
|
||||
export type TenorMedia = {
|
||||
preview: string;
|
||||
url: string;
|
||||
dims: number[];
|
||||
size: number;
|
||||
};
|
||||
|
||||
export type TenorGif = {
|
||||
created: number;
|
||||
hasaudio: boolean;
|
||||
id: string;
|
||||
media: { [type in keyof typeof GifMediaTypes]: TenorMedia }[];
|
||||
tags: string[];
|
||||
title: string;
|
||||
itemurl: string;
|
||||
hascaption: boolean;
|
||||
url: string;
|
||||
};
|
||||
|
||||
export type TenorCategory = {
|
||||
searchterm: string;
|
||||
path: string;
|
||||
image: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
export type TenorCategoriesResults = {
|
||||
tags: TenorCategory[];
|
||||
};
|
||||
|
||||
export type TenorTrendingResults = {
|
||||
next: string;
|
||||
results: TenorGif[];
|
||||
locale: string;
|
||||
};
|
||||
|
||||
export type TenorSearchResults = {
|
||||
next: string;
|
||||
results: TenorGif[];
|
||||
};
|
||||
|
||||
export interface GifResponse {
|
||||
id: string;
|
||||
title: string;
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
|
||||
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
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export * from "./Gifs";
|
||||
@@ -54,7 +54,6 @@ export * from "./PreloadMessagesResponseSchema";
|
||||
export * from "./RefreshUrlsResponse";
|
||||
export * from "./SettingsProtoUpdateResponse";
|
||||
export * from "./TeamListResponse";
|
||||
export * from "./Tenor";
|
||||
export * from "./TokenResponse";
|
||||
export * from "./TypedResponses";
|
||||
export * from "./UpdatesResponse";
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import { HTTPError } from "lambert-server/HTTPError";
|
||||
import { Config } from "./Config";
|
||||
import { TenorGif } from "@spacebar/schemas";
|
||||
|
||||
// @deprecated
|
||||
export function parseGifResult(result: TenorGif) {
|
||||
return {
|
||||
id: result.id,
|
||||
title: result.title,
|
||||
url: result.itemurl,
|
||||
src: result.media[0].mp4.url,
|
||||
gif_src: result.media[0].gif.url,
|
||||
width: result.media[0].mp4.dims[0],
|
||||
height: result.media[0].mp4.dims[1],
|
||||
preview: result.media[0].mp4.preview,
|
||||
};
|
||||
}
|
||||
|
||||
export function getGifApiKey() {
|
||||
const { enabled, provider, apiKey } = Config.get().gif;
|
||||
if (!enabled) throw new HTTPError(`Gifs are disabled`);
|
||||
if (provider !== "tenor" || !apiKey) throw new HTTPError(`${provider} gif provider not supported`);
|
||||
|
||||
return apiKey;
|
||||
}
|
||||
@@ -41,7 +41,6 @@ export * from "./Token";
|
||||
export * from "./TraverseDirectory";
|
||||
export * from "./WebAuthn";
|
||||
export * from "./ChannelFlags";
|
||||
export * from "./Gifs";
|
||||
export * from "./Application";
|
||||
export * from "./NameValidation";
|
||||
export * from "./Version";
|
||||
|
||||
@@ -48,6 +48,8 @@
|
||||
"@spacebar/webrtc*": ["./src/webrtc*"],
|
||||
"@spacebar/schemas": ["./src/schemas"], // Required for stable tsc, typescript-go doesn't need this
|
||||
"@spacebar/schemas*": ["./src/schemas*"],
|
||||
"@spacebar/integrations": ["./src/integrations"], // Required for stable tsc, typescript-go doesn't need this
|
||||
"@spacebar/integrations*": ["./src/integrations*"],
|
||||
"lambert-server": ["./src/util/util/lambert-server"], // Required for stable tsc, typescript-go doesn't need this
|
||||
"lambert-server*": ["./src/util/util/lambert-server*"],
|
||||
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
|
||||
|
||||
Reference in New Issue
Block a user