about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/state/models/feeds/posts.ts47
-rw-r--r--src/view/com/composer/Composer.tsx7
2 files changed, 28 insertions, 26 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})
     }
   }
 
diff --git a/src/view/com/composer/Composer.tsx b/src/view/com/composer/Composer.tsx
index 78c5fd6ea..a8be88980 100644
--- a/src/view/com/composer/Composer.tsx
+++ b/src/view/com/composer/Composer.tsx
@@ -138,8 +138,9 @@ export const ComposePost = observer(function ComposePost({
 
       setIsProcessing(true)
 
+      let createdPost
       try {
-        await apilib.post(store, {
+        createdPost = await apilib.post(store, {
           rawText: rt.text,
           replyTo: replyTo?.uri,
           images: gallery.images,
@@ -163,7 +164,9 @@ export const ComposePost = observer(function ComposePost({
         setIsProcessing(false)
         return
       }
-      store.me.mainFeed.checkForLatest({autoPrepend: true})
+      if (!replyTo) {
+        store.me.mainFeed.addPostToTop(createdPost.uri)
+      }
       onPost?.()
       hackfixOnClose()
       Toast.show(`Your ${replyTo ? 'reply' : 'post'} has been published`)