playlists.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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.moveSongToTop', res => {
  29. utils.socketsFromUser(res.userId, (sockets) => {
  30. sockets.forEach((socket) => {
  31. socket.emit('event:playlist.moveSongToTop', {playlistId: res.playlistId, songId: res.songId});
  32. });
  33. });
  34. });
  35. cache.sub('playlist.moveSongToBottom', res => {
  36. utils.socketsFromUser(res.userId, (sockets) => {
  37. sockets.forEach((socket) => {
  38. socket.emit('event:playlist.moveSongToBottom', {playlistId: res.playlistId, songId: res.songId});
  39. });
  40. });
  41. });
  42. cache.sub('playlist.addSong', res => {
  43. utils.socketsFromUser(res.userId, (sockets) => {
  44. sockets.forEach((socket) => {
  45. socket.emit('event:playlist.addSong', {playlistId: res.playlistId, song: res.song});
  46. });
  47. });
  48. });
  49. cache.sub('playlist.removeSong', res => {
  50. utils.socketsFromUser(res.userId, (sockets) => {
  51. sockets.forEach((socket) => {
  52. socket.emit('event:playlist.removeSong', {playlistId: res.playlistId, songId: res.songId});
  53. });
  54. });
  55. });
  56. cache.sub('playlist.updateDisplayName', res => {
  57. utils.socketsFromUser(res.userId, (sockets) => {
  58. sockets.forEach((socket) => {
  59. socket.emit('event:playlist.updateDisplayName', {playlistId: res.playlistId, displayName: res.displayName});
  60. });
  61. });
  62. });
  63. let lib = {
  64. indexForUser: hooks.loginRequired((session, cb, userId) => {
  65. db.models.playlist.find({ createdBy: userId }, (err, playlists) => {
  66. if (err) return cb({ status: 'failure', message: 'Something went wrong when getting the playlists.'});;
  67. cb({
  68. status: 'success',
  69. data: playlists
  70. });
  71. });
  72. }),
  73. create: hooks.loginRequired((session, data, cb, userId) => {
  74. async.waterfall([
  75. (next) => {
  76. return (data) ? next() : cb({ 'status': 'failure', 'message': 'Invalid data' });
  77. },
  78. (next) => {
  79. const { name, displayName, songs } = data;
  80. db.models.playlist.create({
  81. displayName,
  82. songs,
  83. createdBy: userId,
  84. createdAt: Date.now()
  85. }, next);
  86. }
  87. ], (err, playlist) => {
  88. if (err) return cb({ 'status': 'failure', 'message': 'Something went wrong'});
  89. cache.pub('playlist.create', playlist._id);
  90. return cb({ 'status': 'success', 'message': 'Successfully created playlist' });
  91. });
  92. }),
  93. getPlaylist: hooks.loginRequired((session, id, cb, userId) => {
  94. playlists.getPlaylist(id, (err, playlist) => {
  95. if (err || playlist.createdBy !== userId) return cb({status: 'success', message: 'Playlist not found.'});
  96. if (err == null) return cb({
  97. status: 'success',
  98. data: playlist
  99. });
  100. });
  101. }),
  102. //TODO Remove this
  103. update: hooks.loginRequired((session, _id, playlist, cb, userId) => {
  104. db.models.playlist.update({ _id, createdBy: userId }, playlist, (err, data) => {
  105. if (err) return cb({ status: 'failure', message: 'Something went wrong.' });
  106. playlists.updatePlaylist(_id, (err) => {
  107. if (err) return cb({ status: 'failure', message: 'Something went wrong.' });
  108. return cb({ status: 'success', message: 'Playlist has been successfully updated', data });
  109. });
  110. });
  111. }),
  112. addSongToPlaylist: hooks.loginRequired((session, songId, playlistId, cb, userId) => {
  113. console.log(songId);
  114. async.waterfall([
  115. (next) => {
  116. playlists.getPlaylist(playlistId, (err, playlist) => {
  117. if (err || !playlist || playlist.createdBy !== userId) return next('Something went wrong when trying to get the playlist.');
  118. let found = false;
  119. playlist.songs.forEach((song) => {
  120. if (songId === song._id) {
  121. found = true;
  122. }
  123. });
  124. if (found) return next('That song is already in the playlist.');
  125. return next(null);
  126. });
  127. },
  128. (next) => {
  129. songs.getSong(songId, (err, song) => {
  130. if (err) {
  131. utils.getSongFromYouTube(songId, (song) => {
  132. next(null, song);
  133. });
  134. } else {
  135. next(null, {
  136. _id: songId,
  137. title: song.title,
  138. duration: song.duration
  139. });
  140. }
  141. });
  142. },
  143. (newSong, next) => {
  144. db.models.playlist.update({_id: playlistId}, {$push: {songs: newSong}}, (err) => {
  145. if (err) {
  146. console.error(err);
  147. return next('Failed to add song to playlist');
  148. }
  149. playlists.updatePlaylist(playlistId, (err, playlist) => {
  150. next(err, playlist, newSong);
  151. });
  152. });
  153. }
  154. ],
  155. (err, playlist, newSong) => {
  156. if (err) return cb({ status: 'error', message: err });
  157. else if (playlist.songs) {
  158. cache.pub('playlist.addSong', {playlistId: playlist._id, song: newSong, userId: userId});
  159. return cb({ status: 'success', message: 'Song has been successfully added to the playlist', data: playlist.songs });
  160. }
  161. });
  162. }),
  163. addSetToPlaylist: hooks.loginRequired((session, url, playlistId, cb, userId) => {
  164. async.waterfall([
  165. (next) => {
  166. utils.getPlaylistFromYouTube(url, songs => {
  167. next(null, songs);
  168. });
  169. },
  170. (songs, next) => {
  171. let processed = 0;
  172. function checkDone() {
  173. if (processed === songs.length) {
  174. next();
  175. }
  176. }
  177. for (let s = 0; s < songs.length; s++) {
  178. lib.addSongToPlaylist(session, songs[s].contentDetails.videoId, playlistId, () => {
  179. processed++;
  180. checkDone();
  181. });
  182. }
  183. },
  184. (next) => {
  185. playlists.getPlaylist(playlistId, (err, playlist) => {
  186. if (err || !playlist || playlist.createdBy !== userId) return next('Something went wrong while trying to get the playlist.');
  187. next(null, playlist);
  188. });
  189. }
  190. ],
  191. (err, playlist) => {
  192. if (err) return cb({ status: 'failure', message: err });
  193. else if (playlist.songs) return cb({ status: 'success', message: 'Playlist has been successfully imported.', data: playlist.songs });
  194. });
  195. }),
  196. removeSongFromPlaylist: hooks.loginRequired((session, songId, playlistId, cb, userId) => {
  197. playlists.getPlaylist(playlistId, (err, playlist) => {
  198. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  199. for (let z = 0; z < playlist.songs.length; z++) {
  200. if (playlist.songs[z]._id == songId) playlist.songs.shift(playlist.songs[z]);
  201. }
  202. db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
  203. if (err) {
  204. console.error(err);
  205. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  206. }
  207. playlists.updatePlaylist(playlistId, (err, playlist) => {
  208. cache.pub('playlist.removeSong', {playlistId: playlist._id, songId: songId, userId: userId});
  209. return cb({ status: 'success', message: 'Song has been successfully removed from playlist', data: playlist.songs });
  210. });
  211. });
  212. });
  213. }),
  214. updateDisplayName: hooks.loginRequired((session, _id, displayName, cb, userId) => {
  215. db.models.playlist.update({ _id, createdBy: userId }, { displayName }, (err, res) => {
  216. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  217. playlists.updatePlaylist(_id, (err) => {
  218. if (err) return cb({ status: 'failure', message: err});
  219. cache.pub('playlist.updateDisplayName', {playlistId: _id, displayName: displayName, userId: userId});
  220. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  221. })
  222. });
  223. }),
  224. moveSongToTop: hooks.loginRequired((session, playlistId, songId, cb, userId) => {
  225. playlists.getPlaylist(playlistId, (err, playlist) => {
  226. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  227. let found = false;
  228. let foundSong;
  229. playlist.songs.forEach((song) => {
  230. if (song._id === songId) {
  231. foundSong = song;
  232. found = true;
  233. }
  234. });
  235. if (found) {
  236. db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
  237. console.log(err);
  238. if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
  239. db.models.playlist.update({_id: playlistId}, {
  240. $push: {
  241. songs: {
  242. $each: [foundSong],
  243. $position: 0
  244. }
  245. }
  246. }, (err) => {
  247. console.log(err);
  248. if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
  249. playlists.updatePlaylist(playlistId, (err) => {
  250. if (err) return cb({ status: 'failure', message: err});
  251. cache.pub('playlist.moveSongToTop', {playlistId, songId, userId: userId});
  252. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  253. })
  254. });
  255. });
  256. } else {
  257. return cb({status: 'failure', message: 'Song not found.'});
  258. }
  259. });
  260. }),
  261. moveSongToBottom: hooks.loginRequired((session, playlistId, songId, cb, userId) => {
  262. playlists.getPlaylist(playlistId, (err, playlist) => {
  263. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  264. let found = false;
  265. let foundSong;
  266. playlist.songs.forEach((song) => {
  267. if (song._id === songId) {
  268. foundSong = song;
  269. found = true;
  270. }
  271. });
  272. if (found) {
  273. db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
  274. console.log(err);
  275. if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
  276. db.models.playlist.update({_id: playlistId}, {
  277. $push: { songs: foundSong }
  278. }, (err) => {
  279. console.log(err);
  280. if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
  281. playlists.updatePlaylist(playlistId, (err) => {
  282. if (err) return cb({ status: 'failure', message: err});
  283. cache.pub('playlist.moveSongToBottom', {playlistId, songId, userId: userId});
  284. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  285. })
  286. });
  287. });
  288. } else {
  289. return cb({status: 'failure', message: 'Song not found.'});
  290. }
  291. });
  292. }),
  293. /*
  294. promoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  295. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  296. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  297. let song = playlist.songs[fromIndex];
  298. playlist.songs.splice(fromIndex, 1);
  299. playlist.songs.splice((fromIndex + 1), 0, song);
  300. playlist.save(err => {
  301. if (err) {
  302. console.error(err);
  303. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  304. }
  305. playlists.updatePlaylist(playlistId, (err) => {
  306. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  307. return cb({ status: 'success', data: playlist.songs });
  308. });
  309. });
  310. });
  311. }),
  312. demoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  313. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  314. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  315. let song = playlist.songs[fromIndex];
  316. playlist.songs.splice(fromIndex, 1);
  317. playlist.songs.splice((fromIndex - 1), 0, song);
  318. playlist.save(err => {
  319. if (err) {
  320. console.error(err);
  321. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  322. }
  323. playlists.updatePlaylist(playlistId, (err) => {
  324. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  325. return cb({ status: 'success', data: playlist.songs });
  326. });
  327. });
  328. });
  329. }),*/
  330. remove: hooks.loginRequired((session, _id, cb, userId) => {
  331. db.models.playlist.remove({ _id, createdBy: userId }).exec(err => {
  332. if (err) return cb({ status: 'failure', message: 'Something went wrong when removing the playlist.'});
  333. cache.hdel('playlists', _id, () => {
  334. cache.pub('playlist.delete', {userId: userId, playlistId: _id});
  335. return cb({ status: 'success', message: 'Playlist successfully removed' });
  336. });
  337. });
  338. })
  339. };
  340. module.exports = lib;