about summary refs log tree commit diff
path: root/src/state/models/feeds/posts.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-04-24 19:41:16 -0500
committerGitHub <noreply@github.com>2023-04-24 19:41:16 -0500
commit7a1076271600814629d52b8a1d6eb02b968c947f (patch)
tree6ffd44de89f5670cebdfdd16c78b57a3461d4584 /src/state/models/feeds/posts.ts
parentdf1791bde2dd2ddd22ad5050584cec94fe3a8ccb (diff)
downloadvoidsky-7a1076271600814629d52b8a1d6eb02b968c947f.tar.zst
Rework how recently-created posts are added to the feed (repeat posts issue) (#527)
* Rework new-post behavior to just add the user's created post to the top

* Only add post to top when not a reply

* Fix: run update in action
Diffstat (limited to 'src/state/models/feeds/posts.ts')
-rw-r--r--src/state/models/feeds/posts.ts47
1 files changed, 23 insertions, 24 deletions
diff --git a/src/state/models/feeds/posts.ts b/src/state/models/feeds/posts.ts
index dd342775e..d33dce192 100644
--- a/src/state/models/feeds/posts.ts
+++ b/src/state/models/feeds/posts.ts
@@ -470,36 +470,35 @@ export class PostsFeedModel {
   /**
    * Check if new posts are available
    */
-  async checkForLatest({autoPrepend}: {autoPrepend?: boolean} = {}) {
+  async checkForLatest() {
     if (this.hasNewLatest || this.feedType === 'suggested') {
       return
     }
     const res = await this._getFeed({limit: PAGE_SIZE})
     const tuner = new FeedTuner()
     const slices = tuner.tune(res.data.feed, this.feedTuners)
-    if (slices[0]?.uri !== this.slices[0]?.uri) {
-      if (!autoPrepend) {
-        this.setHasNewLatest(true)
-      } else {
-        this.setHasNewLatest(false)
-        runInAction(() => {
-          const slicesModels = slices.map(
-            slice =>
-              new PostsFeedSliceModel(
-                this.rootStore,
-                `item-${_idCounter++}`,
-                slice,
-              ),
-          )
-          this.slices = slicesModels.concat(
-            this.slices.filter(slice1 =>
-              slicesModels.find(slice2 => slice1.uri === slice2.uri),
-            ),
-          )
-        })
-      }
-    } else {
-      this.setHasNewLatest(false)
+    this.setHasNewLatest(slices[0]?.uri !== this.slices[0]?.uri)
+  }
+
+  /**
+   * Fetches the given post and adds it to the top
+   * Used by the composer to add their new posts
+   */
+  async addPostToTop(uri: string) {
+    try {
+      const res = await this.rootStore.agent.app.bsky.feed.getPosts({
+        uris: [uri],
+      })
+      const toPrepend = new PostsFeedSliceModel(
+        this.rootStore,
+        uri,
+        new FeedViewPostsSlice(res.data.posts.map(post => ({post}))),
+      )
+      runInAction(() => {
+        this.slices = [toPrepend].concat(this.slices)
+      })
+    } catch (e) {
+      this.rootStore.log.error('Failed to load post to prepend', {e})
     }
   }