pinafore/src/routes/_api/statuses.js

56 wiersze
2.0 KiB
JavaScript
Czysty Zwykły widok Historia

import { auth, basename } from './utils.js'
2022-12-18 19:20:17 +00:00
import { DEFAULT_TIMEOUT, get, post, put, WRITE_TIMEOUT } from '../_utils/ajax.js'
2018-03-05 00:27:15 +00:00
2022-12-18 19:20:17 +00:00
// post is create, put is edit
async function postOrPutStatus (url, accessToken, method, text, inReplyToId, mediaIds,
sensitive, spoilerText, visibility, poll) {
2019-08-03 20:49:37 +00:00
const body = {
2018-03-05 00:27:15 +00:00
status: text,
media_ids: mediaIds,
2022-11-18 17:32:31 +00:00
sensitive,
2018-03-05 00:27:15 +00:00
spoiler_text: spoilerText,
2022-12-18 19:20:17 +00:00
poll,
...(method === 'post' && {
// you can't change these properties when editing
in_reply_to_id: inReplyToId,
visibility
})
2018-03-05 00:27:15 +00:00
}
2019-08-03 20:49:37 +00:00
for (const key of Object.keys(body)) {
const value = body[key]
// remove any unnecessary fields, except 'status' which must at least be an empty string
if (key !== 'status' && (!value || (Array.isArray(value) && !value.length))) {
2018-03-05 00:27:15 +00:00
delete body[key]
}
}
2022-12-18 19:20:17 +00:00
const func = method === 'post' ? post : put
return func(url, body, auth(accessToken), { timeout: WRITE_TIMEOUT })
}
export async function postStatus (instanceName, accessToken, text, inReplyToId, mediaIds,
sensitive, spoilerText, visibility, poll) {
const url = `${basename(instanceName)}/api/v1/statuses`
return postOrPutStatus(url, accessToken, 'post', text, inReplyToId, mediaIds,
sensitive, spoilerText, visibility, poll)
}
export async function putStatus (instanceName, accessToken, id, text, inReplyToId, mediaIds,
sensitive, spoilerText, visibility, poll) {
const url = `${basename(instanceName)}/api/v1/statuses/${id}`
return postOrPutStatus(url, accessToken, 'put', text, inReplyToId, mediaIds,
sensitive, spoilerText, visibility, poll)
2018-03-05 01:16:33 +00:00
}
export async function getStatusContext (instanceName, accessToken, statusId) {
2019-08-03 20:49:37 +00:00
const url = `${basename(instanceName)}/api/v1/statuses/${statusId}/context`
return get(url, auth(accessToken), { timeout: DEFAULT_TIMEOUT })
}
export async function getStatus (instanceName, accessToken, statusId) {
2019-08-03 20:49:37 +00:00
const url = `${basename(instanceName)}/api/v1/statuses/${statusId}`
return get(url, auth(accessToken), { timeout: DEFAULT_TIMEOUT })
}