Преглед изворни кода

proper promise chaining, cust. webshot processing

fix missing default param logger warning
Mike L пре 3 година
родитељ
комит
5623c4ba40
16 измењених фајлова са 36 додато и 31 уклоњено
  1. 1 2
      dist/koishi.js
  2. 0 0
      dist/koishi.js.map
  3. 4 4
      dist/main.js
  4. 0 0
      dist/main.js.map
  5. 2 2
      dist/twitter.js
  6. 0 0
      dist/twitter.js.map
  7. 1 1
      dist/utils.d.ts
  8. 1 1
      dist/utils.js
  9. 0 0
      dist/utils.js.map
  10. 7 5
      dist/webshot.js
  11. 0 0
      dist/webshot.js.map
  12. 1 2
      src/koishi.ts
  13. 4 4
      src/main.ts
  14. 2 2
      src/twitter.ts
  15. 3 3
      src/utils.ts
  16. 10 5
      src/webshot.ts

+ 1 - 2
dist/koishi.js

@@ -14,7 +14,6 @@ const koishi_1 = require("koishi");
 require("koishi-adapter-onebot");
 const command_1 = require("./command");
 const loggers_1 = require("./loggers");
-const utils_1 = require("./utils");
 const logger = loggers_1.getLogger('qqbot');
 const cqUrlFix = (factory) => (...args) => factory(...args).replace(/(?<=\[CQ:.*)url=(?=(base64|file|https?):\/\/)/, 'file=');
 exports.Message = {
@@ -87,7 +86,7 @@ class default_1 {
         this.sendToUser = (userID, message) => new Promise(resolve => {
             this.enqueue('private', userID, () => this.bot.sendPrivateMessage(userID, message).then(resolve));
         });
-        this.sendTo = (subscriber, messageChain) => utils_1.chainPromises((splitted => [splitted.message, ...splitted.attachments])(exports.Message.separateAttachment(messageChain)).map(msg => {
+        this.sendTo = (subscriber, messageChain) => Promise.all((splitted => [splitted.message, ...splitted.attachments])(exports.Message.separateAttachment(messageChain)).map(msg => {
             switch (subscriber.chatType) {
                 case 'group':
                     return this.sendToGroup(subscriber.chatID.toString(), msg);

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/koishi.js.map


+ 4 - 4
dist/main.js

@@ -12,14 +12,14 @@ const twitter_1 = require("./twitter");
 const logger = loggers_1.getLogger();
 const sections = [
     {
-        header: 'MiraiTS Twitter Bot',
+        header: 'GoCQHTTP Twitter Bot',
         content: 'The QQ Bot that forwards twitters.',
     },
     {
         header: 'Synopsis',
         content: [
-            '$ mirai-twitter-bot {underline config.json}',
-            '$ mirai-twitter-bot {bold --help}',
+            '$ twitter-bot {underline config.json}',
+            '$ twitter-bot {bold --help}',
         ],
     },
     {
@@ -62,7 +62,7 @@ if (requiredFields.some((value) => config[value] === undefined)) {
 }
 optionalFields.forEach(key => {
     if (config[key] === undefined || typeof (config[key]) !== typeof (exampleConfig[key])) {
-        if (key in warningFields)
+        if (warningFields.includes(key))
             logger.warn(`${key} is undefined, use ${exampleConfig[key] || 'empty string'} as default`);
         config[key] = exampleConfig[key];
     }

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/main.js.map


+ 2 - 2
dist/twitter.js

@@ -297,12 +297,12 @@ class default_1 {
                 noreps: { on: true, off: false }[noreps],
                 norts: { on: true, off: false }[norts],
             })
-                .then(tweets => utils_1.chainPromises(tweets.map(tweet => this.bot.sendTo(receiver, `\
+                .then(tweets => utils_1.chainPromises(tweets.map(tweet => () => this.bot.sendTo(receiver, `\
 编号:${tweet.id_str}
 时间:${tweet.created_at}
 媒体:${tweet.extended_entities ? '有' : '无'}
 正文:\n${tweet.full_text.replace(/^([\s\S\n]{50})[\s\S\n]+?( https:\/\/t.co\/.*)?$/, '$1…$2')}`))
-                .concat(this.bot.sendTo(receiver, tweets.length ?
+                .concat(() => this.bot.sendTo(receiver, tweets.length ?
                 '时间线查询完毕,使用 /twitter_view <编号> 查看推文详细内容。' :
                 '时间线查询完毕,没有找到符合条件的推文。'))))
                 .catch((err) => {

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/twitter.js.map


+ 1 - 1
dist/utils.d.ts

@@ -1,4 +1,4 @@
-export declare const chainPromises: <T>(promises: Promise<T>[], reducer?: (p1: Promise<T>, p2: Promise<T>) => Promise<T>, initialValue?: T) => Promise<T>;
+export declare const chainPromises: <T>(lazyPromises: ((p: T) => Promise<T>)[], reducer?: (lp1: (p: T) => Promise<T>, lp2: (p: T) => Promise<T>) => (p: T) => Promise<T>, initialValue?: T) => Promise<T>;
 export declare const BigNumOps: {
     splitAt: (num: string, at: number) => number[];
     plus: (num1: string, num2: string) => string;

+ 1 - 1
dist/utils.js

@@ -1,7 +1,7 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
 exports.BigNumOps = exports.chainPromises = void 0;
-const chainPromises = (promises, reducer = (p1, p2) => p1.then(() => p2), initialValue) => promises.reduce(reducer, Promise.resolve(initialValue));
+const chainPromises = (lazyPromises, reducer = (lp1, lp2) => (p) => lp1(p).then(lp2), initialValue) => lazyPromises.reduce(reducer, p => Promise.resolve(p))(initialValue);
 exports.chainPromises = chainPromises;
 const splitBigNumAt = (num, at) => num.replace(RegExp(String.raw `^([+-]?)(\d+)(\d{${at}})$`), '$1$2,$1$3')
     .replace(/^([^,]*)$/, '0,$1').split(',')

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/utils.js.map


+ 7 - 5
dist/webshot.js

@@ -52,7 +52,7 @@ class Webshot extends CallableInstance {
         this.extendEntity = (media) => {
             logger.info('not working on a tweet');
         };
-        this.renderWebshot = (url, height, webshotDelay) => {
+        this.renderWebshot = (url, height, webshotDelay, ...morePostProcessings) => {
             temp.track();
             const jpeg = (data) => data.pipe(sharp()).jpeg({ quality: 90, trellisQuantisation: true });
             const sharpToFile = (pic) => new Promise(resolve => {
@@ -133,6 +133,7 @@ class Webshot extends CallableInstance {
                         .then(() => page.addScriptTag({
                         content: 'document.documentElement.scrollTop=0;',
                     }))
+                        .then(() => utils_1.chainPromises(morePostProcessings.map(func => () => func(page))))
                         .then(() => util_1.promisify(setTimeout)(getTimeout()))
                         .then(() => page.screenshot())
                         .then(screenshot => {
@@ -187,12 +188,13 @@ class Webshot extends CallableInstance {
                     .catch(reject);
             });
             return promise.then(data => {
-                if (data.boundary === null)
-                    return this.renderWebshot(url, height + 1920, webshotDelay);
+                if (data.boundary === null) {
+                    return this.renderWebshot(url, height + 1920, webshotDelay, ...morePostProcessings);
+                }
                 else
                     return data.path;
             }).catch(error => this.reconnect(error)
-                .then(() => this.renderWebshot(url, height, webshotDelay)));
+                .then(() => this.renderWebshot(url, height, webshotDelay, ...morePostProcessings)));
         };
         this.fetchMedia = (url) => new Promise((resolve, reject) => {
             logger.info(`fetching ${url}`);
@@ -290,7 +292,7 @@ class Webshot extends CallableInstance {
             if (1 - this.mode % 2)
                 promise = promise.then(() => {
                     if (originTwi.extended_entities) {
-                        return utils_1.chainPromises(originTwi.extended_entities.media.map(media => {
+                        return utils_1.chainPromises(originTwi.extended_entities.media.map(media => () => {
                             let url;
                             if (media.type === 'photo') {
                                 url = media.media_url_https.replace(/\.([a-z]+)$/, '?format=$1') + '&name=orig';

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/webshot.js.map


+ 1 - 2
src/koishi.ts

@@ -3,7 +3,6 @@ import 'koishi-adapter-onebot';
 
 import { parseCmd, query, view } from './command';
 import { getLogger } from './loggers';
-import { chainPromises } from './utils';
 
 const logger = getLogger('qqbot');
 
@@ -98,7 +97,7 @@ export default class {
     this.enqueue('private', userID, () => this.bot.sendPrivateMessage(userID, message).then(resolve));
   });
 
-  public sendTo = (subscriber: IChat, messageChain: string) => chainPromises(
+  public sendTo = (subscriber: IChat, messageChain: string) => Promise.all(
     (splitted => [splitted.message, ...splitted.attachments])(
       Message.separateAttachment(messageChain)
     ).map(msg => {

+ 4 - 4
src/main.ts

@@ -15,14 +15,14 @@ const logger = getLogger();
 
 const sections: commandLineUsage.Section[] = [
   {
-    header: 'MiraiTS Twitter Bot',
+    header: 'GoCQHTTP Twitter Bot',
     content: 'The QQ Bot that forwards twitters.',
   },
   {
     header: 'Synopsis',
     content: [
-      '$ mirai-twitter-bot {underline config.json}',
-      '$ mirai-twitter-bot {bold --help}',
+      '$ twitter-bot {underline config.json}',
+      '$ twitter-bot {bold --help}',
     ],
   },
   {
@@ -75,7 +75,7 @@ if (requiredFields.some((value) => config[value] === undefined)) {
 
 optionalFields.forEach(key => {
   if (config[key] === undefined || typeof(config[key]) !== typeof (exampleConfig[key])) {
-    if (key in warningFields) logger.warn(`${key} is undefined, use ${exampleConfig[key] || 'empty string'} as default`);
+    if (warningFields.includes(key)) logger.warn(`${key} is undefined, use ${exampleConfig[key] || 'empty string'} as default`);
     config[key] = exampleConfig[key as keyof Config];
   }
 });

+ 2 - 2
src/twitter.ts

@@ -158,13 +158,13 @@ export default class {
         norts: {on: true, off: false}[norts],
       })
         .then(tweets => chainPromises(
-          tweets.map(tweet => this.bot.sendTo(receiver, `\
+          tweets.map(tweet => () => this.bot.sendTo(receiver, `\
 编号:${tweet.id_str}
 时间:${tweet.created_at}
 媒体:${tweet.extended_entities ? '有' : '无'}
 正文:\n${tweet.full_text.replace(/^([\s\S\n]{50})[\s\S\n]+?( https:\/\/t.co\/.*)?$/, '$1…$2')}`
           ))
-            .concat(this.bot.sendTo(receiver, tweets.length ?
+            .concat(() => this.bot.sendTo(receiver, tweets.length ?
               '时间线查询完毕,使用 /twitter_view <编号> 查看推文详细内容。' :
               '时间线查询完毕,没有找到符合条件的推文。'
             ))

+ 3 - 3
src/utils.ts

@@ -1,8 +1,8 @@
 export const chainPromises = <T>(
-  promises: Promise<T>[],
-  reducer = (p1: Promise<T>, p2: Promise<T>) => p1.then(() => p2),
+  lazyPromises: ((p: T) => Promise<T>)[],
+  reducer = (lp1: (p: T) => Promise<T>, lp2: (p: T) => Promise<T>) => (p: T) => lp1(p).then(lp2),
   initialValue?: T
-) => promises.reduce(reducer, Promise.resolve(initialValue));
+) => lazyPromises.reduce(reducer, p => Promise.resolve(p))(initialValue);
 
 const splitBigNumAt = (num: string, at: number) => num.replace(RegExp(String.raw`^([+-]?)(\d+)(\d{${at}})$`), '$1$2,$1$3')
   .replace(/^([^,]*)$/, '0,$1').split(',')

+ 10 - 5
src/webshot.ts

@@ -74,7 +74,10 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
     logger.info('not working on a tweet');
   };
 
-  private renderWebshot = (url: string, height: number, webshotDelay: number): Promise<string> => {
+  private renderWebshot = (
+    url: string, height: number, webshotDelay: number,
+    ...morePostProcessings: ((page: puppeteer.Page) => Promise<any>)[]
+  ): Promise<string> => {
     temp.track();
     const jpeg = (data: Readable) => data.pipe(sharp()).jpeg({quality: 90, trellisQuantisation: true});
     const sharpToFile = (pic: sharp.Sharp) => new Promise<string>(resolve => {
@@ -155,6 +158,7 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
             .then(() => page.addScriptTag({
               content: 'document.documentElement.scrollTop=0;',
             }))
+            .then(() => chainPromises(morePostProcessings.map(func => () => func(page))))
             .then(() => promisify(setTimeout)(getTimeout()))
             .then(() => page.screenshot())
             .then(screenshot => {
@@ -212,10 +216,11 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
         .catch(reject);
     });
     return promise.then(data => {
-      if (data.boundary === null) return this.renderWebshot(url, height + 1920, webshotDelay);
-      else return data.path;
+      if (data.boundary === null) {
+        return this.renderWebshot(url, height + 1920, webshotDelay, ...morePostProcessings);
+      } else return data.path;
     }).catch(error => this.reconnect(error)
-      .then(() => this.renderWebshot(url, height, webshotDelay))
+      .then(() => this.renderWebshot(url, height, webshotDelay, ...morePostProcessings))
     );
   };
 
@@ -316,7 +321,7 @@ class Webshot extends CallableInstance<[Tweets, (...args) => void, number], Prom
       // eslint-disable-next-line curly
       if (1 - this.mode % 2) promise = promise.then(() => {
         if (originTwi.extended_entities) {
-          return chainPromises(originTwi.extended_entities.media.map(media => {
+          return chainPromises(originTwi.extended_entities.media.map(media => () => {
             let url: string;
             if (media.type === 'photo') {
               url = media.media_url_https.replace(/\.([a-z]+)$/, '?format=$1') + '&name=orig';

Неке датотеке нису приказане због велике количине промена