2018-02-25 03:20:33 +01:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { toast } from '../_utils/toast'
|
|
|
|
import { reblogStatus, unreblogStatus } from '../_api/reblog'
|
2018-04-17 05:56:21 +02:00
|
|
|
import { setStatusReblogged as setStatusRebloggedInDatabase } from '../_database/timelines/updateStatus'
|
2018-02-25 03:20:33 +01:00
|
|
|
|
|
|
|
export async function setReblogged (statusId, reblogged) {
|
2018-04-19 18:37:05 +02:00
|
|
|
let online = store.get()
|
|
|
|
if (!online) {
|
2018-02-25 03:20:33 +01:00
|
|
|
toast.say(`You cannot ${reblogged ? 'boost' : 'unboost'} while offline.`)
|
|
|
|
return
|
|
|
|
}
|
2018-04-19 18:37:05 +02:00
|
|
|
let { currentInstance, accessToken } = store.get()
|
2018-03-21 01:41:39 +01:00
|
|
|
let networkPromise = reblogged
|
2018-04-19 18:37:05 +02:00
|
|
|
? reblogStatus(currentInstance, accessToken, statusId)
|
|
|
|
: unreblogStatus(currentInstance, accessToken, statusId)
|
|
|
|
store.setStatusReblogged(currentInstance, statusId, reblogged) // optimistic update
|
2018-02-25 03:20:33 +01:00
|
|
|
try {
|
2018-03-21 01:41:39 +01:00
|
|
|
await networkPromise
|
2018-04-19 18:37:05 +02:00
|
|
|
await setStatusRebloggedInDatabase(currentInstance, statusId, reblogged)
|
2018-02-25 03:20:33 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
toast.say(`Failed to ${reblogged ? 'boost' : 'unboost'}. ` + (e.message || ''))
|
2018-04-19 18:37:05 +02:00
|
|
|
store.setStatusReblogged(currentInstance, statusId, !reblogged) // undo optimistic update
|
2018-02-25 03:20:33 +01:00
|
|
|
}
|
|
|
|
}
|