From 871407ce531ba00336a373e0d7e1ffbd74949c5d Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Mon, 25 Sep 2023 04:55:11 +0200 Subject: [PATCH] upd: add new endpoint to mk api, add preferences to masto api --- .../backend/src/server/api/EndpointsModule.ts | 4 ++ packages/backend/src/server/api/endpoints.ts | 2 + .../api/endpoints/i/registry/get-unsecure.ts | 55 +++++++++++++++++++ packages/megalodon/src/misskey.ts | 49 ++++++++++++++--- packages/megalodon/src/misskey/api_client.ts | 10 ++++ 5 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 packages/backend/src/server/api/endpoints/i/registry/get-unsecure.ts diff --git a/packages/backend/src/server/api/EndpointsModule.ts b/packages/backend/src/server/api/EndpointsModule.ts index fef8214bc..15a2621da 100644 --- a/packages/backend/src/server/api/EndpointsModule.ts +++ b/packages/backend/src/server/api/EndpointsModule.ts @@ -219,6 +219,7 @@ import * as ep___i_readAllUnreadNotes from './endpoints/i/read-all-unread-notes. import * as ep___i_readAnnouncement from './endpoints/i/read-announcement.js'; import * as ep___i_regenerateToken from './endpoints/i/regenerate-token.js'; import * as ep___i_registry_getAll from './endpoints/i/registry/get-all.js'; +import * as ep___i_registry_getUnsecure from './endpoints/i/registry/get-unsecure.js'; import * as ep___i_registry_getDetail from './endpoints/i/registry/get-detail.js'; import * as ep___i_registry_get from './endpoints/i/registry/get.js'; import * as ep___i_registry_keysWithType from './endpoints/i/registry/keys-with-type.js'; @@ -568,6 +569,7 @@ const $i_readAllUnreadNotes: Provider = { provide: 'ep:i/read-all-unread-notes', const $i_readAnnouncement: Provider = { provide: 'ep:i/read-announcement', useClass: ep___i_readAnnouncement.default }; const $i_regenerateToken: Provider = { provide: 'ep:i/regenerate-token', useClass: ep___i_regenerateToken.default }; const $i_registry_getAll: Provider = { provide: 'ep:i/registry/get-all', useClass: ep___i_registry_getAll.default }; +const $i_registry_getUnsecure: Provider = { provide: 'ep:i/registry/get-unsecure', useClass: ep___i_registry_getUnsecure.default }; const $i_registry_getDetail: Provider = { provide: 'ep:i/registry/get-detail', useClass: ep___i_registry_getDetail.default }; const $i_registry_get: Provider = { provide: 'ep:i/registry/get', useClass: ep___i_registry_get.default }; const $i_registry_keysWithType: Provider = { provide: 'ep:i/registry/keys-with-type', useClass: ep___i_registry_keysWithType.default }; @@ -921,6 +923,7 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention $i_readAnnouncement, $i_regenerateToken, $i_registry_getAll, + $i_registry_getUnsecure, $i_registry_getDetail, $i_registry_get, $i_registry_keysWithType, @@ -1268,6 +1271,7 @@ const $retention: Provider = { provide: 'ep:retention', useClass: ep___retention $i_readAnnouncement, $i_regenerateToken, $i_registry_getAll, + $i_registry_getUnsecure, $i_registry_getDetail, $i_registry_get, $i_registry_keysWithType, diff --git a/packages/backend/src/server/api/endpoints.ts b/packages/backend/src/server/api/endpoints.ts index 208bffeb6..5ea412191 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -219,6 +219,7 @@ import * as ep___i_readAllUnreadNotes from './endpoints/i/read-all-unread-notes. import * as ep___i_readAnnouncement from './endpoints/i/read-announcement.js'; import * as ep___i_regenerateToken from './endpoints/i/regenerate-token.js'; import * as ep___i_registry_getAll from './endpoints/i/registry/get-all.js'; +import * as ep___i_registry_getUnsecure from './endpoints/i/registry/get-unsecure.js'; import * as ep___i_registry_getDetail from './endpoints/i/registry/get-detail.js'; import * as ep___i_registry_get from './endpoints/i/registry/get.js'; import * as ep___i_registry_keysWithType from './endpoints/i/registry/keys-with-type.js'; @@ -566,6 +567,7 @@ const eps = [ ['i/read-announcement', ep___i_readAnnouncement], ['i/regenerate-token', ep___i_regenerateToken], ['i/registry/get-all', ep___i_registry_getAll], + ['i/registry/get-unsecure', ep___i_registry_getUnsecure], ['i/registry/get-detail', ep___i_registry_getDetail], ['i/registry/get', ep___i_registry_get], ['i/registry/keys-with-type', ep___i_registry_keysWithType], diff --git a/packages/backend/src/server/api/endpoints/i/registry/get-unsecure.ts b/packages/backend/src/server/api/endpoints/i/registry/get-unsecure.ts new file mode 100644 index 000000000..b3effbb82 --- /dev/null +++ b/packages/backend/src/server/api/endpoints/i/registry/get-unsecure.ts @@ -0,0 +1,55 @@ +import { Inject, Injectable } from '@nestjs/common'; +import { Endpoint } from '@/server/api/endpoint-base.js'; +import type { RegistryItemsRepository } from '@/models/_.js'; +import { DI } from '@/di-symbols.js'; +import { ApiError } from '../../../error.js'; + +export const meta = { + requireCredential: true, + + secure: false, + + errors: { + noSuchKey: { + message: 'No such key.', + code: 'NO_SUCH_KEY', + id: 'ac3ed68a-62f0-422b-a7bc-d5e09e8f6a6a', + }, + }, +} as const; + +export const paramDef = { + type: 'object', + properties: { + key: { type: 'string' }, + scope: { type: 'array', default: [], items: { + type: 'string', pattern: /^[a-zA-Z0-9_]+$/.toString().slice(1, -1), + } }, + }, + required: ['key'], +} as const; + +@Injectable() +export default class extends Endpoint { // eslint-disable-line import/no-default-export + constructor( + @Inject(DI.registryItemsRepository) + private registryItemsRepository: RegistryItemsRepository, + ) { + super(meta, paramDef, async (ps, me) => { + if (ps.key !== "reactions" && ps.key !== "defaultNoteVisibility") return; + const query = this.registryItemsRepository.createQueryBuilder('item') + .where('item.domain IS NULL') + .andWhere('item.userId = :userId', { userId: me.id }) + .andWhere('item.key = :key', { key: ps.key }) + .andWhere('item.scope = :scope', { scope: ps.scope }); + + const item = await query.getOne(); + + if (item == null) { + throw new ApiError(meta.errors.noSuchKey); + } + + return item.value; + }); + } +} \ No newline at end of file diff --git a/packages/megalodon/src/misskey.ts b/packages/megalodon/src/misskey.ts index 7bfe4a622..f43e88cfc 100644 --- a/packages/megalodon/src/misskey.ts +++ b/packages/megalodon/src/misskey.ts @@ -961,21 +961,54 @@ export default class Misskey implements MegalodonInterface { // ====================================== // accounts/preferences // ====================================== + + private async getDefaultPostPrivacy(): Promise<"public" | "unlisted" | "private" | "direct"> { + // NOTE: get-unsecure is sharkey's extension. + // Misskey doesn't have this endpoint and regular `/i/registry/get` won't work + // unless you have a 'nativeToken', which is reserved for the frontend webapp. + + return this.client + .post("/api/i/registry/get-unsecure", { + key: "defaultNoteVisibility", + scope: ["client", "base"], + }) + .then((res) => { + if ( + !res.data || + (res.data != "public" && + res.data != "home" && + res.data != "followers" && + res.data != "specified") + ) + return "public"; + return MisskeyAPI.Converter.visibility(res.data); + }) + .catch((_) => "public"); + } + public async getPreferences(): Promise> { - return new Promise((_, reject) => { - const err = new NoImplementedError('misskey does not support') - reject(err) - }) + return this.client.post("/api/i") + .then(async (res) => { + return Object.assign(res, { + data: MisskeyAPI.Converter.userPreferences( + await this.getDefaultPostPrivacy(), + ), + }); + }); } // ====================================== // accounts/followed_tags // ====================================== public async getFollowedTags(): Promise>> { - return new Promise((_, reject) => { - const err = new NoImplementedError('misskey does not support') - reject(err) - }) + const tags: Entity.Tag[] = []; + const res: Response = { + headers: undefined, + statusText: "", + status: 200, + data: tags, + }; + return new Promise((resolve) => resolve(res)); } // ====================================== diff --git a/packages/megalodon/src/misskey/api_client.ts b/packages/megalodon/src/misskey/api_client.ts index 5b7d49f60..36746c1e8 100644 --- a/packages/megalodon/src/misskey/api_client.ts +++ b/packages/megalodon/src/misskey/api_client.ts @@ -140,6 +140,16 @@ namespace MisskeyAPI { } } + export const userPreferences = (v: "public" | "unlisted" | "private" | "direct"): MegalodonEntity.Preferences => { + return { + "reading:expand:media": "default", + "reading:expand:spoilers": false, + "posting:default:language": "english", + "posting:default:sensitive": false, + "posting:default:visibility": v, + }; + }; + export const visibility = (v: 'public' | 'home' | 'followers' | 'specified'): 'public' | 'unlisted' | 'private' | 'direct' => { switch (v) { case 'public':