api.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import config from "config";
  2. import async from "async";
  3. import crypto from "crypto";
  4. import CoreClass from "../core";
  5. let AppModule;
  6. let DBModule;
  7. let PlaylistsModule;
  8. let UtilsModule;
  9. let PunishmentsModule;
  10. let CacheModule;
  11. let NotificationsModule;
  12. class _APIModule extends CoreClass {
  13. // eslint-disable-next-line require-jsdoc
  14. constructor() {
  15. super("api");
  16. }
  17. /**
  18. * Initialises the api module
  19. *
  20. * @returns {Promise} - returns promise (reject, resolve)
  21. */
  22. initialize() {
  23. return new Promise((resolve, reject) => {
  24. AppModule = this.moduleManager.modules.app;
  25. DBModule = this.moduleManager.modules.db;
  26. PlaylistsModule = this.moduleManager.modules.playlists;
  27. UtilsModule = this.moduleManager.modules.utils;
  28. PunishmentsModule = this.moduleManager.modules.punishments;
  29. CacheModule = this.moduleManager.modules.cache;
  30. NotificationsModule = this.moduleManager.modules.notifications;
  31. const SIDname = config.get("cookie.SIDname");
  32. const isLoggedIn = (req, res, next) => {
  33. let SID;
  34. async.waterfall(
  35. [
  36. next => {
  37. UtilsModule.runJob("PARSE_COOKIES", {
  38. cookieString: req.headers.cookie
  39. })
  40. .then(res => {
  41. SID = res[SIDname];
  42. next(null);
  43. })
  44. .catch(next);
  45. },
  46. next => {
  47. if (!SID) return next("No SID.");
  48. return next();
  49. },
  50. next => {
  51. CacheModule.runJob("HGET", { table: "sessions", key: SID }).then(session =>
  52. next(null, session)
  53. );
  54. },
  55. (session, next) => {
  56. if (!session) return next("No session found.");
  57. session.refreshDate = Date.now();
  58. req.session = session;
  59. return CacheModule.runJob("HSET", {
  60. table: "sessions",
  61. key: SID,
  62. value: session
  63. }).then(session => {
  64. next(null, session);
  65. });
  66. },
  67. (res, next) => {
  68. // check if a session's user / IP is banned
  69. PunishmentsModule.runJob("GET_PUNISHMENTS", {})
  70. .then(punishments => {
  71. const isLoggedIn = !!(req.session && req.session.refreshDate);
  72. const userId = isLoggedIn ? req.session.userId : null;
  73. const banishment = { banned: false, ban: 0 };
  74. punishments.forEach(punishment => {
  75. if (punishment.expiresAt > banishment.ban) banishment.ban = punishment;
  76. if (
  77. punishment.type === "banUserId" &&
  78. isLoggedIn &&
  79. punishment.value === userId
  80. )
  81. banishment.banned = true;
  82. if (punishment.type === "banUserIp" && punishment.value === req.ip)
  83. banishment.banned = true;
  84. });
  85. req.banishment = banishment;
  86. next();
  87. })
  88. .catch(() => {
  89. next();
  90. });
  91. }
  92. ],
  93. err => {
  94. if (err) return res.json({ status: "error", message: "You are not logged in" });
  95. return next();
  96. }
  97. );
  98. };
  99. AppModule.runJob("GET_APP", {})
  100. .then(response => {
  101. response.app.get("/", (req, res) => {
  102. res.json({
  103. status: "success",
  104. message: "Coming Soon"
  105. });
  106. });
  107. response.app.get("/export/privatePlaylist/:playlistId", isLoggedIn, (req, res) => {
  108. const { playlistId } = req.params;
  109. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId })
  110. .then(playlist => {
  111. if (playlist.createdBy === req.session.userId)
  112. res.json({ status: "success", playlist });
  113. else res.json({ status: "error", message: "You're not the owner." });
  114. })
  115. .catch(err => {
  116. res.json({ status: "error", message: err.message });
  117. });
  118. });
  119. if (config.get("debug.stationIssue")) {
  120. response.app.get("/debug_station", async (req, res) => {
  121. const responseObject = {};
  122. const stationModel = await DBModule.runJob("GET_MODEL", {
  123. modelName: "station"
  124. });
  125. async.waterfall(
  126. [
  127. next => {
  128. stationModel.find({}, next);
  129. },
  130. (stations, next) => {
  131. responseObject.mongo = {
  132. stations
  133. };
  134. next();
  135. },
  136. next => {
  137. CacheModule.runJob("HGETALL", { table: "stations" })
  138. .then(stations => {
  139. next(null, stations);
  140. })
  141. .catch(err => {
  142. console.log(err);
  143. next(err);
  144. });
  145. },
  146. (stations, next) => {
  147. responseObject.redis = {
  148. stations
  149. };
  150. next();
  151. },
  152. next => {
  153. responseObject.cryptoExamples = {};
  154. responseObject.mongo.stations.forEach(station => {
  155. const payloadName = `stations.nextSong?id=${station._id}`;
  156. responseObject.cryptoExamples[station._id] = crypto
  157. .createHash("md5")
  158. .update(`_notification:${payloadName}_`)
  159. .digest("hex");
  160. });
  161. next();
  162. },
  163. next => {
  164. NotificationsModule.pub.keys("*", next);
  165. },
  166. (redisKeys, next) => {
  167. responseObject.redis = {
  168. ...redisKeys,
  169. ttl: {}
  170. };
  171. async.eachLimit(
  172. redisKeys,
  173. 1,
  174. (redisKey, next) => {
  175. NotificationsModule.pub.ttl(redisKey, (err, ttl) => {
  176. responseObject.redis.ttl[redisKey] = ttl;
  177. next(err);
  178. });
  179. },
  180. next
  181. );
  182. },
  183. next => {
  184. responseObject.debugLogs = this.moduleManager.debugLogs.stationIssue;
  185. next();
  186. },
  187. next => {
  188. responseObject.debugJobs = this.moduleManager.debugJobs;
  189. next();
  190. }
  191. ],
  192. err => {
  193. if (err) {
  194. console.log(err);
  195. return res.json({
  196. error: err,
  197. objectSoFar: responseObject
  198. });
  199. }
  200. const responseJson = JSON.stringify(responseObject, (key, value) => {
  201. if (
  202. key === "module" ||
  203. key === "task" ||
  204. key === "onFinish" ||
  205. key === "server" ||
  206. key === "nsp" ||
  207. key === "socket" ||
  208. key === "res" ||
  209. key === "client" ||
  210. key === "_idleNext" ||
  211. key === "_idlePrev"
  212. ) {
  213. return undefined;
  214. }
  215. if (key === "parentJob" && value) return value.toString();
  216. return value;
  217. });
  218. return res.end(responseJson);
  219. }
  220. );
  221. });
  222. }
  223. resolve();
  224. })
  225. .catch(err => {
  226. reject(err);
  227. });
  228. });
  229. }
  230. }
  231. export default new _APIModule();