2021-07-05 05:19:04 +02:00
|
|
|
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 03:20:33 +01:00
|
|
|
|
|
|
|
export async function setReblogged (statusId, reblogged) {
|
2019-08-03 22:49:37 +02:00
|
|
|
const online = store.get()
|
2018-04-19 18:37:05 +02:00
|
|
|
if (!online) {
|
2020-11-29 23:13:27 +01:00
|
|
|
/* no await */ toast.say(reblogged ? 'intl.cannotReblogOffline' : 'intl.cannotUnreblogOffline')
|
2018-02-25 03:20:33 +01:00
|
|
|
return
|
|
|
|
}
|
2019-08-03 22:49:37 +02:00
|
|
|
const { currentInstance, accessToken } = store.get()
|
|
|
|
const 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-08-30 04:03:12 +02:00
|
|
|
await database.setStatusReblogged(currentInstance, statusId, reblogged)
|
2018-02-25 03:20:33 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
2020-11-29 23:13:27 +01:00
|
|
|
/* no await */ toast.say(reblogged
|
|
|
|
? formatIntl('intl.failedToReblog', { error: (e.message || '') })
|
|
|
|
: formatIntl('intl.failedToUnreblog', { error: (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
|
|
|
}
|
|
|
|
}
|