enwnbot2

Converts MediaWiki [[links]] and {{templates}} to links, informs important events from wiki, handles announces review queue, and under review, and handles when they last saw a given user.
git clone http://git.hanabi.in/repos/enwnbot2.git
Log | Files | Refs | README | LICENSE

utils.js (4207B)


      1 const fetch = require("node-fetch");
      2 
      3 const { ircBotName, URL, wiki } = require("./config");
      4 
      5 async function fetchData(URI) {
      6   const res = {};
      7   try {
      8     const data = await fetch(URI);
      9     const parsed = await data.json();
     10     res.list = parsed.query.categorymembers;
     11   } catch (error) {
     12     res.error = true;
     13     console.warn("Error in fetchData:", error);
     14   }
     15   return res;
     16 }
     17 
     18 function fullUrl(title = "") {
     19   const fixedTitle = title.replace(/\?/g, "%3F"); // fix the title for '?'
     20   let [main, anchor] = fixedTitle.split("#");
     21   main = main.replace(/ /g, "%20");
     22   if (anchor) anchor = anchor.replace(/ /g, "_");
     23   else main = main.replace(/%20/g, "_");
     24   let final = main;
     25   if (anchor) final += `%23${anchor}`;
     26   return `${URL}${final}`;
     27 }
     28 
     29 function getCategory(title = "") {
     30   return title.replace(/^Category:/, "");
     31 }
     32 
     33 function getFullLink(link) {
     34   const len = link.length;
     35   const trimmed = link.substr(2, len - 4);
     36   const finalUrl = fullUrl(trimmed);
     37   return finalUrl;
     38 }
     39 
     40 function getFullTemplate(template) {
     41   const len = template.length;
     42   const word = template
     43     .substr(2, len - 4)
     44     .split("|")[0]
     45     .replace(/ /g, "%20")
     46     .replace(/\?/g, "%3F");
     47   return `${URL}Template:${word}`;
     48 }
     49 
     50 const linkRegex = /\[{2}(.*?)\]{2}/g;
     51 
     52 function streamError(event) {
     53   const knownError = { type: "error" };
     54   const knownErrStr = JSON.stringify(knownError);
     55   const eventStr = JSON.stringify(event);
     56   if (eventStr != knownErrStr) {
     57     ircClient.say("acagastya", eventStr + "\n --- error");
     58     console.error("--- Encountered error", event);
     59   }
     60 }
     61 
     62 function streamMessage(event) {
     63   let msg = "";
     64   const change = JSON.parse(event.data);
     65   const { comment, title, type, user } = change;
     66   if (change.wiki == wiki && type == "categorize") {
     67     const category = getCategory(title);
     68     const pageRegex = /\[\[:(.*)\]\]/;
     69     const page = comment.match(pageRegex)[1];
     70     switch (category) {
     71       case "Editing": {
     72         if (comment.includes("added"))
     73           msg = `Attention!  ${user} is now {{editing}} [[${page}]].`;
     74         else msg = `[[${page}]] is no longer under {{editing}}.`;
     75         break;
     76       }
     77       case "Developing": {
     78         if (comment.includes("added"))
     79           msg = `${user} may be working on [[${page}]].`;
     80         else msg = `${user} has removed [[${page}]] from {{develop}}.`;
     81         break;
     82       }
     83       case "Review": {
     84         if (comment.includes("added"))
     85           msg = `${user} has submitted [[${page}]] for review.`;
     86         else msg = `${user} has removed [[${page}]] from review.`;
     87         break;
     88       }
     89       case "Under review": {
     90         if (comment.includes("added"))
     91           msg = `${user} is reviewing [[${page}]].`;
     92         else msg = `${user} is no longer reviewing [[${page}]].`;
     93         break;
     94       }
     95       case "Peer reviewed/Not ready": {
     96         const failedArticleRegex = /^\[\[:Talk:([^\]\]]*)\]\]/;
     97         const failedArticle = comment.match(failedArticleRegex)[1];
     98         msg = `${user} has not-ready'd the [[${failedArticle}]] article.`;
     99         break;
    100       }
    101       case "Disputed": {
    102         const failedArticleRegex = /^\[\[:Talk:([^\]\]]*)\]\]/;
    103         const maybeMatch = comment.match(failedArticleRegex) || [];
    104         const failedArticle  = maybeMatch[1];
    105         if (comment.includes("added") && failedArticle) msg = `${user} has not-ready'd the [[${failedArticle}]] article.`;
    106         break;
    107       }
    108       case "Abandoned": {
    109         if (comment.includes("added"))
    110           msg = `${user} has marked [[${page}]] as abandoned.`;
    111         break;
    112       }
    113       case "Published": {
    114         if (comment.includes("added"))
    115           msg = `${user} just published [[${page}]].`;
    116         break;
    117       }
    118       case "Archived": {
    119         if (comment.includes("added"))
    120           msg = `${user} has archived [[${page}]].`;
    121         else msg = `${user} has unarchived [[${page}]].`;
    122         break;
    123       }
    124     }
    125     return msg;
    126   }
    127 }
    128 
    129 const templateRegex = /\{{2}(.*?)\}{2}/g;
    130 
    131 const thanksRegex = new RegExp(
    132   `(thanks?|thank you|thankyou),? ${ircBotName}`,
    133   "i"
    134 );
    135 
    136 module.exports = {
    137   fetchData,
    138   fullUrl,
    139   getFullLink,
    140   getFullTemplate,
    141   linkRegex,
    142   streamError,
    143   streamMessage,
    144   templateRegex,
    145   thanksRegex
    146 };