Merge branch 'develop' into mkjs-n
This commit is contained in:
@@ -2,7 +2,7 @@ import * as assert from 'node:assert';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { isAbsolute, basename } from 'node:path';
|
||||
import { inspect } from 'node:util';
|
||||
import WebSocket from 'ws';
|
||||
import WebSocket, { ClientOptions } from 'ws';
|
||||
import fetch, { Blob, File, RequestInit } from 'node-fetch';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { JSDOM } from 'jsdom';
|
||||
@@ -14,14 +14,19 @@ import { SchemaOrUndefined } from 'misskey-js/built/endpoints.types.js';
|
||||
|
||||
export { server as startServer } from '@/boot/common.js';
|
||||
|
||||
interface UserToken {
|
||||
token: string;
|
||||
bearer?: boolean;
|
||||
}
|
||||
|
||||
const config = loadConfig();
|
||||
export const port = config.port;
|
||||
|
||||
export const cookie = (me: any): string => {
|
||||
export const cookie = (me: UserToken): string => {
|
||||
return `token=${me.token};`;
|
||||
};
|
||||
|
||||
export const api = async (endpoint: string, params: any, me?: any) => {
|
||||
export const api = async (endpoint: string, params: any, me?: UserToken) => {
|
||||
const normalized = endpoint.replace(/^\//, '');
|
||||
return await request(`api/${normalized}`, params, me);
|
||||
};
|
||||
@@ -59,27 +64,33 @@ export const failedApiCall = async <X extends keyof misskey.Endpoints>(request:
|
||||
return res.body;
|
||||
};
|
||||
|
||||
const request = async (path: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
|
||||
const auth = me ? {
|
||||
i: me.token,
|
||||
} : {};
|
||||
const request = async (path: string, params: any, me?: UserToken): Promise<{ status: number, headers: Headers, body: any }> => {
|
||||
const bodyAuth: Record<string, string> = {};
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
if (me?.bearer) {
|
||||
headers.Authorization = `Bearer ${me.token}`;
|
||||
} else if (me) {
|
||||
bodyAuth.i = me.token;
|
||||
}
|
||||
|
||||
const res = await relativeFetch(path, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(Object.assign(auth, params)),
|
||||
headers,
|
||||
body: JSON.stringify(Object.assign(bodyAuth, params)),
|
||||
redirect: 'manual',
|
||||
});
|
||||
|
||||
const status = res.status;
|
||||
const body = res.headers.get('content-type') === 'application/json; charset=utf-8'
|
||||
? await res.json()
|
||||
: null;
|
||||
|
||||
return {
|
||||
body, status,
|
||||
status: res.status,
|
||||
headers: res.headers,
|
||||
body,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -87,7 +98,7 @@ const relativeFetch = async (path: string, init?: RequestInit | undefined) => {
|
||||
return await fetch(new URL(path, `http://127.0.0.1:${port}/`).toString(), init);
|
||||
};
|
||||
|
||||
export const signup = async (params?: any): Promise<any> => {
|
||||
export const signup = async (params?: Partial<misskey.Endpoints['signup']['req']>): Promise<NonNullable<misskey.Endpoints['signup']['res']>> => {
|
||||
const q = Object.assign({
|
||||
username: 'test',
|
||||
password: 'test',
|
||||
@@ -98,7 +109,7 @@ export const signup = async (params?: any): Promise<any> => {
|
||||
return res.body;
|
||||
};
|
||||
|
||||
export const post = async (user: any, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
|
||||
export const post = async (user: UserToken, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
|
||||
const q = params;
|
||||
|
||||
const res = await api('notes/create', q, user);
|
||||
@@ -121,21 +132,21 @@ export const hiddenNote = (note: any): any => {
|
||||
return temp;
|
||||
};
|
||||
|
||||
export const react = async (user: any, note: any, reaction: string): Promise<any> => {
|
||||
export const react = async (user: UserToken, note: any, reaction: string): Promise<any> => {
|
||||
await api('notes/reactions/create', {
|
||||
noteId: note.id,
|
||||
reaction: reaction,
|
||||
}, user);
|
||||
};
|
||||
|
||||
export const userList = async (user: any, userList: any = {}): Promise<any> => {
|
||||
export const userList = async (user: UserToken, userList: any = {}): Promise<any> => {
|
||||
const res = await api('users/lists/create', {
|
||||
name: 'test',
|
||||
}, user);
|
||||
return res.body;
|
||||
};
|
||||
|
||||
export const page = async (user: any, page: any = {}): Promise<any> => {
|
||||
export const page = async (user: UserToken, page: any = {}): Promise<any> => {
|
||||
const res = await api('pages/create', {
|
||||
alignCenter: false,
|
||||
content: [
|
||||
@@ -158,7 +169,7 @@ export const page = async (user: any, page: any = {}): Promise<any> => {
|
||||
return res.body;
|
||||
};
|
||||
|
||||
export const play = async (user: any, play: any = {}): Promise<any> => {
|
||||
export const play = async (user: UserToken, play: any = {}): Promise<any> => {
|
||||
const res = await api('flash/create', {
|
||||
permissions: [],
|
||||
script: 'test',
|
||||
@@ -169,7 +180,7 @@ export const play = async (user: any, play: any = {}): Promise<any> => {
|
||||
return res.body;
|
||||
};
|
||||
|
||||
export const clip = async (user: any, clip: any = {}): Promise<any> => {
|
||||
export const clip = async (user: UserToken, clip: any = {}): Promise<any> => {
|
||||
const res = await api('clips/create', {
|
||||
description: null,
|
||||
isPublic: true,
|
||||
@@ -179,7 +190,7 @@ export const clip = async (user: any, clip: any = {}): Promise<any> => {
|
||||
return res.body;
|
||||
};
|
||||
|
||||
export const galleryPost = async (user: any, channel: any = {}): Promise<any> => {
|
||||
export const galleryPost = async (user: UserToken, channel: any = {}): Promise<any> => {
|
||||
const res = await api('gallery/posts/create', {
|
||||
description: null,
|
||||
fileIds: [],
|
||||
@@ -190,7 +201,7 @@ export const galleryPost = async (user: any, channel: any = {}): Promise<any> =>
|
||||
return res.body;
|
||||
};
|
||||
|
||||
export const channel = async (user: any, channel: any = {}): Promise<any> => {
|
||||
export const channel = async (user: UserToken, channel: any = {}): Promise<any> => {
|
||||
const res = await api('channels/create', {
|
||||
bannerId: null,
|
||||
description: null,
|
||||
@@ -200,7 +211,7 @@ export const channel = async (user: any, channel: any = {}): Promise<any> => {
|
||||
return res.body;
|
||||
};
|
||||
|
||||
export const role = async (user: any, role: any = {}, policies: any = {}): Promise<any> => {
|
||||
export const role = async (user: UserToken, role: any = {}, policies: any = {}): Promise<any> => {
|
||||
const res = await api('admin/roles/create', {
|
||||
asBadge: false,
|
||||
canEditMembersByModerator: false,
|
||||
@@ -217,8 +228,8 @@ export const role = async (user: any, role: any = {}, policies: any = {}): Promi
|
||||
isPublic: false,
|
||||
name: 'New Role',
|
||||
target: 'manual',
|
||||
policies: {
|
||||
...Object.entries(DEFAULT_POLICIES).map(([k, v]) => [k, {
|
||||
policies: {
|
||||
...Object.entries(DEFAULT_POLICIES).map(([k, v]) => [k, {
|
||||
priority: 0,
|
||||
useDefault: true,
|
||||
value: v,
|
||||
@@ -243,7 +254,7 @@ interface UploadOptions {
|
||||
* Upload file
|
||||
* @param user User
|
||||
*/
|
||||
export const uploadFile = async (user: any, { path, name, blob }: UploadOptions = {}): Promise<any> => {
|
||||
export const uploadFile = async (user?: UserToken, { path, name, blob }: UploadOptions = {}): Promise<{ status: number, headers: Headers, body: misskey.Endpoints['drive/files/create']['res'] | null }> => {
|
||||
const absPath = path == null
|
||||
? new URL('resources/Lenna.jpg', import.meta.url)
|
||||
: isAbsolute(path.toString())
|
||||
@@ -251,7 +262,6 @@ export const uploadFile = async (user: any, { path, name, blob }: UploadOptions
|
||||
: new URL(path, new URL('resources/', import.meta.url));
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('i', user.token);
|
||||
formData.append('file', blob ??
|
||||
new File([await readFile(absPath)], basename(absPath.toString())));
|
||||
formData.append('force', 'true');
|
||||
@@ -259,20 +269,29 @@ export const uploadFile = async (user: any, { path, name, blob }: UploadOptions
|
||||
formData.append('name', name);
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {};
|
||||
if (user?.bearer) {
|
||||
headers.Authorization = `Bearer ${user.token}`;
|
||||
} else if (user) {
|
||||
formData.append('i', user.token);
|
||||
}
|
||||
|
||||
const res = await relativeFetch('api/drive/files/create', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
headers,
|
||||
});
|
||||
|
||||
const body = res.status !== 204 ? await res.json() : null;
|
||||
const body = res.status !== 204 ? await res.json() as misskey.Endpoints['drive/files/create']['res'] : null;
|
||||
|
||||
return {
|
||||
status: res.status,
|
||||
headers: res.headers,
|
||||
body,
|
||||
};
|
||||
};
|
||||
|
||||
export const uploadUrl = async (user: any, url: string) => {
|
||||
export const uploadUrl = async (user: UserToken, url: string) => {
|
||||
let file: any;
|
||||
const marker = Math.random().toString();
|
||||
|
||||
@@ -294,10 +313,18 @@ export const uploadUrl = async (user: any, url: string) => {
|
||||
return file;
|
||||
};
|
||||
|
||||
export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
|
||||
export function connectStream(user: UserToken, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
|
||||
return new Promise((res, rej) => {
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${port}/streaming?i=${user.token}`);
|
||||
const url = new URL(`ws://127.0.0.1:${port}/streaming`);
|
||||
const options: ClientOptions = {};
|
||||
if (user.bearer) {
|
||||
options.headers = { Authorization: `Bearer ${user.token}` };
|
||||
} else {
|
||||
url.searchParams.set('i', user.token);
|
||||
}
|
||||
const ws = new WebSocket(url, options);
|
||||
|
||||
ws.on('unexpected-response', (req, res) => rej(res));
|
||||
ws.on('open', () => {
|
||||
ws.on('message', data => {
|
||||
const msg = JSON.parse(data.toString());
|
||||
@@ -321,7 +348,7 @@ export function connectStream(user: any, channel: string, listener: (message: Re
|
||||
});
|
||||
}
|
||||
|
||||
export const waitFire = async (user: any, channel: string, trgr: () => any, cond: (msg: Record<string, any>) => boolean, params?: any) => {
|
||||
export const waitFire = async (user: UserToken, channel: string, trgr: () => any, cond: (msg: Record<string, any>) => boolean, params?: any) => {
|
||||
return new Promise<boolean>(async (res, rej) => {
|
||||
let timer: NodeJS.Timeout | null = null;
|
||||
|
||||
@@ -355,11 +382,11 @@ export const waitFire = async (user: any, channel: string, trgr: () => any, cond
|
||||
});
|
||||
};
|
||||
|
||||
export type SimpleGetResponse = {
|
||||
status: number,
|
||||
body: any | JSDOM | null,
|
||||
type: string | null,
|
||||
location: string | null
|
||||
export type SimpleGetResponse = {
|
||||
status: number,
|
||||
body: any | JSDOM | null,
|
||||
type: string | null,
|
||||
location: string | null
|
||||
};
|
||||
export const simpleGet = async (path: string, accept = '*/*', cookie: any = undefined): Promise<SimpleGetResponse> => {
|
||||
const res = await relativeFetch(path, {
|
||||
@@ -378,9 +405,9 @@ export const simpleGet = async (path: string, accept = '*/*', cookie: any = unde
|
||||
'text/html; charset=utf-8',
|
||||
];
|
||||
|
||||
const body =
|
||||
jsonTypes.includes(res.headers.get('content-type') ?? '') ? await res.json() :
|
||||
htmlTypes.includes(res.headers.get('content-type') ?? '') ? new JSDOM(await res.text()) :
|
||||
const body =
|
||||
jsonTypes.includes(res.headers.get('content-type') ?? '') ? await res.json() :
|
||||
htmlTypes.includes(res.headers.get('content-type') ?? '') ? new JSDOM(await res.text()) :
|
||||
null;
|
||||
|
||||
return {
|
||||
|
Reference in New Issue
Block a user