pinafore/routes/_actions/follow.js
Nolan Lawson 8d5690d63d
remove get() with string from Svelte calls (#169)
* remove get() with string pt 1

* remove get() with string pt 2

* fix typo

* fix some null exceptions in get()

* fixup code style
2018-04-19 09:37:05 -07:00

36 lines
1.2 KiB
JavaScript

import { store } from '../_store/store'
import { followAccount, unfollowAccount } from '../_api/follow'
import { toast } from '../_utils/toast'
import { updateProfileAndRelationship } from './accounts'
import {
getRelationship as getRelationshipFromDatabase
} from '../_database/accountsAndRelationships'
export async function setAccountFollowed (accountId, follow, toastOnSuccess) {
let { currentInstance, accessToken } = store.get()
try {
let account
if (follow) {
account = await followAccount(currentInstance, accessToken, accountId)
} else {
account = await unfollowAccount(currentInstance, accessToken, accountId)
}
await updateProfileAndRelationship(accountId)
let relationship = await getRelationshipFromDatabase(currentInstance, accountId)
if (toastOnSuccess) {
if (follow) {
if (account.locked && relationship.requested) {
toast.say('Requested to follow account')
} else {
toast.say('Followed account')
}
} else {
toast.say('Unfollowed account')
}
}
} catch (e) {
console.error(e)
toast.say(`Unable to ${follow ? 'follow' : 'unfollow'} account: ` + (e.message || ''))
}
}