youtube.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. import mongoose from "mongoose";
  2. import async from "async";
  3. import isLoginRequired from "../hooks/loginRequired";
  4. import { useHasPermission } from "../hooks/hasPermission";
  5. // eslint-disable-next-line
  6. import moduleManager from "../../index";
  7. const DBModule = moduleManager.modules.db;
  8. const CacheModule = moduleManager.modules.cache;
  9. const UtilsModule = moduleManager.modules.utils;
  10. const YouTubeModule = moduleManager.modules.youtube;
  11. const MediaModule = moduleManager.modules.media;
  12. export default {
  13. /**
  14. * Returns details about the YouTube quota usage
  15. *
  16. * @returns {{status: string, data: object}}
  17. */
  18. getQuotaStatus: useHasPermission("admin.view.youtube", function getQuotaStatus(session, fromDate, cb) {
  19. YouTubeModule.runJob("GET_QUOTA_STATUS", { fromDate }, this)
  20. .then(response => {
  21. this.log("SUCCESS", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status was successful.`);
  22. return cb({ status: "success", data: { status: response.status } });
  23. })
  24. .catch(async err => {
  25. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  26. this.log("ERROR", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status failed. "${err}"`);
  27. return cb({ status: "error", message: err });
  28. });
  29. }),
  30. /**
  31. * Returns YouTube quota chart data
  32. *
  33. * @param {object} session - the session object automatically added by the websocket
  34. * @param timePeriod - either hours or days
  35. * @param startDate - beginning date
  36. * @param endDate - end date
  37. * @param dataType - either usage or count
  38. * @returns {{status: string, data: object}}
  39. */
  40. getQuotaChartData: useHasPermission(
  41. "admin.view.youtube",
  42. function getQuotaChartData(session, timePeriod, startDate, endDate, dataType, cb) {
  43. YouTubeModule.runJob(
  44. "GET_QUOTA_CHART_DATA",
  45. { timePeriod, startDate: new Date(startDate), endDate: new Date(endDate), dataType },
  46. this
  47. )
  48. .then(data => {
  49. this.log("SUCCESS", "YOUTUBE_GET_QUOTA_CHART_DATA", `Getting quota chart data was successful.`);
  50. return cb({ status: "success", data });
  51. })
  52. .catch(async err => {
  53. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  54. this.log("ERROR", "YOUTUBE_GET_QUOTA_CHART_DATA", `Getting quota chart data failed. "${err}"`);
  55. return cb({ status: "error", message: err });
  56. });
  57. }
  58. ),
  59. /**
  60. * Gets api requests, used in the admin youtube page by the AdvancedTable component
  61. *
  62. * @param {object} session - the session object automatically added by the websocket
  63. * @param page - the page
  64. * @param pageSize - the size per page
  65. * @param properties - the properties to return for each news item
  66. * @param sort - the sort object
  67. * @param queries - the queries array
  68. * @param operator - the operator for queries
  69. * @param cb
  70. */
  71. getApiRequests: useHasPermission(
  72. "admin.view.youtube",
  73. async function getApiRequests(session, page, pageSize, properties, sort, queries, operator, cb) {
  74. async.waterfall(
  75. [
  76. next => {
  77. DBModule.runJob(
  78. "GET_DATA",
  79. {
  80. page,
  81. pageSize,
  82. properties,
  83. sort,
  84. queries,
  85. operator,
  86. modelName: "youtubeApiRequest",
  87. blacklistedProperties: [],
  88. specialProperties: {},
  89. specialQueries: {}
  90. },
  91. this
  92. )
  93. .then(response => {
  94. next(null, response);
  95. })
  96. .catch(err => {
  97. next(err);
  98. });
  99. }
  100. ],
  101. async (err, response) => {
  102. if (err && err !== true) {
  103. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  104. this.log("ERROR", "YOUTUBE_GET_API_REQUESTS", `Failed to get YouTube api requests. "${err}"`);
  105. return cb({ status: "error", message: err });
  106. }
  107. this.log("SUCCESS", "YOUTUBE_GET_API_REQUESTS", `Fetched YouTube api requests successfully.`);
  108. return cb({
  109. status: "success",
  110. message: "Successfully fetched YouTube api requests.",
  111. data: response
  112. });
  113. }
  114. );
  115. }
  116. ),
  117. /**
  118. * Returns a specific api request
  119. *
  120. * @returns {{status: string, data: object}}
  121. */
  122. getApiRequest: useHasPermission("youtube.getApiRequest", function getApiRequest(session, apiRequestId, cb) {
  123. if (!mongoose.Types.ObjectId.isValid(apiRequestId))
  124. return cb({ status: "error", message: "Api request id is not a valid ObjectId." });
  125. return YouTubeModule.runJob("GET_API_REQUEST", { apiRequestId }, this)
  126. .then(response => {
  127. this.log(
  128. "SUCCESS",
  129. "YOUTUBE_GET_API_REQUEST",
  130. `Getting api request with id ${apiRequestId} was successful.`
  131. );
  132. return cb({ status: "success", data: { apiRequest: response.apiRequest } });
  133. })
  134. .catch(async err => {
  135. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  136. this.log(
  137. "ERROR",
  138. "YOUTUBE_GET_API_REQUEST",
  139. `Getting api request with id ${apiRequestId} failed. "${err}"`
  140. );
  141. return cb({ status: "error", message: err });
  142. });
  143. }),
  144. /**
  145. * Reset stored API requests
  146. *
  147. * @returns {{status: string, data: object}}
  148. */
  149. resetStoredApiRequests: useHasPermission(
  150. "youtube.resetStoredApiRequests",
  151. async function resetStoredApiRequests(session, cb) {
  152. this.keepLongJob();
  153. this.publishProgress({
  154. status: "started",
  155. title: "Reset stored API requests",
  156. message: "Resetting stored API requests.",
  157. id: this.toString()
  158. });
  159. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  160. await CacheModule.runJob(
  161. "PUB",
  162. {
  163. channel: "longJob.added",
  164. value: { jobId: this.toString(), userId: session.userId }
  165. },
  166. this
  167. );
  168. YouTubeModule.runJob("RESET_STORED_API_REQUESTS", {}, this)
  169. .then(() => {
  170. this.log(
  171. "SUCCESS",
  172. "YOUTUBE_RESET_STORED_API_REQUESTS",
  173. `Resetting stored API requests was successful.`
  174. );
  175. this.publishProgress({
  176. status: "success",
  177. message: "Successfully reset stored YouTube API requests."
  178. });
  179. return cb({ status: "success", message: "Successfully reset stored YouTube API requests" });
  180. })
  181. .catch(async err => {
  182. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  183. this.log(
  184. "ERROR",
  185. "YOUTUBE_RESET_STORED_API_REQUESTS",
  186. `Resetting stored API requests failed. "${err}"`
  187. );
  188. this.publishProgress({
  189. status: "error",
  190. message: err
  191. });
  192. return cb({ status: "error", message: err });
  193. });
  194. }
  195. ),
  196. /**
  197. * Remove stored API requests
  198. *
  199. * @returns {{status: string, data: object}}
  200. */
  201. removeStoredApiRequest: useHasPermission(
  202. "youtube.removeStoredApiRequest",
  203. function removeStoredApiRequest(session, requestId, cb) {
  204. YouTubeModule.runJob("REMOVE_STORED_API_REQUEST", { requestId }, this)
  205. .then(() => {
  206. this.log(
  207. "SUCCESS",
  208. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  209. `Removing stored API request "${requestId}" was successful.`
  210. );
  211. return cb({ status: "success", message: "Successfully removed stored YouTube API request" });
  212. })
  213. .catch(async err => {
  214. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  215. this.log(
  216. "ERROR",
  217. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  218. `Removing stored API request "${requestId}" failed. "${err}"`
  219. );
  220. return cb({ status: "error", message: err });
  221. });
  222. }
  223. ),
  224. /**
  225. * Gets videos, used in the admin youtube page by the AdvancedTable component
  226. *
  227. * @param {object} session - the session object automatically added by the websocket
  228. * @param page - the page
  229. * @param pageSize - the size per page
  230. * @param properties - the properties to return for each news item
  231. * @param sort - the sort object
  232. * @param queries - the queries array
  233. * @param operator - the operator for queries
  234. * @param cb
  235. */
  236. getVideos: useHasPermission(
  237. "admin.view.youtubeVideos",
  238. async function getVideos(session, page, pageSize, properties, sort, queries, operator, cb) {
  239. async.waterfall(
  240. [
  241. next => {
  242. DBModule.runJob(
  243. "GET_DATA",
  244. {
  245. page,
  246. pageSize,
  247. properties,
  248. sort,
  249. queries,
  250. operator,
  251. modelName: "youtubeVideo",
  252. blacklistedProperties: [],
  253. specialProperties: {
  254. songId: [
  255. // Fetch songs from songs collection with a matching youtubeId
  256. {
  257. $lookup: {
  258. from: "songs",
  259. localField: "youtubeId",
  260. foreignField: "youtubeId",
  261. as: "song"
  262. }
  263. },
  264. // Turn the array of songs returned in the last step into one object, since only one song should have been returned maximum
  265. {
  266. $unwind: {
  267. path: "$song",
  268. preserveNullAndEmptyArrays: true
  269. }
  270. },
  271. // Add new field songId, which grabs the song object's _id and tries turning it into a string
  272. {
  273. $addFields: {
  274. songId: {
  275. $convert: {
  276. input: "$song._id",
  277. to: "string",
  278. onError: "",
  279. onNull: ""
  280. }
  281. }
  282. }
  283. },
  284. // Cleanup, don't return the song object for any further steps
  285. {
  286. $project: {
  287. song: 0
  288. }
  289. }
  290. ]
  291. },
  292. specialQueries: {},
  293. specialFilters: {
  294. importJob: importJobId => [
  295. {
  296. $lookup: {
  297. from: "importjobs",
  298. let: { youtubeId: "$youtubeId" },
  299. pipeline: [
  300. {
  301. $match: {
  302. _id: mongoose.Types.ObjectId(importJobId)
  303. }
  304. },
  305. {
  306. $addFields: {
  307. importJob: {
  308. $in: ["$$youtubeId", "$response.successfulVideoIds"]
  309. }
  310. }
  311. },
  312. {
  313. $project: {
  314. importJob: 1,
  315. _id: 0
  316. }
  317. }
  318. ],
  319. as: "importJob"
  320. }
  321. },
  322. {
  323. $unwind: "$importJob"
  324. },
  325. {
  326. $set: {
  327. importJob: "$importJob.importJob"
  328. }
  329. }
  330. ]
  331. }
  332. },
  333. this
  334. )
  335. .then(response => {
  336. next(null, response);
  337. })
  338. .catch(err => {
  339. next(err);
  340. });
  341. }
  342. ],
  343. async (err, response) => {
  344. if (err && err !== true) {
  345. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  346. this.log("ERROR", "YOUTUBE_GET_VIDEOS", `Failed to get YouTube videos. "${err}"`);
  347. return cb({ status: "error", message: err });
  348. }
  349. this.log("SUCCESS", "YOUTUBE_GET_VIDEOS", `Fetched YouTube videos successfully.`);
  350. return cb({
  351. status: "success",
  352. message: "Successfully fetched YouTube videos.",
  353. data: response
  354. });
  355. }
  356. );
  357. }
  358. ),
  359. /**
  360. * Get a YouTube video
  361. *
  362. * @returns {{status: string, data: object}}
  363. */
  364. getVideo: isLoginRequired(function getVideo(session, identifier, createMissing, cb) {
  365. YouTubeModule.runJob("GET_VIDEO", { identifier, createMissing }, this)
  366. .then(res => {
  367. this.log("SUCCESS", "YOUTUBE_GET_VIDEO", `Fetching video was successful.`);
  368. return cb({ status: "success", message: "Successfully fetched YouTube video", data: res.video });
  369. })
  370. .catch(async err => {
  371. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  372. this.log("ERROR", "YOUTUBE_GET_VIDEO", `Fetching video failed. "${err}"`);
  373. return cb({ status: "error", message: err });
  374. });
  375. }),
  376. /**
  377. * Remove YouTube videos
  378. *
  379. * @returns {{status: string, data: object}}
  380. */
  381. removeVideos: useHasPermission("youtube.removeVideos", async function removeVideos(session, videoIds, cb) {
  382. this.keepLongJob();
  383. this.publishProgress({
  384. status: "started",
  385. title: "Bulk remove YouTube videos",
  386. message: "Bulk removing YouTube videos.",
  387. id: this.toString()
  388. });
  389. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  390. await CacheModule.runJob(
  391. "PUB",
  392. {
  393. channel: "longJob.added",
  394. value: { jobId: this.toString(), userId: session.userId }
  395. },
  396. this
  397. );
  398. YouTubeModule.runJob("REMOVE_VIDEOS", { videoIds }, this)
  399. .then(() => {
  400. this.log("SUCCESS", "YOUTUBE_REMOVE_VIDEOS", `Removing videos was successful.`);
  401. this.publishProgress({
  402. status: "success",
  403. message: "Successfully removed YouTube videos."
  404. });
  405. return cb({ status: "success", message: "Successfully removed YouTube videos" });
  406. })
  407. .catch(async err => {
  408. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  409. this.log("ERROR", "YOUTUBE_REMOVE_VIDEOS", `Removing videos failed. "${err}"`);
  410. this.publishProgress({
  411. status: "error",
  412. message: err
  413. });
  414. return cb({ status: "error", message: err });
  415. });
  416. }),
  417. /**
  418. * Requests a set of YouTube videos
  419. *
  420. * @param {object} session - the session object automatically added by the websocket
  421. * @param {string} url - the url of the the YouTube playlist
  422. * @param {boolean} musicOnly - whether to only get music from the playlist
  423. * @param {boolean} musicOnly - whether to return videos
  424. * @param {Function} cb - gets called with the result
  425. */
  426. requestSet: isLoginRequired(function requestSet(session, url, musicOnly, returnVideos, cb) {
  427. YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
  428. .then(response => {
  429. this.log(
  430. "SUCCESS",
  431. "REQUEST_SET",
  432. `Successfully imported a YouTube playlist to be requested for user "${session.userId}".`
  433. );
  434. return cb({
  435. status: "success",
  436. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`,
  437. videos: returnVideos ? response.videos : null
  438. });
  439. })
  440. .catch(async err => {
  441. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  442. this.log(
  443. "ERROR",
  444. "REQUEST_SET",
  445. `Importing a YouTube playlist to be requested failed for user "${session.userId}". "${err}"`
  446. );
  447. return cb({ status: "error", message: err });
  448. });
  449. }),
  450. /**
  451. * Requests a set of YouTube videos as an admin
  452. *
  453. * @param {object} session - the session object automatically added by the websocket
  454. * @param {string} url - the url of the the YouTube playlist
  455. * @param {boolean} musicOnly - whether to only get music from the playlist
  456. * @param {boolean} musicOnly - whether to return videos
  457. * @param {Function} cb - gets called with the result
  458. */
  459. requestSetAdmin: useHasPermission(
  460. "youtube.requestSetAdmin",
  461. async function requestSetAdmin(session, url, musicOnly, returnVideos, cb) {
  462. const importJobModel = await DBModule.runJob("GET_MODEL", { modelName: "importJob" }, this);
  463. this.keepLongJob();
  464. this.publishProgress({
  465. status: "started",
  466. title: "Import playlist",
  467. message: "Importing playlist.",
  468. id: this.toString()
  469. });
  470. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  471. await CacheModule.runJob(
  472. "PUB",
  473. {
  474. channel: "longJob.added",
  475. value: { jobId: this.toString(), userId: session.userId }
  476. },
  477. this
  478. );
  479. async.waterfall(
  480. [
  481. next => {
  482. importJobModel.create(
  483. {
  484. type: "youtube",
  485. query: {
  486. url,
  487. musicOnly
  488. },
  489. status: "in-progress",
  490. response: {},
  491. requestedBy: session.userId,
  492. requestedAt: Date.now()
  493. },
  494. next
  495. );
  496. },
  497. (importJob, next) => {
  498. YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
  499. .then(response => {
  500. next(null, importJob, response);
  501. })
  502. .catch(err => {
  503. next(err, importJob);
  504. });
  505. },
  506. (importJob, response, next) => {
  507. importJobModel.updateOne(
  508. { _id: importJob._id },
  509. {
  510. $set: {
  511. status: "success",
  512. response: {
  513. failed: response.failed,
  514. successful: response.successful,
  515. alreadyInDatabase: response.alreadyInDatabase,
  516. successfulVideoIds: response.successfulVideoIds,
  517. failedVideoIds: response.failedVideoIds
  518. }
  519. }
  520. },
  521. err => {
  522. if (err) next(err, importJob);
  523. else
  524. MediaModule.runJob("UPDATE_IMPORT_JOBS", { jobIds: importJob._id })
  525. .then(() => next(null, importJob, response))
  526. .catch(error => next(error, importJob));
  527. }
  528. );
  529. }
  530. ],
  531. async (err, importJob, response) => {
  532. if (err) {
  533. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  534. this.log(
  535. "ERROR",
  536. "REQUEST_SET_ADMIN",
  537. `Importing a YouTube playlist to be requested failed for admin "${session.userId}". "${err}"`
  538. );
  539. importJobModel.updateOne({ _id: importJob._id }, { $set: { status: "error" } });
  540. MediaModule.runJob("UPDATE_IMPORT_JOBS", { jobIds: importJob._id });
  541. return cb({ status: "error", message: err });
  542. }
  543. this.log(
  544. "SUCCESS",
  545. "REQUEST_SET_ADMIN",
  546. `Successfully imported a YouTube playlist to be requested for admin "${session.userId}".`
  547. );
  548. this.publishProgress({
  549. status: "success",
  550. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`
  551. });
  552. return cb({
  553. status: "success",
  554. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`,
  555. videos: returnVideos ? response.videos : null
  556. });
  557. }
  558. );
  559. }
  560. )
  561. };