add ability to follow/unfollow an account

This commit is contained in:
Nolan Lawson 2018-03-11 15:22:31 -07:00
parent a0e6672d84
commit 92176df3ab
2 changed files with 34 additions and 0 deletions

22
routes/_actions/follow.js Normal file
View File

@ -0,0 +1,22 @@
import { store } from '../_store/store'
import { followAccount, unfollowAccount } from '../_api/follow'
import { database } from '../_database/database'
import { toast } from '../_utils/toast'
export async function setAccountFollowed (accountId, follow) {
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)
} catch (e) {
console.error(e)
toast.say(`Unable to ${follow ? 'follow' : 'unfollow'} account: ` + (e.message || ''))
}
}

View File

@ -29,6 +29,7 @@
pressable="true" pressable="true"
pressed="{{following}}" pressed="{{following}}"
big="true" big="true"
on:click="onFollowButtonClick()"
/> />
{{/if}} {{/if}}
</div> </div>
@ -186,6 +187,8 @@
import ExternalLink from './ExternalLink.html' import ExternalLink from './ExternalLink.html'
import Avatar from './Avatar.html' import Avatar from './Avatar.html'
import { store } from '../_store/store' import { store } from '../_store/store'
import { setAccountFollowed } from '../_actions/follow'
import { database } from '../_database/database'
export default { export default {
computed: { computed: {
@ -200,6 +203,15 @@
}, },
following: (relationship) => relationship && relationship.following following: (relationship) => relationship && relationship.following
}, },
methods: {
async onFollowButtonClick() {
let accountId = this.get('profile').id
let instanceName = this.store.get('currentInstance')
let following = this.get('following')
await setAccountFollowed(accountId, !following)
this.set({relationship: await database.getRelationship(instanceName, accountId)})
}
},
store: () => store, store: () => store,
components: { components: {
IconButton, IconButton,