queueSongs.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. import config from "config";
  2. import async from "async";
  3. import { isAdminRequired, isLoginRequired } from "./hooks";
  4. import moduleManager from "../../index";
  5. const DBModule = moduleManager.modules.db;
  6. const UtilsModule = moduleManager.modules.utils;
  7. const WSModule = moduleManager.modules.ws;
  8. const YouTubeModule = moduleManager.modules.youtube;
  9. const CacheModule = moduleManager.modules.cache;
  10. // CacheModule.runJob("SUB", {
  11. // channel: "queue.newSong",
  12. // cb: async songId => {
  13. // const queueSongModel = await DBModule.runJob("GET_MODEL", {
  14. // modelName: "queueSong"
  15. // });
  16. // queueSongModel.findOne({ _id: songId }, (err, song) => {
  17. // WSModule.runJob("EMIT_TO_ROOM", {
  18. // room: "admin.queue",
  19. // args: ["event:admin.queueSong.added", song]
  20. // });
  21. // });
  22. // }
  23. // });
  24. // CacheModule.runJob("SUB", {
  25. // channel: "queue.removedSong",
  26. // cb: songId => {
  27. // WSModule.runJob("EMIT_TO_ROOM", {
  28. // room: "admin.queue",
  29. // args: ["event:admin.queueSong.removed", songId]
  30. // });
  31. // }
  32. // });
  33. // CacheModule.runJob("SUB", {
  34. // channel: "queue.update",
  35. // cb: async songId => {
  36. // const queueSongModel = await DBModule.runJob("GET_MODEL", {
  37. // modelName: "queueSong"
  38. // });
  39. // queueSongModel.findOne({ _id: songId }, (err, song) => {
  40. // WSModule.runJob("EMIT_TO_ROOM", {
  41. // room: "admin.queue",
  42. // args: ["event:admin.queueSong.updated", song]
  43. // });
  44. // });
  45. // }
  46. // });
  47. export default {
  48. // /**
  49. // * Returns the length of the queue songs list
  50. // *
  51. // * @param session
  52. // * @param cb
  53. // */
  54. // length: isAdminRequired(async function length(session, cb) {
  55. // const queueSongModel = await DBModule.runJob("GET_MODEL", { modelName: "queueSong" }, this);
  56. // async.waterfall(
  57. // [
  58. // next => {
  59. // queueSongModel.countDocuments({}, next);
  60. // }
  61. // ],
  62. // async (err, count) => {
  63. // if (err) {
  64. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  65. // this.log("ERROR", "QUEUE_SONGS_LENGTH", `Failed to get length from queue songs. "${err}"`);
  66. // return cb({ status: "failure", message: err });
  67. // }
  68. // this.log("SUCCESS", "QUEUE_SONGS_LENGTH", `Got length from queue songs successfully.`);
  69. // return cb(count);
  70. // }
  71. // );
  72. // }),
  73. // /**
  74. // * Gets a set of queue songs
  75. // *
  76. // * @param session
  77. // * @param set - the set number to return
  78. // * @param cb
  79. // */
  80. // getSet: isAdminRequired(async function getSet(session, set, cb) {
  81. // const queueSongModel = await DBModule.runJob(
  82. // "GET_MODEL",
  83. // {
  84. // modelName: "queueSong"
  85. // },
  86. // this
  87. // );
  88. // async.waterfall(
  89. // [
  90. // next => {
  91. // queueSongModel
  92. // .find({})
  93. // .skip(15 * (set - 1))
  94. // .limit(15)
  95. // .exec(next);
  96. // }
  97. // ],
  98. // async (err, songs) => {
  99. // if (err) {
  100. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  101. // this.log("ERROR", "QUEUE_SONGS_GET_SET", `Failed to get set from queue songs. "${err}"`);
  102. // return cb({ status: "failure", message: err });
  103. // }
  104. // this.log("SUCCESS", "QUEUE_SONGS_GET_SET", `Got set from queue songs successfully.`);
  105. // return cb(songs);
  106. // }
  107. // );
  108. // }),
  109. // /**
  110. // * Gets a song from the Musare song id
  111. // *
  112. // * @param {object} session - the session object automatically added by the websocket
  113. // * @param {string} songId - the Musare song id
  114. // * @param {Function} cb
  115. // */
  116. // getSongFromMusareId: isAdminRequired(async function getSong(session, songId, cb) {
  117. // const queueSongModel = await DBModule.runJob(
  118. // "GET_MODEL",
  119. // {
  120. // modelName: "queueSong"
  121. // },
  122. // this
  123. // );
  124. // async.waterfall(
  125. // [
  126. // next => {
  127. // queueSongModel.findOne({ _id: songId }, next);
  128. // }
  129. // ],
  130. // async (err, song) => {
  131. // if (err) {
  132. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  133. // this.log("ERROR", "QUEUE_SONGS_GET_SONG_FROM_MUSARE_ID", `Failed to get song ${songId}. "${err}"`);
  134. // return cb({ status: "failure", message: err });
  135. // }
  136. // this.log("SUCCESS", "QUEUE_SONGS_GET_SONG_FROM_MUSARE_ID", `Got song ${songId} successfully.`);
  137. // return cb({ status: "success", data: { song } });
  138. // }
  139. // );
  140. // }),
  141. // /**
  142. // * Updates a queuesong
  143. // *
  144. // * @param {object} session - the session object automatically added by the websocket
  145. // * @param {string} songId - the id of the queuesong that gets updated
  146. // * @param {object} updatedSong - the object of the updated queueSong
  147. // * @param {Function} cb - gets called with the result
  148. // */
  149. // update: isAdminRequired(async function update(session, songId, updatedSong, cb) {
  150. // const queueSongModel = await DBModule.runJob(
  151. // "GET_MODEL",
  152. // {
  153. // modelName: "queueSong"
  154. // },
  155. // this
  156. // );
  157. // async.waterfall(
  158. // [
  159. // next => {
  160. // queueSongModel.findOne({ _id: songId }, next);
  161. // },
  162. // (song, next) => {
  163. // if (!song) return next("Song not found");
  164. // let updated = false;
  165. // const $set = {};
  166. // Object.keys(updatedSong).forEach(prop => {
  167. // if (updatedSong[prop] !== song[prop]) $set[prop] = updatedSong[prop];
  168. // });
  169. // updated = true;
  170. // if (!updated) return next("No properties changed");
  171. // return queueSongModel.updateOne({ _id: songId }, { $set }, { runValidators: true }, next);
  172. // }
  173. // ],
  174. // async err => {
  175. // if (err) {
  176. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  177. // this.log(
  178. // "ERROR",
  179. // "QUEUE_UPDATE",
  180. // `Updating queuesong "${songId}" failed for user ${session.userId}. "${err}"`
  181. // );
  182. // return cb({ status: "failure", message: err });
  183. // }
  184. // CacheModule.runJob("PUB", { channel: "queue.update", value: songId });
  185. // this.log(
  186. // "SUCCESS",
  187. // "QUEUE_UPDATE",
  188. // `User "${session.userId}" successfully update queuesong "${songId}".`
  189. // );
  190. // return cb({
  191. // status: "success",
  192. // message: "Successfully updated song."
  193. // });
  194. // }
  195. // );
  196. // }),
  197. // /**
  198. // * Removes a queuesong
  199. // *
  200. // * @param {object} session - the session object automatically added by the websocket
  201. // * @param {string} songId - the id of the queuesong that gets removed
  202. // * @param {Function} cb - gets called with the result
  203. // */
  204. // remove: isAdminRequired(async function remove(session, songId, cb) {
  205. // const queueSongModel = await DBModule.runJob(
  206. // "GET_MODEL",
  207. // {
  208. // modelName: "queueSong"
  209. // },
  210. // this
  211. // );
  212. // async.waterfall(
  213. // [
  214. // next => {
  215. // queueSongModel.deleteOne({ _id: songId }, next);
  216. // }
  217. // ],
  218. // async err => {
  219. // if (err) {
  220. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  221. // this.log(
  222. // "ERROR",
  223. // "QUEUE_REMOVE",
  224. // `Removing queuesong "${songId}" failed for user ${session.userId}. "${err}"`
  225. // );
  226. // return cb({ status: "failure", message: err });
  227. // }
  228. // CacheModule.runJob("PUB", {
  229. // channel: "queue.removedSong",
  230. // value: songId
  231. // });
  232. // this.log(
  233. // "SUCCESS",
  234. // "QUEUE_REMOVE",
  235. // `User "${session.userId}" successfully removed queuesong "${songId}".`
  236. // );
  237. // return cb({
  238. // status: "success",
  239. // message: "Successfully updated song."
  240. // });
  241. // }
  242. // );
  243. // }),
  244. // /**
  245. // * Creates a queuesong
  246. // *
  247. // * @param {object} session - the session object automatically added by the websocket
  248. // * @param {string} songId - the id of the song that gets added
  249. // * @param {Function} cb - gets called with the result
  250. // */
  251. // add: isLoginRequired(async function add(session, songId, cb) {
  252. // const requestedAt = Date.now();
  253. // const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  254. // const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  255. // const QueueSongModel = await DBModule.runJob(
  256. // "GET_MODEL",
  257. // {
  258. // modelName: "queueSong"
  259. // },
  260. // this
  261. // );
  262. // async.waterfall(
  263. // [
  264. // next => {
  265. // QueueSongModel.findOne({ songId }, next);
  266. // },
  267. // (song, next) => {
  268. // if (song) return next("This song is already in the queue.");
  269. // return songModel.findOne({ songId }, next);
  270. // },
  271. // // Get YouTube data from id
  272. // (song, next) => {
  273. // if (song) return next("This song has already been added.");
  274. // // TODO Add err object as first param of callback
  275. // return YouTubeModule.runJob("GET_SONG", { songId }, this)
  276. // .then(response => {
  277. // const { song } = response;
  278. // song.duration = -1;
  279. // song.artists = [];
  280. // song.genres = [];
  281. // song.skipDuration = 0;
  282. // song.thumbnail = `${config.get("domain")}/assets/notes.png`;
  283. // song.explicit = false;
  284. // song.requestedBy = session.userId;
  285. // song.requestedAt = requestedAt;
  286. // next(null, song);
  287. // })
  288. // .catch(next);
  289. // },
  290. // (newSong, next) => {
  291. // const song = new QueueSongModel(newSong);
  292. // song.save({ validateBeforeSave: false }, (err, song) => {
  293. // if (err) return next(err);
  294. // return next(null, song);
  295. // });
  296. // },
  297. // (newSong, next) => {
  298. // userModel.findOne({ _id: session.userId }, (err, user) => {
  299. // if (err) return next(err, newSong);
  300. // user.statistics.songsRequested += 1;
  301. // return user.save(err => {
  302. // if (err) return next(err, newSong);
  303. // return next(null, newSong);
  304. // });
  305. // });
  306. // }
  307. // ],
  308. // async (err, newSong) => {
  309. // if (err) {
  310. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  311. // this.log(
  312. // "ERROR",
  313. // "QUEUE_ADD",
  314. // `Adding queuesong "${songId}" failed for user ${session.userId}. "${err}"`
  315. // );
  316. // return cb({ status: "failure", message: err });
  317. // }
  318. // CacheModule.runJob("PUB", {
  319. // channel: "queue.newSong",
  320. // value: newSong._id
  321. // });
  322. // this.log("SUCCESS", "QUEUE_ADD", `User "${session.userId}" successfully added queuesong "${songId}".`);
  323. // return cb({
  324. // status: "success",
  325. // message: "Successfully added that song to the queue"
  326. // });
  327. // }
  328. // );
  329. // }),
  330. // /**
  331. // * Adds a set of songs to the queue
  332. // *
  333. // * @param {object} session - the session object automatically added by the websocket
  334. // * @param {string} url - the url of the the YouTube playlist
  335. // * @param {boolean} musicOnly - whether to only get music from the playlist
  336. // * @param {Function} cb - gets called with the result
  337. // */
  338. // addSetToQueue: isLoginRequired(function addSetToQueue(session, url, musicOnly, cb) {
  339. // async.waterfall(
  340. // [
  341. // next => {
  342. // YouTubeModule.runJob(
  343. // "GET_PLAYLIST",
  344. // {
  345. // url,
  346. // musicOnly
  347. // },
  348. // this
  349. // )
  350. // .then(res => {
  351. // next(null, res.songs);
  352. // })
  353. // .catch(next);
  354. // },
  355. // (songIds, next) => {
  356. // let successful = 0;
  357. // let failed = 0;
  358. // let alreadyInQueue = 0;
  359. // let alreadyAdded = 0;
  360. // if (songIds.length === 0) next();
  361. // async.eachLimit(
  362. // songIds,
  363. // 1,
  364. // (songId, next) => {
  365. // WSModule.runJob(
  366. // "RUN_ACTION2",
  367. // {
  368. // session,
  369. // namespace: "queueSongs",
  370. // action: "add",
  371. // args: [songId]
  372. // },
  373. // this
  374. // )
  375. // .then(res => {
  376. // if (res.status === "success") successful += 1;
  377. // else failed += 1;
  378. // if (res.message === "This song is already in the queue.") alreadyInQueue += 1;
  379. // if (res.message === "This song has already been added.") alreadyAdded += 1;
  380. // })
  381. // .catch(() => {
  382. // failed += 1;
  383. // })
  384. // .finally(() => {
  385. // next();
  386. // });
  387. // },
  388. // () => {
  389. // next(null, { successful, failed, alreadyInQueue, alreadyAdded });
  390. // }
  391. // );
  392. // }
  393. // ],
  394. // async (err, response) => {
  395. // if (err) {
  396. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  397. // this.log(
  398. // "ERROR",
  399. // "QUEUE_IMPORT",
  400. // `Importing a YouTube playlist to the queue failed for user "${session.userId}". "${err}"`
  401. // );
  402. // return cb({ status: "failure", message: err });
  403. // }
  404. // this.log(
  405. // "SUCCESS",
  406. // "QUEUE_IMPORT",
  407. // `Successfully imported a YouTube playlist to the queue for user "${session.userId}".`
  408. // );
  409. // return cb({
  410. // status: "success",
  411. // message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInQueue} were already in queue, ${response.alreadyAdded} were already added)`
  412. // });
  413. // }
  414. // );
  415. // })
  416. };