playlists.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. cache.sub('playlist.create', playlistId => {
  11. playlists.getPlaylist(playlistId, (err, playlist) => {
  12. if (!err) {
  13. utils.socketsFromUser(playlist.createdBy, (sockets) => {
  14. sockets.forEach((socket) => {
  15. socket.emit('event:playlist.create', playlist);
  16. });
  17. });
  18. }
  19. });
  20. });
  21. cache.sub('playlist.delete', res => {
  22. utils.socketsFromUser(res.userId, (sockets) => {
  23. sockets.forEach((socket) => {
  24. socket.emit('event:playlist.delete', res.playlistId);
  25. });
  26. });
  27. });
  28. cache.sub('playlist.addSong', res => {
  29. utils.socketsFromUser(res.userId, (sockets) => {
  30. sockets.forEach((socket) => {
  31. socket.emit('event:playlist.addSong', {playlistId: res.playlistId, song: res.song});
  32. });
  33. });
  34. });
  35. cache.sub('playlist.removeSong', res => {
  36. utils.socketsFromUser(res.userId, (sockets) => {
  37. sockets.forEach((socket) => {
  38. socket.emit('event:playlist.removeSong', {playlistId: res.playlistId, songId: res.songId});
  39. });
  40. });
  41. });
  42. cache.sub('playlist.updateDisplayName', res => {
  43. utils.socketsFromUser(res.userId, (sockets) => {
  44. sockets.forEach((socket) => {
  45. socket.emit('event:playlist.updateDisplayName', {playlistId: res.playlistId, displayName: res.displayName});
  46. });
  47. });
  48. });
  49. let lib = {
  50. indexForUser: hooks.loginRequired((session, cb, userId) => {
  51. db.models.playlist.find({ createdBy: userId }, (err, playlists) => {
  52. if (err) return cb({ status: 'failure', message: 'Something went wrong when getting the playlists.'});;
  53. cb({
  54. status: 'success',
  55. data: playlists
  56. });
  57. });
  58. }),
  59. create: hooks.loginRequired((session, data, cb, userId) => {
  60. async.waterfall([
  61. (next) => {
  62. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
  63. },
  64. (next) => {
  65. const { name, displayName, songs } = data;
  66. db.models.playlist.create({
  67. displayName,
  68. songs,
  69. createdBy: userId,
  70. createdAt: Date.now()
  71. }, next);
  72. }
  73. ], (err, playlist) => {
  74. if (err) return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  75. cache.pub('playlist.create', playlist._id);
  76. return cb({ 'status': 'success', 'message': 'Successfully created playlist' });
  77. });
  78. }),
  79. getPlaylist: hooks.loginRequired((session, id, cb, userId) => {
  80. playlists.getPlaylist(id, (err, playlist) => {
  81. if (err || playlist.createdBy !== userId) return cb({status: 'success', message: 'Playlist not found.'});
  82. if (err == null) return cb({
  83. status: 'success',
  84. data: playlist
  85. });
  86. });
  87. }),
  88. //TODO Remove this
  89. update: hooks.loginRequired((session, _id, playlist, cb, userId) => {
  90. db.models.playlist.update({ _id, createdBy: userId }, playlist, (err, data) => {
  91. if (err) return cb({ status: 'failure', message: 'Something went wrong.' });
  92. playlists.updatePlaylist(_id, (err) => {
  93. if (err) return cb({ status: 'failure', message: 'Something went wrong.' });
  94. return cb({ status: 'success', message: 'Playlist has been successfully updated', data });
  95. });
  96. });
  97. }),
  98. addSongToPlaylist: hooks.loginRequired((session, songId, playlistId, cb, userId) => {
  99. console.log(songId);
  100. async.waterfall([
  101. (next) => {
  102. playlists.getPlaylist(playlistId, (err, playlist) => {
  103. if (err || !playlist || playlist.createdBy !== userId) return next('Something went wrong when trying to get the playlist.');
  104. let found = false;
  105. playlist.songs.forEach((song) => {
  106. if (songId === song._id) {
  107. found = true;
  108. }
  109. });
  110. if (found) return next('That song is already in the playlist.');
  111. return next(null);
  112. });
  113. },
  114. (next) => {
  115. songs.getSong(songId, (err, song) => {
  116. if (err) {
  117. utils.getSongFromYouTube(songId, (song) => {
  118. next(null, song);
  119. });
  120. } else {
  121. next(null, {
  122. _id: songId,
  123. title: song.title,
  124. duration: song.duration
  125. });
  126. }
  127. });
  128. },
  129. (newSong, next) => {
  130. db.models.playlist.update({_id: playlistId}, {$push: {songs: newSong}}, (err) => {
  131. if (err) {
  132. console.error(err);
  133. return next('Failed to add song to playlist');
  134. }
  135. playlists.updatePlaylist(playlistId, (err, playlist) => {
  136. next(err, playlist, newSong);
  137. });
  138. });
  139. }
  140. ],
  141. (err, playlist, newSong) => {
  142. if (err) return cb({ status: 'error', message: err });
  143. else if (playlist.songs) {
  144. cache.pub('playlist.addSong', {playlistId: playlist._id, song: newSong, userId: userId});
  145. return cb({ status: 'success', message: 'Song has been successfully added to the playlist', data: playlist.songs });
  146. }
  147. });
  148. }),
  149. addSetToPlaylist: hooks.loginRequired((session, url, playlistId, cb, userId) => {
  150. async.waterfall([
  151. (next) => {
  152. utils.getPlaylistFromYouTube(url, songs => {
  153. next(null, songs);
  154. });
  155. },
  156. (songs, next) => {
  157. let processed = 0;
  158. function checkDone() {
  159. if (processed === songs.length) {
  160. next();
  161. }
  162. }
  163. for (let s = 0; s < songs.length; s++) {
  164. lib.addSongToPlaylist(session, songs[s].contentDetails.videoId, playlistId, () => {
  165. processed++;
  166. checkDone();
  167. });
  168. }
  169. },
  170. (next) => {
  171. playlists.getPlaylist(playlistId, (err, playlist) => {
  172. if (err || !playlist || playlist.createdBy !== userId) return next('Something went wrong while trying to get the playlist.');
  173. next(null, playlist);
  174. });
  175. }
  176. ],
  177. (err, playlist) => {
  178. if (err) return cb({ status: 'failure', message: err });
  179. else if (playlist.songs) return cb({ status: 'success', message: 'Playlist has been successfully imported.', data: playlist.songs });
  180. });
  181. }),
  182. removeSongFromPlaylist: hooks.loginRequired((session, songId, playlistId, cb, userId) => {
  183. playlists.getPlaylist(playlistId, (err, playlist) => {
  184. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  185. for (let z = 0; z < playlist.songs.length; z++) {
  186. if (playlist.songs[z]._id == songId) playlist.songs.shift(playlist.songs[z]);
  187. }
  188. db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
  189. if (err) {
  190. console.error(err);
  191. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  192. }
  193. playlists.updatePlaylist(playlistId, (err, playlist) => {
  194. cache.pub('playlist.removeSong', {playlistId: playlist._id, songId: songId, userId: userId});
  195. return cb({ status: 'success', message: 'Song has been successfully removed from playlist', data: playlist.songs });
  196. });
  197. });
  198. });
  199. }),
  200. updateDisplayName: hooks.loginRequired((session, _id, displayName, cb, userId) => {
  201. db.models.playlist.update({ _id, createdBy: userId }, { displayName }, (err, res) => {
  202. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  203. playlists.updatePlaylist(_id, (err) => {
  204. if (err) return cb({ status: 'failure', message: err});
  205. cache.pub('playlist.updateDisplayName', {playlistId: _id, displayName: displayName, userId: userId});
  206. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  207. })
  208. });
  209. }),/*
  210. promoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  211. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  212. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  213. let song = playlist.songs[fromIndex];
  214. playlist.songs.splice(fromIndex, 1);
  215. playlist.songs.splice((fromIndex + 1), 0, song);
  216. playlist.save(err => {
  217. if (err) {
  218. console.error(err);
  219. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  220. }
  221. playlists.updatePlaylist(playlistId, (err) => {
  222. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  223. return cb({ status: 'success', data: playlist.songs });
  224. });
  225. });
  226. });
  227. }),
  228. demoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  229. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  230. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  231. let song = playlist.songs[fromIndex];
  232. playlist.songs.splice(fromIndex, 1);
  233. playlist.songs.splice((fromIndex - 1), 0, song);
  234. playlist.save(err => {
  235. if (err) {
  236. console.error(err);
  237. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  238. }
  239. playlists.updatePlaylist(playlistId, (err) => {
  240. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  241. return cb({ status: 'success', data: playlist.songs });
  242. });
  243. });
  244. });
  245. }),*/
  246. remove: hooks.loginRequired((session, _id, cb, userId) => {
  247. db.models.playlist.remove({ _id, createdBy: userId }).exec(err => {
  248. if (err) return cb({ status: 'failure', message: 'Something went wrong when removing the playlist.'});
  249. cache.hdel('playlists', _id, () => {
  250. cache.pub('playlist.delete', {userId: userId, playlistId: _id});
  251. return cb({ status: 'success', message: 'Playlist successfully removed' });
  252. });
  253. });
  254. })
  255. };
  256. module.exports = lib;