playlists.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. const songs = require('../songs');
  10. let lib = {
  11. indexForUser: hooks.loginRequired((session, cb, userId) => {
  12. db.models.playlist.find({ createdBy: userId }, (err, playlists) => {
  13. if (err) return cb({ status: 'failure', message: 'Something went wrong when getting the playlists.'});;
  14. cb({
  15. status: 'success',
  16. data: playlists
  17. });
  18. });
  19. }),
  20. create: hooks.loginRequired((session, data, cb, userId) => {
  21. async.waterfall([
  22. (next) => {
  23. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
  24. },
  25. (next) => {
  26. const { name, displayName, songs } = data;
  27. db.models.playlist.create({
  28. displayName,
  29. songs,
  30. createdBy: userId,
  31. createdAt: Date.now()
  32. }, next);
  33. }
  34. ], (err, playlist) => {
  35. if (err) return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  36. cache.pub('playlist.create', data._id);
  37. return cb({ 'status': 'success', 'message': 'Successfully created playlist' });
  38. });
  39. }),
  40. getPlaylist: hooks.loginRequired((session, id, cb, userId) => {
  41. playlists.getPlaylist(id, (err, playlist) => {
  42. if (err || playlist.createdBy !== userId) return cb({status: 'success', message: 'Playlist not found.'});
  43. if (err == null) return cb({
  44. status: 'success',
  45. data: playlist
  46. });
  47. });
  48. }),
  49. //TODO Remove this
  50. update: hooks.loginRequired((session, _id, playlist, cb, userId) => {
  51. db.models.playlist.update({ _id, createdBy: userId }, playlist, (err, data) => {
  52. if (err) return cb({ status: 'failure', message: 'Something went wrong.' });
  53. playlists.updatePlaylist(_id, (err) => {
  54. if (err) return cb({ status: 'failure', message: 'Something went wrong.' });
  55. return cb({ status: 'success', message: 'Playlist has been successfully updated', data });
  56. });
  57. });
  58. }),
  59. addSongToPlaylist: hooks.loginRequired((session, songId, playlistId, cb, userId) => {
  60. async.waterfall([
  61. (next) => {
  62. songs.getSong(songId, (err, song) => {
  63. if (err) {
  64. utils.getSongFromYouTube(songId, (song) => {
  65. next(null, song);
  66. });
  67. } else {
  68. next(null, {
  69. _id: songId,
  70. title: song.title,
  71. duration: song.duration
  72. });
  73. }
  74. });
  75. },
  76. (newSong, next) => {
  77. playlists.getPlaylist(_id, (err, playlist) => {
  78. if (err || !playlist || playlist.createdBy !== userId) return next('Something went wrong when trying to get the playlist.');
  79. if (Array.isArray(playlist.songs)) {
  80. playlist.songs.push(newSong);
  81. playlist.save(err => {
  82. if (err) {
  83. console.error(err);
  84. return next('Failed to add song to playlist');
  85. }
  86. playlists.updatePlaylist(_id, () => {
  87. next(null, playlist);
  88. });
  89. });
  90. }
  91. });
  92. }
  93. ],
  94. (err, playlist) => {
  95. if (err) return cb({ status: 'error', message: err });
  96. else if (playlist.songs) return cb({ status: 'success', message: 'Song has been successfully added to the playlist', data: playlist.songs });
  97. });
  98. }),
  99. addSetToPlaylist: hooks.loginRequired((session, url, playlistId, cb, userId) => {
  100. async.waterfall([
  101. (next) => {
  102. utils.getPlaylistFromYouTube(url, songs => {
  103. next(null, songs);
  104. });
  105. },
  106. (songs, next) => {
  107. for (let s = 0; s < songs.length; s++) {
  108. lib.addSongToPlaylist(session, songs[s].contentDetails.videoId, playlistId, (res) => {})();
  109. }
  110. next(null);
  111. },
  112. (next) => {
  113. playlists.getPlaylist(_id, (err, playlist) => {
  114. if (err || !playlist || playlist.createdBy !== userId) return next('Something went wrong while trying to get the playlist.');
  115. next(null, playlist);
  116. });
  117. }
  118. ],
  119. (err, playlist) => {
  120. if (err) return cb({ status: 'failure', message: err });
  121. else if (playlist.songs) return cb({ status: 'success', message: 'Playlist has been successfully added', data: playlist.songs });
  122. });
  123. }),
  124. removeSongFromPlaylist: hooks.loginRequired((session, songId, playlistId, cb, userId) => {
  125. playlists.getPlaylist(playlistId, (err, playlist) => {
  126. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  127. for (let z = 0; z < playlist.songs.length; z++) {
  128. if (playlist.songs[z]._id == songId) playlist.songs.shift(playlist.songs[z]);
  129. }
  130. db.models.playlist.update({_id: playlistId}, {$pull: {_id: songId}}, (err) => {
  131. if (err) {
  132. console.error(err);
  133. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  134. }
  135. return cb({ status: 'success', message: 'Song has been successfully removed from playlist', data: playlist.songs });
  136. });
  137. });
  138. }),
  139. updateDisplayName: hooks.loginRequired((session, _id, displayName, cb, userId) => {
  140. db.models.playlist.update({ _id, createdBy: userId }, { displayName }, (err, res) => {
  141. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  142. playlists.updatePlaylist(_id, (err) => {
  143. if (err) return cb({ status: 'failure', message: err});
  144. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  145. })
  146. });
  147. }),
  148. promoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  149. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  150. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  151. let song = playlist.songs[fromIndex];
  152. playlist.songs.splice(fromIndex, 1);
  153. playlist.songs.splice((fromIndex + 1), 0, song);
  154. playlist.save(err => {
  155. if (err) {
  156. console.error(err);
  157. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  158. }
  159. playlists.updatePlaylist(playlistId, (err) => {
  160. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  161. return cb({ status: 'success', data: playlist.songs });
  162. });
  163. });
  164. });
  165. }),
  166. demoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  167. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  168. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  169. let song = playlist.songs[fromIndex];
  170. playlist.songs.splice(fromIndex, 1);
  171. playlist.songs.splice((fromIndex - 1), 0, song);
  172. playlist.save(err => {
  173. if (err) {
  174. console.error(err);
  175. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  176. }
  177. playlists.updatePlaylist(playlistId, (err) => {
  178. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  179. return cb({ status: 'success', data: playlist.songs });
  180. });
  181. });
  182. });
  183. }),
  184. remove: hooks.loginRequired((session, _id, cb, userId) => {
  185. db.models.playlist.remove({ _id, createdBy: userId }).exec(err => {
  186. if (err) return cb({ status: 'failure', message: 'Something went wrong when removing the playlist.'});
  187. cache.hdel('playlists', _id, () => {
  188. return cb({ status: 'success', message: 'Playlist successfully removed' });
  189. });
  190. });
  191. })
  192. };
  193. module.exports = lib;