upd: add new endpoint to mk api, add preferences to masto api

This commit is contained in:
Mar0xy 2023-09-25 04:55:11 +02:00
parent 035d600406
commit 871407ce53
No known key found for this signature in database
GPG Key ID: 56569BBE47D2C828
5 changed files with 112 additions and 8 deletions

View File

@ -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,

View File

@ -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],

View File

@ -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<typeof meta, typeof paramDef> { // 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;
});
}
}

View File

@ -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<string>("/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<Response<Entity.Preferences>> {
return new Promise((_, reject) => {
const err = new NoImplementedError('misskey does not support')
reject(err)
})
return this.client.post<MisskeyAPI.Entity.UserDetail>("/api/i")
.then(async (res) => {
return Object.assign(res, {
data: MisskeyAPI.Converter.userPreferences(
await this.getDefaultPostPrivacy(),
),
});
});
}
// ======================================
// accounts/followed_tags
// ======================================
public async getFollowedTags(): Promise<Response<Array<Entity.Tag>>> {
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));
}
// ======================================

View File

@ -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':