pinafore/src/routes/_actions/reblog.js

26 wiersze
1021 B
JavaScript
Czysty Zwykły widok Historia

2018-02-25 02:20:33 +00:00
import { store } from '../_store/store'
2018-12-22 23:37:51 +00:00
import { toast } from '../_components/toast/toast'
2018-02-25 02:20:33 +00:00
import { reblogStatus, unreblogStatus } from '../_api/reblog'
import { database } from '../_database/database'
2018-02-25 02:20:33 +00:00
export async function setReblogged (statusId, reblogged) {
let online = store.get()
if (!online) {
2018-02-25 02:20:33 +00:00
toast.say(`You cannot ${reblogged ? 'boost' : 'unboost'} while offline.`)
return
}
let { currentInstance, accessToken } = store.get()
2018-03-21 00:41:39 +00:00
let 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)
toast.say(`Failed to ${reblogged ? 'boost' : 'unboost'}. ` + (e.message || ''))
store.setStatusReblogged(currentInstance, statusId, !reblogged) // undo optimistic update
2018-02-25 02:20:33 +00:00
}
}