feat (api): add /api/emojis endpoint
This commit is contained in:
parent
7b64d2cf3f
commit
b9c89aecca
3 changed files with 90 additions and 0 deletions
|
@ -138,6 +138,7 @@ import * as ep___drive_folders_update from "./endpoints/drive/folders/update.js"
|
||||||
import * as ep___drive_stream from "./endpoints/drive/stream.js";
|
import * as ep___drive_stream from "./endpoints/drive/stream.js";
|
||||||
import * as ep___emailAddress_available from "./endpoints/email-address/available.js";
|
import * as ep___emailAddress_available from "./endpoints/email-address/available.js";
|
||||||
import * as ep___emoji from "./endpoints/emoji.js";
|
import * as ep___emoji from "./endpoints/emoji.js";
|
||||||
|
import * as ep___emojis from "./endpoints/emojis.js";
|
||||||
import * as ep___endpoint from "./endpoints/endpoint.js";
|
import * as ep___endpoint from "./endpoints/endpoint.js";
|
||||||
import * as ep___endpoints from "./endpoints/endpoints.js";
|
import * as ep___endpoints from "./endpoints/endpoints.js";
|
||||||
import * as ep___exportCustomEmojis from "./endpoints/export-custom-emojis.js";
|
import * as ep___exportCustomEmojis from "./endpoints/export-custom-emojis.js";
|
||||||
|
@ -493,6 +494,7 @@ const eps = [
|
||||||
["drive/stream", ep___drive_stream],
|
["drive/stream", ep___drive_stream],
|
||||||
["email-address/available", ep___emailAddress_available],
|
["email-address/available", ep___emailAddress_available],
|
||||||
["emoji", ep___emoji],
|
["emoji", ep___emoji],
|
||||||
|
["emojis", ep___emojis],
|
||||||
["endpoint", ep___endpoint],
|
["endpoint", ep___endpoint],
|
||||||
["endpoints", ep___endpoints],
|
["endpoints", ep___endpoints],
|
||||||
["export-custom-emojis", ep___exportCustomEmojis],
|
["export-custom-emojis", ep___exportCustomEmojis],
|
||||||
|
|
50
packages/backend/src/server/api/endpoints/emojis.ts
Normal file
50
packages/backend/src/server/api/endpoints/emojis.ts
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
import { Emojis } from "@/models/index.js";
|
||||||
|
import define from "@/server/api/define.js";
|
||||||
|
import { IsNull } from "typeorm";
|
||||||
|
|
||||||
|
export const meta = {
|
||||||
|
tags: ["meta"],
|
||||||
|
|
||||||
|
requireCredential: false,
|
||||||
|
cacheSec: 3600,
|
||||||
|
|
||||||
|
res: {
|
||||||
|
type: "object",
|
||||||
|
optional: false,
|
||||||
|
nullable: false,
|
||||||
|
properties: {
|
||||||
|
emojis: {
|
||||||
|
type: "array",
|
||||||
|
optional: false,
|
||||||
|
nullable: false,
|
||||||
|
items: {
|
||||||
|
type: "object",
|
||||||
|
optional: false,
|
||||||
|
nullable: false,
|
||||||
|
ref: "Emoji",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export const paramDef = {
|
||||||
|
type: "object",
|
||||||
|
properties: {},
|
||||||
|
required: [],
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export default define(meta, paramDef, async (ps, me) => {
|
||||||
|
const emojis = await Emojis.find({
|
||||||
|
where: {
|
||||||
|
host: IsNull(),
|
||||||
|
},
|
||||||
|
order: {
|
||||||
|
category: "ASC",
|
||||||
|
name: "ASC",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
emojis: await Emojis.packMany(emojis),
|
||||||
|
};
|
||||||
|
});
|
|
@ -25,6 +25,7 @@ import {
|
||||||
Pages,
|
Pages,
|
||||||
Channels,
|
Channels,
|
||||||
Clips,
|
Clips,
|
||||||
|
Emojis,
|
||||||
GalleryPosts,
|
GalleryPosts,
|
||||||
} from "@/models/index.js";
|
} from "@/models/index.js";
|
||||||
import * as Acct from "@/misc/acct.js";
|
import * as Acct from "@/misc/acct.js";
|
||||||
|
@ -111,6 +112,43 @@ app.use(async (ctx, next) => {
|
||||||
// Init router
|
// Init router
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
|
router.get<{ Params: { path: string } }>("/emoji/:path(.*)", async (ctx) => {
|
||||||
|
const path = ctx.params.path;
|
||||||
|
|
||||||
|
ctx.set("Cache-Control", "public, max-age=86400");
|
||||||
|
|
||||||
|
if (!path.match(/^[a-zA-Z0-9\-_@\.]+?\.webp$/)) {
|
||||||
|
ctx.throw(400, "Bad Request");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const name = path.split("@")[0].replace(/\.webp$/i, "");
|
||||||
|
const host = path.split("@")[1]?.replace(/\.webp$/i, "");
|
||||||
|
|
||||||
|
const emoji = await Emojis.findOneBy({
|
||||||
|
host: host == null || host === "." ? IsNull() : host,
|
||||||
|
name: name,
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.set(
|
||||||
|
"Content-Security-Policy",
|
||||||
|
"default-src 'none'; style-src 'unsafe-inline'",
|
||||||
|
);
|
||||||
|
|
||||||
|
if (emoji == null) {
|
||||||
|
ctx.throw(404, "Emoji not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let url = new URL(`${config.mediaProxy || config.url + "/proxy"}/emoji.webp`);
|
||||||
|
// || emoji.originalUrl してるのは後方互換性のため
|
||||||
|
url.searchParams.append("url", emoji.publicUrl || emoji.originalUrl);
|
||||||
|
url.searchParams.append("emoji", "1");
|
||||||
|
if ("static" in ctx.query) url.searchParams.append("static", "1");
|
||||||
|
|
||||||
|
return ctx.redirect(url.toString());
|
||||||
|
});
|
||||||
|
|
||||||
//#region static assets
|
//#region static assets
|
||||||
|
|
||||||
router.get("/static-assets/(.*)", async (ctx) => {
|
router.get("/static-assets/(.*)", async (ctx) => {
|
||||||
|
|
Loading…
Reference in a new issue