pinafore/src/routes/_actions/reblog.js

30 wiersze
1.2 KiB
JavaScript
Czysty Zwykły widok Historia

import { store } from '../_store/store.js'
import { toast } from '../_components/toast/toast.js'
import { reblogStatus, unreblogStatus } from '../_api/reblog.js'
import { database } from '../_database/database.js'
import { formatIntl } from '../_utils/formatIntl.js'
2018-02-25 02:20:33 +00:00
export async function setReblogged (statusId, reblogged) {
2019-08-03 20:49:37 +00:00
const online = store.get()
if (!online) {
/* no await */ toast.say(reblogged ? 'intl.cannotReblogOffline' : 'intl.cannotUnreblogOffline')
2018-02-25 02:20:33 +00:00
return
}
2019-08-03 20:49:37 +00:00
const { currentInstance, accessToken } = store.get()
const networkPromise = reblogged
? reblogStatus(currentInstance, accessToken, statusId)
: unreblogStatus(currentInstance, accessToken, statusId)
store.setStatusReblogged(currentInstance, statusId, reblogged) // optimistic update
2018-02-25 02:20:33 +00:00
try {
2018-03-21 00:41:39 +00:00
await networkPromise
await database.setStatusReblogged(currentInstance, statusId, reblogged)
2018-02-25 02:20:33 +00:00
} catch (e) {
console.error(e)
/* no await */ toast.say(reblogged
? formatIntl('intl.failedToReblog', { error: (e.message || '') })
: formatIntl('intl.failedToUnreblog', { error: (e.message || '') })
)
store.setStatusReblogged(currentInstance, statusId, !reblogged) // undo optimistic update
2018-02-25 02:20:33 +00:00
}
}