playlists.js 4.0 KB

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