mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(database): ♻️ Make emojis use a Media instead of just rawdogging the URI
This commit is contained in:
parent
c7aae24d42
commit
cf1104d762
18 changed files with 4823 additions and 128 deletions
|
|
@ -1,9 +1,9 @@
|
|||
import { parseUserAddress, userAddressValidator } from "@/api";
|
||||
import { Args, type Command, Flags, type Interfaces } from "@oclif/core";
|
||||
import { type Emoji, Instance, User, db } from "@versia/kit/db";
|
||||
import { Emoji, Instance, User } from "@versia/kit/db";
|
||||
import { Emojis, Instances, Users } from "@versia/kit/tables";
|
||||
import chalk from "chalk";
|
||||
import { and, eq, getTableColumns, like } from "drizzle-orm";
|
||||
import { and, eq, inArray, like } from "drizzle-orm";
|
||||
import { BaseCommand } from "./base.ts";
|
||||
|
||||
export type FlagsType<T extends typeof Command> = Interfaces.InferredFlags<
|
||||
|
|
@ -203,14 +203,7 @@ export abstract class EmojiFinderCommand<
|
|||
this.args = args as ArgsType<T>;
|
||||
}
|
||||
|
||||
public async findEmojis(): Promise<
|
||||
Omit<
|
||||
typeof Emoji.$type & {
|
||||
instanceUrl: string | null;
|
||||
},
|
||||
"instance"
|
||||
>[]
|
||||
> {
|
||||
public async findEmojis(): Promise<Emoji[]> {
|
||||
// Check if there are asterisks in the identifier but no pattern flag, warn the user if so
|
||||
if (this.args.identifier.includes("*") && !this.flags.pattern) {
|
||||
this.log(
|
||||
|
|
@ -228,22 +221,26 @@ export abstract class EmojiFinderCommand<
|
|||
? this.args.identifier.replace(/\*/g, "%")
|
||||
: this.args.identifier;
|
||||
|
||||
return await db
|
||||
.select({
|
||||
...getTableColumns(Emojis),
|
||||
instanceUrl: Instances.baseUrl,
|
||||
})
|
||||
.from(Emojis)
|
||||
.leftJoin(Instances, eq(Emojis.instanceId, Instances.id))
|
||||
.where(
|
||||
and(
|
||||
this.flags.type === "shortcode"
|
||||
? operator(Emojis.shortcode, identifier)
|
||||
: undefined,
|
||||
this.flags.type === "instance"
|
||||
? operator(Instances.baseUrl, identifier)
|
||||
: undefined,
|
||||
),
|
||||
);
|
||||
const instanceIds =
|
||||
this.flags.type === "instance"
|
||||
? (
|
||||
await Instance.manyFromSql(
|
||||
operator(Instances.baseUrl, identifier),
|
||||
)
|
||||
).map((instance) => instance.id)
|
||||
: undefined;
|
||||
|
||||
return await Emoji.manyFromSql(
|
||||
and(
|
||||
this.flags.type === "shortcode"
|
||||
? operator(Emojis.shortcode, identifier)
|
||||
: undefined,
|
||||
instanceIds && instanceIds.length > 0
|
||||
? inArray(Emojis.instanceId, instanceIds)
|
||||
: undefined,
|
||||
),
|
||||
undefined,
|
||||
this.flags.limit,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import { Emojis } from "@versia/kit/tables";
|
|||
import chalk from "chalk";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import ora from "ora";
|
||||
import { MediaManager } from "~/classes/media/media-manager";
|
||||
import { BaseCommand } from "~/cli/base";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
|
|
@ -97,35 +96,22 @@ export default class EmojiAdd extends BaseCommand<typeof EmojiAdd> {
|
|||
);
|
||||
}
|
||||
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
const spinner = ora("Uploading emoji").start();
|
||||
|
||||
const uploaded = await mediaManager.addFile(file).catch((e: Error) => {
|
||||
spinner.fail();
|
||||
this.log(`${chalk.red("✗")} Error: ${chalk.red(e.message)}`);
|
||||
return null;
|
||||
});
|
||||
|
||||
if (!uploaded) {
|
||||
return this.exit(1);
|
||||
}
|
||||
const media = await Media.fromFile(file);
|
||||
|
||||
spinner.succeed();
|
||||
|
||||
await Emoji.insert({
|
||||
shortcode: args.shortcode,
|
||||
url: Media.getUrl(uploaded.path),
|
||||
mediaId: media.id,
|
||||
visibleInPicker: true,
|
||||
contentType: uploaded.uploadedFile.type,
|
||||
});
|
||||
|
||||
this.log(
|
||||
`${chalk.green("✓")} Created emoji ${chalk.green(
|
||||
args.shortcode,
|
||||
)} with url ${chalk.blue(
|
||||
chalk.underline(Media.getUrl(uploaded.path)),
|
||||
)}`,
|
||||
)} with url ${chalk.blue(chalk.underline(media.getUrl()))}`,
|
||||
);
|
||||
|
||||
this.exit(0);
|
||||
|
|
|
|||
|
|
@ -55,13 +55,10 @@ export default class EmojiDelete extends EmojiFinderCommand<
|
|||
|
||||
flags.print &&
|
||||
this.log(
|
||||
formatArray(emojis, [
|
||||
"id",
|
||||
"shortcode",
|
||||
"alt",
|
||||
"contentType",
|
||||
"instanceUrl",
|
||||
]),
|
||||
formatArray(
|
||||
emojis.map((e) => e.data),
|
||||
["id", "shortcode", "alt", "contentType", "instanceUrl"],
|
||||
),
|
||||
);
|
||||
|
||||
if (flags.confirm) {
|
||||
|
|
@ -80,13 +77,13 @@ export default class EmojiDelete extends EmojiFinderCommand<
|
|||
const spinner = ora("Deleting emoji(s)").start();
|
||||
|
||||
for (const emoji of emojis) {
|
||||
spinner.text = `Deleting emoji ${chalk.gray(emoji.shortcode)} (${
|
||||
spinner.text = `Deleting emoji ${chalk.gray(emoji.data.shortcode)} (${
|
||||
emojis.findIndex((e) => e.id === emoji.id) + 1
|
||||
}/${emojis.length})`;
|
||||
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
await mediaManager.deleteFileByUrl(emoji.url);
|
||||
await mediaManager.deleteFileByUrl(emoji.media.getUrl());
|
||||
|
||||
await db.delete(Emojis).where(eq(Emojis.id, emoji.id));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import { and, inArray, isNull } from "drizzle-orm";
|
|||
import { lookup } from "mime-types";
|
||||
import ora from "ora";
|
||||
import { unzip } from "unzipit";
|
||||
import { MediaManager } from "~/classes/media/media-manager";
|
||||
import { BaseCommand } from "~/cli/base";
|
||||
import { config } from "~/packages/config-manager";
|
||||
|
||||
|
|
@ -169,8 +168,6 @@ export default class EmojiImport extends BaseCommand<typeof EmojiImport> {
|
|||
|
||||
const importSpinner = ora("Importing emojis").start();
|
||||
|
||||
const mediaManager = new MediaManager(config);
|
||||
|
||||
const successfullyImported: MetaType["emojis"] = [];
|
||||
|
||||
for (const emoji of newEmojis) {
|
||||
|
|
@ -197,26 +194,12 @@ export default class EmojiImport extends BaseCommand<typeof EmojiImport> {
|
|||
type: contentType,
|
||||
});
|
||||
|
||||
const uploaded = await mediaManager
|
||||
.addFile(newFile)
|
||||
.catch((e: Error) => {
|
||||
this.log(
|
||||
`${chalk.red("✗")} Error uploading ${chalk.red(
|
||||
emoji.emoji.name,
|
||||
)}: ${chalk.red(e.message)}`,
|
||||
);
|
||||
return null;
|
||||
});
|
||||
|
||||
if (!uploaded) {
|
||||
continue;
|
||||
}
|
||||
const media = await Media.fromFile(newFile);
|
||||
|
||||
await Emoji.insert({
|
||||
shortcode: emoji.emoji.name,
|
||||
url: Media.getUrl(uploaded.path),
|
||||
mediaId: media.id,
|
||||
visibleInPicker: true,
|
||||
contentType: uploaded.uploadedFile.type,
|
||||
});
|
||||
|
||||
successfullyImported.push(emoji);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue