feat (backend): enhance add emoji API

This commit is contained in:
naskya 2024-07-26 14:30:24 +09:00
parent 8066c46476
commit 3e416a9a07
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
5 changed files with 58 additions and 22 deletions

View file

@ -2,6 +2,11 @@
Breaking changes are indicated by the :warning: icon.
## Unreleased
- Added `name`, `category`, `aliases`, `license` optional parameters to `admin/emoji/add` endpoint.
- Added `name` optional parameter to `drive/files/upload-from-url` endpoint.
## v20240725
- Added `i/export-followers` endpoint.

View file

@ -2,7 +2,7 @@ import define from "@/server/api/define.js";
import { Emojis, DriveFiles } from "@/models/index.js";
import {
type ImageSize,
genId,
genIdAt,
getImageSizeFromUrl,
publishToBroadcastStream,
} from "backend-rs";
@ -36,7 +36,31 @@ export const meta = {
export const paramDef = {
type: "object",
properties: {
fileId: { type: "string", format: "misskey:id" },
fileId: {
type: "string",
format: "misskey:id",
nullable: false,
},
name: {
type: "string",
nullable: false,
},
category: {
type: "string",
nullable: false,
},
aliases: {
type: "array",
nullable: false,
items: {
type: "string",
nullable: false,
},
},
license: {
type: "string",
nullable: false,
},
},
required: ["fileId"],
} as const;
@ -50,7 +74,10 @@ export default define(meta, paramDef, async (ps, me) => {
if (file == null) throw new ApiError(meta.errors.noSuchFile);
const name = file.name.split(".")[0].match(/^[a-z0-9_]+$/)
const name =
ps.name != null
? ps.name
: file.name.split(".")[0].match(/^[a-z0-9_]+$/)
? file.name.split(".")[0]
: `_${rndstr("a-z0-9", 8)}_`;
@ -62,17 +89,19 @@ export default define(meta, paramDef, async (ps, me) => {
apiLogger.debug(inspect(err));
}
const now = new Date();
const emoji = await Emojis.insert({
id: genId(),
updatedAt: new Date(),
id: genIdAt(now),
updatedAt: now,
name: name,
category: null,
category: ps.category ?? null,
host: null,
aliases: [],
aliases: ps.aliases ?? [],
originalUrl: file.url,
publicUrl: file.webpublicUrl ?? file.url,
type: file.webpublicType ?? file.type,
license: null,
license: ps.license ?? null,
width: size?.width || null,
height: size?.height || null,
}).then((x) => Emojis.findOneByOrFail(x.identifiers[0]));

View file

@ -24,6 +24,7 @@ export const paramDef = {
type: "object",
properties: {
url: { type: "string" },
name: { type: "string" },
folderId: {
type: "string",
format: "misskey:id",
@ -41,6 +42,7 @@ export const paramDef = {
export default define(meta, paramDef, async (ps, user) => {
uploadFromUrl({
url: ps.url,
name: ps.name,
user,
folderId: ps.folderId,
sensitive: ps.isSensitive,

View file

@ -37,15 +37,13 @@ export const paramDef = {
required: ["name"],
} as const;
export default define(meta, paramDef, async (ps, me) => {
const emoji = await Emojis.findOne({
where: {
export default define(meta, paramDef, async (ps) => {
const emoji = await Emojis.findOneBy({
name: ps.name,
host: IsNull(),
},
});
if (!emoji) {
if (emoji == null) {
throw new ApiError(meta.errors.noSuchEmoji);
}

View file

@ -21,6 +21,7 @@ type Args = {
host: User["host"];
driveCapacityOverrideMb: User["driveCapacityOverrideMb"];
} | null;
name?: string;
folderId?: DriveFolder["id"] | null;
uri?: string | null;
sensitive?: boolean;
@ -34,6 +35,7 @@ type Args = {
export async function uploadFromUrl({
url,
name,
user,
folderId = null,
uri = null,
@ -53,14 +55,14 @@ export async function uploadFromUrl({
throw new Error("Private IP is not allowed");
}
let name = parsedUrl.pathname.split("/").pop() || null;
if (name == null || !DriveFiles.validateFileName(name)) {
name = null;
let filename = name ?? parsedUrl.pathname.split("/").pop();
if (filename == null || !DriveFiles.validateFileName(filename)) {
filename = null;
}
// If the comment is same as the name, skip comment
// (image.name is passed in when receiving attachment)
if (comment != null && name === comment) {
if (comment != null && filename === comment) {
comment = null;
}
@ -74,7 +76,7 @@ export async function uploadFromUrl({
const driveFile = await addFile({
user,
path,
name,
name: filename,
comment,
folderId,
force,