playlists.js 6.5 KB

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