diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index 1a6fbe051..6f64f5e3c 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -289,14 +289,18 @@ export function apiAccountMastodon(router: Router): void { router.post<{ Params: { id: string } }>( "/v1/accounts/:id/block", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.blockAccount( - convertId(ctx.params.id, IdType.IceshrimpId), - ); - ctx.body = convertRelationship(data.data); + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; + return; + } + + const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); + const result = await UserHelpers.blockUser(target, user); + ctx.body = convertRelationship(result); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -308,14 +312,18 @@ export function apiAccountMastodon(router: Router): void { router.post<{ Params: { id: string } }>( "/v1/accounts/:id/unblock", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - const data = await client.unblockAccount( - convertId(ctx.params.id, IdType.MastodonId), - ); - ctx.body = convertRelationship(data.data); + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; + return; + } + + const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId)); + const result = await UserHelpers.unblockUser(target, user); + ctx.body = convertRelationship(result) } catch (e: any) { console.error(e); console.error(e.response.data); diff --git a/packages/backend/src/server/api/mastodon/helpers/user.ts b/packages/backend/src/server/api/mastodon/helpers/user.ts index 4b6075f79..688c3d8c4 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -1,13 +1,14 @@ import { Note } from "@/models/entities/note.js"; import { ILocalUser, User } from "@/models/entities/user.js"; import { - Followings, - FollowRequests, - NoteFavorites, - NoteReactions, - Notes, - UserProfiles, - Users + Blockings, + Followings, + FollowRequests, + NoteFavorites, + NoteReactions, + Notes, + UserProfiles, + Users } from "@/models/index.js"; import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js"; import { generateRepliesQuery } from "@/server/api/common/generate-replies-query.js"; @@ -23,6 +24,8 @@ import { awaitAll } from "@/prelude/await-all.js"; import createFollowing from "@/services/following/create.js"; import deleteFollowing from "@/services/following/delete.js"; import cancelFollowRequest from "@/services/following/requests/cancel.js"; +import createBlocking from "@/services/blocking/create.js"; +import deleteBlocking from "@/services/blocking/delete.js"; export type AccountCache = { locks: AsyncLock; @@ -60,6 +63,22 @@ export class UserHelpers { return this.getUserRelationshipTo(target.id, localUser.id); } + public static async blockUser(target: User, localUser: ILocalUser) { + const blocked = await Blockings.exist({where: {blockerId: localUser.id, blockeeId: target.id}}); + if (!blocked) + await createBlocking(localUser, target); + + return this.getUserRelationshipTo(target.id, localUser.id); + } + + public static async unblockUser(target: User, localUser: ILocalUser) { + const blocked = await Blockings.exist({where: {blockerId: localUser.id, blockeeId: target.id}}); + if (blocked) + await deleteBlocking(localUser, target); + + return this.getUserRelationshipTo(target.id, localUser.id); + } + public static async getUserStatuses(user: User, localUser: ILocalUser | null, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, onlyMedia: boolean = false, excludeReplies: boolean = false, excludeReblogs: boolean = false, pinned: boolean = false, tagged: string | undefined): Promise { if (limit > 40) limit = 40;