youtube.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 mediaSource
  256. {
  257. $lookup: {
  258. from: "songs", // TODO fix this to support mediasource, so start with youtube:, so add a new pipeline steps
  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. * Gets channels, used in the admin youtube page by the AdvancedTable component
  361. *
  362. * @param {object} session - the session object automatically added by the websocket
  363. * @param page - the page
  364. * @param pageSize - the size per page
  365. * @param properties - the properties to return for each news item
  366. * @param sort - the sort object
  367. * @param queries - the queries array
  368. * @param operator - the operator for queries
  369. * @param cb
  370. */
  371. getChannels: useHasPermission(
  372. "admin.view.youtubeChannels",
  373. async function getChannels(session, page, pageSize, properties, sort, queries, operator, cb) {
  374. async.waterfall(
  375. [
  376. next => {
  377. DBModule.runJob(
  378. "GET_DATA",
  379. {
  380. page,
  381. pageSize,
  382. properties,
  383. sort,
  384. queries,
  385. operator,
  386. modelName: "youtubeChannel",
  387. blacklistedProperties: [],
  388. specialProperties: {},
  389. specialQueries: {},
  390. specialFilters: {}
  391. },
  392. this
  393. )
  394. .then(response => {
  395. next(null, response);
  396. })
  397. .catch(err => {
  398. next(err);
  399. });
  400. }
  401. ],
  402. async (err, response) => {
  403. if (err && err !== true) {
  404. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  405. this.log("ERROR", "YOUTUBE_GET_CHANNELS", `Failed to get YouTube channels. "${err}"`);
  406. return cb({ status: "error", message: err });
  407. }
  408. this.log("SUCCESS", "YOUTUBE_GET_CHANNELS", `Fetched YouTube channels successfully.`);
  409. return cb({
  410. status: "success",
  411. message: "Successfully fetched YouTube channels.",
  412. data: response
  413. });
  414. }
  415. );
  416. }
  417. ),
  418. /**
  419. * Get a YouTube video
  420. *
  421. * @returns {{status: string, data: object}}
  422. */
  423. getVideo: isLoginRequired(function getVideo(session, identifier, createMissing, cb) {
  424. return YouTubeModule.runJob("GET_VIDEOS", { identifiers: [identifier], createMissing }, this)
  425. .then(res => {
  426. this.log("SUCCESS", "YOUTUBE_GET_VIDEO", `Fetching video was successful.`);
  427. return cb({ status: "success", message: "Successfully fetched YouTube video", data: res.videos[0] });
  428. })
  429. .catch(async err => {
  430. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  431. this.log("ERROR", "YOUTUBE_GET_VIDEO", `Fetching video failed. "${err}"`);
  432. return cb({ status: "error", message: err });
  433. });
  434. }),
  435. /**
  436. * Get a YouTube channel from ID
  437. *
  438. * @returns {{status: string, data: object}}
  439. */
  440. getChannel: useHasPermission("youtube.getChannel", function getChannel(session, channelId, cb) {
  441. return YouTubeModule.runJob("GET_CHANNELS_FROM_IDS", { channelIds: [channelId] }, this)
  442. .then(res => {
  443. if (res.channels.length === 0) {
  444. this.log("ERROR", "YOUTUBE_GET_CHANNELS_FROM_IDS", `Fetching channel failed.`);
  445. return cb({ status: "error", message: "Failed to get channel" });
  446. }
  447. this.log("SUCCESS", "YOUTUBE_GET_CHANNELS_FROM_IDS", `Fetching channel was successful.`);
  448. return cb({
  449. status: "success",
  450. message: "Successfully fetched YouTube channel",
  451. data: res.channels[0]
  452. });
  453. })
  454. .catch(async err => {
  455. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  456. this.log("ERROR", "YOUTUBE_GET_CHANNELS_FROM_IDS", `Fetching video failed. "${err}"`);
  457. return cb({ status: "error", message: err });
  458. });
  459. }),
  460. /**
  461. * Remove YouTube videos
  462. *
  463. * @returns {{status: string, data: object}}
  464. */
  465. removeVideos: useHasPermission("youtube.removeVideos", async function removeVideos(session, videoIds, cb) {
  466. this.keepLongJob();
  467. this.publishProgress({
  468. status: "started",
  469. title: "Bulk remove YouTube videos",
  470. message: "Bulk removing YouTube videos.",
  471. id: this.toString()
  472. });
  473. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  474. await CacheModule.runJob(
  475. "PUB",
  476. {
  477. channel: "longJob.added",
  478. value: { jobId: this.toString(), userId: session.userId }
  479. },
  480. this
  481. );
  482. YouTubeModule.runJob("REMOVE_VIDEOS", { videoIds }, this)
  483. .then(() => {
  484. this.log("SUCCESS", "YOUTUBE_REMOVE_VIDEOS", `Removing videos was successful.`);
  485. this.publishProgress({
  486. status: "success",
  487. message: "Successfully removed YouTube videos."
  488. });
  489. return cb({ status: "success", message: "Successfully removed YouTube videos" });
  490. })
  491. .catch(async err => {
  492. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  493. this.log("ERROR", "YOUTUBE_REMOVE_VIDEOS", `Removing videos failed. "${err}"`);
  494. this.publishProgress({
  495. status: "error",
  496. message: err
  497. });
  498. return cb({ status: "error", message: err });
  499. });
  500. }),
  501. /**
  502. * Gets missing YouTube video's from all playlists, stations and songs
  503. *
  504. * @returns {{status: string, data: object}}
  505. */
  506. getMissingVideos: useHasPermission("youtube.getMissingVideos", function getMissingVideos(session, cb) {
  507. return YouTubeModule.runJob("GET_MISSING_VIDEOS", {}, this)
  508. .then(response => {
  509. this.log("SUCCESS", "YOUTUBE_GET_MISSING_VIDEOS", `Getting missing videos was successful.`);
  510. console.log("KRIS", response);
  511. return cb({ status: "success", data: { ...response } });
  512. })
  513. .catch(async err => {
  514. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  515. this.log("ERROR", "YOUTUBE_GET_MISSING_VIDEOS", `Getting missing videos failed. "${err}"`);
  516. return cb({ status: "error", message: err });
  517. });
  518. }),
  519. /**
  520. * Gets missing YouTube video's from all playlists, stations and songs
  521. *
  522. * @returns {{status: string, data: object}}
  523. */
  524. updateVideosV1ToV2: useHasPermission("youtube.updateVideosV1ToV2", function updateVideosV1ToV2(session, cb) {
  525. return YouTubeModule.runJob("UPDATE_VIDEOS_V1_TO_V2", {}, this)
  526. .then(response => {
  527. this.log("SUCCESS", "YOUTUBE_UPDATE_VIDEOS_V1_TO_V2", `Updating v1 videos to v2 was successful.`);
  528. console.log("KRIS", response);
  529. return cb({ status: "success", data: { ...response } });
  530. })
  531. .catch(async err => {
  532. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  533. this.log("ERROR", "YOUTUBE_UPDATE_VIDEOS_V1_TO_V2", `Updating v1 videos to v2 failed. "${err}"`);
  534. return cb({ status: "error", message: err });
  535. });
  536. }),
  537. /**
  538. * Requests a set of YouTube videos
  539. *
  540. * @param {object} session - the session object automatically added by the websocket
  541. * @param {string} url - the url of the the YouTube playlist
  542. * @param {boolean} musicOnly - whether to only get music from the playlist
  543. * @param {boolean} musicOnly - whether to return videos
  544. * @param {Function} cb - gets called with the result
  545. */
  546. requestSet: isLoginRequired(function requestSet(session, url, musicOnly, returnVideos, cb) {
  547. YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
  548. .then(response => {
  549. this.log(
  550. "SUCCESS",
  551. "REQUEST_SET",
  552. `Successfully imported a YouTube playlist to be requested for user "${session.userId}".`
  553. );
  554. return cb({
  555. status: "success",
  556. message: `Playlist is done importing.`,
  557. videos: returnVideos ? response.videos : null
  558. });
  559. })
  560. .catch(async err => {
  561. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  562. this.log(
  563. "ERROR",
  564. "REQUEST_SET",
  565. `Importing a YouTube playlist to be requested failed for user "${session.userId}". "${err}"`
  566. );
  567. return cb({ status: "error", message: err });
  568. });
  569. }),
  570. /**
  571. * Requests a set of YouTube videos as an admin
  572. *
  573. * @param {object} session - the session object automatically added by the websocket
  574. * @param {string} url - the url of the the YouTube playlist
  575. * @param {boolean} musicOnly - whether to only get music from the playlist
  576. * @param {boolean} musicOnly - whether to return videos
  577. * @param {Function} cb - gets called with the result
  578. */
  579. requestSetAdmin: useHasPermission(
  580. "youtube.requestSetAdmin",
  581. async function requestSetAdmin(session, url, musicOnly, returnVideos, cb) {
  582. const importJobModel = await DBModule.runJob("GET_MODEL", { modelName: "importJob" }, this);
  583. this.keepLongJob();
  584. this.publishProgress({
  585. status: "started",
  586. title: "Import playlist",
  587. message: "Importing playlist.",
  588. id: this.toString()
  589. });
  590. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  591. await CacheModule.runJob(
  592. "PUB",
  593. {
  594. channel: "longJob.added",
  595. value: { jobId: this.toString(), userId: session.userId }
  596. },
  597. this
  598. );
  599. async.waterfall(
  600. [
  601. next => {
  602. importJobModel.create(
  603. {
  604. type: "youtube",
  605. query: {
  606. url,
  607. musicOnly
  608. },
  609. status: "in-progress",
  610. response: {},
  611. requestedBy: session.userId,
  612. requestedAt: Date.now()
  613. },
  614. next
  615. );
  616. },
  617. (importJob, next) => {
  618. YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
  619. .then(response => {
  620. next(null, importJob, response);
  621. })
  622. .catch(err => {
  623. next(err, importJob);
  624. });
  625. },
  626. (importJob, response, next) => {
  627. importJobModel.updateOne(
  628. { _id: importJob._id },
  629. {
  630. $set: {
  631. status: "success",
  632. response: {
  633. failed: response.failed,
  634. successful: response.successful,
  635. alreadyInDatabase: response.alreadyInDatabase,
  636. successfulVideoIds: response.successfulVideoIds,
  637. failedVideoIds: response.failedVideoIds
  638. }
  639. }
  640. },
  641. err => {
  642. if (err) next(err, importJob);
  643. else
  644. MediaModule.runJob("UPDATE_IMPORT_JOBS", { jobIds: importJob._id })
  645. .then(() => next(null, importJob, response))
  646. .catch(error => next(error, importJob));
  647. }
  648. );
  649. }
  650. ],
  651. async (err, importJob, response) => {
  652. if (err) {
  653. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  654. this.log(
  655. "ERROR",
  656. "REQUEST_SET_ADMIN",
  657. `Importing a YouTube playlist to be requested failed for admin "${session.userId}". "${err}"`
  658. );
  659. importJobModel.updateOne({ _id: importJob._id }, { $set: { status: "error" } });
  660. MediaModule.runJob("UPDATE_IMPORT_JOBS", { jobIds: importJob._id });
  661. return cb({ status: "error", message: err });
  662. }
  663. this.log(
  664. "SUCCESS",
  665. "REQUEST_SET_ADMIN",
  666. `Successfully imported a YouTube playlist to be requested for admin "${session.userId}".`
  667. );
  668. this.publishProgress({
  669. status: "success",
  670. message: `Playlist is done importing.`
  671. });
  672. return cb({
  673. status: "success",
  674. message: `Playlist is done importing.`,
  675. videos: returnVideos ? response.videos : null
  676. });
  677. }
  678. );
  679. }
  680. ),
  681. /**
  682. * Gets missing YouTube channels
  683. *
  684. * @returns {{status: string, data: object}}
  685. */
  686. getMissingChannels: useHasPermission("youtube.getMissingChannels", function getMissingChannels(session, cb) {
  687. return YouTubeModule.runJob("GET_MISSING_CHANNELS", {}, this)
  688. .then(response => {
  689. this.log("SUCCESS", "YOUTUBE_GET_MISSING_CHANNELS", `Getting missing YouTube channels was successful.`);
  690. return cb({ status: "success", data: { ...response } });
  691. })
  692. .catch(async err => {
  693. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  694. this.log("ERROR", "YOUTUBE_GET_MISSING_CHANNELS", `Getting missing YouTube channels failed. "${err}"`);
  695. return cb({ status: "error", message: err });
  696. });
  697. })
  698. };