diff --git a/packages/backend/src/server/api/mastodon/endpoints/list.ts b/packages/backend/src/server/api/mastodon/endpoints/list.ts index 92ed31044..053f617a8 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/list.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/list.ts @@ -9,6 +9,7 @@ import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; import { UserLists } from "@/models/index.js"; import { NoteHelpers } from "@/server/api/mastodon/helpers/note.js"; import { getUser } from "@/server/api/common/getters.js"; +import { toArray } from "@/prelude/array.js"; export function setupEndpointsList(router: Router): void { router.get("/v1/lists", async (ctx, reply) => { @@ -192,7 +193,7 @@ export function setupEndpointsList(router: Router): void { return; } - const ids = NoteHelpers.normalizeToArray(body['account_ids']).map(p => convertId(p, IdType.IceshrimpId)); + const ids = toArray(body['account_ids']).map(p => convertId(p, IdType.IceshrimpId)); const targets = await Promise.all(ids.map(p => getUser(p))); await ListHelpers.addToList(user, list, targets); ctx.body = {} @@ -229,7 +230,7 @@ export function setupEndpointsList(router: Router): void { return; } - const ids = NoteHelpers.normalizeToArray(body['account_ids']).map(p => convertId(p, IdType.IceshrimpId)); + const ids = toArray(body['account_ids']).map(p => convertId(p, IdType.IceshrimpId)); const targets = await Promise.all(ids.map(p => getUser(p))); await ListHelpers.removeFromList(user, list, targets); ctx.body = {} diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index 4d0eb8bcd..776de78a6 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -13,6 +13,7 @@ import { Cache } from "@/misc/cache.js"; import AsyncLock from "async-lock"; import { ILocalUser } from "@/models/entities/user.js"; import { PollHelpers } from "@/server/api/mastodon/helpers/poll.js"; +import { toArray } from "@/prelude/array.js"; const postIdempotencyCache = new Cache<{ status?: MastodonEntity.Status }>('postIdempotencyCache', 60 * 60); const postIdempotencyLocks = new AsyncLock(); @@ -640,7 +641,7 @@ export function setupEndpointsStatus(router: Router): void { } const body: any = ctx.request.body; - const choices = NoteHelpers.normalizeToArray(body.choices ?? []).map(p => parseInt(p)); + 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'}; diff --git a/packages/backend/src/server/api/mastodon/helpers/note.ts b/packages/backend/src/server/api/mastodon/helpers/note.ts index 340846743..a81a88385 100644 --- a/packages/backend/src/server/api/mastodon/helpers/note.ts +++ b/packages/backend/src/server/api/mastodon/helpers/note.ts @@ -23,6 +23,7 @@ import { VisibilityConverter } from "@/server/api/mastodon/converters/visibility import mfm from "mfm-js"; import { FileConverter } from "@/server/api/mastodon/converters/file.js"; import { MfmHelpers } from "@/server/api/mastodon/helpers/mfm.js"; +import { toArray } from "@/prelude/array.js"; export class NoteHelpers { public static async getDefaultReaction(): Promise { @@ -331,7 +332,7 @@ export class NoteHelpers { result.in_reply_to_id = convertId(body.in_reply_to_id, IdType.IceshrimpId); if (body.media_ids) result.media_ids = body.media_ids && body.media_ids.length > 0 - ? this.normalizeToArray(body.media_ids) + ? toArray(body.media_ids) .map(p => convertId(p, IdType.IceshrimpId)) : undefined; @@ -359,7 +360,7 @@ export class NoteHelpers { result.language = body.language; if (body.media_ids) result.media_ids = body.media_ids && body.media_ids.length > 0 - ? this.normalizeToArray(body.media_ids) + ? toArray(body.media_ids) .map(p => convertId(p, IdType.IceshrimpId)) : undefined; @@ -375,8 +376,4 @@ export class NoteHelpers { return result; } - - public static normalizeToArray(subject: T | T[]) { - return Array.isArray(subject) ? subject : [subject]; - } }