media.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. console.log(345345, err);
  110. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  111. reject(new Error(err));
  112. } else resolve();
  113. }
  114. );
  115. });
  116. }
  117. /**
  118. * Recalculates dislikes and likes
  119. *
  120. * @param {object} payload - returns an object containing the payload
  121. * @param {string} payload.mediaSource - the media source
  122. * @returns {Promise} - returns a promise (resolve, reject)
  123. */
  124. async RECALCULATE_RATINGS(payload) {
  125. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  126. return new Promise((resolve, reject) => {
  127. async.waterfall(
  128. [
  129. next => {
  130. playlistModel.countDocuments(
  131. { songs: { $elemMatch: { mediaSource: payload.mediaSource } }, type: "user-liked" },
  132. (err, likes) => {
  133. if (err) return next(err);
  134. return next(null, likes);
  135. }
  136. );
  137. },
  138. (likes, next) => {
  139. playlistModel.countDocuments(
  140. { songs: { $elemMatch: { mediaSource: payload.mediaSource } }, type: "user-disliked" },
  141. (err, dislikes) => {
  142. if (err) return next(err);
  143. return next(err, { likes, dislikes });
  144. }
  145. );
  146. },
  147. ({ likes, dislikes }, next) => {
  148. MediaModule.RatingsModel.findOneAndUpdate(
  149. { mediaSource: payload.mediaSource },
  150. {
  151. $set: {
  152. likes,
  153. dislikes
  154. }
  155. },
  156. { new: true, upsert: true },
  157. next
  158. );
  159. },
  160. (ratings, next) => {
  161. CacheModule.runJob(
  162. "HSET",
  163. {
  164. table: "ratings",
  165. key: payload.mediaSource,
  166. value: ratings
  167. },
  168. this
  169. )
  170. .then(ratings => next(null, ratings))
  171. .catch(next);
  172. }
  173. ],
  174. (err, { likes, dislikes }) => {
  175. if (err) return reject(new Error(err));
  176. return resolve({ likes, dislikes });
  177. }
  178. );
  179. });
  180. }
  181. /**
  182. * Recalculates all dislikes and likes
  183. *
  184. * @returns {Promise} - returns a promise (resolve, reject)
  185. */
  186. RECALCULATE_ALL_RATINGS() {
  187. return new Promise((resolve, reject) => {
  188. async.waterfall(
  189. [
  190. next => {
  191. SongsModule.SongModel.find({}, { mediaSource: true }, next);
  192. },
  193. (songs, next) => {
  194. // TODO support spotify
  195. YouTubeModule.youtubeVideoModel.find({}, { youtubeId: true }, (err, videos) => {
  196. if (err) next(err);
  197. else
  198. next(null, [
  199. ...songs.map(song => song.mediaSource),
  200. ...videos.map(video => `youtube:${video.youtubeId}`)
  201. ]);
  202. });
  203. },
  204. (mediaSources, next) => {
  205. async.eachLimit(
  206. mediaSources,
  207. 2,
  208. (mediaSource, next) => {
  209. this.publishProgress({
  210. status: "update",
  211. message: `Recalculating ratings for ${mediaSource}`
  212. });
  213. MediaModule.runJob("RECALCULATE_RATINGS", { mediaSource }, this)
  214. .then(() => {
  215. next();
  216. })
  217. .catch(err => {
  218. next(err);
  219. });
  220. },
  221. err => {
  222. next(err);
  223. }
  224. );
  225. }
  226. ],
  227. err => {
  228. if (err) return reject(new Error(err));
  229. return resolve();
  230. }
  231. );
  232. });
  233. }
  234. /**
  235. * Gets ratings by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  236. *
  237. * @param {object} payload - object containing the payload
  238. * @param {string} payload.mediaSource - the media source
  239. * @param {string} payload.createMissing - whether to create missing ratings
  240. * @returns {Promise} - returns a promise (resolve, reject)
  241. */
  242. GET_RATINGS(payload) {
  243. return new Promise((resolve, reject) => {
  244. async.waterfall(
  245. [
  246. next =>
  247. CacheModule.runJob("HGET", { table: "ratings", key: payload.mediaSource }, this)
  248. .then(ratings => next(null, ratings))
  249. .catch(next),
  250. (ratings, next) => {
  251. if (ratings) return next(true, ratings);
  252. return MediaModule.RatingsModel.findOne({ mediaSource: payload.mediaSource }, next);
  253. },
  254. (ratings, next) => {
  255. if (ratings)
  256. return CacheModule.runJob(
  257. "HSET",
  258. {
  259. table: "ratings",
  260. key: payload.mediaSource,
  261. value: ratings
  262. },
  263. this
  264. ).then(ratings => next(true, ratings));
  265. if (!payload.createMissing) return next("Ratings not found.");
  266. return MediaModule.runJob("RECALCULATE_RATINGS", { mediaSource: payload.mediaSource }, this)
  267. .then(() => next())
  268. .catch(next);
  269. },
  270. next =>
  271. MediaModule.runJob("GET_RATINGS", { mediaSource: payload.mediaSource }, this)
  272. .then(res => next(null, res.ratings))
  273. .catch(next)
  274. ],
  275. (err, ratings) => {
  276. if (err && err !== true) return reject(new Error(err));
  277. return resolve({ ratings });
  278. }
  279. );
  280. });
  281. }
  282. /**
  283. * Remove ratings by id from the cache and Mongo
  284. *
  285. * @param {object} payload - object containing the payload
  286. * @param {string} payload.mediaSources - the media source
  287. * @returns {Promise} - returns a promise (resolve, reject)
  288. */
  289. REMOVE_RATINGS(payload) {
  290. return new Promise((resolve, reject) => {
  291. let { mediaSources } = payload;
  292. if (!Array.isArray(mediaSources)) mediaSources = [mediaSources];
  293. async.eachLimit(
  294. mediaSources,
  295. 1,
  296. (mediaSource, next) => {
  297. async.waterfall(
  298. [
  299. next => {
  300. MediaModule.RatingsModel.deleteOne({ mediaSource }, err => {
  301. if (err) next(err);
  302. else next();
  303. });
  304. },
  305. next => {
  306. CacheModule.runJob("HDEL", { table: "ratings", key: mediaSource }, this)
  307. .then(() => {
  308. next();
  309. })
  310. .catch(next);
  311. }
  312. ],
  313. next
  314. );
  315. },
  316. err => {
  317. if (err && err !== true) return reject(new Error(err));
  318. return resolve();
  319. }
  320. );
  321. });
  322. }
  323. /**
  324. * Get song or youtube video by mediaSource
  325. *
  326. * @param {object} payload - an object containing the payload
  327. * @param {string} payload.mediaSource - the media source of the song/video
  328. * @param {string} payload.userId - the user id
  329. * @returns {Promise} - returns a promise (resolve, reject)
  330. */
  331. GET_MEDIA(payload) {
  332. return new Promise((resolve, reject) => {
  333. async.waterfall(
  334. [
  335. next => {
  336. SongsModule.SongModel.findOne({ mediaSource: payload.mediaSource }, next);
  337. },
  338. (song, next) => {
  339. if (song && song.duration > 0) return next(true, song);
  340. if (payload.mediaSource.startsWith("youtube:")) {
  341. const youtubeId = payload.mediaSource.split(":")[1];
  342. return YouTubeModule.runJob(
  343. "GET_VIDEO",
  344. { identifier: youtubeId, createMissing: true },
  345. this
  346. )
  347. .then(response => {
  348. const { youtubeId, title, author, duration } = response.video;
  349. next(null, song, {
  350. mediaSource: `youtube:${youtubeId}`,
  351. title,
  352. artists: [author],
  353. duration
  354. });
  355. })
  356. .catch(next);
  357. }
  358. if (payload.mediaSource.startsWith("soundcloud:")) {
  359. const trackId = payload.mediaSource.split(":")[1];
  360. return SoundCloudModule.runJob(
  361. "GET_TRACK",
  362. { identifier: trackId, createMissing: true },
  363. this
  364. )
  365. .then(response => {
  366. const { trackId, title, username, artworkUrl, duration } = response.track;
  367. next(null, song, {
  368. mediaSource: `soundcloud:${trackId}`,
  369. title,
  370. artists: [username],
  371. thumbnail: artworkUrl,
  372. duration
  373. });
  374. })
  375. .catch(next);
  376. }
  377. // TODO handle Spotify here
  378. return next("Invalid media source provided.");
  379. },
  380. (song, youtubeVideo, next) => {
  381. if (song && song.duration <= 0) {
  382. song.duration = youtubeVideo.duration;
  383. song.save({ validateBeforeSave: true }, err => {
  384. if (err) next(err, song);
  385. next(null, song);
  386. });
  387. } else {
  388. next(null, {
  389. ...youtubeVideo,
  390. skipDuration: 0,
  391. requestedBy: payload.userId,
  392. requestedAt: Date.now(),
  393. verified: false
  394. });
  395. }
  396. }
  397. ],
  398. (err, song) => {
  399. if (err && err !== true) return reject(new Error(err));
  400. return resolve({ song });
  401. }
  402. );
  403. });
  404. }
  405. /**
  406. * Remove import job by id from Mongo
  407. *
  408. * @param {object} payload - object containing the payload
  409. * @param {string} payload.jobIds - the job ids
  410. * @returns {Promise} - returns a promise (resolve, reject)
  411. */
  412. UPDATE_IMPORT_JOBS(payload) {
  413. return new Promise((resolve, reject) => {
  414. let { jobIds } = payload;
  415. if (!Array.isArray(jobIds)) jobIds = [jobIds];
  416. async.waterfall(
  417. [
  418. next => {
  419. MediaModule.ImportJobModel.find({ _id: { $in: jobIds } }, next);
  420. },
  421. (importJobs, next) => {
  422. async.eachLimit(
  423. importJobs,
  424. 1,
  425. (importJob, next) => {
  426. CacheModule.runJob("PUB", {
  427. channel: "importJob.updated",
  428. value: importJob
  429. })
  430. .then(() => next())
  431. .catch(next);
  432. },
  433. err => {
  434. if (err) next(err);
  435. else next(null, importJobs);
  436. }
  437. );
  438. }
  439. ],
  440. (err, importJobs) => {
  441. if (err && err !== true) return reject(new Error(err));
  442. return resolve({ importJobs });
  443. }
  444. );
  445. });
  446. }
  447. /**
  448. * Remove import job by id from Mongo
  449. *
  450. * @param {object} payload - object containing the payload
  451. * @param {string} payload.jobIds - the job ids
  452. * @returns {Promise} - returns a promise (resolve, reject)
  453. */
  454. REMOVE_IMPORT_JOBS(payload) {
  455. return new Promise((resolve, reject) => {
  456. let { jobIds } = payload;
  457. if (!Array.isArray(jobIds)) jobIds = [jobIds];
  458. async.waterfall(
  459. [
  460. next => {
  461. MediaModule.ImportJobModel.deleteMany({ _id: { $in: jobIds } }, err => {
  462. if (err) next(err);
  463. else next();
  464. });
  465. },
  466. next => {
  467. async.eachLimit(
  468. jobIds,
  469. 1,
  470. (jobId, next) => {
  471. CacheModule.runJob("PUB", {
  472. channel: "importJob.removed",
  473. value: jobId
  474. })
  475. .then(() => next())
  476. .catch(next);
  477. },
  478. next
  479. );
  480. }
  481. ],
  482. err => {
  483. if (err && err !== true) return reject(new Error(err));
  484. return resolve();
  485. }
  486. );
  487. });
  488. }
  489. }
  490. export default new _MediaModule();