refactor(federation): ♻️ Move incoming federation handling to custom class

This commit is contained in:
Jesse Wierzbinski 2024-11-01 20:42:32 +01:00
parent d570e8c200
commit f26493140f
No known key found for this signature in database
9 changed files with 1066 additions and 622 deletions

View file

@ -20,7 +20,7 @@ import { config } from "~/packages/config-manager/index.ts";
import { BaseInterface } from "./base.ts";
import { User } from "./user.ts";
export type AttachmentType = InferSelectModel<typeof Instances>;
export type InstanceType = InferSelectModel<typeof Instances>;
export class Instance extends BaseInterface<typeof Instances> {
async reload(): Promise<void> {
@ -78,9 +78,7 @@ export class Instance extends BaseInterface<typeof Instances> {
return found.map((s) => new Instance(s));
}
async update(
newInstance: Partial<AttachmentType>,
): Promise<AttachmentType> {
async update(newInstance: Partial<InstanceType>): Promise<InstanceType> {
await db
.update(Instances)
.set(newInstance)
@ -96,7 +94,7 @@ export class Instance extends BaseInterface<typeof Instances> {
return updated.data;
}
save(): Promise<AttachmentType> {
save(): Promise<InstanceType> {
return this.update(this.data);
}
@ -108,6 +106,14 @@ export class Instance extends BaseInterface<typeof Instances> {
}
}
public static async fromUser(user: User): Promise<Instance | null> {
if (!user.data.instanceId) {
return null;
}
return await Instance.fromId(user.data.instanceId);
}
public static async insert(
data: InferInsertModel<typeof Instances>,
): Promise<Instance> {

View file

@ -38,6 +38,8 @@ import { z } from "zod";
import {
type UserWithRelations,
findManyUsers,
followAcceptToVersia,
followRejectToVersia,
followRequestToVersia,
} from "~/classes/functions/user";
import { searchManager } from "~/classes/search/search-manager";
@ -313,6 +315,20 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
};
}
public async sendFollowAccept(follower: User): Promise<void> {
await this.federateToUser(
followAcceptToVersia(follower, this),
follower,
);
}
public async sendFollowReject(follower: User): Promise<void> {
await this.federateToUser(
followRejectToVersia(follower, this),
follower,
);
}
static async webFinger(
manager: FederationRequester,
username: string,