songs.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. import async from "async";
  2. import config from "config";
  3. import mongoose from "mongoose";
  4. import CoreClass from "../core";
  5. let SongsModule;
  6. let CacheModule;
  7. let DBModule;
  8. let UtilsModule;
  9. let YouTubeModule;
  10. let StationsModule;
  11. let PlaylistsModule;
  12. class ErrorWithData extends Error {
  13. /**
  14. * @param {string} message - the error message
  15. * @param {object} data - the error data
  16. */
  17. constructor(message, data) {
  18. super(message);
  19. this.data = data;
  20. }
  21. }
  22. class _SongsModule extends CoreClass {
  23. // eslint-disable-next-line require-jsdoc
  24. constructor() {
  25. super("songs");
  26. SongsModule = this;
  27. }
  28. /**
  29. * Initialises the songs module
  30. *
  31. * @returns {Promise} - returns promise (reject, resolve)
  32. */
  33. async initialize() {
  34. this.setStage(1);
  35. CacheModule = this.moduleManager.modules.cache;
  36. DBModule = this.moduleManager.modules.db;
  37. UtilsModule = this.moduleManager.modules.utils;
  38. YouTubeModule = this.moduleManager.modules.youtube;
  39. StationsModule = this.moduleManager.modules.stations;
  40. PlaylistsModule = this.moduleManager.modules.playlists;
  41. this.SongModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
  42. this.SongSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "song" });
  43. this.setStage(2);
  44. return new Promise((resolve, reject) =>
  45. async.waterfall(
  46. [
  47. next => {
  48. this.setStage(2);
  49. CacheModule.runJob("HGETALL", { table: "songs" })
  50. .then(songs => {
  51. next(null, songs);
  52. })
  53. .catch(next);
  54. },
  55. (songs, next) => {
  56. this.setStage(3);
  57. if (!songs) return next();
  58. const youtubeIds = Object.keys(songs);
  59. return async.each(
  60. youtubeIds,
  61. (youtubeId, next) => {
  62. SongsModule.SongModel.findOne({ youtubeId }, (err, song) => {
  63. if (err) next(err);
  64. else if (!song)
  65. CacheModule.runJob("HDEL", {
  66. table: "songs",
  67. key: youtubeId
  68. })
  69. .then(() => next())
  70. .catch(next);
  71. else next();
  72. });
  73. },
  74. next
  75. );
  76. },
  77. next => {
  78. this.setStage(4);
  79. SongsModule.SongModel.find({}, next);
  80. },
  81. (songs, next) => {
  82. this.setStage(5);
  83. async.each(
  84. songs,
  85. (song, next) => {
  86. CacheModule.runJob("HSET", {
  87. table: "songs",
  88. key: song.youtubeId,
  89. value: SongsModule.SongSchemaCache(song)
  90. })
  91. .then(() => next())
  92. .catch(next);
  93. },
  94. next
  95. );
  96. }
  97. ],
  98. async err => {
  99. if (err) {
  100. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  101. reject(new Error(err));
  102. } else resolve();
  103. }
  104. )
  105. );
  106. }
  107. /**
  108. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  109. *
  110. * @param {object} payload - object containing the payload
  111. * @param {string} payload.songId - the id of the song we are trying to get
  112. * @returns {Promise} - returns a promise (resolve, reject)
  113. */
  114. GET_SONG(payload) {
  115. return new Promise((resolve, reject) =>
  116. async.waterfall(
  117. [
  118. next => {
  119. if (!mongoose.Types.ObjectId.isValid(payload.songId))
  120. return next("songId is not a valid ObjectId.");
  121. return CacheModule.runJob("HGET", { table: "songs", key: payload.songId }, this)
  122. .then(song => next(null, song))
  123. .catch(next);
  124. },
  125. (song, next) => {
  126. if (song) return next(true, song);
  127. return SongsModule.SongModel.findOne({ _id: payload.songId }, next);
  128. },
  129. (song, next) => {
  130. if (song) {
  131. CacheModule.runJob(
  132. "HSET",
  133. {
  134. table: "songs",
  135. key: payload.songId,
  136. value: song
  137. },
  138. this
  139. ).then(song => next(null, song));
  140. } else next("Song not found.");
  141. }
  142. ],
  143. (err, song) => {
  144. if (err && err !== true) return reject(new Error(err));
  145. return resolve({ song });
  146. }
  147. )
  148. );
  149. }
  150. /**
  151. * Gets songs by id from Mongo
  152. *
  153. * @param {object} payload - object containing the payload
  154. * @param {string} payload.songIds - the ids of the songs we are trying to get
  155. * @param {string} payload.properties - the properties to return
  156. * @returns {Promise} - returns a promise (resolve, reject)
  157. */
  158. GET_SONGS(payload) {
  159. return new Promise((resolve, reject) =>
  160. async.waterfall(
  161. [
  162. next => {
  163. if (!payload.songIds.every(songId => mongoose.Types.ObjectId.isValid(songId)))
  164. next("One or more songIds are not a valid ObjectId.");
  165. else next();
  166. },
  167. next => {
  168. const includeProperties = {};
  169. payload.properties.forEach(property => {
  170. includeProperties[property] = true;
  171. });
  172. return SongsModule.SongModel.find(
  173. {
  174. _id: { $in: payload.songIds }
  175. },
  176. includeProperties,
  177. next
  178. );
  179. }
  180. ],
  181. (err, songs) => {
  182. if (err && err !== true) return reject(new Error(err));
  183. return resolve({ songs });
  184. }
  185. )
  186. );
  187. }
  188. /**
  189. * Gets songs data
  190. *
  191. * @param {object} payload - object containing the payload
  192. * @param {string} payload.page - the page
  193. * @param {string} payload.pageSize - the page size
  194. * @param {string} payload.properties - the properties to return for each song
  195. * @param {string} payload.sort - the sort object
  196. * @param {string} payload.queries - the queries array
  197. * @param {string} payload.operator - the operator for queries
  198. * @returns {Promise} - returns a promise (resolve, reject)
  199. */
  200. GET_DATA(payload) {
  201. return new Promise((resolve, reject) => {
  202. async.waterfall(
  203. [
  204. // Creates pipeline array
  205. next => next(null, []),
  206. // If a filter exists for requestedBy, add requestedByUsername property to all documents
  207. (pipeline, next) => {
  208. const { queries } = payload;
  209. // Check if a filter with the requestedBy property exists
  210. const requestedByFilterExists =
  211. queries.map(query => query.filter.property).indexOf("requestedBy") !== -1;
  212. // If no such filter exists, skip this function
  213. if (!requestedByFilterExists) return next(null, pipeline);
  214. // Adds requestedByOID field, which is an ObjectId version of requestedBy
  215. pipeline.push({
  216. $addFields: {
  217. requestedByOID: {
  218. $convert: {
  219. input: "$requestedBy",
  220. to: "objectId",
  221. onError: "unknown",
  222. onNull: "unknown"
  223. }
  224. }
  225. }
  226. });
  227. // Looks up user(s) with the same _id as the requestedByOID and puts the result in the requestedByUser field
  228. pipeline.push({
  229. $lookup: {
  230. from: "users",
  231. localField: "requestedByOID",
  232. foreignField: "_id",
  233. as: "requestedByUser"
  234. }
  235. });
  236. // Unwinds the requestedByUser array field into an object
  237. pipeline.push({
  238. $unwind: {
  239. path: "$requestedByUser",
  240. preserveNullAndEmptyArrays: true
  241. }
  242. });
  243. // Adds requestedByUsername field from the requestedByUser username, or unknown if it doesn't exist
  244. pipeline.push({
  245. $addFields: {
  246. requestedByUsername: {
  247. $ifNull: ["$requestedByUser.username", "unknown"]
  248. }
  249. }
  250. });
  251. // Removes the requestedByOID and requestedByUser property, just in case it doesn't get removed at a later stage
  252. pipeline.push({
  253. $project: {
  254. requestedByOID: 0,
  255. requestedByUser: 0
  256. }
  257. });
  258. return next(null, pipeline);
  259. },
  260. // If a filter exists for createdBy, add createdByUsername property to all documents
  261. (pipeline, next) => {
  262. const { queries } = payload;
  263. // Check if a filter with the createdBy property exists
  264. const createdByFilterExists =
  265. queries.map(query => query.filter.property).indexOf("createdBy") !== -1;
  266. // If no such filter exists, skip this function
  267. if (!createdByFilterExists) return next(null, pipeline);
  268. // Adds createdByOID field, which is an ObjectId version of createdBy
  269. pipeline.push({
  270. $addFields: {
  271. createdByOID: {
  272. $convert: {
  273. input: "$createdBy",
  274. to: "objectId",
  275. onError: "unknown",
  276. onNull: "unknown"
  277. }
  278. }
  279. }
  280. });
  281. // Looks up user(s) with the same _id as the createdByOID and puts the result in the createdByUser field
  282. pipeline.push({
  283. $lookup: {
  284. from: "users",
  285. localField: "createdByOID",
  286. foreignField: "_id",
  287. as: "createdByUser"
  288. }
  289. });
  290. // Unwinds the createdByUser array field into an object
  291. pipeline.push({
  292. $unwind: {
  293. path: "$createdByUser",
  294. preserveNullAndEmptyArrays: true
  295. }
  296. });
  297. // Adds createdByUsername field from the createdByUser username, or unknown if it doesn't exist
  298. pipeline.push({
  299. $addFields: {
  300. createdByUsername: {
  301. $ifNull: ["$createdByUser.username", "unknown"]
  302. }
  303. }
  304. });
  305. // Removes the createdByOID and createdByUser property, just in case it doesn't get removed at a later stage
  306. pipeline.push({
  307. $project: {
  308. createdByOID: 0,
  309. createdByUser: 0
  310. }
  311. });
  312. return next(null, pipeline);
  313. },
  314. // Adds the match stage to aggregation pipeline, which is responsible for filtering
  315. (pipeline, next) => {
  316. const { queries, operator } = payload;
  317. let queryError;
  318. const newQueries = queries.flatMap(query => {
  319. const { data, filter, filterType } = query;
  320. const newQuery = {};
  321. if (filterType === "regex") {
  322. newQuery[filter.property] = new RegExp(`${data.slice(1, data.length - 1)}`, "i");
  323. } else if (filterType === "contains") {
  324. newQuery[filter.property] = new RegExp(
  325. `${data.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&")}`,
  326. "i"
  327. );
  328. } else if (filterType === "exact") {
  329. newQuery[filter.property] = data.toString();
  330. } else if (filterType === "datetimeBefore") {
  331. newQuery[filter.property] = { $lte: new Date(data) };
  332. } else if (filterType === "datetimeAfter") {
  333. newQuery[filter.property] = { $gte: new Date(data) };
  334. } else if (filterType === "numberLesserEqual") {
  335. newQuery[filter.property] = { $lte: data };
  336. } else if (filterType === "numberLesser") {
  337. newQuery[filter.property] = { $lt: data };
  338. } else if (filterType === "numberGreater") {
  339. newQuery[filter.property] = { $gt: data };
  340. } else if (filterType === "numberGreaterEqual") {
  341. newQuery[filter.property] = { $gte: data };
  342. } else if (filterType === "numberEquals") {
  343. newQuery[filter.property] = { $eq: data };
  344. }
  345. if (filter.property === "requestedBy")
  346. return { $or: [newQuery, { requestedByUsername: newQuery.requestedBy }] };
  347. if (filter.property === "createdBy")
  348. return { $or: [newQuery, { createdByUsername: newQuery.createdBy }] };
  349. return newQuery;
  350. });
  351. if (queryError) next(queryError);
  352. const queryObject = {};
  353. if (newQueries.length > 0) {
  354. if (operator === "and") queryObject.$and = newQueries;
  355. else if (operator === "or") queryObject.$or = newQueries;
  356. else if (operator === "nor") queryObject.$nor = newQueries;
  357. }
  358. pipeline.push({ $match: queryObject });
  359. next(null, pipeline);
  360. },
  361. // Adds sort stage to aggregation pipeline if there is at least one column being sorted, responsible for sorting data
  362. (pipeline, next) => {
  363. const { sort } = payload;
  364. const newSort = Object.fromEntries(
  365. Object.entries(sort).map(([property, direction]) => [
  366. property,
  367. direction === "ascending" ? 1 : -1
  368. ])
  369. );
  370. if (Object.keys(newSort).length > 0) pipeline.push({ $sort: newSort });
  371. next(null, pipeline);
  372. },
  373. // Adds first project stage to aggregation pipeline, responsible for including only the requested properties
  374. (pipeline, next) => {
  375. const { properties } = payload;
  376. pipeline.push({ $project: Object.fromEntries(properties.map(property => [property, 1])) });
  377. next(null, pipeline);
  378. },
  379. // // Adds second project stage to aggregation pipeline, responsible for excluding some specific properties
  380. // (pipeline, next) => {
  381. // pipeline.push({
  382. // $project: {
  383. // title: 0
  384. // }
  385. // });
  386. // next(null, pipeline);
  387. // },
  388. // Adds the facet stage to aggregation pipeline, responsible for returning a total document count, skipping and limitting the documents that will be returned
  389. (pipeline, next) => {
  390. const { page, pageSize } = payload;
  391. pipeline.push({
  392. $facet: {
  393. count: [{ $count: "count" }],
  394. documents: [{ $skip: pageSize * (page - 1) }, { $limit: pageSize }]
  395. }
  396. });
  397. // console.dir(pipeline, { depth: 6 });
  398. next(null, pipeline);
  399. },
  400. // Executes the aggregation pipeline
  401. (pipeline, next) => {
  402. SongsModule.SongModel.aggregate(pipeline).exec((err, result) => {
  403. // console.dir(err);
  404. // console.dir(result, { depth: 6 });
  405. if (err) return next(err);
  406. if (result[0].count.length === 0) return next(null, 0, []);
  407. const { count } = result[0].count[0];
  408. const { documents } = result[0];
  409. // console.log(111, err, result, count, documents[0]);
  410. return next(null, count, documents);
  411. });
  412. }
  413. ],
  414. (err, count, songs) => {
  415. if (err && err !== true) return reject(new Error(err));
  416. return resolve({ data: songs, count });
  417. }
  418. );
  419. });
  420. }
  421. /**
  422. * Makes sure that if a song is not currently in the songs db, to add it
  423. *
  424. * @param {object} payload - an object containing the payload
  425. * @param {string} payload.youtubeId - the youtube song id of the song we are trying to ensure is in the songs db
  426. * @param {string} payload.userId - the youtube song id of the song we are trying to ensure is in the songs db
  427. * @param {string} payload.automaticallyRequested - whether the song was automatically requested or not
  428. * @returns {Promise} - returns a promise (resolve, reject)
  429. */
  430. ENSURE_SONG_EXISTS_BY_YOUTUBE_ID(payload) {
  431. return new Promise((resolve, reject) =>
  432. async.waterfall(
  433. [
  434. next => {
  435. SongsModule.SongModel.findOne({ youtubeId: payload.youtubeId }, next);
  436. },
  437. (song, next) => {
  438. if (song && song.duration > 0) next(true, song);
  439. else {
  440. YouTubeModule.runJob("GET_SONG", { youtubeId: payload.youtubeId }, this)
  441. .then(response => {
  442. next(null, song, response.song);
  443. })
  444. .catch(next);
  445. }
  446. },
  447. (song, youtubeSong, next) => {
  448. if (song && song.duration <= 0) {
  449. song.duration = youtubeSong.duration;
  450. song.save({ validateBeforeSave: true }, err => {
  451. if (err) return next(err, song);
  452. return next(null, song);
  453. });
  454. } else {
  455. const status =
  456. (!payload.userId && config.get("hideAnonymousSongs")) ||
  457. (payload.automaticallyRequested && config.get("hideAutomaticallyRequestedSongs"))
  458. ? "hidden"
  459. : "unverified";
  460. const song = new SongsModule.SongModel({
  461. ...youtubeSong,
  462. status,
  463. requestedBy: payload.userId,
  464. requestedAt: Date.now()
  465. });
  466. song.save({ validateBeforeSave: true }, err => {
  467. if (err) return next(err, song);
  468. return next(null, song);
  469. });
  470. }
  471. }
  472. ],
  473. (err, song) => {
  474. if (err && err !== true) return reject(new Error(err));
  475. return resolve({ song });
  476. }
  477. )
  478. );
  479. }
  480. /**
  481. * Gets a song by youtube id
  482. *
  483. * @param {object} payload - an object containing the payload
  484. * @param {string} payload.youtubeId - the youtube id of the song we are trying to get
  485. * @returns {Promise} - returns a promise (resolve, reject)
  486. */
  487. GET_SONG_FROM_YOUTUBE_ID(payload) {
  488. return new Promise((resolve, reject) =>
  489. async.waterfall(
  490. [
  491. next => {
  492. SongsModule.SongModel.findOne({ youtubeId: payload.youtubeId }, next);
  493. }
  494. ],
  495. (err, song) => {
  496. if (err && err !== true) return reject(new Error(err));
  497. return resolve({ song });
  498. }
  499. )
  500. );
  501. }
  502. /**
  503. * Gets a song from id from Mongo and updates the cache with it
  504. *
  505. * @param {object} payload - an object containing the payload
  506. * @param {string} payload.songId - the id of the song we are trying to update
  507. * @param {string} payload.oldStatus - old status of song being updated (optional)
  508. * @returns {Promise} - returns a promise (resolve, reject)
  509. */
  510. UPDATE_SONG(payload) {
  511. return new Promise((resolve, reject) =>
  512. async.waterfall(
  513. [
  514. next => {
  515. SongsModule.SongModel.findOne({ _id: payload.songId }, next);
  516. },
  517. (song, next) => {
  518. if (!song) {
  519. CacheModule.runJob("HDEL", {
  520. table: "songs",
  521. key: payload.songId
  522. });
  523. return next("Song not found.");
  524. }
  525. return CacheModule.runJob(
  526. "HSET",
  527. {
  528. table: "songs",
  529. key: payload.songId,
  530. value: song
  531. },
  532. this
  533. )
  534. .then(song => {
  535. next(null, song);
  536. })
  537. .catch(next);
  538. },
  539. (song, next) => {
  540. const { _id, youtubeId, title, artists, thumbnail, duration, status } = song;
  541. const trimmedSong = {
  542. _id,
  543. youtubeId,
  544. title,
  545. artists,
  546. thumbnail,
  547. duration,
  548. status
  549. };
  550. this.log("INFO", `Going to update playlists now for song ${_id}`);
  551. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
  552. .then(playlistModel => {
  553. playlistModel.updateMany(
  554. { "songs._id": song._id },
  555. { $set: { "songs.$": trimmedSong } },
  556. err => {
  557. if (err) next(err);
  558. else
  559. playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  560. if (err) next(err);
  561. else {
  562. async.eachLimit(
  563. playlists,
  564. 1,
  565. (playlist, next) => {
  566. PlaylistsModule.runJob(
  567. "UPDATE_PLAYLIST",
  568. {
  569. playlistId: playlist._id
  570. },
  571. this
  572. )
  573. .then(() => {
  574. next();
  575. })
  576. .catch(err => {
  577. next(err);
  578. });
  579. },
  580. err => {
  581. if (err) next(err);
  582. else next(null, song);
  583. }
  584. );
  585. }
  586. });
  587. }
  588. );
  589. })
  590. .catch(err => {
  591. next(err);
  592. });
  593. },
  594. (song, next) => {
  595. const { _id, youtubeId, title, artists, thumbnail, duration, status } = song;
  596. this.log("INFO", `Going to update stations now for song ${_id}`);
  597. DBModule.runJob("GET_MODEL", { modelName: "station" }, this)
  598. .then(stationModel => {
  599. stationModel.updateMany(
  600. { "queue._id": song._id },
  601. {
  602. $set: {
  603. "queue.$.youtubeId": youtubeId,
  604. "queue.$.title": title,
  605. "queue.$.artists": artists,
  606. "queue.$.thumbnail": thumbnail,
  607. "queue.$.duration": duration,
  608. "queue.$.status": status
  609. }
  610. },
  611. err => {
  612. if (err) this.log("ERROR", err);
  613. else
  614. stationModel.find({ "queue._id": song._id }, (err, stations) => {
  615. if (err) next(err);
  616. else {
  617. async.eachLimit(
  618. stations,
  619. 1,
  620. (station, next) => {
  621. StationsModule.runJob(
  622. "UPDATE_STATION",
  623. { stationId: station._id },
  624. this
  625. )
  626. .then(() => {
  627. next();
  628. })
  629. .catch(err => {
  630. next(err);
  631. });
  632. },
  633. err => {
  634. if (err) next(err);
  635. else next(null, song);
  636. }
  637. );
  638. }
  639. });
  640. }
  641. );
  642. })
  643. .catch(err => {
  644. next(err);
  645. });
  646. },
  647. (song, next) => {
  648. async.eachLimit(
  649. song.genres,
  650. 1,
  651. (genre, next) => {
  652. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre }, this)
  653. .then(() => {
  654. next();
  655. })
  656. .catch(err => next(err));
  657. },
  658. err => {
  659. next(err, song);
  660. }
  661. );
  662. }
  663. ],
  664. (err, song) => {
  665. if (err && err !== true) return reject(new Error(err));
  666. if (!payload.oldStatus) payload.oldStatus = null;
  667. CacheModule.runJob("PUB", {
  668. channel: "song.updated",
  669. value: { songId: song._id, oldStatus: payload.oldStatus }
  670. });
  671. return resolve(song);
  672. }
  673. )
  674. );
  675. }
  676. /**
  677. * Updates all songs
  678. *
  679. * @returns {Promise} - returns a promise (resolve, reject)
  680. */
  681. UPDATE_ALL_SONGS() {
  682. return new Promise((resolve, reject) =>
  683. async.waterfall(
  684. [
  685. next => {
  686. SongsModule.SongModel.find({}, next);
  687. },
  688. (songs, next) => {
  689. let index = 0;
  690. const { length } = songs;
  691. async.eachLimit(
  692. songs,
  693. 2,
  694. (song, next) => {
  695. index += 1;
  696. console.log(`Updating song #${index} out of ${length}: ${song._id}`);
  697. SongsModule.runJob("UPDATE_SONG", { songId: song._id }, this)
  698. .then(() => {
  699. next();
  700. })
  701. .catch(err => {
  702. next(err);
  703. });
  704. },
  705. err => {
  706. next(err);
  707. }
  708. );
  709. }
  710. ],
  711. err => {
  712. if (err && err !== true) return reject(new Error(err));
  713. return resolve();
  714. }
  715. )
  716. );
  717. }
  718. // /**
  719. // * Deletes song from id from Mongo and cache
  720. // *
  721. // * @param {object} payload - returns an object containing the payload
  722. // * @param {string} payload.songId - the song id of the song we are trying to delete
  723. // * @returns {Promise} - returns a promise (resolve, reject)
  724. // */
  725. // DELETE_SONG(payload) {
  726. // return new Promise((resolve, reject) =>
  727. // async.waterfall(
  728. // [
  729. // next => {
  730. // SongsModule.SongModel.deleteOne({ _id: payload.songId }, next);
  731. // },
  732. // next => {
  733. // CacheModule.runJob(
  734. // "HDEL",
  735. // {
  736. // table: "songs",
  737. // key: payload.songId
  738. // },
  739. // this
  740. // )
  741. // .then(() => next())
  742. // .catch(next);
  743. // },
  744. // next => {
  745. // this.log("INFO", `Going to update playlists and stations now for deleted song ${payload.songId}`);
  746. // DBModule.runJob("GET_MODEL", { modelName: "playlist" }).then(playlistModel => {
  747. // playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  748. // if (err) this.log("ERROR", err);
  749. // else {
  750. // playlistModel.updateMany(
  751. // { "songs._id": payload.songId },
  752. // { $pull: { "songs.$._id": payload.songId} },
  753. // err => {
  754. // if (err) this.log("ERROR", err);
  755. // else {
  756. // playlists.forEach(playlist => {
  757. // PlaylistsModule.runJob("UPDATE_PLAYLIST", {
  758. // playlistId: playlist._id
  759. // });
  760. // });
  761. // }
  762. // }
  763. // );
  764. // }
  765. // });
  766. // });
  767. // DBModule.runJob("GET_MODEL", { modelName: "station" }).then(stationModel => {
  768. // stationModel.find({ "queue._id": payload.songId }, (err, stations) => {
  769. // stationModel.updateMany(
  770. // { "queue._id": payload.songId },
  771. // {
  772. // $pull: { "queue._id": }
  773. // },
  774. // err => {
  775. // if (err) this.log("ERROR", err);
  776. // else {
  777. // stations.forEach(station => {
  778. // StationsModule.runJob("UPDATE_STATION", { stationId: station._id });
  779. // });
  780. // }
  781. // }
  782. // );
  783. // });
  784. // });
  785. // }
  786. // ],
  787. // err => {
  788. // if (err && err !== true) return reject(new Error(err));
  789. // return resolve();
  790. // }
  791. // )
  792. // );
  793. // }
  794. /**
  795. * Searches through songs
  796. *
  797. * @param {object} payload - object that contains the payload
  798. * @param {string} payload.query - the query
  799. * @param {string} payload.includeHidden - include hidden songs
  800. * @param {string} payload.includeUnverified - include unverified songs
  801. * @param {string} payload.includeVerified - include verified songs
  802. * @param {string} payload.trimmed - include trimmed songs
  803. * @param {string} payload.page - page (default 1)
  804. * @returns {Promise} - returns promise (reject, resolve)
  805. */
  806. SEARCH(payload) {
  807. return new Promise((resolve, reject) =>
  808. async.waterfall(
  809. [
  810. next => {
  811. const statuses = [];
  812. if (payload.includeHidden) statuses.push("hidden");
  813. if (payload.includeUnverified) statuses.push("unverified");
  814. if (payload.includeVerified) statuses.push("verified");
  815. if (statuses.length === 0) return next("No statuses have been included.");
  816. let { query } = payload;
  817. const isRegex =
  818. query.length > 2 && query.indexOf("/") === 0 && query.lastIndexOf("/") === query.length - 1;
  819. if (isRegex) query = query.slice(1, query.length - 1);
  820. else query = query.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&");
  821. const filterArray = [
  822. {
  823. title: new RegExp(`${query}`, "i"),
  824. status: { $in: statuses }
  825. },
  826. {
  827. artists: new RegExp(`${query}`, "i"),
  828. status: { $in: statuses }
  829. }
  830. ];
  831. return next(null, filterArray);
  832. },
  833. (filterArray, next) => {
  834. const page = payload.page ? payload.page : 1;
  835. const pageSize = 15;
  836. const skipAmount = pageSize * (page - 1);
  837. SongsModule.SongModel.find({ $or: filterArray }).count((err, count) => {
  838. if (err) next(err);
  839. else {
  840. SongsModule.SongModel.find({ $or: filterArray })
  841. .skip(skipAmount)
  842. .limit(pageSize)
  843. .exec((err, songs) => {
  844. if (err) next(err);
  845. else {
  846. next(null, {
  847. songs,
  848. page,
  849. pageSize,
  850. skipAmount,
  851. count
  852. });
  853. }
  854. });
  855. }
  856. });
  857. },
  858. (data, next) => {
  859. if (data.songs.length === 0) next("No songs found");
  860. else if (payload.trimmed) {
  861. next(null, {
  862. songs: data.songs.map(song => {
  863. const { _id, youtubeId, title, artists, thumbnail, duration, status } = song;
  864. return {
  865. _id,
  866. youtubeId,
  867. title,
  868. artists,
  869. thumbnail,
  870. duration,
  871. status
  872. };
  873. }),
  874. ...data
  875. });
  876. } else next(null, data);
  877. }
  878. ],
  879. (err, data) => {
  880. if (err && err !== true) return reject(new Error(err));
  881. return resolve(data);
  882. }
  883. )
  884. );
  885. }
  886. /**
  887. * Recalculates dislikes and likes for a song
  888. *
  889. * @param {object} payload - returns an object containing the payload
  890. * @param {string} payload.youtubeId - the youtube id of the song
  891. * @param {string} payload.songId - the song id of the song
  892. * @returns {Promise} - returns a promise (resolve, reject)
  893. */
  894. async RECALCULATE_SONG_RATINGS(payload) {
  895. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  896. return new Promise((resolve, reject) => {
  897. async.waterfall(
  898. [
  899. next => {
  900. playlistModel.countDocuments(
  901. { songs: { $elemMatch: { _id: payload.songId } }, type: "user-liked" },
  902. (err, likes) => {
  903. if (err) return next(err);
  904. return next(null, likes);
  905. }
  906. );
  907. },
  908. (likes, next) => {
  909. playlistModel.countDocuments(
  910. { songs: { $elemMatch: { _id: payload.songId } }, type: "user-disliked" },
  911. (err, dislikes) => {
  912. if (err) return next(err);
  913. return next(err, { likes, dislikes });
  914. }
  915. );
  916. },
  917. ({ likes, dislikes }, next) => {
  918. SongsModule.SongModel.updateOne(
  919. { _id: payload.songId },
  920. {
  921. $set: {
  922. likes,
  923. dislikes
  924. }
  925. },
  926. err => next(err, { likes, dislikes })
  927. );
  928. }
  929. ],
  930. (err, { likes, dislikes }) => {
  931. if (err) return reject(new Error(err));
  932. return resolve({ likes, dislikes });
  933. }
  934. );
  935. });
  936. }
  937. /**
  938. * Recalculates dislikes and likes for all songs
  939. *
  940. * @returns {Promise} - returns a promise (resolve, reject)
  941. */
  942. RECALCULATE_ALL_SONG_RATINGS() {
  943. return new Promise((resolve, reject) => {
  944. async.waterfall(
  945. [
  946. next => {
  947. SongsModule.SongModel.find({}, { _id: true }, next);
  948. },
  949. (songs, next) => {
  950. async.eachLimit(
  951. songs,
  952. 2,
  953. (song, next) => {
  954. SongsModule.runJob("RECALCULATE_SONG_RATINGS", { songId: song._id }, this)
  955. .then(() => {
  956. next();
  957. })
  958. .catch(err => {
  959. next(err);
  960. });
  961. },
  962. err => {
  963. next(err);
  964. }
  965. );
  966. }
  967. ],
  968. err => {
  969. if (err) return reject(new Error(err));
  970. return resolve();
  971. }
  972. );
  973. });
  974. }
  975. /**
  976. * Gets an array of all genres
  977. *
  978. * @returns {Promise} - returns a promise (resolve, reject)
  979. */
  980. GET_ALL_GENRES() {
  981. return new Promise((resolve, reject) =>
  982. async.waterfall(
  983. [
  984. next => {
  985. SongsModule.SongModel.find({ status: "verified" }, { genres: 1, _id: false }, next);
  986. },
  987. (songs, next) => {
  988. let allGenres = [];
  989. songs.forEach(song => {
  990. allGenres = allGenres.concat(song.genres);
  991. });
  992. const lowerCaseGenres = allGenres.map(genre => genre.toLowerCase());
  993. const uniqueGenres = lowerCaseGenres.filter(
  994. (value, index, self) => self.indexOf(value) === index
  995. );
  996. next(null, uniqueGenres);
  997. }
  998. ],
  999. (err, genres) => {
  1000. if (err && err !== true) return reject(new Error(err));
  1001. return resolve({ genres });
  1002. }
  1003. )
  1004. );
  1005. }
  1006. /**
  1007. * Gets an array of all songs with a specific genre
  1008. *
  1009. * @param {object} payload - returns an object containing the payload
  1010. * @param {string} payload.genre - the genre
  1011. * @returns {Promise} - returns a promise (resolve, reject)
  1012. */
  1013. GET_ALL_SONGS_WITH_GENRE(payload) {
  1014. return new Promise((resolve, reject) =>
  1015. async.waterfall(
  1016. [
  1017. next => {
  1018. SongsModule.SongModel.find(
  1019. {
  1020. status: "verified",
  1021. genres: { $regex: new RegExp(`^${payload.genre.toLowerCase()}$`, "i") }
  1022. },
  1023. next
  1024. );
  1025. }
  1026. ],
  1027. (err, songs) => {
  1028. if (err && err !== true) return reject(new Error(err));
  1029. return resolve({ songs });
  1030. }
  1031. )
  1032. );
  1033. }
  1034. // runjob songs GET_ORPHANED_PLAYLIST_SONGS {}
  1035. /**
  1036. * Gets a orphaned playlist songs
  1037. *
  1038. * @returns {Promise} - returns promise (reject, resolve)
  1039. */
  1040. GET_ORPHANED_PLAYLIST_SONGS() {
  1041. return new Promise((resolve, reject) => {
  1042. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel => {
  1043. playlistModel.find({}, (err, playlists) => {
  1044. if (err) reject(new Error(err));
  1045. else {
  1046. SongsModule.SongModel.find({}, { _id: true, youtubeId: true }, (err, songs) => {
  1047. if (err) reject(new Error(err));
  1048. else {
  1049. const songIds = songs.map(song => song._id.toString());
  1050. const orphanedYoutubeIds = new Set();
  1051. async.eachLimit(
  1052. playlists,
  1053. 1,
  1054. (playlist, next) => {
  1055. playlist.songs.forEach(song => {
  1056. if (
  1057. (!song._id || songIds.indexOf(song._id.toString() === -1)) &&
  1058. !orphanedYoutubeIds.has(song.youtubeId)
  1059. ) {
  1060. orphanedYoutubeIds.add(song.youtubeId);
  1061. }
  1062. });
  1063. next();
  1064. },
  1065. () => {
  1066. resolve({ youtubeIds: Array.from(orphanedYoutubeIds) });
  1067. }
  1068. );
  1069. }
  1070. });
  1071. }
  1072. });
  1073. });
  1074. });
  1075. }
  1076. /**
  1077. * Requests a song, adding it to the DB
  1078. *
  1079. * @param {object} payload - The payload
  1080. * @param {string} payload.youtubeId - The YouTube song id of the song
  1081. * @param {string} payload.userId - The user id of the person requesting the song
  1082. * @returns {Promise} - returns promise (reject, resolve)
  1083. */
  1084. REQUEST_SONG(payload) {
  1085. return new Promise((resolve, reject) => {
  1086. const { youtubeId, userId } = payload;
  1087. const requestedAt = Date.now();
  1088. async.waterfall(
  1089. [
  1090. next => {
  1091. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  1092. .then(UserModel => {
  1093. UserModel.findOne({ _id: userId }, { "preferences.anonymousSongRequests": 1 }, next);
  1094. })
  1095. .catch(next);
  1096. },
  1097. (user, next) => {
  1098. SongsModule.SongModel.findOne({ youtubeId }, (err, song) => next(err, user, song));
  1099. },
  1100. // Get YouTube data from id
  1101. (user, song, next) => {
  1102. if (song) return next("This song is already in the database.", song);
  1103. // TODO Add err object as first param of callback
  1104. const requestedBy = user.preferences.anonymousSongRequests ? null : userId;
  1105. const status = !requestedBy && config.get("hideAnonymousSongs") ? "hidden" : "unverified";
  1106. return YouTubeModule.runJob("GET_SONG", { youtubeId }, this)
  1107. .then(response => {
  1108. const { song } = response;
  1109. song.artists = [];
  1110. song.genres = [];
  1111. song.skipDuration = 0;
  1112. song.explicit = false;
  1113. song.requestedBy = user.preferences.anonymousSongRequests ? null : userId;
  1114. song.requestedAt = requestedAt;
  1115. song.status = status;
  1116. next(null, song);
  1117. })
  1118. .catch(next);
  1119. },
  1120. (newSong, next) => {
  1121. const song = new SongsModule.SongModel(newSong);
  1122. song.save({ validateBeforeSave: false }, err => {
  1123. if (err) return next(err, song);
  1124. return next(null, song);
  1125. });
  1126. },
  1127. (song, next) => {
  1128. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  1129. .then(UserModel => {
  1130. UserModel.findOne({ _id: userId }, (err, user) => {
  1131. if (err) return next(err);
  1132. if (!user) return next(null, song);
  1133. user.statistics.songsRequested += 1;
  1134. return user.save(err => {
  1135. if (err) return next(err);
  1136. return next(null, song);
  1137. });
  1138. });
  1139. })
  1140. .catch(next);
  1141. }
  1142. ],
  1143. async (err, song) => {
  1144. if (err && err !== "This song is already in the database.") return reject(err);
  1145. const { _id, youtubeId, title, artists, thumbnail, duration, status } = song;
  1146. const trimmedSong = {
  1147. _id,
  1148. youtubeId,
  1149. title,
  1150. artists,
  1151. thumbnail,
  1152. duration,
  1153. status
  1154. };
  1155. if (err && err === "This song is already in the database.")
  1156. return reject(new ErrorWithData(err, { song: trimmedSong }));
  1157. SongsModule.runJob("UPDATE_SONG", { songId: song._id });
  1158. return resolve({ song: trimmedSong });
  1159. }
  1160. );
  1161. });
  1162. }
  1163. /**
  1164. * Hides a song
  1165. *
  1166. * @param {object} payload - The payload
  1167. * @param {string} payload.songId - The song id of the song
  1168. * @returns {Promise} - returns promise (reject, resolve)
  1169. */
  1170. HIDE_SONG(payload) {
  1171. return new Promise((resolve, reject) => {
  1172. const { songId } = payload;
  1173. async.waterfall(
  1174. [
  1175. next => {
  1176. SongsModule.SongModel.findOne({ _id: songId }, next);
  1177. },
  1178. // Get YouTube data from id
  1179. (song, next) => {
  1180. if (!song) return next("This song does not exist.");
  1181. if (song.status === "hidden") return next("This song is already hidden.");
  1182. // TODO Add err object as first param of callback
  1183. return next(null, song.status);
  1184. },
  1185. (oldStatus, next) => {
  1186. SongsModule.SongModel.updateOne({ _id: songId }, { status: "hidden" }, res =>
  1187. next(null, res, oldStatus)
  1188. );
  1189. },
  1190. (res, oldStatus, next) => {
  1191. SongsModule.runJob("UPDATE_SONG", { songId, oldStatus });
  1192. next();
  1193. }
  1194. ],
  1195. async err => {
  1196. if (err) reject(err);
  1197. resolve();
  1198. }
  1199. );
  1200. });
  1201. }
  1202. /**
  1203. * Unhides a song
  1204. *
  1205. * @param {object} payload - The payload
  1206. * @param {string} payload.songId - The song id of the song
  1207. * @returns {Promise} - returns promise (reject, resolve)
  1208. */
  1209. UNHIDE_SONG(payload) {
  1210. return new Promise((resolve, reject) => {
  1211. const { songId } = payload;
  1212. async.waterfall(
  1213. [
  1214. next => {
  1215. SongsModule.SongModel.findOne({ _id: songId }, next);
  1216. },
  1217. // Get YouTube data from id
  1218. (song, next) => {
  1219. if (!song) return next("This song does not exist.");
  1220. if (song.status !== "hidden") return next("This song is not hidden.");
  1221. // TODO Add err object as first param of callback
  1222. return next();
  1223. },
  1224. next => {
  1225. SongsModule.SongModel.updateOne({ _id: songId }, { status: "unverified" }, next);
  1226. },
  1227. (res, next) => {
  1228. SongsModule.runJob("UPDATE_SONG", { songId, oldStatus: "hidden" });
  1229. next();
  1230. }
  1231. ],
  1232. async err => {
  1233. if (err) reject(err);
  1234. resolve();
  1235. }
  1236. );
  1237. });
  1238. }
  1239. // runjob songs REQUEST_ORPHANED_PLAYLIST_SONGS {}
  1240. /**
  1241. * Requests all orphaned playlist songs, adding them to the database
  1242. *
  1243. * @returns {Promise} - returns promise (reject, resolve)
  1244. */
  1245. REQUEST_ORPHANED_PLAYLIST_SONGS() {
  1246. return new Promise((resolve, reject) => {
  1247. DBModule.runJob("GET_MODEL", { modelName: "playlist" })
  1248. .then(playlistModel => {
  1249. SongsModule.runJob("GET_ORPHANED_PLAYLIST_SONGS", {}, this).then(response => {
  1250. const { youtubeIds } = response;
  1251. const playlistsToUpdate = new Set();
  1252. async.eachLimit(
  1253. youtubeIds,
  1254. 1,
  1255. (youtubeId, next) => {
  1256. async.waterfall(
  1257. [
  1258. next => {
  1259. console.log(
  1260. youtubeId,
  1261. `this is song ${youtubeIds.indexOf(youtubeId) + 1}/${youtubeIds.length}`
  1262. );
  1263. setTimeout(next, 150);
  1264. },
  1265. next => {
  1266. SongsModule.runJob(
  1267. "ENSURE_SONG_EXISTS_BY_SONG_ID",
  1268. { youtubeId, automaticallyRequested: true },
  1269. this
  1270. )
  1271. .then(() => next())
  1272. .catch(next);
  1273. },
  1274. next => {
  1275. console.log(444, youtubeId);
  1276. SongsModule.SongModel.findOne({ youtubeId }, next);
  1277. },
  1278. (song, next) => {
  1279. const { _id, title, artists, thumbnail, duration, status } = song;
  1280. const trimmedSong = {
  1281. _id,
  1282. youtubeId,
  1283. title,
  1284. artists,
  1285. thumbnail,
  1286. duration,
  1287. status
  1288. };
  1289. playlistModel.updateMany(
  1290. { "songs.youtubeId": song.youtubeId },
  1291. { $set: { "songs.$": trimmedSong } },
  1292. err => {
  1293. next(err, song);
  1294. }
  1295. );
  1296. },
  1297. (song, next) => {
  1298. playlistModel.find({ "songs._id": song._id }, next);
  1299. },
  1300. (playlists, next) => {
  1301. playlists.forEach(playlist => {
  1302. playlistsToUpdate.add(playlist._id.toString());
  1303. });
  1304. next();
  1305. }
  1306. ],
  1307. next
  1308. );
  1309. },
  1310. err => {
  1311. if (err) reject(err);
  1312. else {
  1313. async.eachLimit(
  1314. Array.from(playlistsToUpdate),
  1315. 1,
  1316. (playlistId, next) => {
  1317. PlaylistsModule.runJob(
  1318. "UPDATE_PLAYLIST",
  1319. {
  1320. playlistId
  1321. },
  1322. this
  1323. )
  1324. .then(() => {
  1325. next();
  1326. })
  1327. .catch(next);
  1328. },
  1329. err => {
  1330. if (err) reject(err);
  1331. else resolve();
  1332. }
  1333. );
  1334. }
  1335. }
  1336. );
  1337. });
  1338. })
  1339. .catch(reject);
  1340. });
  1341. }
  1342. /**
  1343. * Gets a list of all genres
  1344. *
  1345. * @returns {Promise} - returns promise (reject, resolve)
  1346. */
  1347. GET_GENRES() {
  1348. return new Promise((resolve, reject) => {
  1349. async.waterfall(
  1350. [
  1351. next => {
  1352. SongsModule.SongModel.distinct("genres", next);
  1353. }
  1354. ],
  1355. (err, genres) => {
  1356. if (err) reject(err);
  1357. resolve({ genres });
  1358. }
  1359. );
  1360. });
  1361. }
  1362. /**
  1363. * Gets a list of all artists
  1364. *
  1365. * @returns {Promise} - returns promise (reject, resolve)
  1366. */
  1367. GET_ARTISTS() {
  1368. return new Promise((resolve, reject) => {
  1369. async.waterfall(
  1370. [
  1371. next => {
  1372. SongsModule.SongModel.distinct("artists", next);
  1373. }
  1374. ],
  1375. (err, artists) => {
  1376. if (err) reject(err);
  1377. resolve({ artists });
  1378. }
  1379. );
  1380. });
  1381. }
  1382. }
  1383. export default new _SongsModule();