Kaynağa Gözat

- Add basic notification

Parad0x 7 yıl önce
ebeveyn
işleme
ca44aa3dd4
2 değiştirilmiş dosya ile 30 ekleme ve 1 silme
  1. 13 1
      src/connectionHandler.js
  2. 17 0
      src/telegraf.js

+ 13 - 1
src/connectionHandler.js

@@ -5,6 +5,7 @@ import type { Socket } from "net";
 const countAndSlice = require("./utils/countAndSlice");
 const Queue = require("./utils/Queue");
 const db = require("./db");
+const telegraf = require("./telegraf");
 
 // All connections stored by id
 const Connections = {};
@@ -13,6 +14,7 @@ const Connections = {};
 const secureOp = [
   "register",
   "login",
+  "notify",
   "ping",
   "close"];
 
@@ -76,11 +78,11 @@ class Connection {
   }
 
   async login() {
+    const auth = await this.queue.remove();
     if (typeof this.session !== "undefined") {
       this.write("error:session is logged;");
       return;
     }
-    const auth = await this.queue.remove();
     const user = await db.App.findOne({
       where: {
         auth,
@@ -97,6 +99,16 @@ class Connection {
     this.write("info:loggedin;");
   }
 
+  async notify() {
+    const message = await this.queue.remove();
+    if (typeof this.session === "undefined") {
+      this.write("error:need to be logged;");
+      return;
+    }
+    let messageFormatted = `[${this.session.user.name}] - ${message}`;
+    telegraf.send(messageFormatted);
+  }
+
   ping() {
     this.write("pong;");
   }

+ 17 - 0
src/telegraf.js

@@ -1,10 +1,27 @@
 //@flow
 
 const Telegraf = require("telegraf");
+const db = require("./db");
 
 const returned: {
   [string]: any
 } = {
+  async chats(): Promise<number[]> {
+    let a = await db.User.findAll({
+      where: {
+        activated: true,
+        permanentBan: false
+      }
+    });
+    a = a.map(el => el.userTelegramId).map(el => parseInt(el));
+    return a;
+  },
+  async send(message: string) {
+    let chats = await this.chats();
+    for (let id of chats) {
+      await this.bot.telegram.sendMessage(id, message);
+    }
+  },
   bot: undefined,
   async init() {
     if (typeof process.env.TELEGRAM_TOKEN === "undefined") {