playlists.js 7.1 KB

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