semaphore/src/routes/_actions/accounts.js

73 lines
2.1 KiB
JavaScript
Raw Normal View History

import { getAccount } from '../_api/user.js'
import { getRelationship } from '../_api/relationships.js'
import { database } from '../_database/database.js'
import { store } from '../_store/store.js'
2018-01-28 04:23:52 +00:00
async function _updateAccount (accountId, instanceName, accessToken) {
2019-08-03 21:49:37 +01:00
const localPromise = database.getAccount(instanceName, accountId)
const remotePromise = getAccount(instanceName, accessToken, accountId).then(account => {
/* no await */ database.setAccount(instanceName, account)
2018-01-28 04:23:52 +00:00
return account
})
try {
store.set({ currentAccountProfile: (await localPromise) })
2018-01-28 20:51:48 +00:00
} 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
}
async function _updateRelationship (accountId, instanceName, accessToken) {
2019-08-03 21:49:37 +01:00
const localPromise = database.getRelationship(instanceName, accountId)
const remotePromise = getRelationship(instanceName, accessToken, accountId).then(relationship => {
/* no await */ database.setRelationship(instanceName, relationship)
2018-01-28 20:51:48 +00:00
return relationship
})
try {
store.set({ currentAccountRelationship: (await localPromise) })
2018-01-28 20:51:48 +00:00
} catch (e) {
console.error(e)
}
try {
store.set({ currentAccountRelationship: (await remotePromise) })
2018-01-28 20:51:48 +00:00
} catch (e) {
console.error(e)
}
}
export async function updateLocalRelationship (instanceName, accountId, relationship) {
await database.setRelationship(instanceName, relationship)
try {
store.set({ currentAccountRelationship: relationship })
} catch (e) {
console.error(e)
}
}
export async function clearProfileAndRelationship () {
2018-01-28 20:51:48 +00:00
store.set({
currentAccountProfile: null,
currentAccountRelationship: null
})
}
export async function updateProfileAndRelationship (accountId) {
2019-08-03 21:49:37 +01:00
const { currentInstance, accessToken } = store.get()
2018-01-28 20:51:48 +00:00
await Promise.all([
_updateAccount(accountId, currentInstance, accessToken),
_updateRelationship(accountId, currentInstance, accessToken)
2018-01-28 20:51:48 +00:00
])
2018-02-09 06:29:29 +00:00
}
export async function updateRelationship (accountId) {
2019-08-03 21:49:37 +01:00
const { currentInstance, accessToken } = store.get()
await _updateRelationship(accountId, currentInstance, accessToken)
}