playlists.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import async from "async";
  2. import CoreClass from "../core";
  3. let PlaylistsModule;
  4. let CacheModule;
  5. let DBModule;
  6. let UtilsModule;
  7. class _PlaylistsModule extends CoreClass {
  8. // eslint-disable-next-line require-jsdoc
  9. constructor() {
  10. super("playlists");
  11. PlaylistsModule = this;
  12. }
  13. /**
  14. * Initialises the playlists module
  15. *
  16. * @returns {Promise} - returns promise (reject, resolve)
  17. */
  18. async initialize() {
  19. this.setStage(1);
  20. CacheModule = this.moduleManager.modules.cache;
  21. DBModule = this.moduleManager.modules.db;
  22. UtilsModule = this.moduleManager.modules.utils;
  23. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" });
  24. const playlistSchema = await CacheModule.runJob("GET_SCHEMA", { schemaName: "playlist" });
  25. this.setStage(2);
  26. return new Promise((resolve, reject) =>
  27. async.waterfall(
  28. [
  29. next => {
  30. this.setStage(3);
  31. CacheModule.runJob("HGETALL", { table: "playlists" })
  32. .then(playlists => {
  33. next(null, playlists);
  34. })
  35. .catch(next);
  36. },
  37. (playlists, next) => {
  38. this.setStage(4);
  39. if (!playlists) return next();
  40. const playlistIds = Object.keys(playlists);
  41. return async.each(
  42. playlistIds,
  43. (playlistId, next) => {
  44. playlistModel.findOne({ _id: playlistId }, (err, playlist) => {
  45. if (err) next(err);
  46. else if (!playlist) {
  47. CacheModule.runJob("HDEL", {
  48. table: "playlists",
  49. key: playlistId
  50. })
  51. .then(() => next())
  52. .catch(next);
  53. } else next();
  54. });
  55. },
  56. next
  57. );
  58. },
  59. next => {
  60. this.setStage(5);
  61. playlistModel.find({}, next);
  62. },
  63. (playlists, next) => {
  64. this.setStage(6);
  65. async.each(
  66. playlists,
  67. (playlist, cb) => {
  68. CacheModule.runJob("HSET", {
  69. table: "playlists",
  70. key: playlist._id,
  71. value: playlistSchema(playlist)
  72. })
  73. .then(() => cb())
  74. .catch(next);
  75. },
  76. next
  77. );
  78. }
  79. ],
  80. async err => {
  81. if (err) {
  82. const formattedErr = await UtilsModule.runJob("GET_ERROR", {
  83. error: err
  84. });
  85. reject(new Error(formattedErr));
  86. } else resolve();
  87. }
  88. )
  89. );
  90. }
  91. /**
  92. * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  93. *
  94. * @param {object} payload - object that contains the payload
  95. * @param {string} payload.playlistId - the id of the playlist we are trying to get
  96. * @returns {Promise} - returns promise (reject, resolve)
  97. */
  98. GET_PLAYLIST(payload) {
  99. return new Promise((resolve, reject) => {
  100. let playlistModel;
  101. PlaylistsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
  102. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
  103. .then(model => {
  104. playlistModel = model;
  105. })
  106. .catch(console.error);
  107. return async.waterfall(
  108. [
  109. next => {
  110. CacheModule.runJob("HGETALL", { table: "playlists" }, this)
  111. .then(playlists => {
  112. next(null, playlists);
  113. })
  114. .catch(next);
  115. },
  116. (playlists, next) => {
  117. if (!playlists) return next();
  118. const playlistIds = Object.keys(playlists);
  119. return async.each(
  120. playlistIds,
  121. (playlistId, next) => {
  122. playlistModel.findOne({ _id: playlistId }, (err, playlist) => {
  123. if (err) next(err);
  124. else if (!playlist) {
  125. CacheModule.runJob(
  126. "HDEL",
  127. {
  128. table: "playlists",
  129. key: playlistId
  130. },
  131. this
  132. )
  133. .then(() => next())
  134. .catch(next);
  135. } else next();
  136. });
  137. },
  138. next
  139. );
  140. },
  141. next => {
  142. CacheModule.runJob(
  143. "HGET",
  144. {
  145. table: "playlists",
  146. key: payload.playlistId
  147. },
  148. this
  149. )
  150. .then(playlist => next(null, playlist))
  151. .catch(next);
  152. },
  153. (playlist, next) => {
  154. if (playlist) return next(true, playlist);
  155. return playlistModel.findOne({ _id: payload.playlistId }, next);
  156. },
  157. (playlist, next) => {
  158. if (playlist) {
  159. CacheModule.runJob(
  160. "HSET",
  161. {
  162. table: "playlists",
  163. key: payload.playlistId,
  164. value: playlist
  165. },
  166. this
  167. )
  168. .then(playlist => {
  169. next(null, playlist);
  170. })
  171. .catch(next);
  172. } else next("Playlist not found");
  173. }
  174. ],
  175. (err, playlist) => {
  176. if (err && err !== true) return reject(new Error(err));
  177. return resolve(playlist);
  178. }
  179. );
  180. });
  181. }
  182. /**
  183. * Gets a playlist from id from Mongo and updates the cache with it
  184. *
  185. * @param {object} payload - object that contains the payload
  186. * @param {string} payload.playlistId - the id of the playlist we are trying to update
  187. * @returns {Promise} - returns promise (reject, resolve)
  188. */
  189. UPDATE_PLAYLIST(payload) {
  190. // playlistId, cb
  191. return new Promise((resolve, reject) => {
  192. let playlistModel;
  193. PunishmentsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
  194. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
  195. .then(model => {
  196. playlistModel = model;
  197. })
  198. .catch(console.error);
  199. return async.waterfall(
  200. [
  201. next => {
  202. playlistModel.findOne({ _id: payload.playlistId }, next);
  203. },
  204. (playlist, next) => {
  205. if (!playlist) {
  206. CacheModule.runJob("HDEL", {
  207. table: "playlists",
  208. key: payload.playlistId
  209. });
  210. return next("Playlist not found");
  211. }
  212. return CacheModule.runJob(
  213. "HSET",
  214. {
  215. table: "playlists",
  216. key: payload.playlistId,
  217. value: playlist
  218. },
  219. this
  220. )
  221. .then(playlist => {
  222. next(null, playlist);
  223. })
  224. .catch(next);
  225. }
  226. ],
  227. (err, playlist) => {
  228. if (err && err !== true) return reject(new Error(err));
  229. return resolve(playlist);
  230. }
  231. );
  232. });
  233. }
  234. /**
  235. * Deletes playlist from id from Mongo and cache
  236. *
  237. * @param {object} payload - object that contains the payload
  238. * @param {string} payload.playlistId - the id of the playlist we are trying to delete
  239. * @returns {Promise} - returns promise (reject, resolve)
  240. */
  241. DELETE_PLAYLIST(payload) {
  242. // playlistId, cb
  243. return new Promise((resolve, reject) => {
  244. let playlistModel;
  245. PunishmentsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
  246. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
  247. .then(model => {
  248. playlistModel = model;
  249. })
  250. .catch(console.error);
  251. return async.waterfall(
  252. [
  253. next => {
  254. playlistModel.deleteOne({ _id: payload.playlistId }, next);
  255. },
  256. (res, next) => {
  257. CacheModule.runJob(
  258. "HDEL",
  259. {
  260. table: "playlists",
  261. key: payload.playlistId
  262. },
  263. this
  264. )
  265. .then(() => next())
  266. .catch(next);
  267. }
  268. ],
  269. err => {
  270. if (err && err !== true) return reject(new Error(err));
  271. return resolve();
  272. }
  273. );
  274. });
  275. }
  276. }
  277. export default new _PlaylistsModule();