pinafore/routes/_actions/follow.js

28 lines
925 B
JavaScript
Raw Normal View History

import { store } from '../_store/store'
import { followAccount, unfollowAccount } from '../_api/follow'
import { toast } from '../_utils/toast'
import { updateLocalRelationship } from './accounts'
2018-03-12 03:40:32 +01:00
export async function setAccountFollowed (accountId, follow, toastOnSuccess) {
let { currentInstance, accessToken } = store.get()
try {
let relationship
if (follow) {
relationship = await followAccount(currentInstance, accessToken, accountId)
} else {
relationship = await unfollowAccount(currentInstance, accessToken, accountId)
}
await updateLocalRelationship(currentInstance, accountId, relationship)
2018-03-12 03:40:32 +01:00
if (toastOnSuccess) {
2018-03-15 06:14:06 +01:00
if (follow) {
toast.say('Followed account')
2018-03-15 06:14:06 +01:00
} else {
toast.say('Unfollowed account')
}
2018-03-12 03:40:32 +01:00
}
} catch (e) {
console.error(e)
toast.say(`Unable to ${follow ? 'follow' : 'unfollow'} account: ` + (e.message || ''))
}
}