2018-03-11 23:22:31 +01:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { followAccount, unfollowAccount } from '../_api/follow'
|
|
|
|
import { toast } from '../_utils/toast'
|
2018-08-26 21:14:08 +02:00
|
|
|
import { updateLocalRelationship } from './accounts'
|
2018-03-11 23:22:31 +01:00
|
|
|
|
2018-03-12 03:40:32 +01:00
|
|
|
export async function setAccountFollowed (accountId, follow, toastOnSuccess) {
|
2018-04-19 18:37:05 +02:00
|
|
|
let { currentInstance, accessToken } = store.get()
|
2018-03-11 23:22:31 +01:00
|
|
|
try {
|
2018-08-26 21:14:08 +02:00
|
|
|
let relationship
|
2018-03-11 23:22:31 +01:00
|
|
|
if (follow) {
|
2018-08-26 21:14:08 +02:00
|
|
|
relationship = await followAccount(currentInstance, accessToken, accountId)
|
2018-03-11 23:22:31 +01:00
|
|
|
} else {
|
2018-08-26 21:14:08 +02:00
|
|
|
relationship = await unfollowAccount(currentInstance, accessToken, accountId)
|
2018-03-11 23:22:31 +01:00
|
|
|
}
|
2018-08-26 21:14:08 +02:00
|
|
|
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) {
|
2018-08-26 19:31:13 +02:00
|
|
|
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
|
|
|
}
|
2018-03-11 23:22:31 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
toast.say(`Unable to ${follow ? 'follow' : 'unfollow'} account: ` + (e.message || ''))
|
|
|
|
}
|
|
|
|
}
|