לדלג לתוכן

משתמש:מוטי בוט/מודל שליחת הודעה.js

מתוך המכלול, האנציקלופדיה היהודית

לתשומת ליבך: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
class MessageSender {

  constructor(wikiUrl) {
    this.wikiUrl = wikiUrl;
  }
  async getModelPage(pageName) {
    const url = `${this.wikiUrl}?action=query&format=json&prop=info&indexpageids=1&titles=${pageName}&utf8=1`;
    const response = await fetch(url);
    const data = await response.json();
    return data.query.pages[data.query.pageids[0]].contentmodel;
  }

  async messageEdit(pageName, titleMessage, message) {
    const csrfToken = mw.user.tokens.get("csrfToken");

    const response = await fetch(this.wikiUrl, {
      method: "POST",
      body: new URLSearchParams({
        action: "edit",
        format: "json",
        title: pageName,
        section: "new",
        sectiontitle: titleMessage,
        text: message,
        token: csrfToken,
        utf8: "1",
      }),
    });

    const data = await response.json();
    return data;
  }

  async sendAFlowMessage(pageName, titleMessage, message) {
    const csrfToken = mw.user.tokens.get("csrfToken");
    const encodedTitleMessage = encodeURIComponent(titleMessage);
    const encodedMessage = encodeURIComponent(message);
    const params = {
      action: "flow",
      format: "json",
      page: pageName,
      submodule: "new-topic",
      utf8: 1,
      nttopic: encodedTitleMessage,
      ntcontent: encodedMessage,
      nttoken: csrfToken,
    };

    const body = new URLSearchParams(params);
    try {
      const response = await fetch(this.wikiUrl, {
        method: "POST",
        body: body,
      });
    
      const data = await response.json();
      return data;
    } catch (error) {
      console.error("An error occurred:", error);
    }
  }

  async sendMessage(pageName, titleMessage, message) {
    try {
      const model = await this.getModelPage(pageName);
      let response;
      switch (model) {
        case "wikitext":
          response = await this.messageEdit(pageName, titleMessage, message);
          break;
        case "flow-board":
          response = await this.sendAFlowMessage(pageName, titleMessage, message);
          break;
        default:
          throw new Error("מודל לא חוקי");
      }
      if (response.edit?.result === "Success") {
        mw.notify("ההודעה נשמרה בהצלחה", { type: "success" });
      } else if (response.flow["new-topic"].status === "ok") {
        mw.notify("ההודעה נשלחה בהצלחה", { type: "success" });
      } else {
        mw.notify("ההודעה לא נשמרה", { type: "error" });
      }
    } catch (error) {
      console.log(error);
      throw new Error(error);
    }
  }
}