pinafore/src/routes/_actions/instances.js

110 wiersze
3.7 KiB
JavaScript
Czysty Zwykły widok Historia

import { getVerifyCredentials } from '../_api/user'
import { store } from '../_store/store'
import { switchToTheme } from '../_utils/themeEngine'
2018-12-22 23:37:51 +00:00
import { toast } from '../_components/toast/toast'
import { goto } from '../../../__sapper__/client'
import { cacheFirstUpdateAfter } from '../_utils/sync'
2018-02-11 21:46:57 +00:00
import { getInstanceInfo } from '../_api/instance'
import { database } from '../_database/database'
2018-01-27 22:45:51 +00:00
2018-02-09 06:29:29 +00:00
export function changeTheme (instanceName, newTheme) {
2019-08-03 20:49:37 +00:00
const { instanceThemes } = store.get()
2018-01-27 22:45:51 +00:00
instanceThemes[instanceName] = newTheme
store.set({ instanceThemes: instanceThemes })
2018-01-27 22:45:51 +00:00
store.save()
2019-08-03 20:49:37 +00:00
const { currentInstance } = store.get()
if (instanceName === currentInstance) {
2019-08-03 20:49:37 +00:00
const { enableGrayscale } = store.get()
switchToTheme(newTheme, enableGrayscale)
2018-01-27 22:45:51 +00:00
}
}
2018-02-09 06:29:29 +00:00
export function switchToInstance (instanceName) {
2019-08-03 20:49:37 +00:00
const { instanceThemes } = store.get()
2018-02-27 06:22:56 +00:00
store.set({
currentInstance: instanceName,
searchResults: null,
2018-03-03 22:51:48 +00:00
queryInSearch: ''
2018-02-27 06:22:56 +00:00
})
2018-01-27 22:45:51 +00:00
store.save()
2019-08-03 20:49:37 +00:00
const { enableGrayscale } = store.get()
switchToTheme(instanceThemes[instanceName], enableGrayscale)
2018-01-27 22:45:51 +00:00
}
export async function logOutOfInstance (instanceName, message = `Logged out of ${instanceName}`) {
2019-08-03 20:49:37 +00:00
const {
loggedInInstances,
instanceThemes,
loggedInInstancesInOrder,
composeData,
currentInstance
} = store.get()
2018-01-27 22:45:51 +00:00
loggedInInstancesInOrder.splice(loggedInInstancesInOrder.indexOf(instanceName), 1)
2019-08-03 20:49:37 +00:00
const newInstance = instanceName === currentInstance
2018-02-09 06:29:29 +00:00
? loggedInInstancesInOrder[0]
: currentInstance
2018-01-27 22:45:51 +00:00
delete loggedInInstances[instanceName]
delete instanceThemes[instanceName]
2018-03-03 22:15:50 +00:00
delete composeData[instanceName]
2018-01-27 22:45:51 +00:00
store.set({
loggedInInstances: loggedInInstances,
instanceThemes: instanceThemes,
loggedInInstancesInOrder: loggedInInstancesInOrder,
2018-02-11 18:35:25 +00:00
currentInstance: newInstance,
searchResults: null,
2018-02-27 06:22:56 +00:00
queryInSearch: '',
2018-03-03 22:15:50 +00:00
composeData: composeData
2018-01-27 22:45:51 +00:00
})
store.save()
toast.say(message)
2019-08-03 20:49:37 +00:00
const { enableGrayscale } = store.get()
switchToTheme(instanceThemes[newInstance], enableGrayscale)
/* no await */ database.clearDatabaseForInstance(instanceName)
2018-01-27 22:45:51 +00:00
goto('/settings/instances')
}
2018-02-09 06:29:29 +00:00
function setStoreVerifyCredentials (instanceName, thisVerifyCredentials) {
2019-08-03 20:49:37 +00:00
const { verifyCredentials } = store.get()
2018-01-27 22:45:51 +00:00
verifyCredentials[instanceName] = thisVerifyCredentials
store.set({ verifyCredentials: verifyCredentials })
2018-01-27 22:45:51 +00:00
}
2018-02-09 06:29:29 +00:00
export async function updateVerifyCredentialsForInstance (instanceName) {
2019-08-03 20:49:37 +00:00
const { loggedInInstances } = store.get()
const accessToken = loggedInInstances[instanceName].access_token
await cacheFirstUpdateAfter(
() => getVerifyCredentials(instanceName, accessToken).catch(logOutOnUnauthorized(instanceName)),
() => database.getInstanceVerifyCredentials(instanceName),
verifyCredentials => database.setInstanceVerifyCredentials(instanceName, verifyCredentials),
verifyCredentials => setStoreVerifyCredentials(instanceName, verifyCredentials)
)
2018-02-09 06:29:29 +00:00
}
2018-02-11 21:46:57 +00:00
2018-02-26 00:26:43 +00:00
export async function updateVerifyCredentialsForCurrentInstance () {
2019-08-03 20:49:37 +00:00
const { currentInstance } = store.get()
await updateVerifyCredentialsForInstance(currentInstance)
2018-02-26 00:26:43 +00:00
}
2018-02-11 22:11:03 +00:00
export async function updateInstanceInfo (instanceName) {
2018-02-11 21:46:57 +00:00
await cacheFirstUpdateAfter(
() => getInstanceInfo(instanceName),
() => database.getInstanceInfo(instanceName),
info => database.setInstanceInfo(instanceName, info),
2018-02-11 21:46:57 +00:00
info => {
2019-08-03 20:49:37 +00:00
const { instanceInfos } = store.get()
2018-02-11 21:46:57 +00:00
instanceInfos[instanceName] = info
store.set({ instanceInfos: instanceInfos })
2018-02-11 21:46:57 +00:00
}
)
2018-02-11 22:11:03 +00:00
}
export function logOutOnUnauthorized (instanceName) {
return async error => {
if (error.message.startsWith('401:')) {
await logOutOfInstance(instanceName, `The access token was revoked, logged out of ${instanceName}`)
}
throw error
}
}