[mastodon-client] Code cleanup & reformat

This commit is contained in:
Laura Hausmann 2023-10-06 03:12:52 +02:00
parent 8bc7bf373e
commit afd9e236a3
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
21 changed files with 168 additions and 157 deletions

View file

@ -1,6 +1,12 @@
import Router from "@koa/router"; import Router from "@koa/router";
import { convertId, IdType } from "../../index.js"; import { convertId, IdType } from "../../index.js";
import { convertAccountId, convertPollId, convertStatusIds, convertStatusEditIds, convertStatusSourceId, } from "../converters.js"; import {
convertAccountId,
convertPollId,
convertStatusEditIds,
convertStatusIds,
convertStatusSourceId,
} from "../converters.js";
import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
import { NoteHelpers } from "@/server/api/mastodon/helpers/note.js"; import { NoteHelpers } from "@/server/api/mastodon/helpers/note.js";
import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/server/api/mastodon/endpoints/timeline.js"; import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/server/api/mastodon/endpoints/timeline.js";

View file

@ -32,6 +32,7 @@ export class ListHelpers {
throw new MastoApiError(404); throw new MastoApiError(404);
}) })
} }
public static async getListUsers(user: ILocalUser, id: string, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise<LinkPaginationObject<User[]>> { public static async getListUsers(user: ILocalUser, id: string, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise<LinkPaginationObject<User[]>> {
if (limit > 80) limit = 80; if (limit > 80) limit = 80;
const list = await UserLists.findOneBy({ userId: user.id, id: id }); const list = await UserLists.findOneBy({ userId: user.id, id: id });

View file

@ -7,11 +7,10 @@ import { awaitAll } from "@/prelude/await-all.js";
import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js";
import { convertAccountId } from "@/server/api/mastodon/converters.js"; import { convertAccountId } from "@/server/api/mastodon/converters.js";
import { Announcement } from "@/models/entities/announcement.js"; import { Announcement } from "@/models/entities/announcement.js";
import { ILocalUser } from "@/models/entities/user.js"; import { ILocalUser, User } from "@/models/entities/user.js";
import { AnnouncementConverter } from "@/server/api/mastodon/converters/announcement.js"; import { AnnouncementConverter } from "@/server/api/mastodon/converters/announcement.js";
import { genId } from "@/misc/gen-id.js"; import { genId } from "@/misc/gen-id.js";
import * as Acct from "@/misc/acct.js"; import * as Acct from "@/misc/acct.js";
import { User } from "@/models/entities/user.js";
import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; import { UserHelpers } from "@/server/api/mastodon/helpers/user.js";
import { generateMutedUserQueryForUsers } from "@/server/api/common/generate-muted-user-query.js"; import { generateMutedUserQueryForUsers } from "@/server/api/common/generate-muted-user-query.js";
import { generateBlockQueryForUsers } from "@/server/api/common/generate-block-query.js"; import { generateBlockQueryForUsers } from "@/server/api/common/generate-block-query.js";
@ -192,7 +191,8 @@ export class MiscHelpers {
cache: { cache: {
id: "meta_emojis", id: "meta_emojis",
milliseconds: 3600000, // 1 hour milliseconds: 3600000, // 1 hour
}} }
}
) )
.then(dbRes => populateEmojis(dbRes.map(p => p.name), null) .then(dbRes => populateEmojis(dbRes.map(p => p.name), null)
.then(p => p.map(x => EmojiConverter.encode(x)) .then(p => p.map(x => EmojiConverter.encode(x))

View file

@ -63,7 +63,10 @@ export class SearchHelpers {
if (!match) match = q.match(/^@(?<user>[a-zA-Z0-9_]+)$/) if (!match) match = q.match(/^@(?<user>[a-zA-Z0-9_]+)$/)
if (match) { if (match) {
// check if user is already in database // check if user is already in database
const dbResult = await Users.findOneBy({usernameLower: match.groups!.user.toLowerCase(), host: match.groups?.host ?? IsNull()}); const dbResult = await Users.findOneBy({
usernameLower: match.groups!.user.toLowerCase(),
host: match.groups?.host ?? IsNull()
});
if (dbResult) return [dbResult]; if (dbResult) return [dbResult];
const result = await resolveUser(match.groups!.user.toLowerCase(), match.groups?.host ?? null); const result = await resolveUser(match.groups!.user.toLowerCase(), match.groups?.host ?? null);

View file

@ -538,6 +538,11 @@ export class UserHelpers {
} }
public static async getDefaultNoteVisibility(user: ILocalUser): Promise<IceshrimpVisibility> { public static async getDefaultNoteVisibility(user: ILocalUser): Promise<IceshrimpVisibility> {
return RegistryItems.findOneBy({domain: IsNull(), userId: user.id, key: 'defaultNoteVisibility', scope: '{client,base}'}).then(p => p?.value ?? 'public') return RegistryItems.findOneBy({
domain: IsNull(),
userId: user.id,
key: 'defaultNoteVisibility',
scope: '{client,base}'
}).then(p => p?.value ?? 'public')
} }
} }

View file

@ -23,8 +23,7 @@ export function auth(required: boolean, scopes: string[] = []) {
if (required) { if (required) {
ctx.status = 403; ctx.status = 403;
ctx.body = { error: "This action is outside the authorized scopes" }; ctx.body = { error: "This action is outside the authorized scopes" };
} } else {
else {
ctx.user = null; ctx.user = null;
ctx.scopes = []; ctx.scopes = [];
} }

View file

@ -1,9 +1,10 @@
import { MastoContext, logger } from "@/server/api/mastodon/index.js"; import { logger, MastoContext } from "@/server/api/mastodon/index.js";
import { IdentifiableError } from "@/misc/identifiable-error.js"; import { IdentifiableError } from "@/misc/identifiable-error.js";
import { ApiError } from "@/server/api/error.js"; import { ApiError } from "@/server/api/error.js";
export class MastoApiError extends Error { export class MastoApiError extends Error {
statusCode: number; statusCode: number;
constructor(statusCode: number, message?: string) { constructor(statusCode: number, message?: string) {
if (message == null) { if (message == null) {
switch (statusCode) { switch (statusCode) {
@ -26,20 +27,16 @@ export async function CatchErrorsMiddleware(ctx: MastoContext, next: () => Promi
} catch (e: any) { } catch (e: any) {
if (e instanceof MastoApiError) { if (e instanceof MastoApiError) {
ctx.status = e.statusCode; ctx.status = e.statusCode;
} } else if (e instanceof IdentifiableError) {
else if (e instanceof IdentifiableError) {
ctx.status = 400; ctx.status = 400;
} } else if (e instanceof ApiError) {
else if (e instanceof ApiError) {
ctx.status = e.httpStatusCode ?? 500; ctx.status = e.httpStatusCode ?? 500;
} } else {
else {
logger.error(`Error occured in ${ctx.method} ${ctx.path}:`); logger.error(`Error occured in ${ctx.method} ${ctx.path}:`);
if (e instanceof Error) { if (e instanceof Error) {
if (e.stack) logger.error(e.stack); if (e.stack) logger.error(e.stack);
else logger.error(`${e.name}: ${e.message}`); else logger.error(`${e.name}: ${e.message}`);
} } else {
else {
logger.error(e); logger.error(e);
} }
ctx.status = 500; ctx.status = 500;