mirror of
https://iceshrimp.dev/crimekillz/trashposs
synced 2024-11-25 02:09:05 +01:00
[mastodon-client] POST /accounts/:id/block, /accounts/:id/unblock
This commit is contained in:
parent
deeb71856d
commit
3c22417a31
@ -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);
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Note } from "@/models/entities/note.js";
|
||||
import { ILocalUser, User } from "@/models/entities/user.js";
|
||||
import {
|
||||
Blockings,
|
||||
Followings,
|
||||
FollowRequests,
|
||||
NoteFavorites,
|
||||
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user