[mastodon-client] Use MastoApiError everywhere

This commit is contained in:
Laura Hausmann 2023-10-07 19:49:33 +02:00
parent cfd53259cb
commit 2899873b26
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 10 additions and 19 deletions

View file

@ -1,5 +1,6 @@
import Router from "@koa/router";
import { auth } from "@/server/api/mastodon/middleware/auth.js";
import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js";
export function setupEndpointsFilter(router: Router): void {
router.get(["/v1/filters", "/v2/filters"],
@ -11,8 +12,7 @@ export function setupEndpointsFilter(router: Router): void {
router.post(["/v1/filters", "/v2/filters"],
auth(true, ['write:filters']),
async (ctx) => {
ctx.status = 400;
ctx.body = { error: "Please change word mute settings in the web frontend settings." };
throw new MastoApiError(400, "Please change word mute settings in the web frontend settings.");
}
);
}

View file

@ -15,6 +15,7 @@ import { UserConverter } from "@/server/api/mastodon/converters/user.js";
import { PollHelpers } from "@/server/api/mastodon/helpers/poll.js";
import { toArray } from "@/prelude/array.js";
import { auth } from "@/server/api/mastodon/middleware/auth.js";
import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js";
export function setupEndpointsStatus(router: Router): void {
router.post("/v1/statuses",
@ -280,11 +281,7 @@ export function setupEndpointsStatus(router: Router): void {
const body: any = ctx.request.body;
const choices = toArray(body.choices ?? []).map(p => parseInt(p));
if (choices.length < 1) {
ctx.status = 400;
ctx.body = { error: 'Must vote for at least one option' };
return;
}
if (choices.length < 1) throw new MastoApiError(400, "Must vote for at least one option");
const data = await PollHelpers.voteInPoll(choices, note, ctx.user, ctx.cache);
ctx.body = convertPollId(data);

View file

@ -2,6 +2,7 @@ import authenticate from "@/server/api/authenticate.js";
import { ILocalUser } from "@/models/entities/user.js";
import { MastoContext } from "@/server/api/mastodon/index.js";
import { AuthConverter } from "@/server/api/mastodon/converters/auth.js";
import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js";
export async function AuthMiddleware(ctx: MastoContext, next: () => Promise<any>) {
const auth = await authenticate(ctx.headers.authorization, null, true);
@ -13,20 +14,13 @@ export async function AuthMiddleware(ctx: MastoContext, next: () => Promise<any>
export function auth(required: boolean, scopes: string[] = []) {
return async function auth(ctx: MastoContext, next: () => Promise<any>) {
if (required && !ctx.user) {
ctx.status = 401;
ctx.body = { error: "This method requires an authenticated user" };
return;
}
if (required && !ctx.user) throw new MastoApiError(401, "This method requires an authenticated user");
if (!AuthConverter.decode(scopes).every(p => ctx.scopes.includes(p))) {
if (required) {
ctx.status = 403;
ctx.body = { error: "This action is outside the authorized scopes" };
} else {
ctx.user = null;
ctx.scopes = [];
}
if (required) throw new MastoApiError(403, "This action is outside the authorized scopes")
ctx.user = null;
ctx.scopes = [];
}
ctx.scopes = AuthConverter.encode(ctx.scopes);