api.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.keys("*", next);
  182. },
  183. (redisKeys, next) => {
  184. responseObject.redis = {
  185. ...redisKeys,
  186. ttl: {}
  187. };
  188. async.eachLimit(
  189. redisKeys,
  190. 1,
  191. (redisKey, next) => {
  192. NotificationsModule.pub.ttl(redisKey, (err, ttl) => {
  193. responseObject.redis.ttl[redisKey] = ttl;
  194. next(err);
  195. });
  196. },
  197. next
  198. );
  199. },
  200. next => {
  201. responseObject.debugLogs = this.moduleManager.debugLogs.stationIssue;
  202. next();
  203. },
  204. next => {
  205. responseObject.debugJobs = this.moduleManager.debugJobs;
  206. next();
  207. }
  208. ],
  209. err => {
  210. if (err) {
  211. console.log(err);
  212. return res.json({
  213. error: err,
  214. objectSoFar: responseObject
  215. });
  216. }
  217. const responseJson = JSON.stringify(responseObject, (key, value) => {
  218. if (
  219. key === "module" ||
  220. key === "task" ||
  221. key === "onFinish" ||
  222. key === "server" ||
  223. key === "nsp" ||
  224. key === "socket" ||
  225. key === "res" ||
  226. key === "client" ||
  227. key === "_idleNext" ||
  228. key === "_idlePrev"
  229. ) {
  230. return undefined;
  231. }
  232. if (key === "parentJob" && value) return value.toString();
  233. return value;
  234. });
  235. return res.end(responseJson);
  236. }
  237. );
  238. });
  239. }
  240. resolve();
  241. })
  242. .catch(err => {
  243. reject(err);
  244. });
  245. });
  246. }
  247. }
  248. export default new _APIModule();