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