fix(federation): unfollows don't send Undos to the followee

This commit is contained in:
DevMiner 2024-08-02 17:28:50 +02:00
parent 1368dac77e
commit d2113e349f
2 changed files with 43 additions and 4 deletions

View file

@ -48,6 +48,7 @@ import {
Users,
} from "~/drizzle/schema";
import { type Config, config } from "~/packages/config-manager";
import { undoFederationRequest } from "../../classes/functions/federation.ts";
import { BaseInterface } from "./base";
import { Emoji } from "./emoji";
import { Instance } from "./instance";
@ -254,6 +255,46 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
return foundRelationship;
}
async unfollow(followee: User, relationship: Relationship) {
if (followee.isRemote()) {
// TODO: This should reschedule for a later time and maybe notify the server admin if it fails too often
const { ok } = await this.federateToUser(
undoFederationRequest(
this,
new URL(
`/follows/${relationship.id}`,
config.http.base_url,
).toString(),
),
followee,
);
if (!ok) {
return false;
}
} else if (!this.data.isLocked) {
if (relationship.data.following) {
await db.insert(Notifications).values({
accountId: followee.id,
type: "unfollow",
notifiedId: this.id,
});
} else {
await db.insert(Notifications).values({
accountId: followee.id,
type: "cancel-follow",
notifiedId: this.id,
});
}
}
await relationship.update({
following: false,
});
return true;
}
static async webFinger(
manager: FederationRequester,
username: string,

View file

@ -57,10 +57,8 @@ export default (app: Hono) =>
otherUser,
);
if (foundRelationship.data.following) {
await foundRelationship.update({
following: false,
});
if (!(await self.unfollow(otherUser, foundRelationship))) {
return errorResponse("Failed to unfollow user", 500);
}
return jsonResponse(foundRelationship.toApi());