fix(extra-info): obtain old thread information if available

If up-to-date information is not available for a thread, try to get
expired information if available.

Bug: twpowertools:93
Change-Id: Ie7de9de54995e16c969d314ab8afeaf78b90cda1
diff --git a/src/contentScripts/communityConsole/extraInfo/infoHandlers/thread.js b/src/contentScripts/communityConsole/extraInfo/infoHandlers/thread.js
index c44d7f8..6c8fb73 100644
--- a/src/contentScripts/communityConsole/extraInfo/infoHandlers/thread.js
+++ b/src/contentScripts/communityConsole/extraInfo/infoHandlers/thread.js
@@ -1,3 +1,5 @@
+import {waitFor} from 'poll-until-promise';
+
 import {parseUrl} from '../../../../common/commonUtils.js';
 import ThreadModel from '../../../../models/Thread.js';
 import {kViewThreadResponse} from '../consts.js';
@@ -6,7 +8,7 @@
 import ResponseEventBasedInfoHandler from './basedOnResponseEvent.js';
 
 const kIntervalInMs = 500;
-const kTimeoutInMs = 10 * 1000;
+const kTimeoutInMs = 3 * 1000;
 const kCurrentInfoExpiresInMs = kTimeoutInMs * 1.5;
 
 export default class ThreadInfoHandler extends ResponseEventBasedInfoHandler {
@@ -53,20 +55,41 @@
     this.info.messages = nonUpdatedMessages.concat(newMessages);
   }
 
-  async isInfoCurrent(injectionDetails) {
-    const currentPage = this.parseThreadUrl();
-    const isCurrentThread =
-        Date.now() - this.info.timestamp < kCurrentInfoExpiresInMs &&
-        this.info.thread.getId() == currentPage.thread &&
-        this.info.thread.getForumId() == currentPage.forum;
+  async getCurrentInfo(injectionDetails) {
+    return this
+        .getCurrentThreads(injectionDetails, /* checkRecentTimestamp = */ true)
+        .catch(() => {
+          console.debug(
+              `extraInfo: couldn't get updated thread info. Trying to ` +
+              `get the information even if it is old.`);
+          return this.getCurrentThreads(
+              injectionDetails, /* checkRecentTimestamp = */ false);
+        });
+  }
 
+  async getCurrentThreads(injectionDetails, checkRecentTimestamp) {
+    injectionDetails.checkRecentTimestamp = checkRecentTimestamp;
+    const options = this.getWaitForCurrentInfoOptions();
+    return waitFor(
+        () => this.attemptToGetCurrentInfo(injectionDetails), options);
+  }
+
+  async isInfoCurrent(injectionDetails) {
+    const checkRecentTimestamp = injectionDetails.checkRecentTimestamp;
     const isMessageNode = injectionDetails.isMessageNode;
     const messageNode = injectionDetails.messageNode;
 
-    return isCurrentThread &&
+    return (!checkRecentTimestamp || this.isThreadCurrent()) &&
         (!isMessageNode || this.currentThreadContainsMessage(messageNode));
   }
 
+  isThreadCurrent() {
+    const currentPage = this.parseThreadUrl();
+    return Date.now() - this.info.timestamp < kCurrentInfoExpiresInMs &&
+        this.info.thread.getId() == currentPage.thread &&
+        this.info.thread.getForumId() == currentPage.forum;
+  }
+
   parseThreadUrl() {
     const currentPage = parseUrl(location.href);
     if (currentPage === false)