playlists.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. import async from "async";
  2. import { isAdminRequired, isLoginRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const DBModule = moduleManager.modules.db;
  5. const UtilsModule = moduleManager.modules.utils;
  6. const WSModule = moduleManager.modules.ws;
  7. const SongsModule = moduleManager.modules.songs;
  8. const StationsModule = moduleManager.modules.stations;
  9. const CacheModule = moduleManager.modules.cache;
  10. const PlaylistsModule = moduleManager.modules.playlists;
  11. const YouTubeModule = moduleManager.modules.youtube;
  12. const ActivitiesModule = moduleManager.modules.activities;
  13. CacheModule.runJob("SUB", {
  14. channel: "playlist.create",
  15. cb: playlist => {
  16. WSModule.runJob("SOCKETS_FROM_USER", { userId: playlist.createdBy }, this).then(sockets => {
  17. sockets.forEach(socket => socket.dispatch("event:playlist.create", { data: { playlist } }));
  18. });
  19. if (playlist.privacy === "public")
  20. WSModule.runJob("EMIT_TO_ROOM", {
  21. room: `profile-${playlist.createdBy}-playlists`,
  22. args: ["event:playlist.create", { data: { playlist } }]
  23. });
  24. }
  25. });
  26. CacheModule.runJob("SUB", {
  27. channel: "playlist.delete",
  28. cb: res => {
  29. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  30. sockets.forEach(socket => {
  31. socket.dispatch("event:playlist.delete", { data: { playlistId: res.playlistId } });
  32. });
  33. });
  34. WSModule.runJob("EMIT_TO_ROOM", {
  35. room: `profile-${res.userId}-playlists`,
  36. args: ["event:playlist.delete", { data: { playlistId: res.playlistId } }]
  37. });
  38. }
  39. });
  40. CacheModule.runJob("SUB", {
  41. channel: "playlist.repositionSongs",
  42. cb: res => {
  43. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets =>
  44. sockets.forEach(socket =>
  45. socket.dispatch("event:playlist.repositionSongs", {
  46. data: {
  47. playlistId: res.playlistId,
  48. songsBeingChanged: res.songsBeingChanged
  49. }
  50. })
  51. )
  52. );
  53. }
  54. });
  55. CacheModule.runJob("SUB", {
  56. channel: "playlist.addSong",
  57. cb: res => {
  58. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  59. sockets.forEach(socket => {
  60. socket.dispatch("event:playlist.addSong", {
  61. data: {
  62. playlistId: res.playlistId,
  63. song: res.song
  64. }
  65. });
  66. });
  67. });
  68. if (res.privacy === "public")
  69. WSModule.runJob("EMIT_TO_ROOM", {
  70. room: `profile-${res.userId}-playlists`,
  71. args: [
  72. "event:playlist.addSong",
  73. {
  74. data: {
  75. playlistId: res.playlistId,
  76. song: res.song
  77. }
  78. }
  79. ]
  80. });
  81. }
  82. });
  83. CacheModule.runJob("SUB", {
  84. channel: "playlist.removeSong",
  85. cb: res => {
  86. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  87. sockets.forEach(socket => {
  88. socket.dispatch("event:playlist.removeSong", {
  89. data: {
  90. playlistId: res.playlistId,
  91. youtubeId: res.youtubeId
  92. }
  93. });
  94. });
  95. });
  96. if (res.privacy === "public")
  97. WSModule.runJob("EMIT_TO_ROOM", {
  98. room: `profile-${res.userId}-playlists`,
  99. args: [
  100. "event:playlist.removeSong",
  101. {
  102. data: {
  103. playlistId: res.playlistId,
  104. youtubeId: res.youtubeId
  105. }
  106. }
  107. ]
  108. });
  109. }
  110. });
  111. CacheModule.runJob("SUB", {
  112. channel: "playlist.updateDisplayName",
  113. cb: res => {
  114. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  115. sockets.forEach(socket => {
  116. socket.dispatch("event:playlist.updateDisplayName", {
  117. data: {
  118. playlistId: res.playlistId,
  119. displayName: res.displayName
  120. }
  121. });
  122. });
  123. });
  124. if (res.privacy === "public")
  125. WSModule.runJob("EMIT_TO_ROOM", {
  126. room: `profile-${res.userId}-playlists`,
  127. args: [
  128. "event:playlist.updateDisplayName",
  129. {
  130. data: {
  131. playlistId: res.playlistId,
  132. displayName: res.displayName
  133. }
  134. }
  135. ]
  136. });
  137. }
  138. });
  139. CacheModule.runJob("SUB", {
  140. channel: "playlist.updatePrivacy",
  141. cb: res => {
  142. WSModule.runJob("SOCKETS_FROM_USER", { userId: res.userId }, this).then(sockets => {
  143. sockets.forEach(socket => {
  144. socket.dispatch("event:playlist.updatePrivacy", {
  145. data: {
  146. playlist: res.playlist
  147. }
  148. });
  149. });
  150. });
  151. if (res.playlist.privacy === "public")
  152. return WSModule.runJob("EMIT_TO_ROOM", {
  153. room: `profile-${res.userId}-playlists`,
  154. args: [
  155. "event:playlist.create",
  156. {
  157. data: {
  158. playlist: res.playlist
  159. }
  160. }
  161. ]
  162. });
  163. return WSModule.runJob("EMIT_TO_ROOM", {
  164. room: `profile-${res.userId}-playlists`,
  165. args: [
  166. "event:playlist.delete",
  167. {
  168. data: {
  169. playlistId: res.playlist._id
  170. }
  171. }
  172. ]
  173. });
  174. }
  175. });
  176. export default {
  177. /**
  178. * Gets all playlists
  179. *
  180. * @param {object} session - the session object automatically added by the websocket
  181. * @param {Function} cb - gets called with the result
  182. */
  183. index: isAdminRequired(async function index(session, cb) {
  184. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  185. async.waterfall(
  186. [
  187. next => {
  188. playlistModel.find({}).sort({ createdAt: "desc" }).exec(next);
  189. }
  190. ],
  191. async (err, playlists) => {
  192. if (err) {
  193. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  194. this.log("ERROR", "PLAYLISTS_INDEX", `Indexing playlists failed. "${err}"`);
  195. return cb({ status: "error", message: err });
  196. }
  197. this.log("SUCCESS", "PLAYLISTS_INDEX", "Indexing playlists successful.");
  198. return cb({ status: "success", data: { playlists } });
  199. }
  200. );
  201. }),
  202. /**
  203. * Searches through all playlists that can be included in a community station
  204. *
  205. * @param {object} session - the session object automatically added by the websocket
  206. * @param {string} query - the query
  207. * @param {Function} cb - gets called with the result
  208. */
  209. searchCommunity: isLoginRequired(async function searchCommunity(session, query, cb) {
  210. async.waterfall(
  211. [
  212. next => {
  213. if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
  214. else next();
  215. },
  216. next => {
  217. PlaylistsModule.runJob("SEARCH", {
  218. query,
  219. includeUser: true,
  220. includeGenre: true,
  221. includeOwn: true,
  222. includeSongs: true,
  223. userId: session.userId
  224. })
  225. .then(response => {
  226. next(null, response.playlists);
  227. })
  228. .catch(err => {
  229. next(err);
  230. });
  231. }
  232. ],
  233. async (err, playlists) => {
  234. if (err) {
  235. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  236. this.log("ERROR", "PLAYLISTS_SEARCH_COMMUNITY", `Searching playlists failed. "${err}"`);
  237. return cb({ status: "error", message: err });
  238. }
  239. this.log("SUCCESS", "PLAYLISTS_SEARCH_COMMUNITY", "Searching playlists successful.");
  240. return cb({ status: "success", data: { playlists } });
  241. }
  242. );
  243. }),
  244. /**
  245. * Searches through all playlists that can be included in an official station
  246. *
  247. * @param {object} session - the session object automatically added by the websocket
  248. * @param {string} query - the query
  249. * @param {Function} cb - gets called with the result
  250. */
  251. searchOfficial: isAdminRequired(async function searchOfficial(session, query, cb) {
  252. async.waterfall(
  253. [
  254. next => {
  255. if ((!query && query !== "") || typeof query !== "string") next("Invalid query.");
  256. else next();
  257. },
  258. next => {
  259. PlaylistsModule.runJob("SEARCH", {
  260. query,
  261. includeGenre: true,
  262. includePrivate: true,
  263. includeSongs: true
  264. })
  265. .then(response => {
  266. next(null, response.playlists);
  267. })
  268. .catch(err => {
  269. next(err);
  270. });
  271. }
  272. ],
  273. async (err, playlists) => {
  274. if (err) {
  275. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  276. this.log("ERROR", "PLAYLISTS_SEARCH_OFFICIAL", `Searching playlists failed. "${err}"`);
  277. return cb({ status: "error", message: err });
  278. }
  279. this.log("SUCCESS", "PLAYLISTS_SEARCH_OFFICIAL", "Searching playlists successful.");
  280. return cb({ status: "success", data: { playlists } });
  281. }
  282. );
  283. }),
  284. /**
  285. * Gets the first song from a private playlist
  286. *
  287. * @param {object} session - the session object automatically added by the websocket
  288. * @param {string} playlistId - the id of the playlist we are getting the first song from
  289. * @param {Function} cb - gets called with the result
  290. */
  291. getFirstSong: isLoginRequired(function getFirstSong(session, playlistId, cb) {
  292. async.waterfall(
  293. [
  294. next => {
  295. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  296. .then(playlist => next(null, playlist))
  297. .catch(next);
  298. },
  299. (playlist, next) => {
  300. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
  301. playlist.songs.sort((a, b) => a.position - b.position);
  302. return next(null, playlist.songs[0]);
  303. }
  304. ],
  305. async (err, song) => {
  306. if (err) {
  307. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  308. this.log(
  309. "ERROR",
  310. "PLAYLIST_GET_FIRST_SONG",
  311. `Getting the first song of playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  312. );
  313. return cb({ status: "error", message: err });
  314. }
  315. this.log(
  316. "SUCCESS",
  317. "PLAYLIST_GET_FIRST_SONG",
  318. `Successfully got the first song of playlist "${playlistId}" for user "${session.userId}".`
  319. );
  320. return cb({
  321. status: "success",
  322. data: { song }
  323. });
  324. }
  325. );
  326. }),
  327. /**
  328. * Gets a list of all the playlists for a specific user
  329. *
  330. * @param {object} session - the session object automatically added by the websocket
  331. * @param {string} userId - the user id in question
  332. * @param {Function} cb - gets called with the result
  333. */
  334. indexForUser: async function indexForUser(session, userId, cb) {
  335. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  336. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  337. async.waterfall(
  338. [
  339. next => {
  340. userModel.findById(userId).select({ "preferences.orderOfPlaylists": -1 }).exec(next);
  341. },
  342. ({ preferences }, next) => {
  343. const { orderOfPlaylists } = preferences;
  344. const match = {
  345. createdBy: userId
  346. };
  347. // if a playlist order exists
  348. if (orderOfPlaylists > 0) match._id = { $in: orderOfPlaylists };
  349. playlistModel
  350. .aggregate()
  351. .match(match)
  352. .addFields({
  353. weight: { $indexOfArray: [orderOfPlaylists, "$_id"] }
  354. })
  355. .sort({ weight: 1 })
  356. .exec(next);
  357. },
  358. (playlists, next) => {
  359. if (session.userId === userId) return next(null, playlists); // user requesting playlists is the owner of the playlists
  360. const filteredPlaylists = [];
  361. return async.each(
  362. playlists,
  363. (playlist, nextPlaylist) => {
  364. if (playlist.privacy === "public") filteredPlaylists.push(playlist);
  365. return nextPlaylist();
  366. },
  367. () => next(null, filteredPlaylists)
  368. );
  369. }
  370. ],
  371. async (err, playlists) => {
  372. if (err) {
  373. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  374. this.log(
  375. "ERROR",
  376. "PLAYLIST_INDEX_FOR_USER",
  377. `Indexing playlists for user "${userId}" failed. "${err}"`
  378. );
  379. return cb({ status: "error", message: err });
  380. }
  381. this.log("SUCCESS", "PLAYLIST_INDEX_FOR_USER", `Successfully indexed playlists for user "${userId}".`);
  382. return cb({
  383. status: "success",
  384. data: { playlists }
  385. });
  386. }
  387. );
  388. },
  389. /**
  390. * Gets all playlists for the user requesting it
  391. *
  392. * @param {object} session - the session object automatically added by the websocket
  393. * @param {boolean} showNonModifiablePlaylists - whether or not to show non modifiable playlists e.g. liked songs
  394. * @param {Function} cb - gets called with the result
  395. */
  396. indexMyPlaylists: isLoginRequired(async function indexMyPlaylists(session, showNonModifiablePlaylists, cb) {
  397. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  398. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  399. async.waterfall(
  400. [
  401. next => {
  402. userModel.findById(session.userId).select({ "preferences.orderOfPlaylists": -1 }).exec(next);
  403. },
  404. ({ preferences }, next) => {
  405. const { orderOfPlaylists } = preferences;
  406. const match = {
  407. createdBy: session.userId
  408. };
  409. // if non modifiable playlists should be shown as well
  410. if (!showNonModifiablePlaylists) match.isUserModifiable = true;
  411. // if a playlist order exists
  412. if (orderOfPlaylists > 0) match._id = { $in: orderOfPlaylists };
  413. playlistModel
  414. .aggregate()
  415. .match(match)
  416. .addFields({
  417. weight: { $indexOfArray: [orderOfPlaylists, "$_id"] }
  418. })
  419. .sort({ weight: 1 })
  420. .exec(next);
  421. }
  422. ],
  423. async (err, playlists) => {
  424. if (err) {
  425. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  426. this.log(
  427. "ERROR",
  428. "PLAYLIST_INDEX_FOR_ME",
  429. `Indexing playlists for user "${session.userId}" failed. "${err}"`
  430. );
  431. return cb({ status: "error", message: err });
  432. }
  433. this.log(
  434. "SUCCESS",
  435. "PLAYLIST_INDEX_FOR_ME",
  436. `Successfully indexed playlists for user "${session.userId}".`
  437. );
  438. return cb({
  439. status: "success",
  440. data: { playlists }
  441. });
  442. }
  443. );
  444. }),
  445. /**
  446. * Creates a new private playlist
  447. *
  448. * @param {object} session - the session object automatically added by the websocket
  449. * @param {object} data - the data for the new private playlist
  450. * @param {Function} cb - gets called with the result
  451. */
  452. create: isLoginRequired(async function create(session, data, cb) {
  453. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  454. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  455. const blacklist = ["liked songs", "likedsongs", "disliked songs", "dislikedsongs"];
  456. async.waterfall(
  457. [
  458. next => (data ? next() : cb({ status: "error", message: "Invalid data" })),
  459. next => {
  460. const { displayName, songs, privacy } = data;
  461. if (blacklist.indexOf(displayName.toLowerCase()) !== -1)
  462. return next("That playlist name is blacklisted. Please use a different name.");
  463. return playlistModel.create(
  464. {
  465. displayName,
  466. songs,
  467. privacy,
  468. createdBy: session.userId,
  469. createdAt: Date.now(),
  470. createdFor: null,
  471. type: "user"
  472. },
  473. next
  474. );
  475. },
  476. (playlist, next) => {
  477. userModel.updateOne(
  478. { _id: session.userId },
  479. { $push: { "preferences.orderOfPlaylists": playlist._id } },
  480. err => {
  481. if (err) return next(err);
  482. return next(null, playlist);
  483. }
  484. );
  485. }
  486. ],
  487. async (err, playlist) => {
  488. if (err) {
  489. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  490. this.log(
  491. "ERROR",
  492. "PLAYLIST_CREATE",
  493. `Creating private playlist failed for user "${session.userId}". "${err}"`
  494. );
  495. return cb({ status: "error", message: err });
  496. }
  497. CacheModule.runJob("PUB", {
  498. channel: "playlist.create",
  499. value: playlist
  500. });
  501. ActivitiesModule.runJob("ADD_ACTIVITY", {
  502. userId: playlist.createdBy,
  503. type: "playlist__create",
  504. payload: {
  505. message: `Created playlist <playlistId>${playlist.displayName}</playlistId>`,
  506. playlistId: playlist._id
  507. }
  508. });
  509. this.log(
  510. "SUCCESS",
  511. "PLAYLIST_CREATE",
  512. `Successfully created private playlist for user "${session.userId}".`
  513. );
  514. return cb({
  515. status: "success",
  516. message: "Successfully created playlist",
  517. data: {
  518. playlistId: playlist._id
  519. }
  520. });
  521. }
  522. );
  523. }),
  524. /**
  525. * Gets a playlist from id
  526. *
  527. * @param {object} session - the session object automatically added by the websocket
  528. * @param {string} playlistId - the id of the playlist we are getting
  529. * @param {Function} cb - gets called with the result
  530. */
  531. getPlaylist: function getPlaylist(session, playlistId, cb) {
  532. async.waterfall(
  533. [
  534. next => {
  535. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  536. .then(playlist => next(null, playlist))
  537. .catch(next);
  538. },
  539. (playlist, next) => {
  540. if (!playlist) return next("Playlist not found");
  541. if (playlist.privacy !== "public" && playlist.createdBy !== session.userId) {
  542. if (session)
  543. // check if user requested to get a playlist is an admin
  544. return DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  545. userModel.findOne({ _id: session.userId }, (err, user) => {
  546. if (user && user.role === "admin") return next(null, playlist);
  547. return next("User unauthorised to view playlist.");
  548. });
  549. });
  550. return next("User unauthorised to view playlist.");
  551. }
  552. return next(null, playlist);
  553. }
  554. ],
  555. async (err, playlist) => {
  556. if (err) {
  557. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  558. this.log(
  559. "ERROR",
  560. "PLAYLIST_GET",
  561. `Getting private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  562. );
  563. return cb({ status: "error", message: err });
  564. }
  565. this.log(
  566. "SUCCESS",
  567. "PLAYLIST_GET",
  568. `Successfully got private playlist "${playlistId}" for user "${session.userId}".`
  569. );
  570. return cb({
  571. status: "success",
  572. data: { playlist }
  573. });
  574. }
  575. );
  576. },
  577. /**
  578. * Obtains basic metadata of a playlist in order to format an activity
  579. *
  580. * @param {object} session - the session object automatically added by the websocket
  581. * @param {string} playlistId - the playlist id
  582. * @param {Function} cb - callback
  583. */
  584. getPlaylistForActivity(session, playlistId, cb) {
  585. async.waterfall(
  586. [
  587. next => {
  588. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  589. .then(playlist => {
  590. next(null, playlist);
  591. })
  592. .catch(next);
  593. }
  594. ],
  595. async (err, playlist) => {
  596. if (err) {
  597. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  598. this.log(
  599. "ERROR",
  600. "PLAYLISTS_GET_PLAYLIST_FOR_ACTIVITY",
  601. `Failed to obtain metadata of playlist ${playlistId} for activity formatting. "${err}"`
  602. );
  603. return cb({ status: "error", message: err });
  604. }
  605. this.log(
  606. "SUCCESS",
  607. "PLAYLISTS_GET_PLAYLIST_FOR_ACTIVITY",
  608. `Obtained metadata of playlist ${playlistId} for activity formatting successfully.`
  609. );
  610. return cb({
  611. status: "success",
  612. data: {
  613. title: playlist.displayName
  614. }
  615. });
  616. }
  617. );
  618. },
  619. /**
  620. * Gets a playlist from station id
  621. *
  622. * @param {object} session - the session object automatically added by the websocket
  623. * @param {string} stationId - the id of the station we are getting
  624. * @param {string} includeSongs - include songs
  625. * @param {Function} cb - gets called with the result
  626. */
  627. getPlaylistForStation: function getPlaylist(session, stationId, includeSongs, cb) {
  628. async.waterfall(
  629. [
  630. next => {
  631. PlaylistsModule.runJob("GET_STATION_PLAYLIST", { stationId, includeSongs }, this)
  632. .then(response => next(null, response.playlist))
  633. .catch(next);
  634. },
  635. (playlist, next) => {
  636. if (!playlist) return next("Playlist not found");
  637. if (playlist.privacy !== "public" && playlist.createdBy !== session.userId) {
  638. if (session)
  639. // check if user requested to get a playlist is an admin
  640. return DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  641. userModel.findOne({ _id: session.userId }, (err, user) => {
  642. if (user && user.role === "admin") return next(null, playlist);
  643. return next("User unauthorised to view playlist.");
  644. });
  645. });
  646. return next("User unauthorised to view playlist.");
  647. }
  648. return next(null, playlist);
  649. }
  650. ],
  651. async (err, playlist) => {
  652. if (err) {
  653. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  654. this.log(
  655. "ERROR",
  656. "PLAYLIST_GET",
  657. `Getting playlist for station "${stationId}" failed for user "${session.userId}". "${err}"`
  658. );
  659. return cb({ status: "error", message: err });
  660. }
  661. this.log(
  662. "SUCCESS",
  663. "PLAYLIST_GET",
  664. `Successfully got playlist for station "${stationId}" for user "${session.userId}".`
  665. );
  666. return cb({
  667. status: "success",
  668. data: { playlist }
  669. });
  670. }
  671. );
  672. },
  673. // TODO Remove this
  674. /**
  675. * Updates a private playlist
  676. *
  677. * @param {object} session - the session object automatically added by the websocket
  678. * @param {string} playlistId - the id of the playlist we are updating
  679. * @param {object} playlist - the new private playlist object
  680. * @param {Function} cb - gets called with the result
  681. */
  682. update: isLoginRequired(async function update(session, playlistId, playlist, cb) {
  683. const playlistModel = await DBModule.runJob(
  684. "GET_MODEL",
  685. {
  686. modelName: "playlist"
  687. },
  688. this
  689. );
  690. async.waterfall(
  691. [
  692. next => {
  693. playlistModel.updateOne(
  694. { _id: playlistId, createdBy: session.userId },
  695. playlist,
  696. { runValidators: true },
  697. next
  698. );
  699. },
  700. (res, next) => {
  701. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  702. .then(playlist => {
  703. next(null, playlist);
  704. })
  705. .catch(next);
  706. }
  707. ],
  708. async (err, playlist) => {
  709. if (err) {
  710. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  711. this.log(
  712. "ERROR",
  713. "PLAYLIST_UPDATE",
  714. `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  715. );
  716. return cb({ status: "error", message: err });
  717. }
  718. this.log(
  719. "SUCCESS",
  720. "PLAYLIST_UPDATE",
  721. `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
  722. );
  723. return cb({
  724. status: "success",
  725. data: { playlist }
  726. });
  727. }
  728. );
  729. }),
  730. /**
  731. * Shuffles songs in a private playlist
  732. *
  733. * @param {object} session - the session object automatically added by the websocket
  734. * @param {string} playlistId - the id of the playlist we are updating
  735. * @param {Function} cb - gets called with the result
  736. */
  737. shuffle: isLoginRequired(async function shuffle(session, playlistId, cb) {
  738. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  739. async.waterfall(
  740. [
  741. next => {
  742. if (!playlistId) return next("No playlist id.");
  743. return playlistModel.findById(playlistId, next);
  744. },
  745. (playlist, next) => {
  746. if (!playlist.isUserModifiable) return next("Playlist cannot be shuffled.");
  747. return UtilsModule.runJob("SHUFFLE_SONG_POSITIONS", { array: playlist.songs }, this)
  748. .then(result => next(null, result.array))
  749. .catch(next);
  750. },
  751. (songs, next) => {
  752. playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, { runValidators: true }, next);
  753. },
  754. (res, next) => {
  755. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  756. .then(playlist => next(null, playlist))
  757. .catch(next);
  758. }
  759. ],
  760. async (err, playlist) => {
  761. if (err) {
  762. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  763. this.log(
  764. "ERROR",
  765. "PLAYLIST_SHUFFLE",
  766. `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  767. );
  768. return cb({ status: "error", message: err });
  769. }
  770. this.log(
  771. "SUCCESS",
  772. "PLAYLIST_SHUFFLE",
  773. `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
  774. );
  775. return cb({
  776. status: "success",
  777. message: "Successfully shuffled playlist.",
  778. data: { playlist }
  779. });
  780. }
  781. );
  782. }),
  783. /**
  784. * Changes the order of song(s) in a private playlist
  785. *
  786. * @param {object} session - the session object automatically added by the websocket
  787. * @param {string} playlistId - the id of the playlist we are targeting
  788. * @param {Array} songsBeingChanged - the songs to be repositioned, each element contains "youtubeId" and "position" properties
  789. * @param {Function} cb - gets called with the result
  790. */
  791. repositionSongs: isLoginRequired(async function repositionSongs(session, playlistId, songsBeingChanged, cb) {
  792. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  793. async.waterfall(
  794. [
  795. // update playlist object with each song's new position
  796. next =>
  797. async.each(
  798. songsBeingChanged,
  799. (song, nextSong) =>
  800. playlistModel.updateOne(
  801. { _id: playlistId, "songs.youtubeId": song.youtubeId },
  802. {
  803. $set: {
  804. "songs.$.position": song.position
  805. }
  806. },
  807. err => {
  808. if (err) return next(err);
  809. return nextSong();
  810. }
  811. ),
  812. next
  813. ),
  814. // update the cache with the new songs positioning
  815. next => {
  816. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  817. .then(playlist => next(null, playlist))
  818. .catch(next);
  819. }
  820. ],
  821. async err => {
  822. if (err) {
  823. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  824. this.log(
  825. "ERROR",
  826. "PLAYLIST_REPOSITION_SONGS",
  827. `Repositioning songs for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  828. );
  829. return cb({ status: "error", message: err });
  830. }
  831. this.log(
  832. "SUCCESS",
  833. "PLAYLIST_REPOSITION_SONGS",
  834. `Successfully repositioned songs for private playlist "${playlistId}" for user "${session.userId}".`
  835. );
  836. CacheModule.runJob("PUB", {
  837. channel: "playlist.repositionSongs",
  838. value: {
  839. userId: session.userId,
  840. playlistId,
  841. songsBeingChanged
  842. }
  843. });
  844. return cb({
  845. status: "success",
  846. message: "Order of songs successfully updated"
  847. });
  848. }
  849. );
  850. }),
  851. /**
  852. * Moves a song to the bottom of the list in a private playlist
  853. *
  854. * @param {object} session - the session object automatically added by the websocket
  855. * @param {string} playlistId - the id of the playlist we are moving the song to the bottom from
  856. * @param {string} youtubeId - the youtube id of the song we are moving to the bottom of the list
  857. * @param {Function} cb - gets called with the result
  858. */
  859. moveSongToBottom: isLoginRequired(async function moveSongToBottom(session, playlistId, youtubeId, cb) {
  860. async.waterfall(
  861. [
  862. next => {
  863. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  864. .then(playlist => next(null, playlist))
  865. .catch(next);
  866. },
  867. (playlist, next) => {
  868. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found");
  869. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  870. // sort array by position
  871. playlist.songs.sort((a, b) => a.position - b.position);
  872. // find index of youtubeId
  873. playlist.songs.forEach((song, index) => {
  874. // reorder array (simulates what would be done with a drag and drop interface)
  875. if (song.youtubeId === youtubeId)
  876. playlist.songs.splice(playlist.songs.length, 0, playlist.songs.splice(index, 1)[0]);
  877. });
  878. const songsBeingChanged = [];
  879. playlist.songs.forEach((song, index) => {
  880. // check if position needs updated based on index
  881. if (song.position !== index + 1)
  882. songsBeingChanged.push({
  883. youtubeId: song.youtubeId,
  884. position: index + 1
  885. });
  886. });
  887. // update position property on songs that need to be changed
  888. return WSModule.runJob(
  889. "RUN_ACTION2",
  890. {
  891. session,
  892. namespace: "playlists",
  893. action: "repositionSongs",
  894. args: [playlistId, songsBeingChanged]
  895. },
  896. this
  897. )
  898. .then(res => {
  899. if (res.status === "success") return next();
  900. return next("Unable to reposition song in playlist.");
  901. })
  902. .catch(next);
  903. }
  904. ],
  905. async err => {
  906. if (err) {
  907. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  908. this.log(
  909. "ERROR",
  910. "PLAYLIST_MOVE_SONG_TO_BOTTOM",
  911. `Moving song "${youtubeId}" to the bottom for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  912. );
  913. return cb({ status: "error", message: err });
  914. }
  915. this.log(
  916. "SUCCESS",
  917. "PLAYLIST_MOVE_SONG_TO_BOTTOM",
  918. `Successfully moved song "${youtubeId}" to the bottom for private playlist "${playlistId}" for user "${session.userId}".`
  919. );
  920. return cb({
  921. status: "success",
  922. message: "Order of songs successfully updated"
  923. });
  924. }
  925. );
  926. }),
  927. /**
  928. * Adds a song to a private playlist
  929. *
  930. * @param {object} session - the session object automatically added by the websocket
  931. * @param {boolean} isSet - is the song part of a set of songs to be added
  932. * @param {string} youtubeId - the youtube id of the song we are trying to add
  933. * @param {string} playlistId - the id of the playlist we are adding the song to
  934. * @param {Function} cb - gets called with the result
  935. */
  936. addSongToPlaylist: isLoginRequired(async function addSongToPlaylist(session, isSet, youtubeId, playlistId, cb) {
  937. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  938. async.waterfall(
  939. [
  940. next => {
  941. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  942. .then(playlist => {
  943. if (!playlist || playlist.createdBy !== session.userId)
  944. return next("Something went wrong when trying to get the playlist");
  945. return async.each(
  946. playlist.songs,
  947. (song, nextSong) => {
  948. if (song.youtubeId === youtubeId)
  949. return next("That song is already in the playlist");
  950. return nextSong();
  951. },
  952. err => next(err)
  953. );
  954. })
  955. .catch(next);
  956. },
  957. next => {
  958. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  959. .then(UserModel => {
  960. UserModel.findOne(
  961. { _id: session.userId },
  962. { "preferences.anonymousSongRequests": 1 },
  963. next
  964. );
  965. })
  966. .catch(next);
  967. },
  968. (user, next) => {
  969. SongsModule.runJob(
  970. "ENSURE_SONG_EXISTS_BY_YOUTUBE_ID",
  971. {
  972. youtubeId,
  973. userId: user.preferences.anonymousSongRequests ? null : session.userId,
  974. automaticallyRequested: true
  975. },
  976. this
  977. )
  978. .then(response => {
  979. const { song } = response;
  980. const { _id, title, thumbnail, duration, status } = song;
  981. next(null, {
  982. _id,
  983. youtubeId,
  984. title,
  985. thumbnail,
  986. duration,
  987. status
  988. });
  989. })
  990. .catch(next);
  991. },
  992. (newSong, next) => {
  993. playlistModel.updateOne(
  994. { _id: playlistId },
  995. { $push: { songs: newSong } },
  996. { runValidators: true },
  997. err => {
  998. if (err) return next(err);
  999. return PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1000. .then(playlist => next(null, playlist, newSong))
  1001. .catch(next);
  1002. }
  1003. );
  1004. }
  1005. ],
  1006. async (err, playlist, newSong) => {
  1007. if (err) {
  1008. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1009. this.log(
  1010. "ERROR",
  1011. "PLAYLIST_ADD_SONG",
  1012. `Adding song "${youtubeId}" to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1013. );
  1014. return cb({ status: "error", message: err });
  1015. }
  1016. this.log(
  1017. "SUCCESS",
  1018. "PLAYLIST_ADD_SONG",
  1019. `Successfully added song "${youtubeId}" to private playlist "${playlistId}" for user "${session.userId}".`
  1020. );
  1021. if (!isSet && playlist.displayName !== "Liked Songs" && playlist.displayName !== "Disliked Songs") {
  1022. const songName = newSong.artists
  1023. ? `${newSong.title} by ${newSong.artists.join(", ")}`
  1024. : newSong.title;
  1025. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1026. userId: session.userId,
  1027. type: "playlist__add_song",
  1028. payload: {
  1029. message: `Added <youtubeId>${songName}</youtubeId> to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1030. thumbnail: newSong.thumbnail,
  1031. playlistId,
  1032. youtubeId
  1033. }
  1034. });
  1035. }
  1036. StationsModule.runJob("GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST", { playlistId })
  1037. .then(response => {
  1038. response.stationIds.forEach(stationId => {
  1039. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }).then().catch();
  1040. });
  1041. })
  1042. .catch();
  1043. CacheModule.runJob("PUB", {
  1044. channel: "playlist.addSong",
  1045. value: {
  1046. playlistId: playlist._id,
  1047. song: newSong,
  1048. userId: session.userId,
  1049. privacy: playlist.privacy
  1050. }
  1051. });
  1052. return cb({
  1053. status: "success",
  1054. message: "Song has been successfully added to the playlist",
  1055. data: { songs: playlist.songs }
  1056. });
  1057. }
  1058. );
  1059. }),
  1060. /**
  1061. * Adds a set of songs to a private playlist
  1062. *
  1063. * @param {object} session - the session object automatically added by the websocket
  1064. * @param {string} url - the url of the the YouTube playlist
  1065. * @param {string} playlistId - the id of the playlist we are adding the set of songs to
  1066. * @param {boolean} musicOnly - whether to only add music to the playlist
  1067. * @param {Function} cb - gets called with the result
  1068. */
  1069. addSetToPlaylist: isLoginRequired(function addSetToPlaylist(session, url, playlistId, musicOnly, cb) {
  1070. let videosInPlaylistTotal = 0;
  1071. let songsInPlaylistTotal = 0;
  1072. let addSongsStats = null;
  1073. const addedSongs = [];
  1074. async.waterfall(
  1075. [
  1076. next => {
  1077. YouTubeModule.runJob("GET_PLAYLIST", { url, musicOnly }, this)
  1078. .then(res => {
  1079. if (res.filteredSongs) {
  1080. videosInPlaylistTotal = res.songs.length;
  1081. songsInPlaylistTotal = res.filteredSongs.length;
  1082. } else {
  1083. songsInPlaylistTotal = videosInPlaylistTotal = res.songs.length;
  1084. }
  1085. next(null, res.songs);
  1086. })
  1087. .catch(err => {
  1088. next(err);
  1089. });
  1090. },
  1091. (youtubeIds, next) => {
  1092. let successful = 0;
  1093. let failed = 0;
  1094. let alreadyInPlaylist = 0;
  1095. if (youtubeIds.length === 0) next();
  1096. console.log(youtubeIds);
  1097. async.eachLimit(
  1098. youtubeIds,
  1099. 1,
  1100. (youtubeId, next) => {
  1101. WSModule.runJob(
  1102. "RUN_ACTION2",
  1103. {
  1104. session,
  1105. namespace: "playlists",
  1106. action: "addSongToPlaylist",
  1107. args: [true, youtubeId, playlistId]
  1108. },
  1109. this
  1110. )
  1111. .then(res => {
  1112. if (res.status === "success") {
  1113. successful += 1;
  1114. addedSongs.push(youtubeId);
  1115. } else failed += 1;
  1116. if (res.message === "That song is already in the playlist") alreadyInPlaylist += 1;
  1117. })
  1118. .catch(() => {
  1119. failed += 1;
  1120. })
  1121. .finally(() => next());
  1122. },
  1123. () => {
  1124. addSongsStats = { successful, failed, alreadyInPlaylist };
  1125. next(null);
  1126. }
  1127. );
  1128. },
  1129. next => {
  1130. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1131. .then(playlist => next(null, playlist))
  1132. .catch(next);
  1133. },
  1134. (playlist, next) => {
  1135. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
  1136. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  1137. return next(null, playlist);
  1138. }
  1139. ],
  1140. async (err, playlist) => {
  1141. if (err) {
  1142. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1143. this.log(
  1144. "ERROR",
  1145. "PLAYLIST_IMPORT",
  1146. `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1147. );
  1148. return cb({ status: "error", message: err });
  1149. }
  1150. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1151. userId: session.userId,
  1152. type: "playlist__import_playlist",
  1153. payload: {
  1154. message: `Imported ${addSongsStats.successful} songs to playlist <playlistId>${playlist.displayName}</playlistId>`,
  1155. playlistId
  1156. }
  1157. });
  1158. this.log(
  1159. "SUCCESS",
  1160. "PLAYLIST_IMPORT",
  1161. `Successfully imported a YouTube playlist to private playlist "${playlistId}" for user "${session.userId}". Videos in playlist: ${videosInPlaylistTotal}, songs in playlist: ${songsInPlaylistTotal}, songs successfully added: ${addSongsStats.successful}, songs failed: ${addSongsStats.failed}, already in playlist: ${addSongsStats.alreadyInPlaylist}.`
  1162. );
  1163. return cb({
  1164. status: "success",
  1165. message: `Playlist has been imported. ${addSongsStats.successful} were added successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`,
  1166. data: {
  1167. songs: playlist.songs,
  1168. stats: {
  1169. videosInPlaylistTotal,
  1170. songsInPlaylistTotal
  1171. }
  1172. }
  1173. });
  1174. }
  1175. );
  1176. }),
  1177. /**
  1178. * Removes a song from a private playlist
  1179. *
  1180. * @param {object} session - the session object automatically added by the websocket
  1181. * @param {string} youtubeId - the youtube id of the song we are removing from the private playlist
  1182. * @param {string} playlistId - the id of the playlist we are removing the song from
  1183. * @param {Function} cb - gets called with the result
  1184. */
  1185. removeSongFromPlaylist: isLoginRequired(async function removeSongFromPlaylist(session, youtubeId, playlistId, cb) {
  1186. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1187. async.waterfall(
  1188. [
  1189. next => {
  1190. if (!youtubeId || typeof youtubeId !== "string") return next("Invalid song id.");
  1191. if (!playlistId) return next("Invalid playlist id.");
  1192. return next();
  1193. },
  1194. next => {
  1195. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1196. .then(playlist => next(null, playlist))
  1197. .catch(next);
  1198. },
  1199. (playlist, next) => {
  1200. if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found");
  1201. // sort array by position
  1202. playlist.songs.sort((a, b) => a.position - b.position);
  1203. // find index of youtubeId
  1204. playlist.songs.forEach((song, ind) => {
  1205. // remove song from array
  1206. if (song.youtubeId === youtubeId) playlist.songs.splice(ind, 1);
  1207. });
  1208. const songsBeingChanged = [];
  1209. playlist.songs.forEach((song, index) => {
  1210. // check if position needs updated based on index
  1211. if (song.position !== index + 1)
  1212. songsBeingChanged.push({
  1213. youtubeId: song.youtubeId,
  1214. position: index + 1
  1215. });
  1216. });
  1217. // update position property on songs that need to be changed
  1218. return WSModule.runJob(
  1219. "RUN_ACTION2",
  1220. {
  1221. session,
  1222. namespace: "playlists",
  1223. action: "repositionSongs",
  1224. args: [playlistId, songsBeingChanged]
  1225. },
  1226. this
  1227. )
  1228. .then(res => {
  1229. if (res.status === "success") return next();
  1230. return next("Unable to reposition song in playlist.");
  1231. })
  1232. .catch(next);
  1233. },
  1234. next => playlistModel.updateOne({ _id: playlistId }, { $pull: { songs: { youtubeId } } }, next),
  1235. (res, next) => {
  1236. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1237. .then(playlist => next(null, playlist))
  1238. .catch(next);
  1239. },
  1240. (playlist, next) => {
  1241. StationsModule.runJob("GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST", { playlistId })
  1242. .then(response => {
  1243. response.stationIds.forEach(stationId => {
  1244. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }).then().catch();
  1245. });
  1246. })
  1247. .catch();
  1248. SongsModule.runJob("GET_SONG_FROM_YOUTUBE_ID", { youtubeId }, this)
  1249. .then(res =>
  1250. next(null, playlist, {
  1251. title: res.song.title,
  1252. thumbnail: res.song.thumbnail,
  1253. artists: res.song.artists
  1254. })
  1255. )
  1256. .catch(() => {
  1257. YouTubeModule.runJob("GET_SONG", { youtubeId }, this)
  1258. .then(response => next(null, playlist, response.song))
  1259. .catch(next);
  1260. });
  1261. },
  1262. (playlist, youtubeSong, next) => {
  1263. const songName = youtubeSong.artists
  1264. ? `${youtubeSong.title} by ${youtubeSong.artists.join(", ")}`
  1265. : youtubeSong.title;
  1266. if (playlist.displayName !== "Liked Songs" && playlist.displayName !== "Disliked Songs") {
  1267. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1268. userId: session.userId,
  1269. type: "playlist__remove_song",
  1270. payload: {
  1271. message: `Removed <youtubeId>${songName}</youtubeId> from playlist <playlistId>${playlist.displayName}</playlistId>`,
  1272. thumbnail: youtubeSong.thumbnail,
  1273. playlistId,
  1274. youtubeId
  1275. }
  1276. });
  1277. }
  1278. return next(null, playlist);
  1279. }
  1280. ],
  1281. async (err, playlist) => {
  1282. if (err) {
  1283. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1284. this.log(
  1285. "ERROR",
  1286. "PLAYLIST_REMOVE_SONG",
  1287. `Removing song "${youtubeId}" from private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1288. );
  1289. return cb({ status: "error", message: err });
  1290. }
  1291. this.log(
  1292. "SUCCESS",
  1293. "PLAYLIST_REMOVE_SONG",
  1294. `Successfully removed song "${youtubeId}" from private playlist "${playlistId}" for user "${session.userId}".`
  1295. );
  1296. CacheModule.runJob("PUB", {
  1297. channel: "playlist.removeSong",
  1298. value: {
  1299. playlistId: playlist._id,
  1300. youtubeId,
  1301. userId: session.userId,
  1302. privacy: playlist.privacy
  1303. }
  1304. });
  1305. return cb({
  1306. status: "success",
  1307. message: "Song has been successfully removed from playlist",
  1308. data: { songs: playlist.songs }
  1309. });
  1310. }
  1311. );
  1312. }),
  1313. /**
  1314. * Updates the displayName of a private playlist
  1315. *
  1316. * @param {object} session - the session object automatically added by the websocket
  1317. * @param {string} playlistId - the id of the playlist we are updating the displayName for
  1318. * @param {Function} cb - gets called with the result
  1319. */
  1320. updateDisplayName: isLoginRequired(async function updateDisplayName(session, playlistId, displayName, cb) {
  1321. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1322. async.waterfall(
  1323. [
  1324. next => {
  1325. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1326. .then(playlist => next(null, playlist))
  1327. .catch(next);
  1328. },
  1329. (playlist, next) => {
  1330. if (!playlist.isUserModifiable) return next("Playlist cannot be modified.");
  1331. return next(null);
  1332. },
  1333. next => {
  1334. playlistModel.updateOne(
  1335. { _id: playlistId, createdBy: session.userId },
  1336. { $set: { displayName } },
  1337. { runValidators: true },
  1338. next
  1339. );
  1340. },
  1341. (res, next) => {
  1342. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1343. .then(playlist => next(null, playlist))
  1344. .catch(next);
  1345. }
  1346. ],
  1347. async (err, playlist) => {
  1348. if (err) {
  1349. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1350. this.log(
  1351. "ERROR",
  1352. "PLAYLIST_UPDATE_DISPLAY_NAME",
  1353. `Updating display name to "${displayName}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1354. );
  1355. return cb({ status: "error", message: err });
  1356. }
  1357. this.log(
  1358. "SUCCESS",
  1359. "PLAYLIST_UPDATE_DISPLAY_NAME",
  1360. `Successfully updated display name to "${displayName}" for private playlist "${playlistId}" for user "${session.userId}".`
  1361. );
  1362. CacheModule.runJob("PUB", {
  1363. channel: "playlist.updateDisplayName",
  1364. value: {
  1365. playlistId,
  1366. displayName,
  1367. userId: session.userId,
  1368. privacy: playlist.privacy
  1369. }
  1370. });
  1371. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1372. userId: session.userId,
  1373. type: "playlist__edit_display_name",
  1374. payload: {
  1375. message: `Changed display name of playlist <playlistId>${displayName}</playlistId>`,
  1376. playlistId
  1377. }
  1378. });
  1379. return cb({
  1380. status: "success",
  1381. message: "Playlist has been successfully updated"
  1382. });
  1383. }
  1384. );
  1385. }),
  1386. /**
  1387. * Removes a private playlist
  1388. *
  1389. * @param {object} session - the session object automatically added by the websocket
  1390. * @param {string} playlistId - the id of the playlist we are moving the song to the top from
  1391. * @param {Function} cb - gets called with the result
  1392. */
  1393. remove: isLoginRequired(async function remove(session, playlistId, cb) {
  1394. // const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  1395. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  1396. async.waterfall(
  1397. [
  1398. next => {
  1399. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1400. .then(playlist => next(null, playlist))
  1401. .catch(next);
  1402. },
  1403. (playlist, next) => {
  1404. if (!playlist.isUserModifiable) return next("Playlist cannot be removed.");
  1405. return next(null, playlist);
  1406. },
  1407. (playlist, next) => {
  1408. userModel.updateOne(
  1409. { _id: playlist.createdBy },
  1410. { $pull: { "preferences.orderOfPlaylists": playlist._id } },
  1411. err => next(err, playlist)
  1412. );
  1413. },
  1414. (playlist, next) => {
  1415. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId }, this)
  1416. .then(() => next(null, playlist))
  1417. .catch(next);
  1418. }
  1419. // (playlist, next) => {
  1420. // stationModel.find({ privatePlaylist: playlistId }, (err, res) => {
  1421. // next(err, playlist, res);
  1422. // });
  1423. // },
  1424. // (playlist, stations, next) => {
  1425. // async.each(
  1426. // stations,
  1427. // (station, next) => {
  1428. // async.waterfall(
  1429. // [
  1430. // next => {
  1431. // stationModel.updateOne(
  1432. // { _id: station._id },
  1433. // { $set: { privatePlaylist: null } },
  1434. // { runValidators: true },
  1435. // next
  1436. // );
  1437. // },
  1438. // (res, next) => {
  1439. // if (!station.partyMode) {
  1440. // moduleManager.modules.stations
  1441. // .runJob("UPDATE_STATION", { stationId: station._id }, this)
  1442. // .then(station => next(null, station))
  1443. // .catch(next);
  1444. // CacheModule.runJob("PUB", {
  1445. // channel: "privatePlaylist.selected",
  1446. // value: {
  1447. // playlistId: null,
  1448. // stationId: station._id
  1449. // }
  1450. // });
  1451. // } else next();
  1452. // }
  1453. // ],
  1454. // () => next()
  1455. // );
  1456. // },
  1457. // () => next(null, playlist)
  1458. // );
  1459. // }
  1460. ],
  1461. async (err, playlist) => {
  1462. if (err) {
  1463. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1464. this.log(
  1465. "ERROR",
  1466. "PLAYLIST_REMOVE",
  1467. `Removing private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1468. );
  1469. return cb({ status: "error", message: err });
  1470. }
  1471. this.log(
  1472. "SUCCESS",
  1473. "PLAYLIST_REMOVE",
  1474. `Successfully removed private playlist "${playlistId}" for user "${session.userId}".`
  1475. );
  1476. CacheModule.runJob("PUB", {
  1477. channel: "playlist.delete",
  1478. value: {
  1479. userId: session.userId,
  1480. playlistId
  1481. }
  1482. });
  1483. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1484. userId: playlist.createdBy,
  1485. type: "playlist__remove",
  1486. payload: {
  1487. message: `Removed playlist ${playlist.displayName}`
  1488. }
  1489. });
  1490. return cb({
  1491. status: "success",
  1492. message: "Playlist successfully removed"
  1493. });
  1494. }
  1495. );
  1496. }),
  1497. /**
  1498. * Updates the privacy of a private playlist
  1499. *
  1500. * @param {object} session - the session object automatically added by the websocket
  1501. * @param {string} playlistId - the id of the playlist we are updating the privacy for
  1502. * @param {string} privacy - what the new privacy of the playlist should be e.g. public
  1503. * @param {Function} cb - gets called with the result
  1504. */
  1505. updatePrivacy: isLoginRequired(async function updatePrivacy(session, playlistId, privacy, cb) {
  1506. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  1507. async.waterfall(
  1508. [
  1509. next => {
  1510. playlistModel.updateOne(
  1511. { _id: playlistId, createdBy: session.userId },
  1512. { $set: { privacy } },
  1513. { runValidators: true },
  1514. next
  1515. );
  1516. },
  1517. (res, next) => {
  1518. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  1519. .then(playlist => next(null, playlist))
  1520. .catch(next);
  1521. }
  1522. ],
  1523. async (err, playlist) => {
  1524. if (err) {
  1525. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1526. this.log(
  1527. "ERROR",
  1528. "PLAYLIST_UPDATE_PRIVACY",
  1529. `Updating privacy to "${privacy}" for private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1530. );
  1531. return cb({ status: "error", message: err });
  1532. }
  1533. this.log(
  1534. "SUCCESS",
  1535. "PLAYLIST_UPDATE_PRIVACY",
  1536. `Successfully updated privacy to "${privacy}" for private playlist "${playlistId}" for user "${session.userId}".`
  1537. );
  1538. CacheModule.runJob("PUB", {
  1539. channel: "playlist.updatePrivacy",
  1540. value: {
  1541. userId: session.userId,
  1542. playlist
  1543. }
  1544. });
  1545. ActivitiesModule.runJob("ADD_ACTIVITY", {
  1546. userId: session.userId,
  1547. type: "playlist__edit_privacy",
  1548. payload: {
  1549. message: `Changed privacy of playlist <playlistId>${playlist.displayName}</playlistId> to ${privacy}`,
  1550. playlistId
  1551. }
  1552. });
  1553. return cb({
  1554. status: "success",
  1555. message: "Playlist has been successfully updated"
  1556. });
  1557. }
  1558. );
  1559. }),
  1560. /**
  1561. * Deletes all orphaned station playlists
  1562. *
  1563. * @param {object} session - the session object automatically added by socket.io
  1564. * @param {Function} cb - gets called with the result
  1565. */
  1566. deleteOrphanedStationPlaylists: isAdminRequired(async function index(session, cb) {
  1567. async.waterfall(
  1568. [
  1569. next => {
  1570. PlaylistsModule.runJob("DELETE_ORPHANED_STATION_PLAYLISTS", {}, this)
  1571. .then(() => next())
  1572. .catch(next);
  1573. }
  1574. ],
  1575. async err => {
  1576. if (err) {
  1577. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1578. this.log(
  1579. "ERROR",
  1580. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  1581. `Deleting orphaned station playlists failed. "${err}"`
  1582. );
  1583. return cb({ status: "error", message: err });
  1584. }
  1585. this.log(
  1586. "SUCCESS",
  1587. "PLAYLISTS_DELETE_ORPHANED_STATION_PLAYLISTS",
  1588. "Deleting orphaned station playlists successful."
  1589. );
  1590. return cb({ status: "success", message: "Successfully deleted orphaned station playlists." });
  1591. }
  1592. );
  1593. }),
  1594. /**
  1595. * Deletes all orphaned genre playlists
  1596. *
  1597. * @param {object} session - the session object automatically added by socket.io
  1598. * @param {Function} cb - gets called with the result
  1599. */
  1600. deleteOrphanedGenrePlaylists: isAdminRequired(async function index(session, cb) {
  1601. async.waterfall(
  1602. [
  1603. next => {
  1604. PlaylistsModule.runJob("DELETE_ORPHANED_GENRE_PLAYLISTS", {}, this)
  1605. .then(() => next())
  1606. .catch(next);
  1607. }
  1608. ],
  1609. async err => {
  1610. if (err) {
  1611. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1612. this.log(
  1613. "ERROR",
  1614. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  1615. `Deleting orphaned genre playlists failed. "${err}"`
  1616. );
  1617. return cb({ status: "error", message: err });
  1618. }
  1619. this.log(
  1620. "SUCCESS",
  1621. "PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
  1622. "Deleting orphaned genre playlists successful."
  1623. );
  1624. return cb({ status: "success", message: "Successfully deleted orphaned genre playlists." });
  1625. }
  1626. );
  1627. }),
  1628. /**
  1629. * Requests orpahned playlist songs
  1630. *
  1631. * @param {object} session - the session object automatically added by socket.io
  1632. * @param {Function} cb - gets called with the result
  1633. */
  1634. requestOrphanedPlaylistSongs: isAdminRequired(async function index(session, cb) {
  1635. async.waterfall(
  1636. [
  1637. next => {
  1638. SongsModule.runJob("REQUEST_ORPHANED_PLAYLIST_SONGS", {}, this)
  1639. .then(() => next())
  1640. .catch(next);
  1641. }
  1642. ],
  1643. async err => {
  1644. if (err) {
  1645. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1646. this.log(
  1647. "ERROR",
  1648. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  1649. `Requesting orphaned playlist songs failed. "${err}"`
  1650. );
  1651. return cb({ status: "error", message: err });
  1652. }
  1653. this.log(
  1654. "SUCCESS",
  1655. "REQUEST_ORPHANED_PLAYLIST_SONGS",
  1656. "Requesting orphaned playlist songs was successful."
  1657. );
  1658. return cb({ status: "success", message: "Successfully requested orphaned playlist songs." });
  1659. }
  1660. );
  1661. }),
  1662. /**
  1663. * Clears and refills a station playlist
  1664. *
  1665. * @param {object} session - the session object automatically added by socket.io
  1666. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  1667. * @param {Function} cb - gets called with the result
  1668. */
  1669. clearAndRefillStationPlaylist: isAdminRequired(async function index(session, playlistId, cb) {
  1670. async.waterfall(
  1671. [
  1672. next => {
  1673. if (!playlistId) next("Please specify a playlist id");
  1674. else {
  1675. PlaylistsModule.runJob("CLEAR_AND_REFILL_STATION_PLAYLIST", { playlistId }, this)
  1676. .then(() => {
  1677. next();
  1678. })
  1679. .catch(err => {
  1680. next(err);
  1681. });
  1682. }
  1683. }
  1684. ],
  1685. async err => {
  1686. if (err) {
  1687. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1688. this.log(
  1689. "ERROR",
  1690. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  1691. `Clearing and refilling station playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1692. );
  1693. return cb({ status: "error", message: err });
  1694. }
  1695. this.log(
  1696. "SUCCESS",
  1697. "PLAYLIST_CLEAR_AND_REFILL_STATION_PLAYLIST",
  1698. `Successfully cleared and refilled station playlist "${playlistId}" for user "${session.userId}".`
  1699. );
  1700. return cb({
  1701. status: "success",
  1702. message: "Playlist has been successfully cleared and refilled"
  1703. });
  1704. }
  1705. );
  1706. }),
  1707. /**
  1708. * Clears and refills a genre playlist
  1709. *
  1710. * @param {object} session - the session object automatically added by socket.io
  1711. * @param {string} playlistId - the id of the playlist we are clearing and refilling
  1712. * @param {Function} cb - gets called with the result
  1713. */
  1714. clearAndRefillGenrePlaylist: isAdminRequired(async function index(session, playlistId, cb) {
  1715. async.waterfall(
  1716. [
  1717. next => {
  1718. if (!playlistId) next("Please specify a playlist id");
  1719. else {
  1720. PlaylistsModule.runJob("CLEAR_AND_REFILL_GENRE_PLAYLIST", { playlistId }, this)
  1721. .then(() => {
  1722. next();
  1723. })
  1724. .catch(err => {
  1725. next(err);
  1726. });
  1727. }
  1728. }
  1729. ],
  1730. async err => {
  1731. if (err) {
  1732. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1733. this.log(
  1734. "ERROR",
  1735. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  1736. `Clearing and refilling genre playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
  1737. );
  1738. return cb({ status: "error", message: err });
  1739. }
  1740. this.log(
  1741. "SUCCESS",
  1742. "PLAYLIST_CLEAR_AND_REFILL_GENRE_PLAYLIST",
  1743. `Successfully cleared and refilled genre playlist "${playlistId}" for user "${session.userId}".`
  1744. );
  1745. return cb({
  1746. status: "success",
  1747. message: "Playlist has been successfully cleared and refilled"
  1748. });
  1749. }
  1750. );
  1751. }),
  1752. /**
  1753. * Clears and refills all station playlists
  1754. *
  1755. * @param {object} session - the session object automatically added by socket.io
  1756. * @param {Function} cb - gets called with the result
  1757. */
  1758. clearAndRefillAllStationPlaylists: isAdminRequired(async function index(session, cb) {
  1759. async.waterfall(
  1760. [
  1761. next => {
  1762. PlaylistsModule.runJob("GET_ALL_STATION_PLAYLISTS", {}, this)
  1763. .then(response => {
  1764. next(null, response.playlists);
  1765. })
  1766. .catch(err => {
  1767. next(err);
  1768. });
  1769. },
  1770. (playlists, next) => {
  1771. async.eachLimit(
  1772. playlists,
  1773. 1,
  1774. (playlist, next) => {
  1775. PlaylistsModule.runJob(
  1776. "CLEAR_AND_REFILL_STATION_PLAYLIST",
  1777. { playlistId: playlist._id },
  1778. this
  1779. )
  1780. .then(() => {
  1781. next();
  1782. })
  1783. .catch(err => {
  1784. next(err);
  1785. });
  1786. },
  1787. next
  1788. );
  1789. }
  1790. ],
  1791. async err => {
  1792. if (err) {
  1793. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1794. this.log(
  1795. "ERROR",
  1796. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  1797. `Clearing and refilling all station playlists failed for user "${session.userId}". "${err}"`
  1798. );
  1799. return cb({ status: "error", message: err });
  1800. }
  1801. this.log(
  1802. "SUCCESS",
  1803. "PLAYLIST_CLEAR_AND_REFILL_ALL_STATION_PLAYLISTS",
  1804. `Successfully cleared and refilled all station playlists for user "${session.userId}".`
  1805. );
  1806. return cb({
  1807. status: "success",
  1808. message: "Playlists have been successfully cleared and refilled"
  1809. });
  1810. }
  1811. );
  1812. }),
  1813. /**
  1814. * Clears and refills all genre playlists
  1815. *
  1816. * @param {object} session - the session object automatically added by socket.io
  1817. * @param {Function} cb - gets called with the result
  1818. */
  1819. clearAndRefillAllGenrePlaylists: isAdminRequired(async function index(session, cb) {
  1820. async.waterfall(
  1821. [
  1822. next => {
  1823. PlaylistsModule.runJob("GET_ALL_GENRE_PLAYLISTS", {}, this)
  1824. .then(response => {
  1825. next(null, response.playlists);
  1826. })
  1827. .catch(err => {
  1828. next(err);
  1829. });
  1830. },
  1831. (playlists, next) => {
  1832. async.eachLimit(
  1833. playlists,
  1834. 1,
  1835. (playlist, next) => {
  1836. PlaylistsModule.runJob(
  1837. "CLEAR_AND_REFILL_GENRE_PLAYLIST",
  1838. { playlistId: playlist._id },
  1839. this
  1840. )
  1841. .then(() => {
  1842. next();
  1843. })
  1844. .catch(err => {
  1845. next(err);
  1846. });
  1847. },
  1848. next
  1849. );
  1850. }
  1851. ],
  1852. async err => {
  1853. if (err) {
  1854. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1855. this.log(
  1856. "ERROR",
  1857. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  1858. `Clearing and refilling all genre playlists failed for user "${session.userId}". "${err}"`
  1859. );
  1860. return cb({ status: "error", message: err });
  1861. }
  1862. this.log(
  1863. "SUCCESS",
  1864. "PLAYLIST_CLEAR_AND_REFILL_ALL_GENRE_PLAYLISTS",
  1865. `Successfully cleared and refilled all genre playlists for user "${session.userId}".`
  1866. );
  1867. return cb({
  1868. status: "success",
  1869. message: "Playlists have been successfully cleared and refilled"
  1870. });
  1871. }
  1872. );
  1873. })
  1874. };