semaphore/routes/_actions/favorite.js

31 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-02-24 02:23:36 +00:00
import { favoriteStatus, unfavoriteStatus } from '../_api/favorite'
import { store } from '../_store/store'
import { database } from '../_database/database'
import { toast } from '../_utils/toast'
2018-02-24 22:49:28 +00:00
export async function setFavorited (statusId, favorited) {
if (!store.get('online')) {
toast.say('You cannot favorite or unfavorite while offline.')
return
}
2018-02-24 02:23:36 +00:00
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
try {
2018-02-24 22:49:28 +00:00
let result = await (favorited
2018-02-24 02:23:36 +00:00
? favoriteStatus(instanceName, accessToken, statusId)
: unfavoriteStatus(instanceName, accessToken, statusId))
2018-02-24 22:49:28 +00:00
if (result.error) {
throw new Error(result.error)
}
await database.setStatusFavorited(instanceName, statusId, favorited)
2018-02-24 02:23:36 +00:00
let statusModifications = store.get('statusModifications')
let currentStatusModifications = statusModifications[instanceName] =
(statusModifications[instanceName] || {favorites: {}, reblogs: {}})
currentStatusModifications.favorites[statusId] = favorited
store.set({statusModifications: statusModifications})
} catch (e) {
2018-02-24 22:49:28 +00:00
console.error(e)
toast.say('Failed to favorite or unfavorite. ' + (e.message || ''))
2018-02-24 02:23:36 +00:00
}
2018-02-24 22:49:28 +00:00
}