playlists.js 13 KB

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