2021-07-05 05:19:04 +02:00
|
|
|
import { store } from '../_store/store.js'
|
|
|
|
import { followAccount, unfollowAccount } from '../_api/follow.js'
|
|
|
|
import { toast } from '../_components/toast/toast.js'
|
|
|
|
import { updateLocalRelationship } from './accounts.js'
|
|
|
|
import { formatIntl } from '../_utils/formatIntl.js'
|
2018-03-11 23:22:31 +01:00
|
|
|
|
2018-03-12 03:40:32 +01:00
|
|
|
export async function setAccountFollowed (accountId, follow, toastOnSuccess) {
|
2019-08-03 22:49:37 +02:00
|
|
|
const { 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) {
|
2020-11-29 23:13:27 +01:00
|
|
|
/* no await */ toast.say(follow ? 'intl.followedAccount' : 'intl.unfollowedAccount')
|
2018-03-12 03:40:32 +01:00
|
|
|
}
|
2018-03-11 23:22:31 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
2020-11-29 23:13:27 +01:00
|
|
|
/* no await */ toast.say(follow
|
|
|
|
? formatIntl('intl.unableToFollow', { error: (e.message || '') })
|
|
|
|
: formatIntl('intl.unableToUnfollow', { error: (e.message || '') })
|
|
|
|
)
|
2018-03-11 23:22:31 +01:00
|
|
|
}
|
|
|
|
}
|