From 0fe5e8738c43b1a6b0889135b054f4eaeafe590a Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Mon, 25 Sep 2023 22:02:33 +0200 Subject: [PATCH] upd: attempt to accept file during update --- .../api/mastodon/MastodonApiServerService.ts | 12 ++- packages/megalodon/src/megalodon.ts | 2 +- packages/megalodon/src/misskey.ts | 94 ++++++++++++------- 3 files changed, 72 insertions(+), 36 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts index 08976db2b..adcbe6077 100644 --- a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts +++ b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts @@ -236,14 +236,20 @@ export class MastodonApiServerService { } }); - fastify.patch('/v1/accounts/update_credentials', { preHandler: upload.none() }, async (_request, reply) => { + fastify.patch('/v1/accounts/update_credentials', { preHandler: upload.single('file') }, async (_request, reply) => { const BASE_URL = `${_request.protocol}://${_request.hostname}`; const accessTokens = _request.headers.authorization; const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt // displayed without being logged in try { - const data = await client.updateCredentials(_request.body!); - reply.send(convertAccount(data.data)); + const multipartData = await _request.file; + if (!multipartData) { + const data = await client.updateCredentials(_request.body!); + reply.send(convertAccount(data.data)); + } else { + const data = await client.updateCredentials(_request.body!, multipartData); + reply.send(convertAccount(data.data)); + } } catch (e: any) { /* console.error(e); */ reply.code(401).send(e.response.data); diff --git a/packages/megalodon/src/megalodon.ts b/packages/megalodon/src/megalodon.ts index 19cd5c555..b29325477 100644 --- a/packages/megalodon/src/megalodon.ts +++ b/packages/megalodon/src/megalodon.ts @@ -149,7 +149,7 @@ export interface MegalodonInterface { language?: string } fields_attributes?: Array<{ name: string; value: string }> - }): Promise> + }, file?: any): Promise> /** * View information about a profile. * diff --git a/packages/megalodon/src/misskey.ts b/packages/megalodon/src/misskey.ts index 5640ce19b..b800df04d 100644 --- a/packages/megalodon/src/misskey.ts +++ b/packages/megalodon/src/misskey.ts @@ -220,47 +220,77 @@ export default class Misskey implements MegalodonInterface { language?: string } | null fields_attributes?: Array<{ name: string; value: string }> - }): Promise> { + }, file?: any): Promise> { let params = {} - if (options) { - if (options.bot !== undefined) { - params = Object.assign(params, { - isBot: options.bot - }) - } - if (options.display_name) { - params = Object.assign(params, { - name: options.display_name - }) - } - if (options.note) { - params = Object.assign(params, { - description: options.note - }) - } - if (options.locked !== undefined) { - params = Object.assign(params, { - isLocked: options.locked - }) - } - if (options.source) { - if (options.source.language) { + if (!file) { + if (options) { + if (options.bot !== undefined) { params = Object.assign(params, { - lang: options.source.language + isBot: options.bot }) } - if (options.source.sensitive) { + if (options.display_name) { params = Object.assign(params, { - alwaysMarkNsfw: options.source.sensitive + name: options.display_name }) } + if (options.note) { + params = Object.assign(params, { + description: options.note + }) + } + if (options.locked !== undefined) { + params = Object.assign(params, { + isLocked: options.locked + }) + } + if (options.source) { + if (options.source.language) { + params = Object.assign(params, { + lang: options.source.language + }) + } + if (options.source.sensitive) { + params = Object.assign(params, { + alwaysMarkNsfw: options.source.sensitive + }) + } + } } - } - return this.client.post('/api/i/update', params).then(res => { - return Object.assign(res, { - data: MisskeyAPI.Converter.userDetail(res.data) + return this.client.post('/api/i/update', params).then(res => { + return Object.assign(res, { + data: MisskeyAPI.Converter.userDetail(res.data) + }) }) - }) + } else { + const formData = new FormData() + formData.append('file', fs.createReadStream(file.path), { + contentType: file.mimetype, + }); + if (file.originalname != null && file.originalname !== "file") formData.append("name", file.originalname); + let headers: { [key: string]: string } = {} + if (typeof formData.getHeaders === 'function') { + headers = formData.getHeaders() + } + await this.client + .post('/api/drive/files/create', formData, headers) + .then(res => { + if (file.name === "header") { + params = Object.assign(params, { + bannerId: res.data.id + }) + } else if (file.name === "avatar") { + params = Object.assign(params, { + avatarId: res.data.id + }) + } + }) + return this.client.post('/api/i/update', params).then(res => { + return Object.assign(res, { + data: MisskeyAPI.Converter.userDetail(res.data) + }) + }) + } } /**