about summary refs log tree commit diff
path: root/src/state
diff options
context:
space:
mode:
Diffstat (limited to 'src/state')
-rw-r--r--src/state/lib/api.ts29
-rw-r--r--src/state/models/profile-view.ts24
2 files changed, 51 insertions, 2 deletions
diff --git a/src/state/lib/api.ts b/src/state/lib/api.ts
index df0047991..64a88cdec 100644
--- a/src/state/lib/api.ts
+++ b/src/state/lib/api.ts
@@ -97,6 +97,33 @@ export async function unrepost(adx: AdxClient, user: string, uri: string) {
   return numDels > 0
 }
 
+export async function follow(
+  adx: AdxClient,
+  user: string,
+  subject: {did: string; name: string},
+) {
+  return await adx
+    .repo(user, true)
+    .collection('blueskyweb.xyz:Follows')
+    .create('Follow', {
+      $type: 'blueskyweb.xyz:Follow',
+      subject,
+      createdAt: new Date().toISOString(),
+    })
+}
+
+export async function unfollow(
+  adx: AdxClient,
+  user: string,
+  subject: {did: string},
+) {
+  const coll = adx.repo(user, true).collection('blueskyweb.xyz:Follows')
+  const numDels = await deleteWhere(coll, 'Follow', record => {
+    return record.value.subject.did === subject.did
+  })
+  return numDels > 0
+}
+
 type WherePred = (_record: GetRecordResponseValidated) => Boolean
 async function deleteWhere(
   coll: AdxRepoCollectionClient,
@@ -104,7 +131,7 @@ async function deleteWhere(
   cond: WherePred,
 ) {
   const toDelete: string[] = []
-  iterateAll(coll, schema, record => {
+  await iterateAll(coll, schema, record => {
     if (cond(record)) {
       toDelete.push(record.key)
     }
diff --git a/src/state/models/profile-view.ts b/src/state/models/profile-view.ts
index bca4c6158..b245335f1 100644
--- a/src/state/models/profile-view.ts
+++ b/src/state/models/profile-view.ts
@@ -1,6 +1,7 @@
-import {makeAutoObservable} from 'mobx'
+import {makeAutoObservable, runInAction} from 'mobx'
 import {bsky} from '@adxp/mock-api'
 import {RootStoreModel} from './root-store'
+import * as apilib from '../lib/api'
 
 export class ProfileViewMyStateModel {
   hasFollowed: boolean = false
@@ -67,6 +68,27 @@ export class ProfileViewModel implements bsky.ProfileView.Response {
     await this._load()
   }
 
+  async toggleFollowing() {
+    if (this.myState.hasFollowed) {
+      await apilib.unfollow(this.rootStore.api, 'alice.com', {
+        did: this.did,
+      })
+      runInAction(() => {
+        this.followersCount--
+        this.myState.hasFollowed = false
+      })
+    } else {
+      await apilib.follow(this.rootStore.api, 'alice.com', {
+        did: this.did,
+        name: this.name,
+      })
+      runInAction(() => {
+        this.followersCount++
+        this.myState.hasFollowed = true
+      })
+    }
+  }
+
   // state transitions
   // =