io.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. 'use strict';
  2. // This file contains all the logic for Socket.IO
  3. const coreClass = require("../core");
  4. const async = require("async");
  5. const config = require("config");
  6. const express = require("express");
  7. const http = require("http");
  8. const socketio = require('socket.io');
  9. let temporaryAutosuggestCache = {};
  10. let temporaryAutosuggestMap = {};
  11. module.exports = class extends coreClass {
  12. constructor(name, moduleManager) {
  13. super(name, moduleManager);
  14. this.dependsOn = ["mongo"];
  15. }
  16. initialize() {
  17. return new Promise(resolve => {
  18. this.setStage(1);
  19. const app = express();
  20. const server = http.createServer(app);
  21. const io = socketio(server);
  22. this.mongo = this.moduleManager.modules["mongo"];
  23. this.mongo.models.accountSchema.find({}, null, { sort: "-version", limit: 1 }, (err, res) => {
  24. if (!err) {
  25. res[0].fields.forEach(field => {
  26. field.fieldTypes.forEach(fieldType => {
  27. if (fieldType.type === "text" && fieldType.autosuggestGroup) {
  28. temporaryAutosuggestMap[`${field.fieldId}.${fieldType.fieldTypeId}`] = fieldType.autosuggestGroup;
  29. temporaryAutosuggestCache[fieldType.autosuggestGroup] = [];
  30. }
  31. });
  32. });
  33. this.mongo.models.account.find({}, (err, accounts) => {
  34. if (!err) {
  35. accounts.forEach(account => {
  36. Object.keys(temporaryAutosuggestMap).forEach(key => {
  37. let autosuggestGroup = temporaryAutosuggestMap[key];
  38. let fieldId = key.split(".")[0];
  39. let fieldTypeId = key.split(".")[1];
  40. account.fields[fieldId].forEach(field => {
  41. if (temporaryAutosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
  42. temporaryAutosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
  43. });
  44. });
  45. });
  46. console.log(temporaryAutosuggestCache);
  47. console.log(temporaryAutosuggestMap);
  48. }
  49. });
  50. }
  51. });
  52. this.handlers = {
  53. "getAutosuggest": cb => {
  54. cb({
  55. autosuggest: temporaryAutosuggestCache
  56. });
  57. },
  58. "getAccounts": cb => {
  59. this.mongo.models.account.find({}, (err, accounts) => {
  60. if (err)
  61. return cb({
  62. status: "failure",
  63. err: err
  64. });
  65. else
  66. return cb({
  67. status: "success",
  68. accounts
  69. });
  70. });
  71. },
  72. "getAccount": (cb, accountId) => {
  73. this.mongo.models.account.findById(accountId, (err, account) => {
  74. if (err || !account)
  75. return cb({
  76. status: "failure",
  77. err: err
  78. });
  79. else
  80. return cb({
  81. status: "success",
  82. account
  83. });
  84. });
  85. },
  86. "addAccount": (cb, account) => {
  87. this.mongo.models.account.create(account, (err) => {
  88. if (err)
  89. return cb({
  90. status: "failure",
  91. err: err
  92. });
  93. else {
  94. Object.keys(temporaryAutosuggestMap).forEach(key => {
  95. let autosuggestGroup = temporaryAutosuggestMap[key];
  96. let fieldId = key.split(".")[0];
  97. let fieldTypeId = key.split(".")[1];
  98. account.fields[fieldId].forEach(field => {
  99. if (temporaryAutosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
  100. temporaryAutosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
  101. });
  102. });
  103. console.log("Added account!");
  104. return cb({
  105. status: "success"
  106. });
  107. }
  108. });
  109. },
  110. "editAccount": (cb, accountId, account) => {
  111. this.mongo.models.account.updateOne({ _id: accountId }, account, (err) => {
  112. if (err)
  113. return cb({
  114. status: "failure",
  115. err: err
  116. });
  117. else {
  118. Object.keys(temporaryAutosuggestMap).forEach(key => {
  119. let autosuggestGroup = temporaryAutosuggestMap[key];
  120. let fieldId = key.split(".")[0];
  121. let fieldTypeId = key.split(".")[1];
  122. account.fields[fieldId].forEach(field => {
  123. if (temporaryAutosuggestCache[autosuggestGroup].indexOf(field[fieldTypeId]) === -1)
  124. temporaryAutosuggestCache[autosuggestGroup].push(field[fieldTypeId]);
  125. });
  126. });
  127. console.log("Editted account!");
  128. return cb({
  129. status: "success"
  130. });
  131. }
  132. });
  133. },
  134. "getAccountSchema": cb => {
  135. this.mongo.models.accountSchema.find({}, null, { sort: "-version", limit: 1 }, (err, res) => {
  136. if (err || !res || res.length !== 1)
  137. return cb({
  138. status: "failure",
  139. message: "Something went wrong."
  140. });
  141. else
  142. return cb({
  143. status: "success",
  144. schema: res[0]
  145. });
  146. });
  147. },
  148. "importAccountSchema": (cb, name) => {
  149. let schema = require(`../schemas/${name}`);
  150. this.mongo.models.accountSchema.create(schema, (err) => {
  151. if (err)
  152. return cb({
  153. status: "failure",
  154. err: err
  155. });
  156. else
  157. return cb({
  158. status: "success"
  159. });
  160. });
  161. }
  162. }
  163. io.on('connection', (socket) => {
  164. console.log('a user connected');
  165. Object.keys(this.handlers).forEach(handlerName => {
  166. socket.on(handlerName, (...args) => {
  167. let cb = args[args.length - 1];
  168. if (typeof cb !== "function")
  169. cb = () => {
  170. this.logger.info("IO_MODULE", `There was no callback provided for ${name}.`);
  171. }
  172. else args.pop();
  173. this.handlers[handlerName].apply(null, [cb].concat(args));
  174. });
  175. });
  176. });
  177. server.listen(8080, function(){
  178. console.log('listening on *:8080');
  179. resolve();
  180. });
  181. });
  182. }
  183. }