pinafore/routes/_actions/instances.js

66 lines
2.5 KiB
JavaScript
Raw Normal View History

import { getVerifyCredentials } from '../_api/user'
import { store } from '../_store/store'
import { switchToTheme } from '../_utils/themeEngine'
import { toast } from '../_utils/toast'
import { database } from '../_database/database'
2018-01-27 23:45:51 +01:00
import { goto } from 'sapper/runtime.js'
import { cacheFirstUpdateAfter } from '../_utils/sync'
2018-01-27 23:45:51 +01:00
2018-02-09 07:29:29 +01:00
export function changeTheme (instanceName, newTheme) {
2018-01-27 23:45:51 +01:00
let instanceThemes = store.get('instanceThemes')
instanceThemes[instanceName] = newTheme
store.set({instanceThemes: instanceThemes})
store.save()
if (instanceName === store.get('currentInstance')) {
switchToTheme(newTheme)
}
}
2018-02-09 07:29:29 +01:00
export function switchToInstance (instanceName) {
2018-01-27 23:45:51 +01:00
let instanceThemes = store.get('instanceThemes')
store.set({currentInstance: instanceName})
store.save()
switchToTheme(instanceThemes[instanceName])
}
2018-02-09 07:29:29 +01:00
export async function logOutOfInstance (instanceName) {
2018-01-27 23:45:51 +01:00
let loggedInInstances = store.get('loggedInInstances')
let instanceThemes = store.get('instanceThemes')
let loggedInInstancesInOrder = store.get('loggedInInstancesInOrder')
let currentInstance = store.get('currentInstance')
loggedInInstancesInOrder.splice(loggedInInstancesInOrder.indexOf(instanceName), 1)
2018-02-09 07:29:29 +01:00
let newInstance = instanceName === currentInstance
? loggedInInstancesInOrder[0]
: currentInstance
2018-01-27 23:45:51 +01:00
delete loggedInInstances[instanceName]
delete instanceThemes[instanceName]
store.set({
loggedInInstances: loggedInInstances,
instanceThemes: instanceThemes,
loggedInInstancesInOrder: loggedInInstancesInOrder,
currentInstance: newInstance
})
store.save()
toast.say(`Logged out of ${instanceName}`)
switchToTheme(instanceThemes[newInstance] || 'default')
await database.clearDatabaseForInstance(instanceName)
goto('/settings/instances')
}
2018-02-09 07:29:29 +01:00
function setStoreVerifyCredentials (instanceName, thisVerifyCredentials) {
2018-01-27 23:45:51 +01:00
let verifyCredentials = store.get('verifyCredentials') || {}
verifyCredentials[instanceName] = thisVerifyCredentials
store.set({verifyCredentials: verifyCredentials})
}
2018-02-09 07:29:29 +01:00
export async function updateVerifyCredentialsForInstance (instanceName) {
2018-01-27 23:45:51 +01:00
let loggedInInstances = store.get('loggedInInstances')
let accessToken = loggedInInstances[instanceName].access_token
await cacheFirstUpdateAfter(
() => getVerifyCredentials(instanceName, accessToken),
() => database.getInstanceVerifyCredentials(instanceName),
verifyCredentials => database.setInstanceVerifyCredentials(instanceName, verifyCredentials),
verifyCredentials => setStoreVerifyCredentials(instanceName, verifyCredentials)
)
2018-02-09 07:29:29 +01:00
}