feat (backend): enhance add emoji API
This commit is contained in:
parent
8066c46476
commit
3e416a9a07
5 changed files with 58 additions and 22 deletions
|
@ -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.
|
||||
|
|
|
@ -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,9 +74,12 @@ 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_]+$/)
|
||||
? file.name.split(".")[0]
|
||||
: `_${rndstr("a-z0-9", 8)}_`;
|
||||
const name =
|
||||
ps.name != null
|
||||
? ps.name
|
||||
: file.name.split(".")[0].match(/^[a-z0-9_]+$/)
|
||||
? file.name.split(".")[0]
|
||||
: `_${rndstr("a-z0-9", 8)}_`;
|
||||
|
||||
let size: ImageSize | null = null;
|
||||
try {
|
||||
|
@ -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]));
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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: {
|
||||
name: ps.name,
|
||||
host: IsNull(),
|
||||
},
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue