diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index eacec8ffa..f44f4a838 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -1,10 +1,9 @@ import Router from "@koa/router"; import { getClient } from "../index.js"; -import { emojiRegexAtStartToEnd } from "@/misc/emoji-regex.js"; import querystring from "node:querystring"; import qs from "qs"; import { convertId, IdType } from "../../index.js"; -import { convertAccount, convertAttachment, convertPoll, convertStatus, } from "../converters.js"; +import { convertAccount, convertPoll, convertStatus, } from "../converters.js"; import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; import { getNote } from "@/server/api/common/getters.js"; import authenticate from "@/server/api/authenticate.js"; @@ -14,7 +13,6 @@ import { convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "@/serve import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { Cache } from "@/misc/cache.js"; -import { redisClient } from "@/db/redis.js"; import AsyncLock from "async-lock"; import { ILocalUser } from "@/models/entities/user.js"; @@ -55,49 +53,31 @@ export function setupEndpointsStatus(router: Router): void { } }); router.put("/v1/statuses/:id", async (ctx) => { - const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; - const accessTokens = ctx.headers.authorization; - const client = getClient(BASE_URL, accessTokens); try { - ctx.params.id = convertId(ctx.params.id, IdType.IceshrimpId); - let body: any = ctx.request.body; - if ( - (!body.poll && body["poll[options][]"]) || - (!body.media_ids && body["media_ids[]"]) - ) { - body = normalizeQuery(body); - } - if (!body.media_ids) body.media_ids = undefined; - if (body.media_ids && !body.media_ids.length) body.media_ids = undefined; - if (body.media_ids) { - body.media_ids = (body.media_ids as string[]).map((p) => - convertId(p, IdType.IceshrimpId), - ); - } - const {sensitive} = body; - body.sensitive = - typeof sensitive === "string" ? sensitive === "true" : sensitive; + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; - if (body.poll) { - if ( - body.poll.expires_in != null && - typeof body.poll.expires_in === "string" - ) - body.poll.expires_in = parseInt(body.poll.expires_in); - if ( - body.poll.multiple != null && - typeof body.poll.multiple === "string" - ) - body.poll.multiple = body.poll.multiple == "true"; - if ( - body.poll.hide_totals != null && - typeof body.poll.hide_totals === "string" - ) - body.poll.hide_totals = body.poll.hide_totals == "true"; + if (!user) { + ctx.status = 401; + return; } - const data = await client.editStatus(ctx.params.id, body); - ctx.body = convertStatus(data.data); + const noteId = convertId(ctx.params.id, IdType.IceshrimpId); + const note = await getNote(noteId, user ?? null).then(n => n).catch(() => null); + if (!note) { + if (!note) { + ctx.status = 404; + ctx.body = { + error: "Note not found" + }; + return; + } + } + + let request = NoteHelpers.normalizeEditOptions(ctx.request.body); + ctx.body = await NoteHelpers.editNote(request, note, user) + .then(p => NoteConverter.encode(p, user)) + .then(p => convertStatus(p)); } catch (e: any) { console.error(e); ctx.status = ctx.status == 404 ? 404 : 401; @@ -142,7 +122,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.status = 404; ctx.body = { error: "Note not found" - } + }; return; } @@ -150,7 +130,7 @@ export function setupEndpointsStatus(router: Router): void { ctx.status = 403; ctx.body = { error: "Cannot delete someone else's note" - } + }; return; } diff --git a/packages/backend/src/server/api/mastodon/entities/status.ts b/packages/backend/src/server/api/mastodon/entities/status.ts index 98a4b34c4..720d32c8b 100644 --- a/packages/backend/src/server/api/mastodon/entities/status.ts +++ b/packages/backend/src/server/api/mastodon/entities/status.ts @@ -58,4 +58,17 @@ namespace MastodonEntity { language?: string, scheduled_at?: Date } + + export type StatusEditRequest = { + text?: string, + media_ids?: string[], + poll?: { + options: string[], + expires_in: number, + multiple: boolean + }, + sensitive?: boolean, + spoiler_text?: string, + language?: string + } } diff --git a/packages/backend/src/server/api/mastodon/helpers/note.ts b/packages/backend/src/server/api/mastodon/helpers/note.ts index ccfb04aaa..0196e8c50 100644 --- a/packages/backend/src/server/api/mastodon/helpers/note.ts +++ b/packages/backend/src/server/api/mastodon/helpers/note.ts @@ -18,6 +18,7 @@ import { getNote } from "@/server/api/common/getters.js"; import createReaction from "@/services/note/reaction/create.js"; import deleteReaction from "@/services/note/reaction/delete.js"; import createNote, { extractMentionedUsers } from "@/services/note/create.js"; +import editNote from "@/services/note/edit.js"; import deleteNote from "@/services/note/delete.js"; import { genId } from "@/misc/gen-id.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; @@ -249,6 +250,27 @@ export class NoteHelpers { return createNote(user, await awaitAll(data)); } + public static async editNote(request: MastodonEntity.StatusEditRequest, note: Note, user: ILocalUser): Promise { + const files = request.media_ids && request.media_ids.length > 0 + ? DriveFiles.findByIds(request.media_ids) + : []; + + const data = { + files: files, + poll: request.poll + ? { + choices: request.poll.options, + multiple: request.poll.multiple, + expiresAt: request.poll.expires_in && request.poll.expires_in > 0 ? new Date(new Date().getTime() + (request.poll.expires_in * 1000)) : null, + } + : undefined, + text: request.text, + cw: request.spoiler_text + } + + return editNote(user, note, await awaitAll(data)); + } + public static async extractMentions(text: string, user: ILocalUser): Promise { return extractMentionedUsers(user, mfm.parse(text)!); } @@ -289,6 +311,36 @@ export class NoteHelpers { return result; } + public static normalizeEditOptions(body: any): MastodonEntity.StatusEditRequest { + const result: MastodonEntity.StatusEditRequest = {}; + + body = qs.parse(querystring.stringify(body)); + + if (body.status !== null) + result.text = body.status; + if (body.spoiler_text !== null) + result.spoiler_text = body.spoiler_text; + if (body.language !== null) + result.language = body.language; + if (body.media_ids) + result.media_ids = body.media_ids && body.media_ids.length > 0 + ? this.normalizeToArray(body.media_ids) + .map(p => convertId(p, IdType.IceshrimpId)) + : undefined; + + if (body.poll) { + result.poll = { + expires_in: parseInt(body.poll.expires_in, 10), + options: body.poll.options, + multiple: !!body.poll.multiple, + } + } + + result.sensitive = !!body.sensitive; + + return result; + } + private static normalizeToArray(subject: T | T[]) { return Array.isArray(subject) ? subject : [subject]; }