2018-03-11 22:22:31 +00:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { followAccount, unfollowAccount } from '../_api/follow'
|
|
|
|
import { database } from '../_database/database'
|
|
|
|
import { toast } from '../_utils/toast'
|
|
|
|
|
2018-03-12 02:40:32 +00:00
|
|
|
export async function setAccountFollowed (accountId, follow, toastOnSuccess) {
|
2018-03-11 22:22:31 +00:00
|
|
|
let instanceName = store.get('currentInstance')
|
|
|
|
let accessToken = store.get('accessToken')
|
|
|
|
try {
|
|
|
|
if (follow) {
|
|
|
|
await followAccount(instanceName, accessToken, accountId)
|
|
|
|
} else {
|
|
|
|
await unfollowAccount(instanceName, accessToken, accountId)
|
|
|
|
}
|
|
|
|
let relationship = await database.getRelationship(instanceName, accountId)
|
|
|
|
relationship.following = follow
|
|
|
|
await database.setRelationship(instanceName, relationship)
|
2018-03-12 02:40:32 +00:00
|
|
|
if (toastOnSuccess) {
|
|
|
|
toast.say(`${follow ? 'Followed' : 'Unfollowed'}`)
|
|
|
|
}
|
2018-03-11 22:22:31 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
toast.say(`Unable to ${follow ? 'follow' : 'unfollow'} account: ` + (e.message || ''))
|
|
|
|
}
|
|
|
|
}
|