瀏覽代碼

fix still working on unsubscribed feeds

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

+ 12 - 0
dist/twitter.js

@@ -183,6 +183,18 @@ class default_1 {
     constructor(opt) {
         this.launch = () => {
             this.webshot = new webshot_1.default(this.wsUrl, this.mode, () => {
+                const subscribedIds = this.lock.feed.map(feed => this.lock.threads[feed].id.toString());
+                for (const id in this.cache) {
+                    if (this.cache[id].pullOrder !== 0 && !subscribedIds.includes(id)) {
+                        logger.warn(`disabling pull job of unsubscribed user @${this.cache[id].user.username}`);
+                        this.cache[id].pullOrder = 0;
+                    }
+                }
+                const userIdCache = this.pullOrders;
+                if (Object.values(userIdCache).length !== userIdCache.length) {
+                    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();

+ 1 - 1
dist/utils.js

@@ -5,7 +5,7 @@ const crypto = require("crypto");
 const chainPromises = (lazyPromises, reducer = (lp1, lp2) => (p) => lp1(p).then(lp2), initialValue) => lazyPromises.reduce(reducer, p => Promise.resolve(p))(initialValue);
 exports.chainPromises = chainPromises;
 const shuffleArray = (arr) => {
-    const res = arr.slice(0);
+    const res = arr.filter(e => e);
     for (let i = res.length - 1; i > 0; i--) {
         const j = crypto.randomInt(0, i + 1);
         [res[i], res[j]] = [res[j], res[i]];

+ 12 - 0
src/twitter.ts

@@ -377,6 +377,18 @@ export default class {
       this.wsUrl,
       this.mode,
       () => {
+        const subscribedIds = this.lock.feed.map(feed => this.lock.threads[feed].id.toString());
+        for (const id in this.cache) {
+          if (this.cache[id].pullOrder !== 0 && !subscribedIds.includes(id)) {
+            logger.warn(`disabling pull job of unsubscribed user @${this.cache[id].user.username}`);
+            this.cache[id].pullOrder = 0;
+          }
+        }
+        const userIdCache = this.pullOrders;
+        if (Object.values(userIdCache).length !== userIdCache.length) {
+          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();

+ 1 - 1
src/utils.ts

@@ -7,7 +7,7 @@ export const chainPromises = <T>(
 ) => lazyPromises.reduce(reducer, p => Promise.resolve(p))(initialValue);
 
 const shuffleArray = <T>(arr: T[]) => {
-  const res = arr.slice(0);
+  const res = arr.filter(e => e);
   for (let i = res.length - 1; i > 0; i--) {
     const j = crypto.randomInt(0, i + 1);
     [res[i], res[j]] = [res[j], res[i]];