pinafore/routes/_actions/accounts.js

73 lines
2.1 KiB
JavaScript
Raw Normal View History

import { getAccount } from '../_api/user'
import { getRelationship } from '../_api/relationships'
import {
getAccount as getAccountFromDatabase,
setAccount as setAccountInDatabase} from '../_database/accounts'
import {
getRelationship as getRelationshipFromDatabase,
setRelationship as setRelationshipInDatabase
} from '../_database/relationships'
import { store } from '../_store/store'
2018-01-28 05:23:52 +01:00
2018-02-09 07:29:29 +01:00
async function updateAccount (accountId, instanceName, accessToken) {
let localPromise = getAccountFromDatabase(instanceName, accountId)
2018-01-28 05:23:52 +01:00
let remotePromise = getAccount(instanceName, accessToken, accountId).then(account => {
/* no await */ setAccountInDatabase(instanceName, account)
2018-01-28 05:23:52 +01:00
return account
})
try {
2018-01-28 21:51:48 +01:00
store.set({currentAccountProfile: (await localPromise)})
} catch (e) {
console.error(e)
}
try {
store.set({currentAccountProfile: (await remotePromise)})
2018-01-28 05:23:52 +01:00
} catch (e) {
2018-01-28 21:51:48 +01:00
console.error(e)
2018-01-28 05:23:52 +01:00
}
2018-01-28 21:51:48 +01:00
}
2018-02-09 07:29:29 +01:00
async function updateRelationship (accountId, instanceName, accessToken) {
let localPromise = getRelationshipFromDatabase(instanceName, accountId)
2018-01-28 21:51:48 +01:00
let remotePromise = getRelationship(instanceName, accessToken, accountId).then(relationship => {
/* no await */ setRelationshipInDatabase(instanceName, relationship)
2018-01-28 21:51:48 +01:00
return relationship
})
try {
store.set({currentAccountRelationship: (await localPromise)})
} catch (e) {
console.error(e)
}
try {
store.set({currentAccountRelationship: (await remotePromise)})
} catch (e) {
console.error(e)
}
}
export async function updateLocalRelationship (instanceName, accountId, relationship) {
await setRelationshipInDatabase(instanceName, relationship)
try {
store.set({currentAccountRelationship: relationship})
} catch (e) {
console.error(e)
}
}
export async function clearProfileAndRelationship () {
2018-01-28 21:51:48 +01:00
store.set({
currentAccountProfile: null,
currentAccountRelationship: null
})
}
export async function updateProfileAndRelationship (accountId) {
let { currentInstance, accessToken } = store.get()
2018-01-28 21:51:48 +01:00
await Promise.all([
updateAccount(accountId, currentInstance, accessToken),
updateRelationship(accountId, currentInstance, accessToken)
2018-01-28 21:51:48 +01:00
])
2018-02-09 07:29:29 +01:00
}