playlists.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. 'use strict';
  2. const db = require('../db');
  3. const io = require('../io');
  4. const cache = require('../cache');
  5. const utils = require('../utils');
  6. const hooks = require('./hooks');
  7. const async = require('async');
  8. const playlists = require('../playlists');
  9. module.exports = {
  10. indexForUser: (session, createdBy, cb) => {
  11. db.models.playlist.find({ createdBy }, (err, playlists) => {
  12. if (err) throw err;
  13. cb({
  14. status: 'success',
  15. data: playlists
  16. });
  17. });
  18. },
  19. create: (session, data, cb) => {
  20. async.waterfall([
  21. (next) => {
  22. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
  23. },
  24. // check the cache for the playlist
  25. (next) => cache.hget('playlists', data._id, next),
  26. // if the cached version exist
  27. (playlist, next) => {
  28. if (playlist) return next({ 'status': 'failure', 'message': 'A playlist with that id already exists' });
  29. db.models.playlist.findOne({ _id: data._id }, next);
  30. },
  31. (playlist, next) => {
  32. if (playlist) return next({ 'status': 'failure', 'message': 'A playlist with that id already exists' });
  33. const { _id, displayName, songs, createdBy } = data;
  34. db.models.playlist.create({
  35. _id,
  36. displayName,
  37. songs,
  38. createdBy,
  39. createdAt: Date.now()
  40. }, next);
  41. }
  42. ], (err, playlist) => {
  43. if (err) return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  44. cache.pub('playlist.create', data._id);
  45. return cb({ 'status': 'success', 'message': 'Successfully created playlist' });
  46. });
  47. },
  48. getPlaylist: (session, id, cb) => {
  49. playlists.getPlaylist(id, (err, playlist) => {
  50. if (err == null) return cb({
  51. status: 'success',
  52. data: playlist
  53. });
  54. });
  55. },
  56. update: (session, _id, playlist, cb) => {
  57. db.models.playlist.findOneAndUpdate({ _id }, playlist, { upsert: true }, (err, data) => {
  58. if (err) throw err;
  59. return cb({ status: 'success', message: 'Playlist has been successfully updated', data });
  60. });
  61. },
  62. addSongToPlaylist: (session, songId, playlistId, cb) => {
  63. async.waterfall([
  64. (next) => {
  65. utils.getSongFromYouTube(songId, (song) => {
  66. song.artists = [];
  67. song.genres = [];
  68. song.skipDuration = 0;
  69. song.thumbnail = 'empty';
  70. song.explicit = false;
  71. song.requestedBy = 'temp';
  72. song.requestedAt = Date.now();
  73. next(null, song);
  74. });
  75. },
  76. (newSong, next) => {
  77. utils.getSongFromSpotify(newSong, (song) => {
  78. next(null, song);
  79. });
  80. },
  81. (newSong, next) => {
  82. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  83. if (err) throw err;
  84. playlist.songs.push(newSong);
  85. playlist.save(err => {
  86. if (err) {
  87. console.error(err);
  88. return next('Failed to add song to playlist');
  89. }
  90. cache.hset('playlists', playlistId, playlist);
  91. next(null, playlist);
  92. });
  93. });
  94. }
  95. ],
  96. (err, playlist) => {
  97. if (err) return cb({ status: 'error', message: err });
  98. else return cb({ status: 'success', message: 'Song has been successfully added to the playlist', data: playlist.songs });
  99. });
  100. },
  101. removeSongFromPlaylist: (session, songId, playlistId, cb) => {
  102. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  103. if (err) throw err;
  104. for (let z = 0; z < playlist.songs.length; z++) {
  105. if (playlist.songs[z]._id == songId) playlist.songs.shift(playlist.songs[z]);
  106. }
  107. playlist.save(err => {
  108. if (err) {
  109. console.error(err);
  110. return next('Failed to remove song to playlist');
  111. }
  112. cache.hset('playlists', playlistId, playlist);
  113. return cb({ status: 'success', message: 'Song has been successfully removed from playlist', data: playlist.songs });
  114. });
  115. });
  116. },
  117. updateDisplayName: (session, _id, displayName, cb) => {
  118. db.models.playlist.findOneAndUpdate({ _id }, { displayName }, { upsert: true }, (err, res) => {
  119. if (err) throw err;
  120. cache.hset('playlists', _id, res);
  121. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  122. });
  123. },
  124. updatePlaylistId: (session, oldId, newId, cb) => {
  125. db.models.playlist.findOne({ _id: oldId }).exec((err, doc) => {
  126. if (err) throw err;
  127. doc._id = newId;
  128. let newPlaylist = new db.models.playlist(doc);
  129. newPlaylist.isNew = true;
  130. newPlaylist.save(err => {
  131. if (err) console.error(err);
  132. });
  133. db.models.playlist.remove({ _id: oldId });
  134. cache.hdel('playlists', oldId, () => {
  135. cache.hset('playlists', newId, doc);
  136. return cb({ status: 'success', data: doc });
  137. });
  138. });
  139. },
  140. promoteSong: (session, playlistId, fromIndex, cb) => {
  141. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  142. if (err) throw err;
  143. let song = playlist.songs[fromIndex];
  144. playlist.songs.splice(fromIndex, 1);
  145. playlist.songs.splice((fromIndex + 1), 0, song);
  146. playlist.save(err => {
  147. if (err) {
  148. console.error(err);
  149. return next('Failed to promote song');
  150. }
  151. cache.hset('playlists', playlistId, playlist);
  152. return cb({ status: 'success', data: playlist.songs });
  153. });
  154. });
  155. },
  156. demoteSong: (session, playlistId, fromIndex, cb) => {
  157. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  158. if (err) throw err;
  159. let song = playlist.songs[fromIndex];
  160. playlist.songs.splice(fromIndex, 1);
  161. playlist.songs.splice((fromIndex - 1), 0, song);
  162. playlist.save(err => {
  163. if (err) {
  164. console.error(err);
  165. return next('Failed to demote song');
  166. }
  167. cache.hset('playlists', playlistId, playlist);
  168. return cb({ status: 'success', data: playlist.songs });
  169. });
  170. });
  171. },
  172. remove: (session, _id, cb) => {
  173. db.models.playlist.remove({ _id }).exec(err => {
  174. if (err) throw err;
  175. cache.hdel('playlists', _id, () => {
  176. return cb({ status: 'success', message: 'Playlist successfully removed' });
  177. });
  178. });
  179. }
  180. };