pinafore/routes/_actions/instances.js

103 lines
3.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'
2018-01-27 23:45:51 +01:00
import { goto } from 'sapper/runtime.js'
import { cacheFirstUpdateAfter } from '../_utils/sync'
2018-02-11 22:46:57 +01:00
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'
2018-01-27 23:45:51 +01:00
2018-02-09 07:29:29 +01:00
export function changeTheme (instanceName, newTheme) {
let { instanceThemes } = store.get()
2018-01-27 23:45:51 +01:00
instanceThemes[instanceName] = newTheme
store.set({instanceThemes: instanceThemes})
store.save()
let { currentInstance } = store.get()
if (instanceName === currentInstance) {
2018-01-27 23:45:51 +01:00
switchToTheme(newTheme)
}
}
2018-02-09 07:29:29 +01:00
export function switchToInstance (instanceName) {
let { instanceThemes } = store.get()
2018-02-27 07:22:56 +01:00
store.set({
currentInstance: instanceName,
searchResults: null,
2018-03-03 23:51:48 +01:00
queryInSearch: ''
2018-02-27 07:22:56 +01:00
})
2018-01-27 23:45:51 +01:00
store.save()
switchToTheme(instanceThemes[instanceName])
}
2018-02-09 07:29:29 +01:00
export async function logOutOfInstance (instanceName) {
let {
loggedInInstances,
instanceThemes,
loggedInInstancesInOrder,
composeData,
currentInstance
} = store.get()
2018-01-27 23:45:51 +01:00
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]
2018-03-03 23:15:50 +01:00
delete composeData[instanceName]
2018-01-27 23:45:51 +01:00
store.set({
loggedInInstances: loggedInInstances,
instanceThemes: instanceThemes,
loggedInInstancesInOrder: loggedInInstancesInOrder,
2018-02-11 19:35:25 +01:00
currentInstance: newInstance,
searchResults: null,
2018-02-27 07:22:56 +01:00
queryInSearch: '',
2018-03-03 23:15:50 +01:00
composeData: composeData
2018-01-27 23:45:51 +01:00
})
store.save()
toast.say(`Logged out of ${instanceName}`)
switchToTheme(instanceThemes[newInstance] || 'default')
await clearDatabaseForInstance(instanceName)
2018-01-27 23:45:51 +01:00
goto('/settings/instances')
}
2018-02-09 07:29:29 +01:00
function setStoreVerifyCredentials (instanceName, thisVerifyCredentials) {
let { verifyCredentials } = store.get()
2018-01-27 23:45:51 +01:00
verifyCredentials[instanceName] = thisVerifyCredentials
store.set({verifyCredentials: verifyCredentials})
}
2018-02-09 07:29:29 +01:00
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)
)
2018-02-09 07:29:29 +01:00
}
2018-02-11 22:46:57 +01:00
2018-02-26 01:26:43 +01:00
export async function updateVerifyCredentialsForCurrentInstance () {
let { currentInstance } = store.get()
await updateVerifyCredentialsForInstance(currentInstance)
2018-02-26 01:26:43 +01:00
}
2018-02-11 23:11:03 +01:00
export async function updateInstanceInfo (instanceName) {
2018-02-11 22:46:57 +01:00
await cacheFirstUpdateAfter(
() => getInstanceInfo(instanceName),
() => getInstanceInfoFromDatabase(instanceName),
info => setInstanceInfoInDatabase(instanceName, info),
2018-02-11 22:46:57 +01:00
info => {
let { instanceInfos } = store.get()
2018-02-11 22:46:57 +01:00
instanceInfos[instanceName] = info
store.set({instanceInfos: instanceInfos})
}
)
2018-02-11 23:11:03 +01:00
}