refactor(federation): ♻️ More federation logic cleanup

This commit is contained in:
Jesse Wierzbinski 2024-12-09 15:01:19 +01:00
parent 83399ba5f1
commit 0ae9cfe26c
No known key found for this signature in database
10 changed files with 124 additions and 133 deletions

View file

@ -3,7 +3,7 @@ import { proxyUrl } from "@/response";
import type { Emoji as APIEmoji } from "@versia/client/types";
import type { CustomEmojiExtension } from "@versia/federation/types";
import { type Instance, db } from "@versia/kit/db";
import { Emojis, Instances } from "@versia/kit/tables";
import { Emojis, type Instances } from "@versia/kit/tables";
import {
type InferInsertModel,
type InferSelectModel,
@ -137,26 +137,15 @@ export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
emojiToFetch: CustomEmojiExtension["emojis"][0],
instance: Instance,
): Promise<Emoji> {
const existingEmoji = await db
.select()
.from(Emojis)
.innerJoin(Instances, eq(Emojis.instanceId, Instances.id))
.where(
and(
eq(Emojis.shortcode, emojiToFetch.name),
eq(Instances.id, instance.id),
),
)
.limit(1);
const existingEmoji = await Emoji.fromSql(
and(
eq(Emojis.shortcode, emojiToFetch.name),
eq(Emojis.instanceId, instance.id),
),
);
if (existingEmoji[0]) {
const found = await Emoji.fromId(existingEmoji[0].Emojis.id);
if (!found) {
throw new Error("Failed to fetch emoji");
}
return found;
if (existingEmoji) {
return existingEmoji;
}
return await Emoji.fromVersia(emojiToFetch, instance);

View file

@ -492,11 +492,7 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
// Send notifications for mentioned local users
for (const mention of parsedMentions) {
if (mention.isLocal()) {
await mention.createNotification(
"mention",
data.author,
newNote,
);
await mention.notify("mention", data.author, newNote);
}
}

View file

@ -275,7 +275,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
senderId: this.id,
});
} else {
await otherUser.createNotification(
await otherUser.notify(
otherUser.data.isLocked ? "follow_request" : "follow",
this,
);
@ -362,19 +362,31 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
});
}
public static async webFinger(
/**
* Perform a WebFinger lookup to find a user's URI
* @param manager
* @param username
* @param hostname
* @returns URI, or null if not found
*/
public static webFinger(
manager: FederationRequester,
username: string,
hostname: string,
): Promise<string> {
return (
(await manager.webFinger(username, hostname).catch(() => null)) ??
(await manager.webFinger(
username,
hostname,
"application/activity+json",
))
);
): Promise<string | null> {
try {
return manager.webFinger(username, hostname);
} catch {
try {
return manager.webFinger(
username,
hostname,
"application/activity+json",
);
} catch {
return Promise.resolve(null);
}
}
}
public static getCount(): Promise<number> {
@ -511,7 +523,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 note.author.createNotification("favourite", this, note);
await note.author.notify("favourite", this, note);
} else if (this.isLocal() && note.author.isRemote()) {
// Federate the like
this.federateToFollowers(newLike.toVersia());
@ -547,7 +559,7 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
}
}
public async createNotification(
public async notify(
type: "mention" | "follow_request" | "follow" | "favourite" | "reblog",
relatedUser: User,
note?: Note,