playlists.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. const coreClass = require("../core");
  3. const async = require('async');
  4. module.exports = class extends coreClass {
  5. constructor(name, moduleManager) {
  6. super(name, moduleManager);
  7. this.dependsOn = ["cache", "db", "utils"];
  8. }
  9. initialize() {
  10. return new Promise((resolve, reject) => {
  11. this.setStage(1);
  12. this.cache = this.moduleManager.modules["cache"];
  13. this.db = this.moduleManager.modules["db"];
  14. this.utils = this.moduleManager.modules["utils"];
  15. async.waterfall([
  16. (next) => {
  17. this.setStage(2);
  18. this.cache.hgetall('playlists', next);
  19. },
  20. (playlists, next) => {
  21. this.setStage(3);
  22. if (!playlists) return next();
  23. let playlistIds = Object.keys(playlists);
  24. async.each(playlistIds, (playlistId, next) => {
  25. this.db.models.playlist.findOne({_id: playlistId}, (err, playlist) => {
  26. if (err) next(err);
  27. else if (!playlist) {
  28. this.cache.hdel('playlists', playlistId, next);
  29. }
  30. else next();
  31. });
  32. }, next);
  33. },
  34. (next) => {
  35. this.setStage(4);
  36. this.db.models.playlist.find({}, next);
  37. },
  38. (playlists, next) => {
  39. this.setStage(5);
  40. async.each(playlists, (playlist, next) => {
  41. this.cache.hset('playlists', playlist._id, this.cache.schemas.playlist(playlist), next);
  42. }, next);
  43. }
  44. ], async (err) => {
  45. if (err) {
  46. err = await this.utils.getError(err);
  47. reject(err);
  48. } else {
  49. resolve();
  50. }
  51. });
  52. });
  53. }
  54. /**
  55. * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  56. *
  57. * @param {String} playlistId - the id of the playlist we are trying to get
  58. * @param {Function} cb - gets called once we're done initializing
  59. */
  60. async getPlaylist(playlistId, cb) {
  61. try { await this._validateHook(); } catch { return; }
  62. async.waterfall([
  63. (next) => {
  64. this.cache.hgetall('playlists', next);
  65. },
  66. (playlists, next) => {
  67. if (!playlists) return next();
  68. let playlistIds = Object.keys(playlists);
  69. async.each(playlistIds, (playlistId, next) => {
  70. this.db.models.playlist.findOne({_id: playlistId}, (err, playlist) => {
  71. if (err) next(err);
  72. else if (!playlist) {
  73. this.cache.hdel('playlists', playlistId, next);
  74. }
  75. else next();
  76. });
  77. }, next);
  78. },
  79. (next) => {
  80. this.cache.hget('playlists', playlistId, next);
  81. },
  82. (playlist, next) => {
  83. if (playlist) return next(true, playlist);
  84. this.db.models.playlist.findOne({ _id: playlistId }, next);
  85. },
  86. (playlist, next) => {
  87. if (playlist) {
  88. this.cache.hset('playlists', playlistId, playlist, next);
  89. } else next('Playlist not found');
  90. },
  91. ], (err, playlist) => {
  92. if (err && err !== true) return cb(err);
  93. else cb(null, playlist);
  94. });
  95. }
  96. /**
  97. * Gets a playlist from id from Mongo and updates the cache with it
  98. *
  99. * @param {String} playlistId - the id of the playlist we are trying to update
  100. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  101. */
  102. async updatePlaylist(playlistId, cb) {
  103. try { await this._validateHook(); } catch { return; }
  104. async.waterfall([
  105. (next) => {
  106. this.db.models.playlist.findOne({ _id: playlistId }, next);
  107. },
  108. (playlist, next) => {
  109. if (!playlist) {
  110. this.cache.hdel('playlists', playlistId);
  111. return next('Playlist not found');
  112. }
  113. this.cache.hset('playlists', playlistId, playlist, next);
  114. }
  115. ], (err, playlist) => {
  116. if (err && err !== true) return cb(err);
  117. cb(null, playlist);
  118. });
  119. }
  120. /**
  121. * Deletes playlist from id from Mongo and cache
  122. *
  123. * @param {String} playlistId - the id of the playlist we are trying to delete
  124. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  125. */
  126. async deletePlaylist(playlistId, cb) {
  127. try { await this._validateHook(); } catch { return; }
  128. async.waterfall([
  129. (next) => {
  130. this.db.models.playlist.deleteOne({ _id: playlistId }, next);
  131. },
  132. (res, next) => {
  133. this.cache.hdel('playlists', playlistId, next);
  134. }
  135. ], (err) => {
  136. if (err && err !== true) return cb(err);
  137. cb(null);
  138. });
  139. }
  140. }