playlists.js 13 KB

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