refactor(database): ♻️ Simplify User and Note logic further

This commit is contained in:
Jesse Wierzbinski 2024-12-09 13:50:46 +01:00
parent a8541bdc44
commit 83399ba5f1
No known key found for this signature in database
6 changed files with 67 additions and 107 deletions

View file

@ -1,5 +1,6 @@
import { idValidator } from "@/api";
import { localObjectUri } from "@/constants";
import { mergeAndDeduplicate } from "@/lib.ts";
import { sanitizedHtmlStrip } from "@/sanitization";
import { sentry } from "@/sentry";
import { getLogger } from "@logtape/logtape";
@ -13,7 +14,7 @@ import type {
Delete as VersiaDelete,
Note as VersiaNote,
} from "@versia/federation/types";
import { Instance, Notification, db } from "@versia/kit/db";
import { Instance, db } from "@versia/kit/db";
import {
Attachments,
EmojiToNote,
@ -364,14 +365,12 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
},
);
const fusedUsers = [...mentionedUsers, ...usersThatCanSeePost];
const deduplicatedUsersById = fusedUsers.filter(
(user, index, self) =>
index === self.findIndex((t) => t.id === user.id),
const fusedUsers = mergeAndDeduplicate(
mentionedUsers,
usersThatCanSeePost,
);
return deduplicatedUsersById;
return fusedUsers;
}
public get author(): User {
@ -452,22 +451,13 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
data.content["text/plain"]?.content ??
Object.entries(data.content)[0][1].content;
const parsedMentions = [
...(data.mentions ?? []),
...(await parseTextMentions(plaintextContent, data.author)),
// Deduplicate by .id
].filter(
(mention, index, self) =>
index === self.findIndex((t) => t.id === mention.id),
const parsedMentions = mergeAndDeduplicate(
data.mentions ?? [],
await parseTextMentions(plaintextContent, data.author),
);
const parsedEmojis = [
...(data.emojis ?? []),
...(await Emoji.parseFromText(plaintextContent)),
// Deduplicate by .id
].filter(
(emoji, index, self) =>
index === self.findIndex((t) => t.id === emoji.id),
const parsedEmojis = mergeAndDeduplicate(
data.emojis ?? [],
await Emoji.parseFromText(plaintextContent),
);
const htmlContent = await contentToHtml(data.content, parsedMentions);
@ -500,14 +490,13 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
await newNote.updateAttachments(data.mediaAttachments ?? []);
// Send notifications for mentioned local users
for (const mention of parsedMentions ?? []) {
for (const mention of parsedMentions) {
if (mention.isLocal()) {
await Notification.insert({
accountId: data.author.id,
notifiedId: mention.id,
type: "mention",
noteId: newNote.id,
});
await mention.createNotification(
"mention",
data.author,
newNote,
);
}
}
@ -540,26 +529,15 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
Object.entries(data.content)[0][1].content)
: undefined;
const parsedMentions = [
...(data.mentions ?? []),
...(plaintextContent
const parsedMentions = mergeAndDeduplicate(
data.mentions ?? [],
plaintextContent
? await parseTextMentions(plaintextContent, data.author)
: []),
// Deduplicate by .id
].filter(
(mention, index, self) =>
index === self.findIndex((t) => t.id === mention.id),
: [],
);
const parsedEmojis = [
...(data.emojis ?? []),
...(plaintextContent
? await Emoji.parseFromText(plaintextContent)
: []),
// Deduplicate by .id
].filter(
(emoji, index, self) =>
index === self.findIndex((t) => t.id === emoji.id),
const parsedEmojis = mergeAndDeduplicate(
data.emojis ?? [],
plaintextContent ? await Emoji.parseFromText(plaintextContent) : [],
);
const htmlContent = data.content
@ -608,18 +586,12 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
return;
}
// Fuse and deduplicate
const fusedEmojis = emojis.filter(
(emoji, index, self) =>
index === self.findIndex((t) => t.id === emoji.id),
);
// Connect emojis
await db
.delete(EmojiToNote)
.where(eq(EmojiToNote.noteId, this.data.id));
await db.insert(EmojiToNote).values(
fusedEmojis.map((emoji) => ({
emojis.map((emoji) => ({
emojiId: emoji.id,
noteId: this.data.id,
})),

View file

@ -275,11 +275,10 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
senderId: this.id,
});
} else {
await Notification.insert({
accountId: this.id,
type: otherUser.data.isLocked ? "follow_request" : "follow",
notifiedId: otherUser.id,
});
await otherUser.createNotification(
otherUser.data.isLocked ? "follow_request" : "follow",
this,
);
}
return foundRelationship;
@ -295,20 +294,6 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
recipientId: followee.id,
senderId: this.id,
});
} else if (!this.data.isLocked) {
if (relationship.data.following) {
await Notification.insert({
accountId: followee.id,
type: "unfollow",
notifiedId: this.id,
});
} else {
await Notification.insert({
accountId: followee.id,
type: "cancel-follow",
notifiedId: this.id,
});
}
}
await relationship.update({
@ -526,12 +511,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
if (this.isLocal() && note.author.isLocal()) {
// Notify the user that their post has been favourited
await Notification.insert({
accountId: this.id,
type: "favourite",
notifiedId: note.author.id,
noteId: note.id,
});
await note.author.createNotification("favourite", this, note);
} else if (this.isLocal() && note.author.isRemote()) {
// Federate the like
this.federateToFollowers(newLike.toVersia());
@ -567,6 +547,19 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
}
}
public async createNotification(
type: "mention" | "follow_request" | "follow" | "favourite" | "reblog",
relatedUser: User,
note?: Note,
): Promise<void> {
await Notification.insert({
accountId: relatedUser.id,
type,
notifiedId: this.id,
noteId: note?.id ?? null,
});
}
public async clearAllNotifications(): Promise<void> {
await db
.update(Notifications)