upd: change handling of file on update

This commit is contained in:
Mar0xy 2023-09-25 22:11:10 +02:00
parent 0fe5e8738c
commit 6a52509821
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828

View file

@ -222,7 +222,6 @@ export default class Misskey implements MegalodonInterface {
fields_attributes?: Array<{ name: string; value: string }>
}, file?: any): Promise<Response<Entity.Account>> {
let params = {}
if (!file) {
if (options) {
if (options.bot !== undefined) {
params = Object.assign(params, {
@ -234,6 +233,36 @@ export default class Misskey implements MegalodonInterface {
name: options.display_name
})
}
if (options.avatar) {
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()
}
const media = await this.client.post<MisskeyAPI.Entity.File>('/api/drive/files/create', formData, headers);
params = Object.assign(params, {
avatarId: media.data.id
})
}
if (options.header) {
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()
}
const media = await this.client.post<MisskeyAPI.Entity.File>('/api/drive/files/create', formData, headers);
params = Object.assign(params, {
headerId: media.data.id
})
}
if (options.note) {
params = Object.assign(params, {
description: options.note
@ -262,35 +291,6 @@ export default class Misskey implements MegalodonInterface {
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<MisskeyAPI.Entity.File>('/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<MisskeyAPI.Entity.UserDetail>('/api/i/update', params).then(res => {
return Object.assign(res, {
data: MisskeyAPI.Converter.userDetail(res.data)
})
})
}
}
/**