api.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/playlist/:playlistId", async (req, res) => {
  108. const { playlistId } = req.params;
  109. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" });
  110. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId })
  111. .then(playlist => {
  112. if (playlist.privacy === "public")
  113. res.json({ status: "success", playlist });
  114. else {
  115. isLoggedIn(req, res, () => {
  116. if (playlist.createdBy === req.session.userId)
  117. res.json({ status: "success", playlist });
  118. else {
  119. userModel.findOne({ _id: req.session.userId }, (err, user) => {
  120. if (err) res.json({ status: "error", message: err.message });
  121. else if (user.role === "admin")
  122. res.json({ status: "success", playlist });
  123. else res.json({ status: "error", message: "You're not allowed to download this playlist." });
  124. });
  125. }
  126. })
  127. }
  128. })
  129. .catch(err => {
  130. res.json({ status: "error", message: err.message });
  131. });
  132. });
  133. if (config.get("debug.stationIssue")) {
  134. response.app.get("/debug_station", async (req, res) => {
  135. const responseObject = {};
  136. const stationModel = await DBModule.runJob("GET_MODEL", {
  137. modelName: "station"
  138. });
  139. async.waterfall(
  140. [
  141. next => {
  142. stationModel.find({}, next);
  143. },
  144. (stations, next) => {
  145. responseObject.mongo = {
  146. stations
  147. };
  148. next();
  149. },
  150. next => {
  151. CacheModule.runJob("HGETALL", { table: "stations" })
  152. .then(stations => {
  153. next(null, stations);
  154. })
  155. .catch(err => {
  156. console.log(err);
  157. next(err);
  158. });
  159. },
  160. (stations, next) => {
  161. responseObject.redis = {
  162. stations
  163. };
  164. next();
  165. },
  166. next => {
  167. responseObject.cryptoExamples = {};
  168. responseObject.mongo.stations.forEach(station => {
  169. const payloadName = `stations.nextSong?id=${station._id}`;
  170. responseObject.cryptoExamples[station._id] = crypto
  171. .createHash("md5")
  172. .update(`_notification:${payloadName}_`)
  173. .digest("hex");
  174. });
  175. next();
  176. },
  177. next => {
  178. NotificationsModule.pub.keys("*", next);
  179. },
  180. (redisKeys, next) => {
  181. responseObject.redis = {
  182. ...redisKeys,
  183. ttl: {}
  184. };
  185. async.eachLimit(
  186. redisKeys,
  187. 1,
  188. (redisKey, next) => {
  189. NotificationsModule.pub.ttl(redisKey, (err, ttl) => {
  190. responseObject.redis.ttl[redisKey] = ttl;
  191. next(err);
  192. });
  193. },
  194. next
  195. );
  196. },
  197. next => {
  198. responseObject.debugLogs = this.moduleManager.debugLogs.stationIssue;
  199. next();
  200. },
  201. next => {
  202. responseObject.debugJobs = this.moduleManager.debugJobs;
  203. next();
  204. }
  205. ],
  206. err => {
  207. if (err) {
  208. console.log(err);
  209. return res.json({
  210. error: err,
  211. objectSoFar: responseObject
  212. });
  213. }
  214. const responseJson = JSON.stringify(responseObject, (key, value) => {
  215. if (
  216. key === "module" ||
  217. key === "task" ||
  218. key === "onFinish" ||
  219. key === "server" ||
  220. key === "nsp" ||
  221. key === "socket" ||
  222. key === "res" ||
  223. key === "client" ||
  224. key === "_idleNext" ||
  225. key === "_idlePrev"
  226. ) {
  227. return undefined;
  228. }
  229. if (key === "parentJob" && value) return value.toString();
  230. return value;
  231. });
  232. return res.end(responseJson);
  233. }
  234. );
  235. });
  236. }
  237. resolve();
  238. })
  239. .catch(err => {
  240. reject(err);
  241. });
  242. });
  243. }
  244. }
  245. export default new _APIModule();