api.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import config from "config";
  2. import async from "async";
  3. import CoreClass from "../core";
  4. let AppModule;
  5. let PlaylistsModule;
  6. let UtilsModule;
  7. let PunishmentsModule;
  8. let CacheModule;
  9. class _APIModule extends CoreClass {
  10. // eslint-disable-next-line require-jsdoc
  11. constructor() {
  12. super("api");
  13. }
  14. /**
  15. * Initialises the api module
  16. *
  17. * @returns {Promise} - returns promise (reject, resolve)
  18. */
  19. initialize() {
  20. return new Promise((resolve, reject) => {
  21. AppModule = this.moduleManager.modules.app;
  22. PlaylistsModule = this.moduleManager.modules.playlists;
  23. UtilsModule = this.moduleManager.modules.utils;
  24. PunishmentsModule = this.moduleManager.modules.punishments;
  25. CacheModule = this.moduleManager.modules.cache;
  26. const SIDname = config.get("cookie.SIDname");
  27. const isLoggedIn = (req, res, next) => {
  28. let SID;
  29. async.waterfall(
  30. [
  31. next => {
  32. UtilsModule.runJob("PARSE_COOKIES", {
  33. cookieString: req.headers.cookie
  34. })
  35. .then(res => {
  36. SID = res[SIDname];
  37. next(null);
  38. })
  39. .catch(next);
  40. },
  41. next => {
  42. if (!SID) return next("No SID.");
  43. return next();
  44. },
  45. next => {
  46. CacheModule.runJob("HGET", { table: "sessions", key: SID }).then(session =>
  47. next(null, session)
  48. );
  49. },
  50. (session, next) => {
  51. if (!session) return next("No session found.");
  52. session.refreshDate = Date.now();
  53. req.session = session;
  54. return CacheModule.runJob("HSET", {
  55. table: "sessions",
  56. key: SID,
  57. value: session
  58. }).then(session => {
  59. next(null, session);
  60. });
  61. },
  62. (res, next) => {
  63. // check if a session's user / IP is banned
  64. PunishmentsModule.runJob("GET_PUNISHMENTS", {})
  65. .then(punishments => {
  66. const isLoggedIn = !!(req.session && req.session.refreshDate);
  67. const userId = isLoggedIn ? req.session.userId : null;
  68. const banishment = { banned: false, ban: 0 };
  69. punishments.forEach(punishment => {
  70. if (punishment.expiresAt > banishment.ban) banishment.ban = punishment;
  71. if (
  72. punishment.type === "banUserId" &&
  73. isLoggedIn &&
  74. punishment.value === userId
  75. )
  76. banishment.banned = true;
  77. if (punishment.type === "banUserIp" && punishment.value === req.ip)
  78. banishment.banned = true;
  79. });
  80. req.banishment = banishment;
  81. next();
  82. })
  83. .catch(() => {
  84. next();
  85. });
  86. }
  87. ],
  88. err => {
  89. if (err) return res.json({ status: "error", message: "You are not logged in" });
  90. return next();
  91. }
  92. );
  93. };
  94. AppModule.runJob("GET_APP", {})
  95. .then(response => {
  96. response.app.get("/", (req, res) => {
  97. res.json({
  98. status: "success",
  99. message: "Coming Soon"
  100. });
  101. });
  102. response.app.get("/export/privatePlaylist/:playlistId", isLoggedIn, (req, res) => {
  103. const { playlistId } = req.params;
  104. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId })
  105. .then(playlist => {
  106. if (playlist.createdBy === req.session.userId)
  107. res.json({ status: "success", playlist });
  108. else res.json({ status: "error", message: "You're not the owner." });
  109. })
  110. .catch(err => {
  111. res.json({ status: "error", message: err.message });
  112. });
  113. });
  114. resolve();
  115. })
  116. .catch(err => {
  117. reject(err);
  118. });
  119. });
  120. }
  121. }
  122. export default new _APIModule();