playlists.js 13 KB

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