about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/state/messages/convo/agent.ts33
-rw-r--r--src/state/messages/convo/const.ts1
2 files changed, 27 insertions, 7 deletions
diff --git a/src/state/messages/convo/agent.ts b/src/state/messages/convo/agent.ts
index a453e81c6..6d59e1369 100644
--- a/src/state/messages/convo/agent.ts
+++ b/src/state/messages/convo/agent.ts
@@ -13,6 +13,7 @@ import {isNative} from '#/platform/detection'
 import {
   ACTIVE_POLL_INTERVAL,
   BACKGROUND_POLL_INTERVAL,
+  INACTIVE_TIMEOUT,
 } from '#/state/messages/convo/const'
 import {
   ConvoDispatch,
@@ -79,6 +80,8 @@ export class Convo {
 
   private isProcessingPendingMessages = false
 
+  private lastActiveTimestamp: number | undefined
+
   convoId: string
   convo: ChatBskyConvoDefs.ConvoView | undefined
   sender: AppBskyActorDefs.ProfileViewBasic | undefined
@@ -272,16 +275,19 @@ export class Convo {
       }
       case ConvoStatus.Backgrounded: {
         switch (action.event) {
-          // TODO truncate history if needed
           case ConvoDispatchEvent.Resume: {
-            if (this.convo) {
-              this.status = ConvoStatus.Ready
-              this.refreshConvo()
+            if (this.wasChatInactive()) {
+              this.reset()
             } else {
-              this.status = ConvoStatus.Initializing
-              this.setup()
+              if (this.convo) {
+                this.status = ConvoStatus.Ready
+                this.refreshConvo()
+              } else {
+                this.status = ConvoStatus.Initializing
+                this.setup()
+              }
+              this.requestPollInterval(ACTIVE_POLL_INTERVAL)
             }
-            this.requestPollInterval(ACTIVE_POLL_INTERVAL)
             break
           }
           case ConvoDispatchEvent.Suspend: {
@@ -354,6 +360,7 @@ export class Convo {
       logger.DebugContext.convo,
     )
 
+    this.updateLastActiveTimestamp()
     this.commit()
   }
 
@@ -436,6 +443,18 @@ export class Convo {
     DEBUG_ACTIVE_CHAT = undefined
   }
 
+  /**
+   * Called on any state transition, like when the chat is backgrounded. This
+   * value is then checked on background -> foreground transitions.
+   */
+  private updateLastActiveTimestamp() {
+    this.lastActiveTimestamp = Date.now()
+  }
+  private wasChatInactive() {
+    if (!this.lastActiveTimestamp) return true
+    return Date.now() - this.lastActiveTimestamp > INACTIVE_TIMEOUT
+  }
+
   private requestedPollInterval: (() => void) | undefined
   private requestPollInterval(interval: number) {
     this.withdrawRequestedPollInterval()
diff --git a/src/state/messages/convo/const.ts b/src/state/messages/convo/const.ts
index 0b8873341..abea5205e 100644
--- a/src/state/messages/convo/const.ts
+++ b/src/state/messages/convo/const.ts
@@ -1,2 +1,3 @@
 export const ACTIVE_POLL_INTERVAL = 1e3
 export const BACKGROUND_POLL_INTERVAL = 5e3
+export const INACTIVE_TIMEOUT = 60e3 * 5