media.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. import async from "async";
  2. import CoreClass from "../core";
  3. let MediaModule;
  4. let CacheModule;
  5. let DBModule;
  6. let UtilsModule;
  7. let YouTubeModule;
  8. let SoundCloudModule;
  9. let SongsModule;
  10. let WSModule;
  11. class _MediaModule extends CoreClass {
  12. // eslint-disable-next-line require-jsdoc
  13. constructor() {
  14. super("media");
  15. MediaModule = this;
  16. }
  17. /**
  18. * Initialises the media module
  19. *
  20. * @returns {Promise} - returns promise (reject, resolve)
  21. */
  22. async initialize() {
  23. this.setStage(1);
  24. CacheModule = this.moduleManager.modules.cache;
  25. DBModule = this.moduleManager.modules.db;
  26. UtilsModule = this.moduleManager.modules.utils;
  27. YouTubeModule = this.moduleManager.modules.youtube;
  28. SoundCloudModule = this.moduleManager.modules.soundcloud;
  29. SongsModule = this.moduleManager.modules.songs;
  30. WSModule = this.moduleManager.modules.ws;
  31. this.RatingsModel = await DBModule.runJob("GET_MODEL", { modelName: "ratings" });
  32. this.RatingsSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "ratings" });
  33. this.ImportJobModel = await DBModule.runJob("GET_MODEL", { modelName: "importJob" });
  34. this.setStage(2);
  35. return new Promise((resolve, reject) => {
  36. CacheModule.runJob("SUB", {
  37. channel: "importJob.updated",
  38. cb: importJob => {
  39. WSModule.runJob("EMIT_TO_ROOM", {
  40. room: "admin.import",
  41. args: ["event:admin.importJob.updated", { data: { importJob } }]
  42. });
  43. }
  44. });
  45. CacheModule.runJob("SUB", {
  46. channel: "importJob.removed",
  47. cb: jobId => {
  48. WSModule.runJob("EMIT_TO_ROOM", {
  49. room: "admin.import",
  50. args: ["event:admin.importJob.removed", { data: { jobId } }]
  51. });
  52. }
  53. });
  54. async.waterfall(
  55. [
  56. next => {
  57. this.setStage(2);
  58. CacheModule.runJob("HGETALL", { table: "ratings" })
  59. .then(ratings => {
  60. next(null, ratings);
  61. })
  62. .catch(next);
  63. },
  64. (ratings, next) => {
  65. this.setStage(3);
  66. if (!ratings) return next();
  67. const mediaSources = Object.keys(ratings);
  68. return async.each(
  69. mediaSources,
  70. (mediaSource, next) => {
  71. MediaModule.RatingsModel.findOne({ mediaSource }, (err, rating) => {
  72. if (err) next(err);
  73. else if (!rating)
  74. CacheModule.runJob("HDEL", {
  75. table: "ratings",
  76. key: mediaSource
  77. })
  78. .then(() => next())
  79. .catch(next);
  80. else next();
  81. });
  82. },
  83. next
  84. );
  85. },
  86. next => {
  87. this.setStage(4);
  88. MediaModule.RatingsModel.find({}, next);
  89. },
  90. (ratings, next) => {
  91. this.setStage(5);
  92. async.each(
  93. ratings,
  94. (rating, next) => {
  95. CacheModule.runJob("HSET", {
  96. table: "ratings",
  97. key: rating.mediaSource,
  98. value: MediaModule.RatingsSchemaCache(rating)
  99. })
  100. .then(() => next())
  101. .catch(next);
  102. },
  103. next
  104. );
  105. }
  106. ],
  107. async err => {
  108. if (err) {
  109. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  110. reject(new Error(err));
  111. } else resolve();
  112. }
  113. );
  114. });
  115. }
  116. /**
  117. * Recalculates dislikes and likes
  118. *
  119. * @param {object} payload - returns an object containing the payload
  120. * @param {string} payload.mediaSource - the media source
  121. * @returns {Promise} - returns a promise (resolve, reject)
  122. */
  123. async RECALCULATE_RATINGS(payload) {
  124. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  125. return new Promise((resolve, reject) => {
  126. async.waterfall(
  127. [
  128. next => {
  129. playlistModel.countDocuments(
  130. { songs: { $elemMatch: { mediaSource: payload.mediaSource } }, type: "user-liked" },
  131. (err, likes) => {
  132. if (err) return next(err);
  133. return next(null, likes);
  134. }
  135. );
  136. },
  137. (likes, next) => {
  138. playlistModel.countDocuments(
  139. { songs: { $elemMatch: { mediaSource: payload.mediaSource } }, type: "user-disliked" },
  140. (err, dislikes) => {
  141. if (err) return next(err);
  142. return next(err, { likes, dislikes });
  143. }
  144. );
  145. },
  146. ({ likes, dislikes }, next) => {
  147. MediaModule.RatingsModel.findOneAndUpdate(
  148. { mediaSource: payload.mediaSource },
  149. {
  150. $set: {
  151. likes,
  152. dislikes
  153. }
  154. },
  155. { new: true, upsert: true },
  156. next
  157. );
  158. },
  159. (ratings, next) => {
  160. CacheModule.runJob(
  161. "HSET",
  162. {
  163. table: "ratings",
  164. key: payload.mediaSource,
  165. value: ratings
  166. },
  167. this
  168. )
  169. .then(ratings => next(null, ratings))
  170. .catch(next);
  171. }
  172. ],
  173. (err, { likes, dislikes }) => {
  174. if (err) return reject(new Error(err));
  175. return resolve({ likes, dislikes });
  176. }
  177. );
  178. });
  179. }
  180. /**
  181. * Recalculates all dislikes and likes
  182. *
  183. * @returns {Promise} - returns a promise (resolve, reject)
  184. */
  185. RECALCULATE_ALL_RATINGS() {
  186. return new Promise((resolve, reject) => {
  187. async.waterfall(
  188. [
  189. next => {
  190. SongsModule.SongModel.find({}, { mediaSource: true }, next);
  191. },
  192. (songs, next) => {
  193. // TODO support spotify
  194. YouTubeModule.youtubeVideoModel.find({}, { youtubeId: true }, (err, videos) => {
  195. if (err) next(err);
  196. else
  197. next(null, [
  198. ...songs.map(song => song.mediaSource),
  199. ...videos.map(video => `youtube:${video.youtubeId}`)
  200. ]);
  201. });
  202. },
  203. (mediaSources, next) => {
  204. async.eachLimit(
  205. mediaSources,
  206. 2,
  207. (mediaSource, next) => {
  208. this.publishProgress({
  209. status: "update",
  210. message: `Recalculating ratings for ${mediaSource}`
  211. });
  212. MediaModule.runJob("RECALCULATE_RATINGS", { mediaSource }, this)
  213. .then(() => {
  214. next();
  215. })
  216. .catch(err => {
  217. next(err);
  218. });
  219. },
  220. err => {
  221. next(err);
  222. }
  223. );
  224. }
  225. ],
  226. err => {
  227. if (err) return reject(new Error(err));
  228. return resolve();
  229. }
  230. );
  231. });
  232. }
  233. /**
  234. * Gets ratings by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  235. *
  236. * @param {object} payload - object containing the payload
  237. * @param {string} payload.mediaSource - the media source
  238. * @param {string} payload.createMissing - whether to create missing ratings
  239. * @returns {Promise} - returns a promise (resolve, reject)
  240. */
  241. GET_RATINGS(payload) {
  242. return new Promise((resolve, reject) => {
  243. async.waterfall(
  244. [
  245. next =>
  246. CacheModule.runJob("HGET", { table: "ratings", key: payload.mediaSource }, this)
  247. .then(ratings => next(null, ratings))
  248. .catch(next),
  249. (ratings, next) => {
  250. if (ratings) return next(true, ratings);
  251. return MediaModule.RatingsModel.findOne({ mediaSource: payload.mediaSource }, next);
  252. },
  253. (ratings, next) => {
  254. if (ratings)
  255. return CacheModule.runJob(
  256. "HSET",
  257. {
  258. table: "ratings",
  259. key: payload.mediaSource,
  260. value: ratings
  261. },
  262. this
  263. ).then(ratings => next(true, ratings));
  264. if (!payload.createMissing) return next("Ratings not found.");
  265. return MediaModule.runJob("RECALCULATE_RATINGS", { mediaSource: payload.mediaSource }, this)
  266. .then(() => next())
  267. .catch(next);
  268. },
  269. next =>
  270. MediaModule.runJob("GET_RATINGS", { mediaSource: payload.mediaSource }, this)
  271. .then(res => next(null, res.ratings))
  272. .catch(next)
  273. ],
  274. (err, ratings) => {
  275. if (err && err !== true) return reject(new Error(err));
  276. return resolve({ ratings });
  277. }
  278. );
  279. });
  280. }
  281. /**
  282. * Remove ratings by id from the cache and Mongo
  283. *
  284. * @param {object} payload - object containing the payload
  285. * @param {string} payload.mediaSources - the media source
  286. * @returns {Promise} - returns a promise (resolve, reject)
  287. */
  288. REMOVE_RATINGS(payload) {
  289. return new Promise((resolve, reject) => {
  290. let { mediaSources } = payload;
  291. if (!Array.isArray(mediaSources)) mediaSources = [mediaSources];
  292. async.eachLimit(
  293. mediaSources,
  294. 1,
  295. (mediaSource, next) => {
  296. async.waterfall(
  297. [
  298. next => {
  299. MediaModule.RatingsModel.deleteOne({ mediaSource }, err => {
  300. if (err) next(err);
  301. else next();
  302. });
  303. },
  304. next => {
  305. CacheModule.runJob("HDEL", { table: "ratings", key: mediaSource }, this)
  306. .then(() => {
  307. next();
  308. })
  309. .catch(next);
  310. }
  311. ],
  312. next
  313. );
  314. },
  315. err => {
  316. if (err && err !== true) return reject(new Error(err));
  317. return resolve();
  318. }
  319. );
  320. });
  321. }
  322. /**
  323. * Get song or youtube video by mediaSource
  324. *
  325. * @param {object} payload - an object containing the payload
  326. * @param {string} payload.mediaSource - the media source of the song/video
  327. * @param {string} payload.userId - the user id
  328. * @returns {Promise} - returns a promise (resolve, reject)
  329. */
  330. GET_MEDIA(payload) {
  331. return new Promise((resolve, reject) => {
  332. async.waterfall(
  333. [
  334. next => {
  335. SongsModule.SongModel.findOne({ mediaSource: payload.mediaSource }, next);
  336. },
  337. (song, next) => {
  338. if (song && song.duration > 0) return next(true, song);
  339. if (payload.mediaSource.startsWith("youtube:")) {
  340. const youtubeId = payload.mediaSource.split(":")[1];
  341. return YouTubeModule.runJob(
  342. "GET_VIDEO",
  343. { identifier: youtubeId, createMissing: true },
  344. this
  345. )
  346. .then(response => {
  347. const { youtubeId, title, author, duration } = response.video;
  348. next(null, song, {
  349. mediaSource: `youtube:${youtubeId}`,
  350. title,
  351. artists: [author],
  352. duration
  353. });
  354. })
  355. .catch(next);
  356. }
  357. if (payload.mediaSource.startsWith("soundcloud:")) {
  358. const trackId = payload.mediaSource.split(":")[1];
  359. return SoundCloudModule.runJob(
  360. "GET_TRACK",
  361. { identifier: trackId, createMissing: true },
  362. this
  363. )
  364. .then(response => {
  365. const { trackId, title, username, artworkUrl, duration } = response.track;
  366. next(null, song, {
  367. mediaSource: `soundcloud:${trackId}`,
  368. title,
  369. artists: [username],
  370. thumbnail: artworkUrl,
  371. duration
  372. });
  373. })
  374. .catch(next);
  375. }
  376. if (payload.mediaSource.indexOf("soundcloud.com") !== -1) {
  377. return SoundCloudModule.runJob(
  378. "GET_TRACK_FROM_URL",
  379. { identifier: payload.mediaSource, createMissing: true },
  380. this
  381. )
  382. .then(response => {
  383. const { trackId, title, username, artworkUrl, duration } = response.track;
  384. next(null, song, {
  385. mediaSource: `soundcloud:${trackId}`,
  386. title,
  387. artists: [username],
  388. thumbnail: artworkUrl,
  389. duration
  390. });
  391. })
  392. .catch(next);
  393. }
  394. // TODO handle Spotify here
  395. return next("Invalid media source provided.");
  396. },
  397. (song, youtubeVideo, next) => {
  398. if (song && song.duration <= 0) {
  399. song.duration = youtubeVideo.duration;
  400. song.save({ validateBeforeSave: true }, err => {
  401. if (err) next(err, song);
  402. next(null, song);
  403. });
  404. } else {
  405. next(null, {
  406. ...youtubeVideo,
  407. skipDuration: 0,
  408. requestedBy: payload.userId,
  409. requestedAt: Date.now(),
  410. verified: false
  411. });
  412. }
  413. }
  414. ],
  415. (err, song) => {
  416. if (err && err !== true) return reject(new Error(err));
  417. return resolve({ song });
  418. }
  419. );
  420. });
  421. }
  422. /**
  423. * Remove import job by id from Mongo
  424. *
  425. * @param {object} payload - object containing the payload
  426. * @param {string} payload.jobIds - the job ids
  427. * @returns {Promise} - returns a promise (resolve, reject)
  428. */
  429. UPDATE_IMPORT_JOBS(payload) {
  430. return new Promise((resolve, reject) => {
  431. let { jobIds } = payload;
  432. if (!Array.isArray(jobIds)) jobIds = [jobIds];
  433. async.waterfall(
  434. [
  435. next => {
  436. MediaModule.ImportJobModel.find({ _id: { $in: jobIds } }, next);
  437. },
  438. (importJobs, next) => {
  439. async.eachLimit(
  440. importJobs,
  441. 1,
  442. (importJob, next) => {
  443. CacheModule.runJob("PUB", {
  444. channel: "importJob.updated",
  445. value: importJob
  446. })
  447. .then(() => next())
  448. .catch(next);
  449. },
  450. err => {
  451. if (err) next(err);
  452. else next(null, importJobs);
  453. }
  454. );
  455. }
  456. ],
  457. (err, importJobs) => {
  458. if (err && err !== true) return reject(new Error(err));
  459. return resolve({ importJobs });
  460. }
  461. );
  462. });
  463. }
  464. /**
  465. * Remove import job by id from Mongo
  466. *
  467. * @param {object} payload - object containing the payload
  468. * @param {string} payload.jobIds - the job ids
  469. * @returns {Promise} - returns a promise (resolve, reject)
  470. */
  471. REMOVE_IMPORT_JOBS(payload) {
  472. return new Promise((resolve, reject) => {
  473. let { jobIds } = payload;
  474. if (!Array.isArray(jobIds)) jobIds = [jobIds];
  475. async.waterfall(
  476. [
  477. next => {
  478. MediaModule.ImportJobModel.deleteMany({ _id: { $in: jobIds } }, err => {
  479. if (err) next(err);
  480. else next();
  481. });
  482. },
  483. next => {
  484. async.eachLimit(
  485. jobIds,
  486. 1,
  487. (jobId, next) => {
  488. CacheModule.runJob("PUB", {
  489. channel: "importJob.removed",
  490. value: jobId
  491. })
  492. .then(() => next())
  493. .catch(next);
  494. },
  495. next
  496. );
  497. }
  498. ],
  499. err => {
  500. if (err && err !== true) return reject(new Error(err));
  501. return resolve();
  502. }
  503. );
  504. });
  505. }
  506. }
  507. export default new _MediaModule();