pinafore/routes/_utils/mastodon.js

62 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-01-08 06:00:16 +00:00
const WEBSITE = 'https://pinafore.social'
const REDIRECT_URI = (typeof location !== 'undefined' ? location.origin : 'https://pinafore.social') + '/settings/add-instance'
const SCOPES = 'read write follow'
const CLIENT_NAME = 'Pinafore'
export function registerApplication(instanceName) {
const url = `https://${instanceName}/api/v1/apps`
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_name: CLIENT_NAME,
redirect_uris: REDIRECT_URI,
scopes: SCOPES,
website: WEBSITE
})
})
}
export function generateAuthLink(instanceName, clientId) {
let url = `https://${instanceName}/oauth/authorize`
let params = new URLSearchParams()
params.set('client_id', clientId)
params.set('redirect_uri', REDIRECT_URI)
params.set('response_type', 'code')
params.set('scope', SCOPES)
url += '?' + params.toString()
return url
}
export function getAccessTokenFromAuthCode(instanceName, clientId, clientSecret, code) {
let url = `https://${instanceName}/oauth/token`
return fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: clientId,
client_secret: clientSecret,
redirect_uri: REDIRECT_URI,
grant_type: 'authorization_code',
code: code
})
})
2018-01-08 06:13:15 +00:00
}
export function getHomeTimeline(instanceName, accessToken) {
let url = `https://${instanceName}/api/v1/timelines/home`
return fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${accessToken}`
}
})
2018-01-08 06:00:16 +00:00
}