playlists.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. const cache = require('./cache');
  3. const db = require('./db');
  4. const async = require('async');
  5. module.exports = {
  6. /**
  7. * Initializes the playlists module, and exits if it is unsuccessful
  8. *
  9. * @param {Function} cb - gets called once we're done initializing
  10. */
  11. init: cb => {
  12. async.waterfall([
  13. (next) => {
  14. cache.hgetall('playlists', next);
  15. },
  16. (playlists, next) => {
  17. if (!playlists) return next();
  18. let playlistIds = Object.keys(playlists);
  19. async.each(playlistIds, (playlistId, next) => {
  20. db.models.playlist.findOne({_id: playlistId}, (err, playlist) => {
  21. if (err) next(err);
  22. else if (!playlist) {
  23. cache.hdel('playlists', playlistId, next);
  24. }
  25. });
  26. }, next);
  27. },
  28. (next) => {
  29. db.models.playlist.find({}, next);
  30. },
  31. (playlists, next) => {
  32. async.each(playlists, (playlist, next) => {
  33. cache.hset('playlists', playlist._id, cache.schemas.playlist(playlist), next);
  34. }, next);
  35. }
  36. ], (err) => {
  37. if (err) {
  38. console.log(`FAILED TO INITIALIZE PLAYLISTS. ABORTING. "${err.message}"`);
  39. process.exit();
  40. } else {
  41. cb();
  42. }
  43. });
  44. },
  45. /**
  46. * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  47. *
  48. * @param {String} playlistId - the id of the playlist we are trying to get
  49. * @param {Function} cb - gets called once we're done initializing
  50. */
  51. getPlaylist: (playlistId, cb) => {
  52. async.waterfall([
  53. (next) => {
  54. cache.hgetall('playlists', next);
  55. },
  56. (playlists) => {
  57. let playlistIds = Object.keys(playlists);
  58. async.each(playlistIds, (playlistId, next) => {
  59. db.models.playlist.findOne({_id: playlistId}, (err, playlist) => {
  60. if (err) next(err);
  61. else if (!playlist) {
  62. cache.hdel('playlists', playlistId, next);
  63. }
  64. });
  65. }, next);
  66. },
  67. (next) => {
  68. cache.hget('playlists', playlistId, next);
  69. },
  70. (playlist, next) => {
  71. if (playlist) return next(true, playlist);
  72. db.models.playlist.findOne({ _id: playlistId }, next);
  73. },
  74. (playlist, next) => {
  75. if (playlist) {
  76. cache.hset('playlists', playlistId, playlist, next);
  77. } else next('Playlist not found');
  78. },
  79. ], (err, playlist) => {
  80. if (err && err !== true) return cb(err);
  81. else cb(null, playlist);
  82. });
  83. },
  84. /**
  85. * Gets a playlist from id from Mongo and updates the cache with it
  86. *
  87. * @param {String} playlistId - the id of the playlist we are trying to update
  88. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  89. */
  90. updatePlaylist: (playlistId, cb) => {
  91. async.waterfall([
  92. (next) => {
  93. db.models.playlist.findOne({ _id: playlistId }, next);
  94. },
  95. (playlist, next) => {
  96. if (!playlist) return next('Playlist not found');
  97. cache.hset('playlists', playlistId, playlist, next);
  98. }
  99. ], (err, playlist) => {
  100. if (err && err !== true) cb(err);
  101. cb(null, playlist);
  102. });
  103. },
  104. /**
  105. * Deletes playlist from id from Mongo and cache
  106. *
  107. * @param {String} playlistId - the id of the playlist we are trying to delete
  108. * @param {Function} cb - gets called when an error occurred or when the operation was successful
  109. */
  110. deleteSong: (playlistId, cb) => {
  111. async.waterfall([
  112. (next) => {
  113. db.models.playlist.remove({ _id: playlistId }, next);
  114. },
  115. (next) => {
  116. cache.hdel('playlists', playlistId, next);
  117. }
  118. ], (err) => {
  119. if (err && err !== true) cb(err);
  120. cb(null);
  121. });
  122. }
  123. };