pinafore/routes/_actions/follow.js

37 lines
1.3 KiB
JavaScript
Raw Normal View History

import { store } from '../_store/store'
import { followAccount, unfollowAccount } from '../_api/follow'
import { database } from '../_database/database'
import { toast } from '../_utils/toast'
2018-03-15 06:14:06 +01:00
import { updateProfileAndRelationship } from './accounts'
2018-03-12 03:40:32 +01:00
export async function setAccountFollowed (accountId, follow, toastOnSuccess) {
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
try {
let account
if (follow) {
account = await followAccount(instanceName, accessToken, accountId)
} else {
account = await unfollowAccount(instanceName, accessToken, accountId)
}
2018-03-23 17:10:49 +01:00
// TODO: hack to let the animation fully play
await new Promise(resolve => setTimeout(resolve, 400))
2018-03-15 06:14:06 +01:00
await updateProfileAndRelationship(accountId)
let relationship = await database.getRelationship(instanceName, accountId)
2018-03-12 03:40:32 +01:00
if (toastOnSuccess) {
2018-03-15 06:14:06 +01:00
if (follow) {
if (account.locked && relationship.requested) {
2018-03-15 06:14:06 +01:00
toast.say('Requested to follow account')
} else {
toast.say('Followed account')
}
} 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 || ''))
}
}