mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(database): ♻️ Simplify Note and User federation logic
This commit is contained in:
parent
cbbf49905b
commit
a8541bdc44
7 changed files with 137 additions and 153 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import { apiRoute, applyConfig, auth, jsonOrForm } from "@/api";
|
||||
import { sanitizedHtmlStrip } from "@/sanitization";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { Attachment, Emoji, User, db } from "@versia/kit/db";
|
||||
import { EmojiToUser, RolePermissions, Users } from "@versia/kit/tables";
|
||||
import { Attachment, Emoji, User } from "@versia/kit/db";
|
||||
import { RolePermissions, Users } from "@versia/kit/tables";
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import ISO6391 from "iso-639-1";
|
||||
import { z } from "zod";
|
||||
|
|
@ -335,36 +335,19 @@ export default apiRoute((app) =>
|
|||
await Emoji.parseFromText(sanitizedDisplayName);
|
||||
const noteEmojis = await Emoji.parseFromText(self.note);
|
||||
|
||||
self.emojis = [...displaynameEmojis, ...noteEmojis, ...fieldEmojis]
|
||||
.map((e) => e.data)
|
||||
.filter(
|
||||
// Deduplicate emojis
|
||||
(emoji, index, self) =>
|
||||
self.findIndex((e) => e.id === emoji.id) === index,
|
||||
);
|
||||
const emojis = [
|
||||
...displaynameEmojis,
|
||||
...noteEmojis,
|
||||
...fieldEmojis,
|
||||
].filter(
|
||||
// Deduplicate emojis
|
||||
(emoji, index, self) =>
|
||||
self.findIndex((e) => e.id === emoji.id) === index,
|
||||
);
|
||||
|
||||
// Connect emojis, if any
|
||||
// Do it before updating user, so that federation takes that into account
|
||||
for (const emoji of self.emojis) {
|
||||
await db
|
||||
.delete(EmojiToUser)
|
||||
.where(
|
||||
and(
|
||||
eq(EmojiToUser.emojiId, emoji.id),
|
||||
eq(EmojiToUser.userId, self.id),
|
||||
),
|
||||
)
|
||||
.execute();
|
||||
|
||||
await db
|
||||
.insert(EmojiToUser)
|
||||
.values({
|
||||
emojiId: emoji.id,
|
||||
userId: self.id,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
await user.updateEmojis(emojis);
|
||||
await user.update({
|
||||
displayName: self.displayName,
|
||||
username: self.username,
|
||||
|
|
@ -379,6 +362,7 @@ export default apiRoute((app) =>
|
|||
});
|
||||
|
||||
const output = await User.fromId(self.id);
|
||||
|
||||
if (!output) {
|
||||
return context.json({ error: "Couldn't edit user" }, 500);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -271,12 +271,11 @@ export default apiRoute((app) => {
|
|||
sensitive,
|
||||
} = context.req.valid("json");
|
||||
|
||||
if (media_ids.length > 0) {
|
||||
const foundAttachments = await Attachment.fromIds(media_ids);
|
||||
const foundAttachments =
|
||||
media_ids.length > 0 ? await Attachment.fromIds(media_ids) : [];
|
||||
|
||||
if (foundAttachments.length !== media_ids.length) {
|
||||
return context.json({ error: "Invalid media IDs" }, 422);
|
||||
}
|
||||
if (foundAttachments.length !== media_ids.length) {
|
||||
return context.json({ error: "Invalid media IDs" }, 422);
|
||||
}
|
||||
|
||||
const newNote = await note.updateFromData({
|
||||
|
|
@ -291,7 +290,7 @@ export default apiRoute((app) => {
|
|||
: undefined,
|
||||
isSensitive: sensitive,
|
||||
spoilerText: spoiler_text,
|
||||
mediaAttachments: media_ids,
|
||||
mediaAttachments: foundAttachments,
|
||||
});
|
||||
|
||||
return context.json(await newNote.toApi(user), 200);
|
||||
|
|
|
|||
|
|
@ -168,12 +168,11 @@ export default apiRoute((app) =>
|
|||
} = context.req.valid("json");
|
||||
|
||||
// Check if media attachments are all valid
|
||||
if (media_ids.length > 0) {
|
||||
const foundAttachments = await Attachment.fromIds(media_ids);
|
||||
const foundAttachments =
|
||||
media_ids.length > 0 ? await Attachment.fromIds(media_ids) : [];
|
||||
|
||||
if (foundAttachments.length !== media_ids.length) {
|
||||
return context.json({ error: "Invalid media IDs" }, 422);
|
||||
}
|
||||
if (foundAttachments.length !== media_ids.length) {
|
||||
return context.json({ error: "Invalid media IDs" }, 422);
|
||||
}
|
||||
|
||||
// Check that in_reply_to_id and quote_id are real posts if provided
|
||||
|
|
@ -199,7 +198,7 @@ export default apiRoute((app) =>
|
|||
visibility,
|
||||
isSensitive: sensitive ?? false,
|
||||
spoilerText: spoiler_text ?? "",
|
||||
mediaAttachments: media_ids,
|
||||
mediaAttachments: foundAttachments,
|
||||
replyId: in_reply_to_id ?? undefined,
|
||||
quoteId: quote_id ?? undefined,
|
||||
application: application ?? undefined,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue