From 92176df3ab4a6d87d1816fc06c4a58cf4a4d1e25 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 11 Mar 2018 15:22:31 -0700 Subject: [PATCH] add ability to follow/unfollow an account --- routes/_actions/follow.js | 22 ++++++++++++++++++++++ routes/_components/AccountProfile.html | 12 ++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 routes/_actions/follow.js diff --git a/routes/_actions/follow.js b/routes/_actions/follow.js new file mode 100644 index 0000000..5c6c7e4 --- /dev/null +++ b/routes/_actions/follow.js @@ -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 || '')) + } +} diff --git a/routes/_components/AccountProfile.html b/routes/_components/AccountProfile.html index 5851010..a942ff5 100644 --- a/routes/_components/AccountProfile.html +++ b/routes/_components/AccountProfile.html @@ -29,6 +29,7 @@ pressable="true" pressed="{{following}}" big="true" + on:click="onFollowButtonClick()" /> {{/if}} @@ -186,6 +187,8 @@ import ExternalLink from './ExternalLink.html' import Avatar from './Avatar.html' import { store } from '../_store/store' + import { setAccountFollowed } from '../_actions/follow' + import { database } from '../_database/database' export default { computed: { @@ -200,6 +203,15 @@ }, 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, components: { IconButton,