about summary refs log tree commit diff
path: root/src/lib/api/feed-manip.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-03-22 15:46:49 -0500
committerGitHub <noreply@github.com>2023-03-22 15:46:49 -0500
commitf6f1fe2558cd00e4dcac9685a4209c79b224d3bb (patch)
tree9e906c76a0311dae6b93fb53bd24bf1a11feb13e /src/lib/api/feed-manip.ts
parent449f9243f3e58f684826e7d5830fc23deaa5c901 (diff)
downloadvoidsky-f6f1fe2558cd00e4dcac9685a4209c79b224d3bb.tar.zst
Feed updates (Closes #344) (#356)
* Rework feed polling to correctly detect when new content is available (close #344)

* Tweak how the tuner works for consistency

* Improve the feed-update behavior after posting

* Load latest notifications when opening the tab
Diffstat (limited to 'src/lib/api/feed-manip.ts')
-rw-r--r--src/lib/api/feed-manip.ts25
1 files changed, 12 insertions, 13 deletions
diff --git a/src/lib/api/feed-manip.ts b/src/lib/api/feed-manip.ts
index a54b7d25d..e9a32b7a6 100644
--- a/src/lib/api/feed-manip.ts
+++ b/src/lib/api/feed-manip.ts
@@ -10,10 +10,12 @@ export type FeedTunerFn = (
 ) => void
 
 export class FeedViewPostsSlice {
+  isFlattenedReply = false
+
   constructor(public items: FeedViewPost[] = []) {}
 
   get uri() {
-    if (this.isReply) {
+    if (this.isFlattenedReply) {
       return this.items[1].post.uri
     }
     return this.items[0].post.uri
@@ -39,12 +41,8 @@ export class FeedViewPostsSlice {
     return this.isThread && !this.items[0].reply
   }
 
-  get isReply() {
-    return this.items.length > 1 && !this.isThread
-  }
-
   get rootItem() {
-    if (this.isReply) {
+    if (this.isFlattenedReply) {
       return this.items[1]
     }
     return this.items[0]
@@ -70,6 +68,7 @@ export class FeedViewPostsSlice {
 
   flattenReplyParent() {
     if (this.items[0].reply?.parent) {
+      this.isFlattenedReply = true
       this.items.splice(0, 0, {post: this.items[0].reply?.parent})
     }
   }
@@ -105,6 +104,11 @@ export class FeedTuner {
       slices.unshift(new FeedViewPostsSlice([item]))
     }
 
+    // run the custom tuners
+    for (const tunerFn of tunerFns) {
+      tunerFn(this, slices)
+    }
+
     // remove any items already "seen"
     const soonToBeSeenUris: Set<string> = new Set()
     for (let i = slices.length - 1; i >= 0; i--) {
@@ -135,11 +139,6 @@ export class FeedTuner {
     // sort by slice roots' timestamps
     slices.sort((a, b) => b.ts.localeCompare(a.ts))
 
-    // run the custom tuners
-    for (const tunerFn of tunerFns) {
-      tunerFn(this, slices)
-    }
-
     for (const slice of slices) {
       for (const item of slice.items) {
         this.seenUris.add(item.post.uri)
@@ -170,12 +169,12 @@ export class FeedTuner {
   static likedRepliesOnly(tuner: FeedTuner, slices: FeedViewPostsSlice[]) {
     // remove any replies without at least 2 likes
     for (let i = slices.length - 1; i >= 0; i--) {
-      if (slices[i].isFullThread) {
+      if (slices[i].isFullThread || !slices[i].rootItem.reply) {
         continue
       }
       const item = slices[i].rootItem
       const isRepost = Boolean(item.reason)
-      if (item.reply && !isRepost && item.post.upvoteCount < 2) {
+      if (!isRepost && item.post.upvoteCount < 2) {
         slices.splice(i, 1)
       }
     }