playlists.js 56 KB

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