2018-02-11 18:35:25 +00:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { cacheFirstUpdateAfter } from '../_utils/sync'
|
2018-08-30 02:03:12 +00:00
|
|
|
import { database } from '../_database/database'
|
2018-04-17 03:56:21 +00:00
|
|
|
import {
|
|
|
|
getPinnedStatuses
|
|
|
|
} from '../_api/pinnedStatuses'
|
2018-02-11 18:35:25 +00:00
|
|
|
|
2018-02-11 22:11:03 +00:00
|
|
|
export async function updatePinnedStatusesForAccount (accountId) {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { currentInstance, accessToken } = store.get()
|
2018-02-11 18:35:25 +00:00
|
|
|
|
|
|
|
await cacheFirstUpdateAfter(
|
2018-04-19 16:37:05 +00:00
|
|
|
() => getPinnedStatuses(currentInstance, accessToken, accountId),
|
2019-02-23 23:18:48 +00:00
|
|
|
async () => {
|
2019-08-03 20:49:37 +00:00
|
|
|
const pinnedStatuses = await database.getPinnedStatuses(currentInstance, accountId)
|
2019-02-23 23:18:48 +00:00
|
|
|
if (!pinnedStatuses || !pinnedStatuses.every(Boolean)) {
|
|
|
|
throw new Error('missing pinned statuses in idb')
|
|
|
|
}
|
|
|
|
return pinnedStatuses
|
|
|
|
},
|
2018-08-30 02:03:12 +00:00
|
|
|
statuses => database.insertPinnedStatuses(currentInstance, accountId, statuses),
|
2018-02-11 18:35:25 +00:00
|
|
|
statuses => {
|
2019-08-03 20:49:37 +00:00
|
|
|
const { pinnedStatuses } = store.get()
|
2018-04-19 16:37:05 +00:00
|
|
|
pinnedStatuses[currentInstance] = pinnedStatuses[currentInstance] || {}
|
|
|
|
pinnedStatuses[currentInstance][accountId] = statuses
|
2018-08-30 04:42:57 +00:00
|
|
|
store.set({ pinnedStatuses: pinnedStatuses })
|
2018-02-11 18:35:25 +00:00
|
|
|
}
|
|
|
|
)
|
2018-02-11 22:11:03 +00:00
|
|
|
}
|