playlists.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. async.waterfall([
  100. (next) => {
  101. playlists.getPlaylist(playlistId, (err, playlist) => {
  102. if (err || !playlist || playlist.createdBy !== userId) return next('Something went wrong when trying to get the playlist.');
  103. let found = false;
  104. playlist.songs.forEach((song) => {
  105. if (songId === song._id) {
  106. found = true;
  107. }
  108. });
  109. if (found) return next('That song is already in the playlist.');
  110. return next(null);
  111. });
  112. },
  113. (next) => {
  114. songs.getSong(songId, (err, song) => {
  115. if (err) {
  116. utils.getSongFromYouTube(songId, (song) => {
  117. next(null, song);
  118. });
  119. } else {
  120. next(null, {
  121. _id: songId,
  122. title: song.title,
  123. duration: song.duration
  124. });
  125. }
  126. });
  127. },
  128. (newSong, next) => {
  129. db.models.playlist.update({_id: playlistId}, {$push: {songs: newSong}}, (err) => {
  130. if (err) {
  131. console.error(err);
  132. return next('Failed to add song to playlist');
  133. }
  134. playlists.updatePlaylist(playlistId, (err, playlist) => {
  135. next(err, playlist, newSong);
  136. });
  137. });
  138. }
  139. ],
  140. (err, playlist, newSong) => {
  141. if (err) return cb({ status: 'error', message: err });
  142. else if (playlist.songs) {
  143. cache.pub('playlist.addSong', {playlistId: playlist._id, song: newSong, userId: userId});
  144. return cb({ status: 'success', message: 'Song has been successfully added to the playlist', data: playlist.songs });
  145. }
  146. });
  147. }),
  148. /*addSetToPlaylist: hooks.loginRequired((session, url, playlistId, cb, userId) => {
  149. async.waterfall([
  150. (next) => {
  151. utils.getPlaylistFromYouTube(url, songs => {
  152. next(null, songs);
  153. });
  154. },
  155. (songs, next) => {
  156. for (let s = 0; s < songs.length; s++) {
  157. lib.addSongToPlaylist(session, songs[s].contentDetails.videoId, playlistId, (res) => {})();
  158. }
  159. next(null);
  160. },
  161. (next) => {
  162. playlists.getPlaylist(playlistId, (err, playlist) => {
  163. if (err || !playlist || playlist.createdBy !== userId) return next('Something went wrong while trying to get the playlist.');
  164. next(null, playlist);
  165. });
  166. }
  167. ],
  168. (err, playlist) => {
  169. if (err) return cb({ status: 'failure', message: err });
  170. else if (playlist.songs) return cb({ status: 'success', message: 'Playlist has been successfully added', data: playlist.songs });
  171. });
  172. }),*/
  173. removeSongFromPlaylist: hooks.loginRequired((session, songId, playlistId, cb, userId) => {
  174. playlists.getPlaylist(playlistId, (err, playlist) => {
  175. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  176. for (let z = 0; z < playlist.songs.length; z++) {
  177. if (playlist.songs[z]._id == songId) playlist.songs.shift(playlist.songs[z]);
  178. }
  179. db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
  180. if (err) {
  181. console.error(err);
  182. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  183. }
  184. playlists.updatePlaylist(playlistId, (err, playlist) => {
  185. cache.pub('playlist.removeSong', {playlistId: playlist._id, songId: songId, userId: userId});
  186. return cb({ status: 'success', message: 'Song has been successfully removed from playlist', data: playlist.songs });
  187. });
  188. });
  189. });
  190. }),
  191. updateDisplayName: hooks.loginRequired((session, _id, displayName, cb, userId) => {
  192. db.models.playlist.update({ _id, createdBy: userId }, { displayName }, (err, res) => {
  193. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  194. playlists.updatePlaylist(_id, (err) => {
  195. if (err) return cb({ status: 'failure', message: err});
  196. cache.pub('playlist.updateDisplayName', {playlistId: _id, displayName: displayName, userId: userId});
  197. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  198. })
  199. });
  200. }),/*
  201. promoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  202. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  203. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  204. let song = playlist.songs[fromIndex];
  205. playlist.songs.splice(fromIndex, 1);
  206. playlist.songs.splice((fromIndex + 1), 0, song);
  207. playlist.save(err => {
  208. if (err) {
  209. console.error(err);
  210. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  211. }
  212. playlists.updatePlaylist(playlistId, (err) => {
  213. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  214. return cb({ status: 'success', data: playlist.songs });
  215. });
  216. });
  217. });
  218. }),
  219. demoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  220. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  221. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  222. let song = playlist.songs[fromIndex];
  223. playlist.songs.splice(fromIndex, 1);
  224. playlist.songs.splice((fromIndex - 1), 0, song);
  225. playlist.save(err => {
  226. if (err) {
  227. console.error(err);
  228. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  229. }
  230. playlists.updatePlaylist(playlistId, (err) => {
  231. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  232. return cb({ status: 'success', data: playlist.songs });
  233. });
  234. });
  235. });
  236. }),*/
  237. remove: hooks.loginRequired((session, _id, cb, userId) => {
  238. db.models.playlist.remove({ _id, createdBy: userId }).exec(err => {
  239. if (err) return cb({ status: 'failure', message: 'Something went wrong when removing the playlist.'});
  240. cache.hdel('playlists', _id, () => {
  241. cache.pub('playlist.delete', {userId: userId, playlistId: _id});
  242. return cb({ status: 'success', message: 'Playlist successfully removed' });
  243. });
  244. });
  245. })
  246. };
  247. module.exports = lib;