pinafore/routes/_actions/instances.js
Nolan Lawson 8d5690d63d
remove get() with string from Svelte calls (#169)
* remove get() with string pt 1

* remove get() with string pt 2

* fix typo

* fix some null exceptions in get()

* fixup code style
2018-04-19 09:37:05 -07:00

103 lines
3.5 KiB
JavaScript

import { getVerifyCredentials } from '../_api/user'
import { store } from '../_store/store'
import { switchToTheme } from '../_utils/themeEngine'
import { toast } from '../_utils/toast'
import { goto } from 'sapper/runtime.js'
import { cacheFirstUpdateAfter } from '../_utils/sync'
import { getInstanceInfo } from '../_api/instance'
import { clearDatabaseForInstance } from '../_database/clear'
import {
getInstanceVerifyCredentials as getInstanceVerifyCredentialsFromDatabase,
setInstanceVerifyCredentials as setInstanceVerifyCredentialsInDatabase,
getInstanceInfo as getInstanceInfoFromDatabase,
setInstanceInfo as setInstanceInfoInDatabase
} from '../_database/meta'
export function changeTheme (instanceName, newTheme) {
let { instanceThemes } = store.get()
instanceThemes[instanceName] = newTheme
store.set({instanceThemes: instanceThemes})
store.save()
let { currentInstance } = store.get()
if (instanceName === currentInstance) {
switchToTheme(newTheme)
}
}
export function switchToInstance (instanceName) {
let { instanceThemes } = store.get()
store.set({
currentInstance: instanceName,
searchResults: null,
queryInSearch: ''
})
store.save()
switchToTheme(instanceThemes[instanceName])
}
export async function logOutOfInstance (instanceName) {
let {
loggedInInstances,
instanceThemes,
loggedInInstancesInOrder,
composeData,
currentInstance
} = store.get()
loggedInInstancesInOrder.splice(loggedInInstancesInOrder.indexOf(instanceName), 1)
let newInstance = instanceName === currentInstance
? loggedInInstancesInOrder[0]
: currentInstance
delete loggedInInstances[instanceName]
delete instanceThemes[instanceName]
delete composeData[instanceName]
store.set({
loggedInInstances: loggedInInstances,
instanceThemes: instanceThemes,
loggedInInstancesInOrder: loggedInInstancesInOrder,
currentInstance: newInstance,
searchResults: null,
queryInSearch: '',
composeData: composeData
})
store.save()
toast.say(`Logged out of ${instanceName}`)
switchToTheme(instanceThemes[newInstance] || 'default')
await clearDatabaseForInstance(instanceName)
goto('/settings/instances')
}
function setStoreVerifyCredentials (instanceName, thisVerifyCredentials) {
let { verifyCredentials } = store.get()
verifyCredentials[instanceName] = thisVerifyCredentials
store.set({verifyCredentials: verifyCredentials})
}
export async function updateVerifyCredentialsForInstance (instanceName) {
let { loggedInInstances } = store.get()
let accessToken = loggedInInstances[instanceName].access_token
await cacheFirstUpdateAfter(
() => getVerifyCredentials(instanceName, accessToken),
() => getInstanceVerifyCredentialsFromDatabase(instanceName),
verifyCredentials => setInstanceVerifyCredentialsInDatabase(instanceName, verifyCredentials),
verifyCredentials => setStoreVerifyCredentials(instanceName, verifyCredentials)
)
}
export async function updateVerifyCredentialsForCurrentInstance () {
let { currentInstance } = store.get()
await updateVerifyCredentialsForInstance(currentInstance)
}
export async function updateInstanceInfo (instanceName) {
await cacheFirstUpdateAfter(
() => getInstanceInfo(instanceName),
() => getInstanceInfoFromDatabase(instanceName),
info => setInstanceInfoInDatabase(instanceName, info),
info => {
let { instanceInfos } = store.get()
instanceInfos[instanceName] = info
store.set({instanceInfos: instanceInfos})
}
)
}