Spaces:
Configuration error
Configuration error
File size: 6,367 Bytes
19918ed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | /* global $cssSelect $cssLink $header $login $home $server */
/** Singleton object encapsulating interactions with the Mastodon server. */
export default (() => {
// server PRIVATE:
const SERVER_KEY = 'server'
const ACCESS_TOKEN_KEY = 'access_token'
const TOKEN_TYPE_KEY = 'token_type'
const CSS_KEY = 'css_index'
const headers = {}
const origin = 'https://allmastodon.com/simplodon/'
const scope = 'read+write+follow'
/* eslint-disable camelcase -- because Mastodon API has camelcase JSON fields */
const client_id = 'S1X3r40DyEN6qX8RjxkoL4uRm6arRqEcoYK2NVrHSf8'
const client_secret = '6nnyTmudEH6l0iL2nP7ONDoeUUFkgll0N7r7iC3EEzg'
const redirect_uri = document.location.origin + document.location.pathname
/* eslint-enable */
const urlParams = (object) =>
Object.keys(object)
.map((k) => `${k}=${object[k]}`)
.join('&')
const token = async (code) =>
await (
await fetch(`https://${$server.value}/oauth/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: urlParams({
grant_type: 'authorization_code',
code,
/* eslint-disable camelcase -- because Mastodon API has camelcase JSON fields */
client_id,
client_secret,
redirect_uri,
/* eslint-enable */
scope
})
})
).json()
function updateCssTheme () {
$cssLink.setAttribute('href', $cssSelect.value)
}
const isLoggedIn = () => !!accessToken
const updateNavButtons = () => {
if (isLoggedIn()) {
// When logged in show $home
$home.classList.remove('hidden')
$login.classList.add('hidden')
} else {
// When not logged in show $login
$home.classList.add('hidden')
$login.classList.remove('hidden')
}
}
function update () {
updateCssTheme()
$header.innerHTML = $server.value || '(no hostname)'
if (accessToken) {
headers.Authorization = `${tokenType} ${accessToken}`
} else {
delete headers.Authorization
}
updateNavButtons()
}
// server CONSTRUCTOR:
$server.value = window.localStorage.getItem(SERVER_KEY)
let accessToken = window.localStorage.getItem(ACCESS_TOKEN_KEY)
let tokenType = window.localStorage.getItem(TOKEN_TYPE_KEY)
$cssSelect.selectedIndex = window.localStorage.getItem(CSS_KEY) || 0
update()
// server PUBLIC:
return Object.freeze({
/** Has a hostname been defined (from user or from localStorage)? */
hasHostname: () => !!$server.value,
/** Set hostname as entered by user, and store in localStorage */
setHostname: (name) => {
$server.value = name
window.localStorage.setItem(SERVER_KEY, $server.value)
// TODO: update select to correct option (be careful of infinite loop)
update()
},
setCssTheme: () => {
window.localStorage.setItem(CSS_KEY, $cssSelect.selectedIndex)
updateCssTheme()
},
/** Have we gone through the OAuth flow? */
isLoggedIn,
/** Kick off the first step in the OAuth flow by sending the user to the server. */
setAuthorizeHref: ($anchor) => {
$anchor.href = `https://${$server.value}/oauth/authorize?${urlParams({
force_login: false,
scope,
/* eslint-disable camelcase -- because Mastodon API has camelcase JSON fields */
client_id,
redirect_uri,
/* eslint-enable */
origin,
response_type: 'code',
lang: navigator.language
})}`
},
/** Execute the second step of the OAuth flow after receiving the code from the server */
login: async (code) => {
({ access_token: accessToken, token_type: tokenType } = await token(
code
))
window.localStorage.setItem(ACCESS_TOKEN_KEY, accessToken)
window.localStorage.setItem(TOKEN_TYPE_KEY, tokenType)
update()
},
/** Logout and disconnect from the server (deleting everything from localstorage) */
removeHostname: () => {
$server.value = null
accessToken = null
window.localStorage.removeItem(SERVER_KEY)
window.localStorage.removeItem(ACCESS_TOKEN_KEY)
window.localStorage.removeItem(TOKEN_TYPE_KEY)
update()
},
/** Fetch data for a list of posts. */
timeline: async (querySuffix) =>
await (
await fetch(
`https://${$server.value}/api/v1/timelines/${querySuffix}`,
{
headers
}
)
).json(),
/** Fetch data for an account's list of posts. */
accountTimeline: async (accountId, querySuffix) =>
await (
await fetch(
`https://${$server.value}/api/v1/accounts/${accountId}/statuses?${querySuffix}`,
{
headers
}
)
).json(),
/** Fetch data for one post. */
status: async (id) =>
await (
await fetch(`https://${$server.value}/api/v1/statuses/${id}`, {
headers
})
).json(),
/** Lookup account information for a user. */
lookupAccount: async (username) =>
await (
await fetch(
`https://${$server.value}/api/v1/accounts/lookup?acct=${username}`,
{ headers }
)
).json(),
/** fav or unfav a status, sent asynchronously without waiting for the result */
fav: (statusId, isfaved) => {
const command = isfaved ? 'favourite' : 'unfavourite'
fetch(`https://${$server.value}/api/v1/statuses/${statusId}/${command}`, {
method: 'POST',
headers
})
},
/** boost or unboost a status, sent asynchronously without waiting for the result */
boost: (statusId, isboosted) => {
const command = isboosted ? 'reblog' : 'unreblog'
fetch(`https://${$server.value}/api/v1/statuses/${statusId}/${command}`, {
method: 'POST',
headers
})
},
/** bookmark or unbookmark a status, sent asynchronously without waiting for the result */
bookmark: (statusId, isBookmarked) => {
const command = isBookmarked ? 'bookmark' : 'unbookmark'
fetch(`https://${$server.value}/api/v1/statuses/${statusId}/${command}`, {
method: 'POST',
headers
})
},
/** View or hide nav buttons depending on whether hostname is set and user is logged in. */
updateNavButtons
})
})()
|