api.js 7.2 KB

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