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, Users,
} from "~/drizzle/schema"; } from "~/drizzle/schema";
import { type Config, config } from "~/packages/config-manager"; import { type Config, config } from "~/packages/config-manager";
import { undoFederationRequest } from "../../classes/functions/federation.ts";
import { BaseInterface } from "./base"; import { BaseInterface } from "./base";
import { Emoji } from "./emoji"; import { Emoji } from "./emoji";
import { Instance } from "./instance"; import { Instance } from "./instance";
@ -254,6 +255,46 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
return foundRelationship; 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( static async webFinger(
manager: FederationRequester, manager: FederationRequester,
username: string, username: string,

View file

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