2018-08-26 05:03:33 +00:00
|
|
|
import { getAccount } from '../_api/user'
|
|
|
|
import { getRelationship } from '../_api/relationships'
|
2018-04-17 03:56:21 +00:00
|
|
|
import {
|
|
|
|
getAccount as getAccountFromDatabase,
|
2018-08-26 05:03:33 +00:00
|
|
|
setAccount as setAccountInDatabase} from '../_database/accounts'
|
|
|
|
import {
|
2018-04-17 03:56:21 +00:00
|
|
|
getRelationship as getRelationshipFromDatabase,
|
|
|
|
setRelationship as setRelationshipInDatabase
|
2018-08-26 05:03:33 +00:00
|
|
|
} from '../_database/relationships'
|
2018-02-09 01:51:48 +00:00
|
|
|
import { store } from '../_store/store'
|
2018-01-28 04:23:52 +00:00
|
|
|
|
2018-08-26 21:16:00 +00:00
|
|
|
async function _updateAccount (accountId, instanceName, accessToken) {
|
2018-04-17 03:56:21 +00:00
|
|
|
let localPromise = getAccountFromDatabase(instanceName, accountId)
|
2018-01-28 04:23:52 +00:00
|
|
|
let remotePromise = getAccount(instanceName, accessToken, accountId).then(account => {
|
2018-04-17 03:56:21 +00:00
|
|
|
/* no await */ setAccountInDatabase(instanceName, account)
|
2018-01-28 04:23:52 +00:00
|
|
|
return account
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
2018-01-28 20:51:48 +00:00
|
|
|
store.set({currentAccountProfile: (await localPromise)})
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
store.set({currentAccountProfile: (await remotePromise)})
|
2018-01-28 04:23:52 +00:00
|
|
|
} catch (e) {
|
2018-01-28 20:51:48 +00:00
|
|
|
console.error(e)
|
2018-01-28 04:23:52 +00:00
|
|
|
}
|
2018-01-28 20:51:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 21:16:00 +00:00
|
|
|
async function _updateRelationship (accountId, instanceName, accessToken) {
|
2018-04-17 03:56:21 +00:00
|
|
|
let localPromise = getRelationshipFromDatabase(instanceName, accountId)
|
2018-01-28 20:51:48 +00:00
|
|
|
let remotePromise = getRelationship(instanceName, accessToken, accountId).then(relationship => {
|
2018-04-17 03:56:21 +00:00
|
|
|
/* no await */ setRelationshipInDatabase(instanceName, relationship)
|
2018-01-28 20:51:48 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 19:14:08 +00:00
|
|
|
export async function updateLocalRelationship (instanceName, accountId, relationship) {
|
|
|
|
await setRelationshipInDatabase(instanceName, relationship)
|
|
|
|
try {
|
|
|
|
store.set({currentAccountRelationship: relationship})
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 05:55:04 +00:00
|
|
|
export async function clearProfileAndRelationship () {
|
2018-01-28 20:51:48 +00:00
|
|
|
store.set({
|
|
|
|
currentAccountProfile: null,
|
|
|
|
currentAccountRelationship: null
|
|
|
|
})
|
2018-04-12 05:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateProfileAndRelationship (accountId) {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { currentInstance, accessToken } = store.get()
|
2018-01-28 20:51:48 +00:00
|
|
|
|
|
|
|
await Promise.all([
|
2018-08-26 21:16:00 +00:00
|
|
|
_updateAccount(accountId, currentInstance, accessToken),
|
|
|
|
_updateRelationship(accountId, currentInstance, accessToken)
|
2018-01-28 20:51:48 +00:00
|
|
|
])
|
2018-02-09 06:29:29 +00:00
|
|
|
}
|
2018-08-26 21:16:00 +00:00
|
|
|
|
|
|
|
export async function updateRelationship (accountId) {
|
|
|
|
let { currentInstance, accessToken } = store.get()
|
|
|
|
|
|
|
|
await _updateRelationship(accountId, currentInstance, accessToken)
|
|
|
|
}
|