about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/state/models/post-thread-view.ts19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/state/models/post-thread-view.ts b/src/state/models/post-thread-view.ts
index 0334c8442..860a8f6ad 100644
--- a/src/state/models/post-thread-view.ts
+++ b/src/state/models/post-thread-view.ts
@@ -297,6 +297,7 @@ export class PostThreadViewModel {
 
   private _replaceAll(res: GetPostThread.Response) {
     // TODO: validate .record
+    sortThread(res.data.thread)
     const keyGen = reactKeyGenerator()
     const thread = new PostThreadViewPostModel(
       this.rootStore,
@@ -308,3 +309,21 @@ export class PostThreadViewModel {
     this.thread = thread
   }
 }
+
+function sortThread(post: GetPostThread.Post) {
+  if (post.replies) {
+    post.replies.sort((a: GetPostThread.Post, b: GetPostThread.Post) => {
+      const aIsByOp = a.author.did === post.author.did
+      const bIsByOp = b.author.did === post.author.did
+      if (aIsByOp && bIsByOp) {
+        return a.indexedAt.localeCompare(b.indexedAt) // oldest
+      } else if (aIsByOp) {
+        return -1 // op's own reply
+      } else if (bIsByOp) {
+        return 1 // op's own reply
+      }
+      return b.indexedAt.localeCompare(a.indexedAt) // newest
+    })
+    post.replies.forEach(reply => sortThread(reply))
+  }
+}