refactor(api): 🎨 Refactor emojis into their own class

This commit is contained in:
Jesse Wierzbinski 2024-06-12 18:52:01 -10:00
parent c61f519a34
commit d8cb1d475b
No known key found for this signature in database
11 changed files with 327 additions and 278 deletions

View file

@ -1,11 +1,12 @@
import { Args } from "@oclif/core";
import chalk from "chalk";
import { and, eq, isNull } from "drizzle-orm";
import ora from "ora";
import { BaseCommand } from "~/cli/base";
import { getUrl } from "~/database/entities/attachment";
import { db } from "~/drizzle/db";
import { Emojis } from "~/drizzle/schema";
import { config } from "~/packages/config-manager";
import { Emoji } from "~/packages/database-interface/emoji";
import { MediaBackend } from "~/packages/media-manager";
export default class EmojiAdd extends BaseCommand<typeof EmojiAdd> {
@ -33,13 +34,12 @@ export default class EmojiAdd extends BaseCommand<typeof EmojiAdd> {
const { args } = await this.parse(EmojiAdd);
// Check if emoji already exists
const existingEmoji = await db.query.Emojis.findFirst({
where: (Emojis, { eq, and, isNull }) =>
and(
eq(Emojis.shortcode, args.shortcode),
isNull(Emojis.instanceId),
),
});
const existingEmoji = await Emoji.fromSql(
and(
eq(Emojis.shortcode, args.shortcode),
isNull(Emojis.instanceId),
),
);
if (existingEmoji) {
this.log(
@ -115,24 +115,12 @@ export default class EmojiAdd extends BaseCommand<typeof EmojiAdd> {
spinner.succeed();
const emoji = await db
.insert(Emojis)
.values({
shortcode: args.shortcode,
url: getUrl(uploaded.path, config),
visibleInPicker: true,
contentType: uploaded.uploadedFile.type,
})
.returning();
if (!emoji || emoji.length === 0) {
this.log(
`${chalk.red("✗")} Failed to create emoji ${chalk.red(
args.shortcode,
)}`,
);
this.exit(1);
}
await Emoji.insert({
shortcode: args.shortcode,
url: getUrl(uploaded.path, config),
visibleInPicker: true,
contentType: uploaded.uploadedFile.type,
});
this.log(
`${chalk.green("✓")} Created emoji ${chalk.green(

View file

@ -6,9 +6,9 @@ import ora from "ora";
import { unzip } from "unzipit";
import { BaseCommand } from "~/cli/base";
import { getUrl } from "~/database/entities/attachment";
import { db } from "~/drizzle/db";
import { Emojis } from "~/drizzle/schema";
import { config } from "~/packages/config-manager";
import { Emoji } from "~/packages/database-interface/emoji";
import { MediaBackend } from "~/packages/media-manager";
type MetaType = {
@ -130,28 +130,28 @@ export default class EmojiImport extends BaseCommand<typeof EmojiImport> {
} as MetaType);
// Get all emojis that already exist
const existingEmojis = await db
.select()
.from(Emojis)
.where(
and(
isNull(Emojis.instanceId),
inArray(
Emojis.shortcode,
meta.emojis.map((e) => e.emoji.name),
),
const existingEmojis = await Emoji.manyFromSql(
and(
isNull(Emojis.instanceId),
inArray(
Emojis.shortcode,
meta.emojis.map((e) => e.emoji.name),
),
);
),
);
// Filter out existing emojis
const newEmojis = meta.emojis.filter(
(e) => !existingEmojis.find((ee) => ee.shortcode === e.emoji.name),
(e) =>
!existingEmojis.find(
(ee) => ee.data.shortcode === e.emoji.name,
),
);
existingEmojis.length > 0 &&
this.log(
`${chalk.yellow("⚠")} Emojis with shortcode ${chalk.yellow(
existingEmojis.map((e) => e.shortcode).join(", "),
existingEmojis.map((e) => e.data.shortcode).join(", "),
)} already exist in the database and will not be imported`,
);
@ -212,15 +212,12 @@ export default class EmojiImport extends BaseCommand<typeof EmojiImport> {
continue;
}
await db
.insert(Emojis)
.values({
shortcode: emoji.emoji.name,
url: getUrl(uploaded.path, config),
visibleInPicker: true,
contentType: uploaded.uploadedFile.type,
})
.execute();
await Emoji.insert({
shortcode: emoji.emoji.name,
url: getUrl(uploaded.path, config),
visibleInPicker: true,
contentType: uploaded.uploadedFile.type,
});
successfullyImported.push(emoji);
}