瀏覽代碼

fix not capping at question, better readability

Mike L 3 年之前
父節點
當前提交
e27b183e8d
共有 2 個文件被更改,包括 29 次插入26 次删除
  1. 13 12
      dist/twitter.js
  2. 16 14
      src/twitter.ts

+ 13 - 12
dist/twitter.js

@@ -195,12 +195,10 @@ class default_1 {
                     this.pullOrders = utils_1.Arr.shuffle(userIdCache);
                     fs.writeFileSync(path.resolve(this.cachefile), JSON.stringify(this.cache));
                 }
-                setTimeout(this.workForAll, this.workInterval * 1000 / this.lock.feed.length);
-                setTimeout(() => {
-                    this.work();
-                    setInterval(() => { this.pullOrders = utils_1.Arr.shuffle(this.pullOrders); }, 21600000);
-                    setInterval(this.workForAll, this.workInterval * 1000);
-                }, this.workInterval * 1200 / this.lock.feed.length);
+                const timeout = this.workInterval * 1000 / this.lock.feed.length;
+                setInterval(() => { this.pullOrders = utils_1.Arr.shuffle(this.pullOrders); }, 21600000);
+                setTimeout(this.workForAll, timeout);
+                setTimeout(this.work, timeout * 1.2);
             });
         };
         this.queryUserObject = (userName) => this.client.user.searchExact(userName)
@@ -305,6 +303,9 @@ class default_1 {
                 else {
                     logger.error(`unhandled error on fetching media for all: ${error}`);
                 }
+            })
+                .then(() => {
+                setTimeout(this.workForAll, this.workInterval * 1000);
             });
         };
         this.work = () => {
@@ -355,24 +356,24 @@ class default_1 {
                 const currentThread = lock.threads[currentFeed];
                 if (!mediaItems || mediaItems.length === 0)
                     return;
-                const question = mediaItems.find(story => story.original.story_questions);
-                const topOfFeed = question ? question.pk : mediaItems[0].pk;
-                const updateOffset = () => currentThread.offset = topOfFeed;
+                const updateOffset = () => currentThread.offset = mediaItems[0].pk;
                 if (currentThread.offset === '-1') {
                     updateOffset();
                     return;
                 }
+                const questionIndex = mediaItems.findIndex(story => story.original.story_questions);
+                if (questionIndex > -1)
+                    mediaItems.splice(0, questionIndex);
                 if (currentThread.offset === '0')
                     mediaItems.splice(1);
                 return this.workOnMedia(mediaItems.reverse(), this.sendStories(`thread ${currentFeed}`, ...currentThread.subscribers))
                     .then(updateOffset)
                     .then(() => {
-                    if (question) {
+                    if (questionIndex > -1) {
                         currentThread.subscribers.forEach(subscriber => {
                             const username = cachedFeed.user.username;
                             const author = `${cachedFeed.user.full_name} (@${username}) `;
-                            this.bot.sendTo(subscriber, `请注意,用户${author}已开启问答互动。需退订请回复:/igstory_unsub ${username}${Object.keys(cachedFeed.stories).some(id => id > topOfFeed) ?
-                                `\n本次推送已截止于此条动态,下次推送在 ${Math.floor(this.workInterval / lock.feed.length)} 秒后。` : ''}`);
+                            this.bot.sendTo(subscriber, `请注意,用户${author}已开启问答互动。需退订请回复:/igstory_unsub ${username}${(questionIndex > 0) ? `\n本次推送已截止于此条动态,下次推送在 ${this.workInterval} 秒后。` : ''}`);
                         });
                     }
                 });

+ 16 - 14
src/twitter.ts

@@ -389,12 +389,10 @@ export default class {
           this.pullOrders = Arr.shuffle(userIdCache);
           fs.writeFileSync(path.resolve(this.cachefile), JSON.stringify(this.cache));
         }
-        setTimeout(this.workForAll, this.workInterval * 1000 / this.lock.feed.length);
-        setTimeout(() => {
-          this.work();
-          setInterval(() => { this.pullOrders = Arr.shuffle(this.pullOrders); }, 21600000);
-          setInterval(this.workForAll, this.workInterval * 1000);
-        }, this.workInterval * 1200 / this.lock.feed.length);
+        const timeout = this.workInterval * 1000 / this.lock.feed.length;
+        setInterval(() => { this.pullOrders = Arr.shuffle(this.pullOrders); }, 21600000);
+        setTimeout(this.workForAll, timeout);
+        setTimeout(this.work, timeout * 1.2);
       }
     );
   };
@@ -529,7 +527,10 @@ export default class {
         } else {
           logger.error(`unhandled error on fetching media for all: ${error}`);
         }
-      });
+      })
+      .then(() => {
+        setTimeout(this.workForAll, this.workInterval * 1000);
+      })
   };
 
   public get isInactiveTime() {
@@ -589,25 +590,26 @@ export default class {
       const currentThread = lock.threads[currentFeed];
 
       if (!mediaItems || mediaItems.length === 0) return;
-
-      const question = mediaItems.find(story => story.original.story_questions);
-      const topOfFeed = question? question.pk : mediaItems[0].pk;
-      const updateOffset = () => currentThread.offset = topOfFeed;
+      const updateOffset = () => currentThread.offset = mediaItems[0].pk;
 
       if (currentThread.offset === '-1') { updateOffset(); return; }
+
+      const questionIndex = mediaItems.findIndex(story => story.original.story_questions);
+      if (questionIndex > -1) mediaItems.splice(0, questionIndex);
       if (currentThread.offset === '0') mediaItems.splice(1);
 
       return this.workOnMedia(mediaItems.reverse(), this.sendStories(`thread ${currentFeed}`, ...currentThread.subscribers))
         .then(updateOffset)
         .then(() => {
-          if (question) {
+          if (questionIndex > -1) {
             currentThread.subscribers.forEach(subscriber => {
               const username = cachedFeed.user.username;
               const author = `${cachedFeed.user.full_name} (@${username}) `;
               this.bot.sendTo(subscriber,
                 `请注意,用户${author}已开启问答互动。需退订请回复:/igstory_unsub ${username}${
-                  Object.keys(cachedFeed.stories).some(id => id > topOfFeed) ?
-                    `\n本次推送已截止于此条动态,下次推送在 ${Math.floor(this.workInterval / lock.feed.length)} 秒后。` : ''}`);
+                  (questionIndex > 0) ? `\n本次推送已截止于此条动态,下次推送在 ${this.workInterval} 秒后。` : ''
+                }`
+              );
             });
           }
         });