45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
'use strict';
|
|||
|
|
|
||
|
|
function getConfig() {
|
||
|
|
return {
|
||
|
|
instanceUrl: process.env.BOKANTE_INSTANCE_URL || 'https://bokante.o-k-i.net',
|
||
|
|
accessToken: process.env.BOKANTE_ACCESS_TOKEN
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
async function mastodonFetch(url, options) {
|
||
|
|
const {accessToken} = getConfig();
|
||
|
|
const response = await fetch(url, {
|
||
|
|
...options,
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${accessToken}`,
|
||
|
|
...options.headers
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
const body = await response.text();
|
||
|
|
throw new Error(`Mastodon ${response.status}: ${body}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return response.json();
|
||
|
|
}
|
||
|
|
|
||
|
|
async function createStatus(text, {visibility = 'public'} = {}) {
|
||
|
|
const {instanceUrl} = getConfig();
|
||
|
|
return mastodonFetch(`${instanceUrl}/api/v1/statuses`, {
|
||
|
|
method: 'POST',
|
||
|
|
headers: {'Content-Type': 'application/json'},
|
||
|
|
body: JSON.stringify({status: text, visibility})
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async function getStatusContext(statusId) {
|
||
|
|
const {instanceUrl} = getConfig();
|
||
|
|
return mastodonFetch(`${instanceUrl}/api/v1/statuses/${statusId}/context`, {
|
||
|
|
method: 'GET'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = {createStatus, getStatusContext};
|