pinafore/routes/_actions/follow.js

36 lines
1.2 KiB
JavaScript
Raw Normal View History

import { store } from '../_store/store'
import { followAccount, unfollowAccount } from '../_api/follow'
import { toast } from '../_utils/toast'
2018-03-15 05:14:06 +00:00
import { updateProfileAndRelationship } from './accounts'
import {
getRelationship as getRelationshipFromDatabase
} from '../_database/accountsAndRelationships'
2018-03-12 02:40:32 +00:00
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)
}
2018-03-15 05:14:06 +00:00
await updateProfileAndRelationship(accountId)
let relationship = await getRelationshipFromDatabase(currentInstance, accountId)
2018-03-12 02:40:32 +00:00
if (toastOnSuccess) {
2018-03-15 05:14:06 +00:00
if (follow) {
if (account.locked && relationship.requested) {
2018-03-15 05:14:06 +00:00
toast.say('Requested to follow account')
} else {
toast.say('Followed account')
}
} else {
toast.say('Unfollowed account')
}
2018-03-12 02:40:32 +00:00
}
} catch (e) {
console.error(e)
toast.say(`Unable to ${follow ? 'follow' : 'unfollow'} account: ` + (e.message || ''))
}
}