[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
No known key found for this signature in database
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 } }>( router.post<{ Params: { id: string } }>(
"/v1/accounts/:id/block", "/v1/accounts/:id/block",
async (ctx) => { async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.blockAccount( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.IceshrimpId), const user = auth[0] ?? null;
);
ctx.body = convertRelationship(data.data); 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) { } catch (e: any) {
console.error(e); console.error(e);
console.error(e.response.data); console.error(e.response.data);
@ -308,14 +312,18 @@ export function apiAccountMastodon(router: Router): void {
router.post<{ Params: { id: string } }>( router.post<{ Params: { id: string } }>(
"/v1/accounts/:id/unblock", "/v1/accounts/:id/unblock",
async (ctx) => { async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.unblockAccount( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.MastodonId), const user = auth[0] ?? null;
);
ctx.body = convertRelationship(data.data); 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) { } catch (e: any) {
console.error(e); console.error(e);
console.error(e.response.data); console.error(e.response.data);

View File

@ -1,13 +1,14 @@
import { Note } from "@/models/entities/note.js"; import { Note } from "@/models/entities/note.js";
import { ILocalUser, User } from "@/models/entities/user.js"; import { ILocalUser, User } from "@/models/entities/user.js";
import { import {
Followings, Blockings,
FollowRequests, Followings,
NoteFavorites, FollowRequests,
NoteReactions, NoteFavorites,
Notes, NoteReactions,
UserProfiles, Notes,
Users UserProfiles,
Users
} from "@/models/index.js"; } from "@/models/index.js";
import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js"; import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js";
import { generateRepliesQuery } from "@/server/api/common/generate-replies-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 createFollowing from "@/services/following/create.js";
import deleteFollowing from "@/services/following/delete.js"; import deleteFollowing from "@/services/following/delete.js";
import cancelFollowRequest from "@/services/following/requests/cancel.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 = { export type AccountCache = {
locks: AsyncLock; locks: AsyncLock;
@ -60,6 +63,22 @@ export class UserHelpers {
return this.getUserRelationshipTo(target.id, localUser.id); 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[]> { 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; if (limit > 40) limit = 40;