Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var app = require('express')();
  2. var server = require('http').Server(app);
  3. var io = require('socket.io').listen(server);
  4. var MySQLEvents = require('mysql-events');
  5. var dsn = {
  6.   host:     "149.202.54.214",
  7.   user:     "Revocean-DB-User",
  8.   password: "Revocean-DB-User-2016ABNP"
  9. };
  10. var mysqlEventWatcher = MySQLEvents(dsn);
  11. server.listen(2424);
  12. var pairs = new Array();
  13. var login = {};
  14. var smartphonevertification = {}
  15. var path = {};
  16.  
  17. io.sockets.on('connection', function (client) {
  18.     client.on('login', function (data) {
  19.         if(typeof login[data["u_id"]] == "undefined")
  20.             login[data["u_id"]] = {};
  21.         if(typeof login[data["u_id"]][data["b_id"]] == "undefined")
  22.             login[data["u_id"]][data["b_id"]] = {};
  23.         login[data["u_id"]][data["b_id"]] = {socket: client, domain: data["domain"]};
  24.         console.log(login);
  25.         path[client.id] = {"type": "login", "u_id": data["u_id"], "b_id": data["b_id"]};
  26.     });
  27.     client.on('smartphonevertification', function (data) {
  28.         if(typeof smartphonevertification[data["token"]] == "undefined")
  29.             smartphonevertification[data["token"]] = {};
  30.         smartphonevertification[data["token"]] = {socket: client};
  31.         console.log(smartphonevertification);
  32.         path[client.id] = {"type": "smartphonevertification", "token": data["token"]};
  33.     });
  34.     client.on('disconnect', function() {
  35.         if(typeof path[client.id] != "undefined") {
  36.             if(typeof path[client.id]["type"] != "undefined" && path[client.id]["type"] == "login") {
  37.                 delete login[path[client.id]["u_id"]][path[client.id]["b_id"]];
  38.             } else if(typeof path[client.id]["type"] != "undefined" && path[client.id]["type"] == "smartphonevertification") {
  39.                 delete smartphonevertification[path[client.id]["token"]];
  40.             }
  41.         }
  42.     });
  43. });
  44.  
  45. var Account = mysqlEventWatcher.add(
  46.     'Account',
  47.     function (oldRow, newRow) {
  48.         if (oldRow === null) {
  49.             if(newRow["table"] == "notifications") {
  50.                 var user = login[newRow["fields"]["n_u_id"]];
  51.                 if(typeof user != "undefined") {
  52.                     for (var key in user) {
  53.                         if(user.hasOwnProperty(key)) {
  54.                             io.sockets.connected[user[key]["socket"].id].emit('newnotification');
  55.                         }
  56.                     }
  57.                 }
  58.             }
  59.         } else if (newRow === null) {
  60.         } else if (oldRow !== null && newRow !== null) {
  61.             if(newRow["table"] == "notifications") {
  62.                 var user = login[newRow["fields"]["n_u_id"]];
  63.                 if(typeof user != "undefined") {
  64.                     for (var key in user) {
  65.                         if(user.hasOwnProperty(key)) {
  66.                             io.sockets.connected[user[key]["socket"].id].emit('notificationchanged');
  67.                         }
  68.                     }
  69.                 }
  70.             } else if(newRow["table"] == "loginattemps") {
  71.                 if(oldRow["fields"]["la_smartphone_success"] == null && newRow["fields"]["la_smartphone_success"]) {
  72.                     var user = checkSmartphoneLogin[newRow["fields"]["la_id"]][newRow["fields"]["la_authtoken"]];
  73.                     if(typeof user != "undefined") {
  74.                         io.sockets.connected[user["socket"].id].emit('smartphone_success');
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     },
  80.     'Active'
  81. );
  82. var Users = mysqlEventWatcher.add(
  83.     'Users',
  84.     function (oldRow, newRow) {
  85.         if (oldRow === null) {
  86.             if(newRow["table"] == "blockedusers") {
  87.                 var user = login[newRow["fields"]["bu_u_id"]];
  88.                 if(typeof user != "undefined") {
  89.                     for (var key in user) {
  90.                         if(user.hasOwnProperty(key)) {
  91.                             io.sockets.connected[user[key]["socket"].id].emit('refreshpage');
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         } else if (newRow === null) {
  97.             if(oldRow["table"] == "blockedusers") {
  98.                 var user = login[oldRow["fields"]["bu_u_id"]];
  99.                 if(typeof user != "undefined") {
  100.                     for (var key in user) {
  101.                         if(user.hasOwnProperty(key)) {
  102.                             io.sockets.connected[user[key]["socket"].id].emit('refreshpage');
  103.                         }
  104.                     }
  105.                 }
  106.             }
  107.         } else if (oldRow !== null && newRow !== null) {
  108.             if(oldRow["table"] == "usersbrowsers") {
  109.                 if(oldRow["fields"]["ub_loggedout"] == "0" && newRow["fields"]["ub_loggedout"] == "1") {
  110.                     var infos = login[oldRow["fields"]["ub_u_id"]][oldRow["fields"]["ub_b_id"]];
  111.                     if(typeof infos != "undefined") {
  112.                         if(newRow["fields"]["ub_loggedout"]) {
  113.                             io.sockets.connected[infos["socket"].id].emit('loguserout');
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.             if(oldRow["table"] == "users") {
  119.                 if(oldRow["fields"]["u_deleted"] == "0" && newRow["fields"]["u_deleted"] == "1") {
  120.                     var user = login[oldRow["fields"]["u_id"]];
  121.                     for (var key in user) {
  122.                         if(user.hasOwnProperty(key)) {
  123.                             io.sockets.connected[user[key]["socket"].id].emit('loguserout');
  124.                         }
  125.                     }
  126.                 }
  127.                 if(oldRow["fields"]["u_active"] == "0" && newRow["fields"]["u_active"] == "1" || oldRow["fields"]["u_active"] == "1" && newRow["fields"]["u_active"] == "0") {
  128.                     var user = login[oldRow["fields"]["u_id"]];
  129.                     for (var key in user) {
  130.                         if(user.hasOwnProperty(key)) {
  131.                             io.sockets.connected[user[key]["socket"].id].emit('refreshpage');
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.             if(oldRow["table"] == "smartphonevertification") {
  137.                 var user = smartphonevertification[newRow["fields"]["sv_token"]];
  138.                 if(typeof user != "undefined") {
  139.                     io.sockets.connected[user["socket"].id].emit('smartphone_success');
  140.                 }
  141.             }
  142.         }
  143.     },
  144.     'Active'
  145. );
  146.  
  147. var Events = mysqlEventWatcher.add(
  148.     'Events',
  149.     function (oldRow, newRow) {
  150.         if (oldRow === null) {
  151.             if(newRow["table"] == "votes") {
  152.                 console.log(login);
  153.                 for(var users in login) {
  154.                     for(var key in login[users]) {
  155.                         if(login[users].hasOwnProperty(key)) {
  156.                             if(login[users][key]["domain"] == "events") {
  157.                                 io.sockets.connected[login[users][key]["socket"].id].emit('updatevote',{e_id: newRow["fields"]["v_e_id"]});
  158.                             }
  159.                         }
  160.                     }
  161.                 }
  162.             }
  163.         } else if (newRow === null) {
  164.         } else if (oldRow !== null && newRow !== null) {
  165.         }
  166.     },
  167.     'Active'
  168. );
  169.  
  170. var Class = mysqlEventWatcher.add(
  171.     'Class',
  172.     function (oldRow, newRow) {
  173.         if((oldRow != null && oldRow["table"] == "votes") || (newRow != null && newRow["table"] == "votes")) {
  174.             console.log(login);
  175.             for(var users in login) {
  176.                 for(var key in login[users]) {
  177.                     if(login[users].hasOwnProperty(key)) {
  178.                         if(login[users][key]["domain"] == "class") {
  179.                             io.sockets.connected[login[users][key]["socket"].id].emit('updatevote');
  180.                         }
  181.                     }
  182.                 }
  183.             }
  184.         }
  185.         if (oldRow === null) {
  186.         } else if (newRow === null) {
  187.         } else if (oldRow !== null && newRow !== null) {
  188.         }
  189.     },
  190.     'Active'
  191. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement