Browse Source

Merge branch 'koishi-redis-waiting' into mediaonly-koishi-redis-waiting

Mike L 3 years ago
parent
commit
16e3ed9d1c
2 changed files with 24 additions and 22 deletions
  1. 12 11
      dist/twitter.js
  2. 12 11
      src/twitter.ts

+ 12 - 11
dist/twitter.js

@@ -150,10 +150,10 @@ class default_1 {
                 .then(() => {
                 if (!this.redis)
                     return;
-                const twId = cacheId.split(',')[0];
-                logger.info(`caching webshot of tweet ${twId} to redis database`);
-                this.redis.cacheContent(`webshot/${twId}`, JSON.stringify({ msg, text, author }))
-                    .then(() => this.redis.finishProcess(`webshot/${twId}`));
+                const [twid, rtid] = cacheId.split(',rt:');
+                logger.info(`caching webshot of tweet ${twid} to redis database`);
+                this.redis.cacheContent(`webshot/${twid}`, JSON.stringify({ msg, text, author, rtid }))
+                    .then(() => this.redis.finishProcess(`webshot/${twid}`));
             })
                 .then(() => sendTweets(cacheId, msg, text, author));
         }, this.webshotDelay));
@@ -169,7 +169,8 @@ class default_1 {
                     .then(content => {
                     if (content === null)
                         throw Error();
-                    return { id_str: id };
+                    const { rtid } = JSON.parse(content);
+                    return { id_str: id, retweeted_status: rtid ? { id_str: rtid } : undefined };
                 }) :
                 Promise.reject())
                 .catch(() => this.client.get(endpoint, config))
@@ -185,7 +186,7 @@ class default_1 {
         };
         this.sendTweets = (config = { reportOnSkip: false, force: false }, ...to) => (id, msg, text, author) => {
             to.forEach(subscriber => {
-                const [twId, rtId] = (/(\d+)(?:,rt:(\d+))?/.exec(id) || []).slice(1);
+                const [twid, rtid] = id.split(',rt:');
                 const { sourceInfo: source, reportOnSkip, force } = config;
                 const targetStr = JSON.stringify(subscriber);
                 const send = () => retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
@@ -198,17 +199,17 @@ class default_1 {
                     }
                 }).then(() => {
                     if (this.redis) {
-                        logger.info(`caching push status of tweet ${rtId ? `${rtId} (RT)` : twId} for ${targetStr}...`);
-                        return this.redis.cacheForChat(rtId || twId, subscriber);
+                        logger.info(`caching push status of tweet ${rtid ? `${rtid} (RTed as ${twid})` : twid} for ${targetStr}...`);
+                        return this.redis.cacheForChat(rtid || twid, subscriber);
                     }
                 });
-                ((this.redis && !force) ? this.redis.isCachedForChat(rtId || twId, subscriber) : Promise.resolve(false))
+                ((this.redis && !force) ? this.redis.isCachedForChat(rtid || twid, subscriber) : Promise.resolve(false))
                     .then(isCached => {
                     if (isCached) {
-                        logger.info(`skipped subscriber ${targetStr} as tweet ${rtId ? `${rtId} (or its RT)` : twId} has been sent already`);
+                        logger.info(`skipped subscriber ${targetStr} as tweet ${rtid ? `${rtid} (or its RT)` : twid} has been sent already`);
                         if (!reportOnSkip)
                             return;
-                        text = `[最近发送过的推文:${rtId || twId}]`;
+                        text = `[最近发送过的推文:${rtid || twid}]`;
                         msg = author + text;
                     }
                     logger.info(`pushing data${source ? ` of ${source}` : ''} to ${targetStr}`);

+ 12 - 11
src/twitter.ts

@@ -302,10 +302,10 @@ export default class {
         Promise.resolve()
           .then(() => {
             if (!this.redis) return;
-            const twId = cacheId.split(',')[0];
-            logger.info(`caching webshot of tweet ${twId} to redis database`);
-            this.redis.cacheContent(`webshot/${twId}`, JSON.stringify({msg, text, author}))
-              .then(() => this.redis.finishProcess(`webshot/${twId}`));
+            const [twid, rtid] = cacheId.split(',rt:');
+            logger.info(`caching webshot of tweet ${twid} to redis database`);
+            this.redis.cacheContent(`webshot/${twid}`, JSON.stringify({msg, text, author, rtid}))
+              .then(() => this.redis.finishProcess(`webshot/${twid}`));
           })
           .then(() => sendTweets(cacheId, msg, text, author));
       },
@@ -328,7 +328,8 @@ export default class {
         .then(() => this.redis.getContent(`webshot/${id}`))
         .then(content => {
           if (content === null) throw Error();
-          return {id_str: id} as Tweet;
+          const {rtid} = JSON.parse(content);
+          return {id_str: id, retweeted_status: rtid ? {id_str: rtid} : undefined} as Tweet;
         }) :
       Promise.reject())
       .catch(() => this.client.get(endpoint, config))
@@ -348,7 +349,7 @@ export default class {
     ...to: IChat[]
   ) => (id: string, msg: string, text: string, author: string) => {
     to.forEach(subscriber => {
-      const [twId, rtId] =  (/(\d+)(?:,rt:(\d+))?/.exec(id) || []).slice(1);
+      const [twid, rtid] = id.split(',rt:');
       const {sourceInfo: source, reportOnSkip, force} = config;
       const targetStr = JSON.stringify(subscriber);
       const send = () => retryOnError(
@@ -363,16 +364,16 @@ export default class {
         }
       ).then(() => {
         if (this.redis) {
-          logger.info(`caching push status of tweet ${rtId ? `${rtId} (RT)` : twId} for ${targetStr}...`);
-          return this.redis.cacheForChat(rtId || twId, subscriber);
+          logger.info(`caching push status of tweet ${rtid ? `${rtid} (RTed as ${twid})` : twid} for ${targetStr}...`);
+          return this.redis.cacheForChat(rtid || twid, subscriber);
         }
       });
-      ((this.redis && !force) ? this.redis.isCachedForChat(rtId || twId, subscriber) : Promise.resolve(false))
+      ((this.redis && !force) ? this.redis.isCachedForChat(rtid || twid, subscriber) : Promise.resolve(false))
         .then(isCached => {
           if (isCached) {
-            logger.info(`skipped subscriber ${targetStr} as tweet ${rtId ? `${rtId} (or its RT)` : twId} has been sent already`);
+            logger.info(`skipped subscriber ${targetStr} as tweet ${rtid ? `${rtid} (or its RT)` : twid} has been sent already`);
             if (!reportOnSkip) return;
-            text = `[最近发送过的推文:${rtId || twId}]`;
+            text = `[最近发送过的推文:${rtid || twid}]`;
             msg = author + text;
           }
           logger.info(`pushing data${source ? ` of ${source}` : ''} to ${targetStr}`);