utils.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. const CoreClass = require("../core.js");
  2. const config = require("config");
  3. const async = require("async");
  4. const request = require("request");
  5. const crypto = require("crypto");
  6. let youtubeRequestCallbacks = [];
  7. let youtubeRequestsPending = 0;
  8. let youtubeRequestsActive = false;
  9. class UtilsModule extends CoreClass {
  10. constructor() {
  11. super("utils");
  12. }
  13. initialize() {
  14. return new Promise((resolve, reject) => {
  15. this.io = this.moduleManager.modules["io"];
  16. this.db = this.moduleManager.modules["db"];
  17. this.spotify = this.moduleManager.modules["spotify"];
  18. this.cache = this.moduleManager.modules["cache"];
  19. resolve();
  20. });
  21. }
  22. PARSE_COOKIES(payload) {
  23. //cookieString
  24. return new Promise((resolve, reject) => {
  25. let cookies = {};
  26. if (typeof payload.cookieString !== "string") return reject("Cookie string is not a string");
  27. payload.cookieString.split("; ").map((cookie) => {
  28. cookies[
  29. cookie.substring(0, cookie.indexOf("="))
  30. ] = cookie.substring(cookie.indexOf("=") + 1, cookie.length);
  31. });
  32. resolve(cookies);
  33. });
  34. }
  35. // COOKIES_TO_STRING() {//cookies
  36. // return new Promise((resolve, reject) => {
  37. // let newCookie = [];
  38. // for (let prop in cookie) {
  39. // newCookie.push(prop + "=" + cookie[prop]);
  40. // }
  41. // return newCookie.join("; ");
  42. // });
  43. // }
  44. REMOVE_COOKIE(payload) {
  45. //cookieString, cookieName
  46. return new Promise(async (resolve, reject) => {
  47. var cookies = await this.runJob("PARSE_COOKIES", {
  48. cookieString: payload.cookieString,
  49. });
  50. delete cookies[payload.cookieName];
  51. resolve(this.toString(cookies));
  52. });
  53. }
  54. HTML_ENTITIES(payload) {
  55. //str
  56. return new Promise((resolve, reject) => {
  57. resolve(
  58. String(payload.str)
  59. .replace(/&/g, "&")
  60. .replace(/</g, "&lt;")
  61. .replace(/>/g, "&gt;")
  62. .replace(/"/g, "&quot;")
  63. );
  64. });
  65. }
  66. GENERATE_RANDOM_STRING(payload) {
  67. //length
  68. return new Promise(async (resolve, reject) => {
  69. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split(
  70. ""
  71. );
  72. let result = [];
  73. for (let i = 0; i < payload.length; i++) {
  74. result.push(
  75. chars[
  76. await this.runJob("GET_RANDOM_NUMBER", {
  77. min: 0,
  78. max: chars.length - 1,
  79. })
  80. ]
  81. );
  82. }
  83. resolve(result.join(""));
  84. });
  85. }
  86. GET_SOCKET_FROM_ID(payload) {
  87. //socketId
  88. return new Promise(async (resolve, reject) => {
  89. let io = await this.io.runJob("IO", {});
  90. resolve(io.sockets.sockets[payload.socketId]);
  91. });
  92. }
  93. GET_RANDOM_NUMBER(payload) {
  94. //min, max
  95. return new Promise((resolve, reject) => {
  96. resolve(
  97. Math.floor(Math.random() * (payload.max - payload.min + 1)) +
  98. payload.min
  99. );
  100. });
  101. }
  102. CONVERT_TIME(payload) {
  103. //duration
  104. return new Promise((resolve, reject) => {
  105. let duration = payload.duration;
  106. let a = duration.match(/\d+/g);
  107. if (
  108. duration.indexOf("M") >= 0 &&
  109. duration.indexOf("H") == -1 &&
  110. duration.indexOf("S") == -1
  111. ) {
  112. a = [0, a[0], 0];
  113. }
  114. if (duration.indexOf("H") >= 0 && duration.indexOf("M") == -1) {
  115. a = [a[0], 0, a[1]];
  116. }
  117. if (
  118. duration.indexOf("H") >= 0 &&
  119. duration.indexOf("M") == -1 &&
  120. duration.indexOf("S") == -1
  121. ) {
  122. a = [a[0], 0, 0];
  123. }
  124. duration = 0;
  125. if (a.length == 3) {
  126. duration = duration + parseInt(a[0]) * 3600;
  127. duration = duration + parseInt(a[1]) * 60;
  128. duration = duration + parseInt(a[2]);
  129. }
  130. if (a.length == 2) {
  131. duration = duration + parseInt(a[0]) * 60;
  132. duration = duration + parseInt(a[1]);
  133. }
  134. if (a.length == 1) {
  135. duration = duration + parseInt(a[0]);
  136. }
  137. let hours = Math.floor(duration / 3600);
  138. let minutes = Math.floor((duration % 3600) / 60);
  139. let seconds = Math.floor((duration % 3600) % 60);
  140. resolve(
  141. (hours < 10 ? "0" + hours + ":" : hours + ":") +
  142. (minutes < 10 ? "0" + minutes + ":" : minutes + ":") +
  143. (seconds < 10 ? "0" + seconds : seconds)
  144. );
  145. });
  146. }
  147. GUID(payload) {
  148. return new Promise((resolve, reject) => {
  149. resolve(
  150. [1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1]
  151. .map((b) =>
  152. b
  153. ? Math.floor((1 + Math.random()) * 0x10000)
  154. .toString(16)
  155. .substring(1)
  156. : "-"
  157. )
  158. .join("")
  159. );
  160. });
  161. }
  162. SOCKET_FROM_SESSION(payload) {
  163. //socketId
  164. return new Promise(async (resolve, reject) => {
  165. let io = await this.io.runJob("IO", {});
  166. let ns = io.of("/");
  167. if (ns) {
  168. resolve(ns.connected[payload.socketId]);
  169. }
  170. });
  171. }
  172. SOCKETS_FROM_SESSION_ID(payload) {
  173. return new Promise(async (resolve, reject) => {
  174. let io = await this.io.runJob("IO", {});
  175. let ns = io.of("/");
  176. let sockets = [];
  177. if (ns) {
  178. async.each(
  179. Object.keys(ns.connected),
  180. (id, next) => {
  181. let session = ns.connected[id].session;
  182. if (session.sessionId === payload.sessionId)
  183. sockets.push(session.sessionId);
  184. next();
  185. },
  186. () => {
  187. resolve({ sockets });
  188. }
  189. );
  190. }
  191. });
  192. }
  193. SOCKETS_FROM_USER(payload) {
  194. return new Promise(async (resolve, reject) => {
  195. let io = await this.io.runJob("IO", {});
  196. let ns = io.of("/");
  197. let sockets = [];
  198. if (ns) {
  199. async.each(
  200. Object.keys(ns.connected),
  201. (id, next) => {
  202. let session = ns.connected[id].session;
  203. this.cache
  204. .runJob("HGET", {
  205. table: "sessions",
  206. key: session.sessionId,
  207. })
  208. .then((session) => {
  209. if (
  210. session &&
  211. session.userId === payload.userId
  212. )
  213. sockets.push(ns.connected[id]);
  214. next();
  215. })
  216. .catch(err => {
  217. next(err);
  218. });
  219. },
  220. err => {
  221. if (err) return reject(err);
  222. return resolve({ sockets });
  223. }
  224. );
  225. }
  226. });
  227. }
  228. SOCKETS_FROM_IP(payload) {
  229. return new Promise(async (resolve, reject) => {
  230. let io = await this.io.runJob("IO", {});
  231. let ns = io.of("/");
  232. let sockets = [];
  233. if (ns) {
  234. async.each(
  235. Object.keys(ns.connected),
  236. (id, next) => {
  237. let session = ns.connected[id].session;
  238. this.cache
  239. .runJob("HGET", {
  240. table: "sessions",
  241. key: session.sessionId,
  242. })
  243. .then((session) => {
  244. if (
  245. session &&
  246. ns.connected[id].ip === payload.ip
  247. )
  248. sockets.push(ns.connected[id]);
  249. next();
  250. })
  251. .catch((err) => {
  252. next();
  253. });
  254. },
  255. () => {
  256. resolve({ sockets });
  257. }
  258. );
  259. }
  260. });
  261. }
  262. SOCKETS_FROM_USER_WITHOUT_CACHE(payload) {
  263. return new Promise(async (resolve, reject) => {
  264. let io = await this.io.runJob("IO", {});
  265. let ns = io.of("/");
  266. let sockets = [];
  267. if (ns) {
  268. async.each(
  269. Object.keys(ns.connected),
  270. (id, next) => {
  271. let session = ns.connected[id].session;
  272. if (session.userId === payload.userId)
  273. sockets.push(ns.connected[id]);
  274. next();
  275. },
  276. () => {
  277. resolve({ sockets });
  278. }
  279. );
  280. }
  281. });
  282. }
  283. SOCKET_LEAVE_ROOMS(payload) {
  284. //socketId
  285. return new Promise(async (resolve, reject) => {
  286. let socket = await this.runJob("SOCKET_FROM_SESSION", {
  287. socketId: payload.socketId,
  288. });
  289. let rooms = socket.rooms;
  290. for (let room in rooms) {
  291. socket.leave(room);
  292. }
  293. resolve();
  294. });
  295. }
  296. SOCKET_JOIN_ROOM(payload) {
  297. //socketId, room
  298. return new Promise(async (resolve, reject) => {
  299. let socket = await this.runJob("SOCKET_FROM_SESSION", {
  300. socketId: payload.socketId,
  301. });
  302. let rooms = socket.rooms;
  303. for (let room in rooms) {
  304. socket.leave(room);
  305. }
  306. socket.join(payload.room);
  307. resolve();
  308. });
  309. }
  310. SOCKET_JOIN_SONG_ROOM(payload) {
  311. //socketId, room
  312. return new Promise(async (resolve, reject) => {
  313. let socket = await this.runJob("SOCKET_FROM_SESSION", {
  314. socketId: payload.socketId,
  315. });
  316. let rooms = socket.rooms;
  317. for (let room in rooms) {
  318. if (room.indexOf("song.") !== -1) socket.leave(rooms);
  319. }
  320. socket.join(payload.room);
  321. resolve();
  322. });
  323. }
  324. SOCKETS_JOIN_SONG_ROOM(payload) {
  325. //sockets, room
  326. return new Promise((resolve, reject) => {
  327. for (let id in payload.sockets) {
  328. let socket = payload.sockets[id];
  329. let rooms = socket.rooms;
  330. for (let room in rooms) {
  331. if (room.indexOf("song.") !== -1) socket.leave(room);
  332. }
  333. socket.join(payload.room);
  334. }
  335. resolve();
  336. });
  337. }
  338. SOCKETS_LEAVE_SONG_ROOMS(payload) {
  339. //sockets
  340. return new Promise((resolve, reject) => {
  341. for (let id in payload.sockets) {
  342. let socket = payload.sockets[id];
  343. let rooms = socket.rooms;
  344. for (let room in rooms) {
  345. if (room.indexOf("song.") !== -1) socket.leave(room);
  346. }
  347. }
  348. resolve();
  349. });
  350. }
  351. EMIT_TO_ROOM(payload) {
  352. //room, ...args
  353. return new Promise(async (resolve, reject) => {
  354. let io = await this.io.runJob("IO", {});
  355. let sockets = io.sockets.sockets;
  356. for (let id in sockets) {
  357. let socket = sockets[id];
  358. if (socket.rooms[payload.room]) {
  359. socket.emit.apply(socket, payload.args);
  360. }
  361. }
  362. resolve();
  363. });
  364. }
  365. GET_ROOM_SOCKETS(payload) {
  366. //room
  367. return new Promise(async (resolve, reject) => {
  368. let io = await this.io.runJob("IO", {});
  369. let sockets = io.sockets.sockets;
  370. let roomSockets = [];
  371. for (let id in sockets) {
  372. let socket = sockets[id];
  373. if (socket.rooms[payload.room]) roomSockets.push(socket);
  374. }
  375. resolve(roomSockets);
  376. });
  377. }
  378. GET_SONG_FROM_YOUTUBE(payload) {
  379. //songId, cb
  380. return new Promise((resolve, reject) => {
  381. youtubeRequestCallbacks.push({
  382. cb: (test) => {
  383. youtubeRequestsActive = true;
  384. const youtubeParams = [
  385. "part=snippet,contentDetails,statistics,status",
  386. `id=${encodeURIComponent(payload.songId)}`,
  387. `key=${config.get("apis.youtube.key")}`,
  388. ].join("&");
  389. request(
  390. `https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`,
  391. (err, res, body) => {
  392. youtubeRequestCallbacks.splice(0, 1);
  393. if (youtubeRequestCallbacks.length > 0) {
  394. youtubeRequestCallbacks[0].cb(
  395. youtubeRequestCallbacks[0].songId
  396. );
  397. } else youtubeRequestsActive = false;
  398. if (err) {
  399. console.error(err);
  400. return null;
  401. }
  402. body = JSON.parse(body);
  403. if (body.error) {
  404. console.log(
  405. "ERROR",
  406. "GET_SONG_FROM_YOUTUBE",
  407. `${body.error.message}`
  408. );
  409. return reject(new Error("An error has occured. Please try again later."));
  410. }
  411. if (body.items[0] === undefined)
  412. return reject(new Error("The specified video does not exist or cannot be publicly accessed."));
  413. //TODO Clean up duration converter
  414. let dur = body.items[0].contentDetails.duration;
  415. dur = dur.replace("PT", "");
  416. let duration = 0;
  417. dur = dur.replace(/([\d]*)H/, (v, v2) => {
  418. v2 = Number(v2);
  419. duration = v2 * 60 * 60;
  420. return "";
  421. });
  422. dur = dur.replace(/([\d]*)M/, (v, v2) => {
  423. v2 = Number(v2);
  424. duration += v2 * 60;
  425. return "";
  426. });
  427. dur = dur.replace(/([\d]*)S/, (v, v2) => {
  428. v2 = Number(v2);
  429. duration += v2;
  430. return "";
  431. });
  432. let song = {
  433. songId: body.items[0].id,
  434. title: body.items[0].snippet.title,
  435. duration,
  436. };
  437. resolve({ song });
  438. }
  439. );
  440. },
  441. songId: payload.songId,
  442. });
  443. if (!youtubeRequestsActive) {
  444. youtubeRequestCallbacks[0].cb(
  445. youtubeRequestCallbacks[0].songId
  446. );
  447. }
  448. });
  449. }
  450. FILTER_MUSIC_VIDEOS_YOUTUBE(payload) {
  451. //videoIds, cb
  452. return new Promise((resolve, reject) => {
  453. function getNextPage(cb2) {
  454. let localVideoIds = payload.videoIds.splice(0, 50);
  455. const youtubeParams = [
  456. "part=topicDetails",
  457. `id=${encodeURIComponent(localVideoIds.join(","))}`,
  458. `maxResults=50`,
  459. `key=${config.get("apis.youtube.key")}`,
  460. ].join("&");
  461. request(
  462. `https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`,
  463. async (err, res, body) => {
  464. if (err) {
  465. console.error(err);
  466. return next("Failed to find playlist from YouTube");
  467. }
  468. body = JSON.parse(body);
  469. if (body.error) {
  470. console.log(
  471. "ERROR",
  472. "FILTER_MUSIC_VIDEOS_YOUTUBE",
  473. `${body.error.message}`
  474. );
  475. return reject(new Error("An error has occured. Please try again later."));
  476. }
  477. let songIds = [];
  478. body.items.forEach((item) => {
  479. const songId = item.id;
  480. if (!item.topicDetails) return;
  481. else if (
  482. item.topicDetails.relevantTopicIds.indexOf(
  483. "/m/04rlf"
  484. ) !== -1
  485. ) {
  486. songIds.push(songId);
  487. }
  488. });
  489. if (payload.videoIds.length > 0) {
  490. getNextPage((newSongIds) => {
  491. cb2(songIds.concat(newSongIds));
  492. });
  493. } else cb2(songIds);
  494. }
  495. );
  496. }
  497. if (payload.videoIds.length === 0) resolve({ songIds: [] });
  498. else
  499. getNextPage((songIds) => {
  500. resolve({ songIds });
  501. });
  502. });
  503. }
  504. GET_PLAYLIST_FROM_YOUTUBE(payload) {
  505. //url, musicOnly, cb
  506. return new Promise((resolve, reject) => {
  507. let local = this;
  508. let name = "list".replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  509. var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
  510. let playlistId = regex.exec(payload.url)[1];
  511. function getPage(pageToken, songs) {
  512. let nextPageToken = pageToken ? `pageToken=${pageToken}` : "";
  513. const youtubeParams = [
  514. "part=contentDetails",
  515. `playlistId=${encodeURIComponent(playlistId)}`,
  516. `maxResults=50`,
  517. `key=${config.get("apis.youtube.key")}`,
  518. nextPageToken,
  519. ].join("&");
  520. request(
  521. `https://www.googleapis.com/youtube/v3/playlistItems?${youtubeParams}`,
  522. async (err, res, body) => {
  523. if (err) {
  524. console.error(err);
  525. return next("Failed to find playlist from YouTube");
  526. }
  527. body = JSON.parse(body);
  528. if (body.error) {
  529. console.log(
  530. "ERROR",
  531. "GET_PLAYLIST_FROM_YOUTUBE",
  532. `${body.error.message}`
  533. );
  534. return reject(new Error("An error has occured. Please try again later."));
  535. }
  536. songs = songs.concat(body.items);
  537. if (body.nextPageToken)
  538. getPage(body.nextPageToken, songs);
  539. else {
  540. songs = songs.map(
  541. (song) => song.contentDetails.videoId
  542. );
  543. if (!payload.musicOnly) resolve({ songs });
  544. else {
  545. local
  546. .runJob("FILTER_MUSIC_VIDEOS_YOUTUBE", {
  547. videoIds: songs.slice(),
  548. })
  549. .then((filteredSongs) => {
  550. resolve({ filteredSongs, songs });
  551. });
  552. }
  553. }
  554. }
  555. );
  556. }
  557. getPage(null, []);
  558. });
  559. }
  560. GET_SONG_FROM_SPOTIFY(payload) {
  561. //song, cb
  562. return new Promise(async (resolve, reject) => {
  563. if (!config.get("apis.spotify.enabled"))
  564. return reject(new Error("Spotify is not enabled."));
  565. const song = Object.assign({}, payload.song);
  566. const spotifyParams = [
  567. `q=${encodeURIComponent(payload.song.title)}`,
  568. `type=track`,
  569. ].join("&");
  570. const token = await this.spotify.runJob("GET_TOKEN", {});
  571. const options = {
  572. url: `https://api.spotify.com/v1/search?${spotifyParams}`,
  573. headers: {
  574. Authorization: `Bearer ${token}`,
  575. },
  576. };
  577. request(options, (err, res, body) => {
  578. if (err) console.error(err);
  579. body = JSON.parse(body);
  580. if (body.error) console.error(body.error);
  581. durationArtistLoop: for (let i in body) {
  582. let items = body[i].items;
  583. for (let j in items) {
  584. let item = items[j];
  585. let hasArtist = false;
  586. for (let k = 0; k < item.artists.length; k++) {
  587. let artist = item.artists[k];
  588. if (song.title.indexOf(artist.name) !== -1)
  589. hasArtist = true;
  590. }
  591. if (hasArtist && song.title.indexOf(item.name) !== -1) {
  592. song.duration = item.duration_ms / 1000;
  593. song.artists = item.artists.map((artist) => {
  594. return artist.name;
  595. });
  596. song.title = item.name;
  597. song.explicit = item.explicit;
  598. song.thumbnail = item.album.images[1].url;
  599. break durationArtistLoop;
  600. }
  601. }
  602. }
  603. resolve({ song });
  604. });
  605. });
  606. }
  607. GET_SONGS_FROM_SPOTIFY(payload) {
  608. //title, artist, cb
  609. return new Promise(async (resolve, reject) => {
  610. if (!config.get("apis.spotify.enabled"))
  611. return reject(new Error("Spotify is not enabled."));
  612. const spotifyParams = [
  613. `q=${encodeURIComponent(payload.title)}`,
  614. `type=track`,
  615. ].join("&");
  616. const token = await this.spotify.runJob("GET_TOKEN", {});
  617. const options = {
  618. url: `https://api.spotify.com/v1/search?${spotifyParams}`,
  619. headers: {
  620. Authorization: `Bearer ${token}`,
  621. },
  622. };
  623. request(options, (err, res, body) => {
  624. if (err) return console.error(err);
  625. body = JSON.parse(body);
  626. if (body.error) return console.error(body.error);
  627. let songs = [];
  628. for (let i in body) {
  629. let items = body[i].items;
  630. for (let j in items) {
  631. let item = items[j];
  632. let hasArtist = false;
  633. for (let k = 0; k < item.artists.length; k++) {
  634. let localArtist = item.artists[k];
  635. if (
  636. payload.artist.toLowerCase() ===
  637. localArtist.name.toLowerCase()
  638. )
  639. hasArtist = true;
  640. }
  641. if (
  642. hasArtist &&
  643. (payload.title.indexOf(item.name) !== -1 ||
  644. item.name.indexOf(payload.title) !== -1)
  645. ) {
  646. let song = {};
  647. song.duration = item.duration_ms / 1000;
  648. song.artists = item.artists.map((artist) => {
  649. return artist.name;
  650. });
  651. song.title = item.name;
  652. song.explicit = item.explicit;
  653. song.thumbnail = item.album.images[1].url;
  654. songs.push(song);
  655. }
  656. }
  657. }
  658. resolve({ songs });
  659. });
  660. });
  661. }
  662. SHUFFLE(payload) {
  663. //array
  664. return new Promise((resolve, reject) => {
  665. const array = payload.array.slice();
  666. let currentIndex = payload.array.length,
  667. temporaryValue,
  668. randomIndex;
  669. // While there remain elements to shuffle...
  670. while (0 !== currentIndex) {
  671. // Pick a remaining element...
  672. randomIndex = Math.floor(Math.random() * currentIndex);
  673. currentIndex -= 1;
  674. // And swap it with the current element.
  675. temporaryValue = array[currentIndex];
  676. array[currentIndex] = array[randomIndex];
  677. array[randomIndex] = temporaryValue;
  678. }
  679. resolve({ array });
  680. });
  681. }
  682. GET_ERROR(payload) {
  683. //err
  684. return new Promise((resolve, reject) => {
  685. let error = "An error occurred.";
  686. if (typeof payload.error === "string") error = payload.error;
  687. else if (payload.error.message) {
  688. if (payload.error.message !== "Validation failed")
  689. error = payload.error.message;
  690. else
  691. error =
  692. payload.error.errors[Object.keys(payload.error.errors)]
  693. .message;
  694. }
  695. resolve(error);
  696. });
  697. }
  698. CREATE_GRAVATAR(payload) {
  699. //email
  700. return new Promise((resolve, reject) => {
  701. const hash = crypto
  702. .createHash("md5")
  703. .update(payload.email)
  704. .digest("hex");
  705. resolve(`https://www.gravatar.com/avatar/${hash}`);
  706. });
  707. }
  708. DEBUG(payload) {
  709. return new Promise((resolve, reject) => {
  710. resolve();
  711. });
  712. }
  713. }
  714. module.exports = new UtilsModule();