playlists.js 7.2 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. this.playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" });
  24. this.playlistSchemaCache = 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. PlaylistsModule.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. PlaylistsModule.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: PlaylistsModule.playlistSchemaCache(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. * Creates a playlist that is not generated or editable by a user e.g. liked songs playlist
  93. *
  94. * @param {object} payload - object that contains the payload
  95. * @param {string} payload.userId - the id of the user to create the playlist for
  96. * @param {string} payload.displayName - the display name of the playlist
  97. * @returns {Promise} - returns promise (reject, resolve)
  98. */
  99. CREATE_READ_ONLY_PLAYLIST(payload) {
  100. return new Promise((resolve, reject) => {
  101. PlaylistsModule.playlistModel.create(
  102. {
  103. isUserModifiable: false,
  104. displayName: payload.displayName,
  105. songs: [],
  106. createdBy: payload.userId,
  107. createdAt: Date.now(),
  108. type: payload.type
  109. },
  110. (err, playlist) => {
  111. if (err) return reject(new Error(err));
  112. return resolve(playlist._id);
  113. }
  114. );
  115. });
  116. }
  117. /**
  118. * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  119. *
  120. * @param {object} payload - object that contains the payload
  121. * @param {string} payload.playlistId - the id of the playlist we are trying to get
  122. * @returns {Promise} - returns promise (reject, resolve)
  123. */
  124. GET_PLAYLIST(payload) {
  125. return new Promise((resolve, reject) =>
  126. async.waterfall(
  127. [
  128. next => {
  129. CacheModule.runJob("HGETALL", { table: "playlists" }, this)
  130. .then(playlists => {
  131. next(null, playlists);
  132. })
  133. .catch(next);
  134. },
  135. (playlists, next) => {
  136. if (!playlists) return next();
  137. const playlistIds = Object.keys(playlists);
  138. return async.each(
  139. playlistIds,
  140. (playlistId, next) => {
  141. PlaylistsModule.playlistModel.findOne({ _id: playlistId }, (err, playlist) => {
  142. if (err) next(err);
  143. else if (!playlist) {
  144. CacheModule.runJob(
  145. "HDEL",
  146. {
  147. table: "playlists",
  148. key: playlistId
  149. },
  150. this
  151. )
  152. .then(() => next())
  153. .catch(next);
  154. } else next();
  155. });
  156. },
  157. next
  158. );
  159. },
  160. next => {
  161. CacheModule.runJob(
  162. "HGET",
  163. {
  164. table: "playlists",
  165. key: payload.playlistId
  166. },
  167. this
  168. )
  169. .then(playlist => next(null, playlist))
  170. .catch(next);
  171. },
  172. (playlist, next) => {
  173. if (playlist) return next(true, playlist);
  174. return PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
  175. },
  176. (playlist, next) => {
  177. if (playlist) {
  178. CacheModule.runJob(
  179. "HSET",
  180. {
  181. table: "playlists",
  182. key: payload.playlistId,
  183. value: playlist
  184. },
  185. this
  186. )
  187. .then(playlist => {
  188. next(null, playlist);
  189. })
  190. .catch(next);
  191. } else next("Playlist not found");
  192. }
  193. ],
  194. (err, playlist) => {
  195. if (err && err !== true) return reject(new Error(err));
  196. return resolve(playlist);
  197. }
  198. )
  199. );
  200. }
  201. /**
  202. * Gets a playlist from id from Mongo and updates the cache with it
  203. *
  204. * @param {object} payload - object that contains the payload
  205. * @param {string} payload.playlistId - the id of the playlist we are trying to update
  206. * @returns {Promise} - returns promise (reject, resolve)
  207. */
  208. UPDATE_PLAYLIST(payload) {
  209. // playlistId, cb
  210. return new Promise((resolve, reject) =>
  211. async.waterfall(
  212. [
  213. next => {
  214. PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
  215. },
  216. (playlist, next) => {
  217. if (!playlist) {
  218. CacheModule.runJob("HDEL", {
  219. table: "playlists",
  220. key: payload.playlistId
  221. });
  222. return next("Playlist not found");
  223. }
  224. return CacheModule.runJob(
  225. "HSET",
  226. {
  227. table: "playlists",
  228. key: payload.playlistId,
  229. value: playlist
  230. },
  231. this
  232. )
  233. .then(playlist => {
  234. next(null, playlist);
  235. })
  236. .catch(next);
  237. }
  238. ],
  239. (err, playlist) => {
  240. if (err && err !== true) return reject(new Error(err));
  241. return resolve(playlist);
  242. }
  243. )
  244. );
  245. }
  246. /**
  247. * Deletes playlist from id from Mongo and cache
  248. *
  249. * @param {object} payload - object that contains the payload
  250. * @param {string} payload.playlistId - the id of the playlist we are trying to delete
  251. * @returns {Promise} - returns promise (reject, resolve)
  252. */
  253. DELETE_PLAYLIST(payload) {
  254. // playlistId, cb
  255. return new Promise((resolve, reject) =>
  256. async.waterfall(
  257. [
  258. next => {
  259. PlaylistsModule.playlistModel.deleteOne({ _id: payload.playlistId }, next);
  260. },
  261. (res, next) => {
  262. CacheModule.runJob(
  263. "HDEL",
  264. {
  265. table: "playlists",
  266. key: payload.playlistId
  267. },
  268. this
  269. )
  270. .then(() => next())
  271. .catch(next);
  272. }
  273. ],
  274. err => {
  275. if (err && err !== true) return reject(new Error(err));
  276. return resolve();
  277. }
  278. )
  279. );
  280. }
  281. }
  282. export default new _PlaylistsModule();