import { RequestPromise, RequestPromiseAPI } from 'request-promise-native' import { Account, Context, Status, StatusPost } from './types/mastodon' import Limiter from './utils/Limiter' import Logger from './utils/Logger'; export default class Rest { constructor(private api: RequestPromiseAPI, private limiter: Limiter) {} async getMe() { return this.get('accounts/verify_credentials') } async getStatus(id: string) { return this.get(`statuses/${id}`) } async getContext(id: string) { return this.get(`statuses/${id}/context`) } async getDescendants(id: string) { return this.getContext(id) .then(context => context.descendants) } async getReplies(id: string, deep: boolean, account?: string) { return this.getDescendants(id).then(replies => replies .filter(s => deep || s.in_reply_to_id === id) .filter(s => !account || s.account.id === account)) } async getFollowers(id: string) { // TODO: use link header return this.get(`accounts/${id}/followers`, { limit: 999 }) } async getFavouritedBy(id: string) { // TODO: use link header return this.get(`statuses/${id}/favourited_by`, { limit: 999 }) } async postStatus(status: StatusPost) { return this.post('statuses', status) } async deleteStatus(id: string) { return this.delete(`statuses/${id}`) } async postApp(redirectUri: string, scopes: string) { return this.post<{ client_id: string, client_secret: string }>('/apps', { client_name: 'botodon', redirect_uris: redirectUri, scopes, website: 'https://git.wadza.fr/me/botodon' }) } protected async call(promise: RequestPromise) { return this.limiter.promise(promise.catch(err => { Logger.error(`Rest: ${err.message} on ${err.options.uri}`) throw err }) as undefined as Promise) } protected async get(url: string, qs: object = {}) { return this.call(this.api.get(url, { qs })) } protected async post(url: string, body: any) { return this.call(this.api.post(url, { body })) } protected async delete(url: string) { return this.call(this.api.delete(url)) } }