about summary refs log tree commit diff
path: root/src/state/models/feeds/algo/algo-item.ts
diff options
context:
space:
mode:
authorAnsh Nanda <anshnanda10@gmail.com>2023-05-15 22:39:47 -0700
committerAnsh Nanda <anshnanda10@gmail.com>2023-05-15 22:39:47 -0700
commit8071ae313d7ad94bd051ba95a96b72f543576292 (patch)
treef26488f777316273916d5b878753e9947acc5132 /src/state/models/feeds/algo/algo-item.ts
parentc4a666c2210c2e9c14812ddc5a0f797dd538014c (diff)
downloadvoidsky-8071ae313d7ad94bd051ba95a96b72f543576292.tar.zst
like/unlike improvements WIP
Diffstat (limited to 'src/state/models/feeds/algo/algo-item.ts')
-rw-r--r--src/state/models/feeds/algo/algo-item.ts48
1 files changed, 41 insertions, 7 deletions
diff --git a/src/state/models/feeds/algo/algo-item.ts b/src/state/models/feeds/algo/algo-item.ts
index 41fe6a976..0eaeebd39 100644
--- a/src/state/models/feeds/algo/algo-item.ts
+++ b/src/state/models/feeds/algo/algo-item.ts
@@ -1,4 +1,4 @@
-import {AppBskyFeedDefs} from '@atproto/api'
+import {AppBskyFeedDefs, AtUri} from '@atproto/api'
 import {makeAutoObservable} from 'mobx'
 import {RootStoreModel} from 'state/models/root-store'
 
@@ -38,12 +38,32 @@ export class AlgoItemModel {
   }
 
   get isLiked() {
-    return this.data.viewer?.liked
+    return this.data.viewer?.like
   }
 
-  set toggleLiked(value: boolean) {
+  private toggleLiked(s?: string) {
     if (this.data.viewer) {
-      this.data.viewer.liked = value
+      if (this.data.viewer.like) {
+        this.data.viewer.like = undefined
+      } else {
+        this.data.viewer.like = s
+      }
+    }
+  }
+
+  private incrementLike() {
+    if (this.data.likeCount) {
+      this.data.likeCount += 1
+    } else {
+      this.data.likeCount = 1
+    }
+  }
+
+  private decrementLike() {
+    if (this.data.likeCount) {
+      this.data.likeCount -= 1
+    } else {
+      this.data.likeCount = 0
     }
   }
 
@@ -73,8 +93,7 @@ export class AlgoItemModel {
 
   async like() {
     try {
-      this.toggleLiked = true
-      await this.rootStore.agent.app.bsky.feed.like.create(
+      const res = await this.rootStore.agent.app.bsky.feed.like.create(
         {
           repo: this.rootStore.me.did,
         },
@@ -83,14 +102,29 @@ export class AlgoItemModel {
             uri: this.data.uri,
             cid: this.data.cid,
           },
-          createdAt: new Date().toString(),
+          createdAt: new Date().toISOString(),
         },
       )
+      this.toggleLiked(res.uri)
+      this.incrementLike()
     } catch (e: any) {
       this.rootStore.log.error('Failed to like feed', e)
     }
   }
 
+  async unlike() {
+    try {
+      await this.rootStore.agent.app.bsky.feed.like.delete({
+        repo: this.rootStore.me.did,
+        rkey: new AtUri(this.data.uri).rkey,
+      })
+      this.toggleLiked()
+      this.decrementLike()
+    } catch (e: any) {
+      this.rootStore.log.error('Failed to unlike feed', e)
+    }
+  }
+
   static async getView(store: RootStoreModel, uri: string) {
     const res = await store.agent.app.bsky.feed.getFeedGenerator({
       feed: uri,