playlists.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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) next();
  174. }
  175. for (let s = 0; s < songs.length; s++) {
  176. lib.addSongToPlaylist(session, songs[s].contentDetails.videoId, playlistId, () => {
  177. processed++;
  178. checkDone();
  179. });
  180. }
  181. },
  182. (next) => {
  183. playlists.getPlaylist(playlistId, (err, playlist) => {
  184. if (err || !playlist || playlist.createdBy !== userId) return next('Something went wrong while trying to get the playlist');
  185. next(null, playlist);
  186. });
  187. }
  188. ],
  189. (err, playlist) => {
  190. if (err) return cb({ status: 'failure', message: err });
  191. else if (playlist.songs) return cb({ status: 'success', message: 'Playlist has been successfully imported.', data: playlist.songs });
  192. });
  193. }),
  194. removeSongFromPlaylist: hooks.loginRequired((session, songId, playlistId, cb, userId) => {
  195. playlists.getPlaylist(playlistId, (err, playlist) => {
  196. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist'});
  197. for (let z = 0; z < playlist.songs.length; z++) {
  198. if (playlist.songs[z]._id == songId) playlist.songs.shift(playlist.songs[z]);
  199. }
  200. db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
  201. if (err) {
  202. console.error(err);
  203. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  204. }
  205. playlists.updatePlaylist(playlistId, (err, playlist) => {
  206. cache.pub('playlist.removeSong', {playlistId: playlist._id, songId: songId, userId: userId});
  207. return cb({ status: 'success', message: 'Song has been successfully removed from playlist', data: playlist.songs });
  208. });
  209. });
  210. });
  211. }),
  212. updateDisplayName: hooks.loginRequired((session, _id, displayName, cb, userId) => {
  213. db.models.playlist.update({ _id, createdBy: userId }, { displayName }, (err, res) => {
  214. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  215. playlists.updatePlaylist(_id, (err) => {
  216. if (err) return cb({ status: 'failure', message: err});
  217. cache.pub('playlist.updateDisplayName', {playlistId: _id, displayName: displayName, userId: userId});
  218. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  219. })
  220. });
  221. }),
  222. moveSongToTop: hooks.loginRequired((session, playlistId, songId, cb, userId) => {
  223. playlists.getPlaylist(playlistId, (err, playlist) => {
  224. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist'});
  225. let found = false;
  226. let foundSong;
  227. playlist.songs.forEach((song) => {
  228. if (song._id === songId) {
  229. foundSong = song;
  230. found = true;
  231. }
  232. });
  233. if (found) {
  234. db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
  235. console.log(err);
  236. if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song'});
  237. db.models.playlist.update({_id: playlistId}, {
  238. $push: {
  239. songs: {
  240. $each: [foundSong],
  241. $position: 0
  242. }
  243. }
  244. }, (err) => {
  245. console.log(err);
  246. if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song'});
  247. playlists.updatePlaylist(playlistId, (err) => {
  248. if (err) return cb({ status: 'failure', message: err});
  249. cache.pub('playlist.moveSongToTop', {playlistId, songId, userId: userId});
  250. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  251. })
  252. });
  253. });
  254. } else {
  255. return cb({status: 'failure', message: 'Song not found.'});
  256. }
  257. });
  258. }),
  259. moveSongToBottom: hooks.loginRequired((session, playlistId, songId, cb, userId) => {
  260. playlists.getPlaylist(playlistId, (err, playlist) => {
  261. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist'});
  262. let found = false;
  263. let foundSong;
  264. playlist.songs.forEach((song) => {
  265. if (song._id === songId) {
  266. foundSong = song;
  267. found = true;
  268. }
  269. });
  270. if (found) {
  271. db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
  272. console.log(err);
  273. if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song'});
  274. db.models.playlist.update({_id: playlistId}, {
  275. $push: { songs: foundSong }
  276. }, (err) => {
  277. console.log(err);
  278. if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song'});
  279. playlists.updatePlaylist(playlistId, (err) => {
  280. if (err) return cb({ status: 'failure', message: err});
  281. cache.pub('playlist.moveSongToBottom', {playlistId, songId, userId: userId});
  282. return cb({ status: 'success', message: 'Playlist has been successfully updated' });
  283. })
  284. });
  285. });
  286. } else return cb({status: 'failure', message: 'Song not found'});
  287. });
  288. }),
  289. /*
  290. promoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  291. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  292. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  293. let song = playlist.songs[fromIndex];
  294. playlist.songs.splice(fromIndex, 1);
  295. playlist.songs.splice((fromIndex + 1), 0, song);
  296. playlist.save(err => {
  297. if (err) {
  298. console.error(err);
  299. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  300. }
  301. playlists.updatePlaylist(playlistId, (err) => {
  302. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  303. return cb({ status: 'success', data: playlist.songs });
  304. });
  305. });
  306. });
  307. }),
  308. demoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
  309. db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
  310. if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
  311. let song = playlist.songs[fromIndex];
  312. playlist.songs.splice(fromIndex, 1);
  313. playlist.songs.splice((fromIndex - 1), 0, song);
  314. playlist.save(err => {
  315. if (err) {
  316. console.error(err);
  317. return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  318. }
  319. playlists.updatePlaylist(playlistId, (err) => {
  320. if (err) return cb({ status: 'failure', message: 'Something went wrong when saving the playlist.'});
  321. return cb({ status: 'success', data: playlist.songs });
  322. });
  323. });
  324. });
  325. }),*/
  326. remove: hooks.loginRequired((session, _id, cb, userId) => {
  327. db.models.playlist.remove({ _id, createdBy: userId }).exec(err => {
  328. if (err) return cb({ status: 'failure', message: 'Something went wrong when removing the playlist.'});
  329. cache.hdel('playlists', _id, () => {
  330. cache.pub('playlist.delete', {userId: userId, playlistId: _id});
  331. return cb({ status: 'success', message: 'Playlist successfully removed' });
  332. });
  333. });
  334. })
  335. };
  336. module.exports = lib;