Prechádzať zdrojové kódy

Merge branch 'koishi-guild' into mediaonly-koishi-guild

Mike L 2 rokov pred
rodič
commit
2ec911a4ae
2 zmenil súbory, kde vykonal 28 pridanie a 20 odobranie
  1. 9 11
      dist/twitter.js
  2. 19 9
      src/twitter.ts

+ 9 - 11
dist/twitter.js

@@ -204,6 +204,13 @@ class default_1 {
             })
                 .then(() => sendTweets(cacheId, msg, text, author));
         }, this.webshotDelay));
+        this.handleRetweet = (tweet) => {
+            const retweetRef = (tweet.data.referenced_tweets || []).find(ref => ref.type === 'retweeted');
+            if (retweetRef)
+                return this.client.v2.singleTweet(retweetRef.id, v2SingleParams)
+                    .then(({ data: { referenced_tweets }, includes: { media } }) => (Object.assign(Object.assign({}, tweet), { data: Object.assign(Object.assign({}, tweet.data), { referenced_tweets: [retweetRef, ...(referenced_tweets || [])] }), includes: Object.assign(Object.assign({}, tweet.includes), { media }) })));
+            return Promise.resolve(tweet);
+        };
         this.getTweet = (id, sender, refresh = false) => ((this.redis && !refresh) ?
             this.redis.waitForProcess(`webshot/${id}`, this.webshotDelay * 4)
                 .then(() => this.redis.getContent(`webshot/${id}`))
@@ -218,10 +225,7 @@ class default_1 {
             .then((tweet) => {
             if (tweet.data.text) {
                 logger.debug(`api returned tweet ${JSON.stringify(tweet)} for query id=${id}`);
-                const retweetRef = (tweet.data.referenced_tweets || []).find(ref => ref.type === 'retweeted');
-                if (retweetRef)
-                    return this.client.v2.singleTweet(retweetRef.id, v2SingleParams)
-                        .then(({ includes: { media } }) => (Object.assign(Object.assign({}, tweet), { includes: Object.assign(Object.assign({}, tweet.includes), { media }) })));
+                return this.handleRetweet(tweet);
             }
             else {
                 logger.debug(`skipped querying api as this tweet has been cached`);
@@ -289,13 +293,7 @@ class default_1 {
                     users: [includes.author(tweet)]
                 }
             })))
-                .then(tweets => Promise.all(tweets.map(tweet => {
-                const retweetRef = (tweet.data.referenced_tweets || []).find(ref => ref.type === 'retweeted');
-                if (retweetRef)
-                    return this.client.v2.singleTweet(retweetRef.id, v2SingleParams)
-                        .then(({ includes: { media } }) => (Object.assign(Object.assign({}, tweet), { includes: Object.assign(Object.assign({}, tweet.includes), { media }) })));
-                return tweet;
-            })));
+                .then(tweets => Promise.all(tweets.map(this.handleRetweet)));
         };
         this.work = () => {
             const lock = this.lock;

+ 19 - 9
src/twitter.ts

@@ -378,6 +378,23 @@ export default class {
     )
   );
 
+  private handleRetweet = (tweet: Tweet) => {
+    const retweetRef = (tweet.data.referenced_tweets || []).find(ref => ref.type === 'retweeted');
+    if (retweetRef) return this.client.v2.singleTweet(retweetRef.id, v2SingleParams)
+      .then(({data: {referenced_tweets}, includes: {media}}) => ({
+        ...tweet, 
+        data: {
+          ...tweet.data,
+          referenced_tweets: [retweetRef, ...(referenced_tweets || [])],
+        },
+        includes: {
+          ...tweet.includes,
+          media
+        }
+      }) as Tweet);
+    return Promise.resolve(tweet);
+  };
+
   public getTweet = (
     id: string,
     sender: (cacheId: string, msg: string, text: string, author: string) => void,
@@ -397,9 +414,7 @@ export default class {
       .then((tweet: Tweet) => {
         if (tweet.data.text) {
           logger.debug(`api returned tweet ${JSON.stringify(tweet)} for query id=${id}`);
-          const retweetRef = (tweet.data.referenced_tweets || []).find(ref => ref.type === 'retweeted');
-          if (retweetRef) return this.client.v2.singleTweet(retweetRef.id, v2SingleParams)
-            .then(({includes: {media}}) => ({...tweet, includes: {...tweet.includes, media}}) as Tweet);
+          return this.handleRetweet(tweet);
         } else {
           logger.debug(`skipped querying api as this tweet has been cached`);
         }
@@ -475,12 +490,7 @@ export default class {
           }
         })
       ))
-      .then(tweets => Promise.all(tweets.map(tweet => {
-        const retweetRef = (tweet.data.referenced_tweets || []).find(ref => ref.type === 'retweeted');
-        if (retweetRef) return this.client.v2.singleTweet(retweetRef.id, v2SingleParams)
-          .then(({includes: {media}}) => ({...tweet, includes: {...tweet.includes, media}}) as Tweet);
-        return tweet;
-      })));
+      .then(tweets => Promise.all(tweets.map(this.handleRetweet)));
   };
 
   public work = () => {