From a12d1c52dd50cbb8aee1f18dde9473fadb7c88cd Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Wed, 27 Sep 2023 14:45:57 +0200 Subject: [PATCH] add: source endpoint for editing through masto api --- .../api/mastodon/MastodonApiServerService.ts | 1 + .../src/server/api/mastodon/converters.ts | 4 ++++ .../src/server/api/mastodon/endpoints/status.ts | 17 ++++++++++++++++- packages/megalodon/src/misskey.ts | 12 ++++++++---- packages/megalodon/src/misskey/api_client.ts | 8 ++++++++ 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts index 08976db2b..fe9f1fc87 100644 --- a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts +++ b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts @@ -773,6 +773,7 @@ export class MastodonApiServerService { // GET Endpoints NoteEndpoint.getStatus(); + NoteEndpoint.getStatusSource(); NoteEndpoint.getContext(); NoteEndpoint.getHistory(); NoteEndpoint.getReblogged(); diff --git a/packages/backend/src/server/api/mastodon/converters.ts b/packages/backend/src/server/api/mastodon/converters.ts index 4621a50ff..58b8dc23c 100644 --- a/packages/backend/src/server/api/mastodon/converters.ts +++ b/packages/backend/src/server/api/mastodon/converters.ts @@ -121,6 +121,10 @@ export function convertStatus(status: Entity.Status) { return status; } +export function convertStatusSource(status: Entity.StatusSource) { + return simpleConvert(status); +} + export function convertConversation(conversation: Entity.Conversation) { conversation.id = convertId(conversation.id, IdConvertType.MastodonId); conversation.accounts = conversation.accounts.map(convertAccount); diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index 07fdb4963..a295564b9 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -1,6 +1,6 @@ import querystring from 'querystring'; import { emojiRegexAtStartToEnd } from '@/misc/emoji-regex.js'; -import { convertId, IdConvertType as IdType, convertAccount, convertAttachment, convertPoll, convertStatus } from '../converters.js'; +import { convertId, IdConvertType as IdType, convertAccount, convertAttachment, convertPoll, convertStatus, convertStatusSource } from '../converters.js'; import { getClient } from '../MastodonApiServerService.js'; import { convertTimelinesArgsId, limitToInt } from './timeline.js'; import type { Entity } from 'megalodon'; @@ -33,6 +33,21 @@ export class ApiStatusMastodon { }); } + public async getStatusSource() { + this.fastify.get<{ Params: { id: string } }>('/v1/statuses/:id/source', async (_request, reply) => { + const BASE_URL = `${_request.protocol}://${_request.hostname}`; + const accessTokens = _request.headers.authorization; + const client = getClient(BASE_URL, accessTokens); + try { + const data = await client.getStatusSource(convertId(_request.params.id, IdType.SharkeyId)); + reply.send(convertStatusSource(data.data)); + } catch (e: any) { + console.error(e); + reply.code(_request.is404 ? 404 : 401).send(e.response.data); + } + }); + } + public async getContext() { this.fastify.get<{ Params: { id: string } }>('/v1/statuses/:id/context', async (_request, reply) => { const BASE_URL = `${_request.protocol}://${_request.hostname}`; diff --git a/packages/megalodon/src/misskey.ts b/packages/megalodon/src/misskey.ts index e568af1d0..104d27a5d 100644 --- a/packages/megalodon/src/misskey.ts +++ b/packages/megalodon/src/misskey.ts @@ -1295,11 +1295,15 @@ export default class Misskey implements MegalodonInterface { return result; } + /** + * GET /api/notes/show + */ public async getStatusSource(_id: string): Promise> { - return new Promise((_, reject) => { - const err = new NoImplementedError('misskey does not support') - reject(err) - }) + return this.client + .post('/api/notes/show', { + noteId: _id + }) + .then(res => ({ ...res, data: MisskeyAPI.Converter.notesource(res.data) })) } /** diff --git a/packages/megalodon/src/misskey/api_client.ts b/packages/megalodon/src/misskey/api_client.ts index 9cbf3db6c..6582cf3e7 100644 --- a/packages/megalodon/src/misskey/api_client.ts +++ b/packages/megalodon/src/misskey/api_client.ts @@ -305,6 +305,14 @@ namespace MisskeyAPI { } } + export const notesource = (n: Entity.Note): MegalodonEntity.StatusSource => { + return { + id: n.id, + text: n.text ?? '', + spoiler_text: n.cw ? n.cw : '' + } + } + const mapEmojis = (e: Array | { [key: string]: string }): Array => { if (Array.isArray(e)) { return e.map(e => emoji(e))