[mastodon-client] POST /accounts/:id/block, /accounts/:id/unblock

This commit is contained in:
Laura Hausmann 2023-09-28 21:12:56 +02:00
parent deeb71856d
commit 3c22417a31
Signed by: zotan
GPG key ID: D044E84C5BE01605
2 changed files with 48 additions and 21 deletions

View file

@ -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);

View file

@ -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<Note[]> {
if (limit > 40) limit = 40;