youtube.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. import mongoose from "mongoose";
  2. import async from "async";
  3. import { isAdminRequired, isLoginRequired } from "./hooks";
  4. // eslint-disable-next-line
  5. import moduleManager from "../../index";
  6. const DBModule = moduleManager.modules.db;
  7. const UtilsModule = moduleManager.modules.utils;
  8. const YouTubeModule = moduleManager.modules.youtube;
  9. const WSModule = moduleManager.modules.ws;
  10. const CacheModule = moduleManager.modules.cache;
  11. CacheModule.runJob("SUB", {
  12. channel: "youtube.removeYoutubeApiRequest",
  13. cb: requestId => {
  14. WSModule.runJob("EMIT_TO_ROOM", {
  15. room: `view-api-request.${requestId}`,
  16. args: ["event:youtubeApiRequest.removed"]
  17. });
  18. WSModule.runJob("EMIT_TO_ROOM", {
  19. room: "admin.youtube",
  20. args: ["event:admin.youtubeApiRequest.removed", { data: { requestId } }]
  21. });
  22. }
  23. });
  24. CacheModule.runJob("SUB", {
  25. channel: "youtube.removeVideos",
  26. cb: videoIds => {
  27. const videos = Array.isArray(videoIds) ? videoIds : [videoIds];
  28. videos.forEach(videoId => {
  29. WSModule.runJob("EMIT_TO_ROOM", {
  30. room: `view-youtube-video.${videoId}`,
  31. args: ["event:youtubeVideo.removed"]
  32. });
  33. WSModule.runJob("EMIT_TO_ROOM", {
  34. room: "admin.youtubeVideos",
  35. args: ["event:admin.youtubeVideo.removed", { data: { videoId } }]
  36. });
  37. });
  38. }
  39. });
  40. export default {
  41. /**
  42. * Returns details about the YouTube quota usage
  43. *
  44. * @returns {{status: string, data: object}}
  45. */
  46. getQuotaStatus: isAdminRequired(function getQuotaStatus(session, fromDate, cb) {
  47. YouTubeModule.runJob("GET_QUOTA_STATUS", { fromDate }, this)
  48. .then(response => {
  49. this.log("SUCCESS", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status was successful.`);
  50. return cb({ status: "success", data: { status: response.status } });
  51. })
  52. .catch(async err => {
  53. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  54. this.log("ERROR", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status failed. "${err}"`);
  55. return cb({ status: "error", message: err });
  56. });
  57. }),
  58. /**
  59. * Gets api requests, used in the admin youtube page by the AdvancedTable component
  60. *
  61. * @param {object} session - the session object automatically added by the websocket
  62. * @param page - the page
  63. * @param pageSize - the size per page
  64. * @param properties - the properties to return for each news item
  65. * @param sort - the sort object
  66. * @param queries - the queries array
  67. * @param operator - the operator for queries
  68. * @param cb
  69. */
  70. getApiRequests: isAdminRequired(async function getApiRequests(
  71. session,
  72. page,
  73. pageSize,
  74. properties,
  75. sort,
  76. queries,
  77. operator,
  78. cb
  79. ) {
  80. async.waterfall(
  81. [
  82. next => {
  83. DBModule.runJob(
  84. "GET_DATA",
  85. {
  86. page,
  87. pageSize,
  88. properties,
  89. sort,
  90. queries,
  91. operator,
  92. modelName: "youtubeApiRequest",
  93. blacklistedProperties: [],
  94. specialProperties: {},
  95. specialQueries: {}
  96. },
  97. this
  98. )
  99. .then(response => {
  100. next(null, response);
  101. })
  102. .catch(err => {
  103. next(err);
  104. });
  105. }
  106. ],
  107. async (err, response) => {
  108. if (err && err !== true) {
  109. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  110. this.log("ERROR", "YOUTUBE_GET_API_REQUESTS", `Failed to get YouTube api requests. "${err}"`);
  111. return cb({ status: "error", message: err });
  112. }
  113. this.log("SUCCESS", "YOUTUBE_GET_API_REQUESTS", `Fetched YouTube api requests successfully.`);
  114. return cb({
  115. status: "success",
  116. message: "Successfully fetched YouTube api requests.",
  117. data: response
  118. });
  119. }
  120. );
  121. }),
  122. /**
  123. * Returns a specific api request
  124. *
  125. * @returns {{status: string, data: object}}
  126. */
  127. getApiRequest: isAdminRequired(function getApiRequest(session, apiRequestId, cb) {
  128. if (!mongoose.Types.ObjectId.isValid(apiRequestId))
  129. return cb({ status: "error", message: "Api request id is not a valid ObjectId." });
  130. return YouTubeModule.runJob("GET_API_REQUEST", { apiRequestId }, this)
  131. .then(response => {
  132. this.log(
  133. "SUCCESS",
  134. "YOUTUBE_GET_API_REQUEST",
  135. `Getting api request with id ${apiRequestId} was successful.`
  136. );
  137. return cb({ status: "success", data: { apiRequest: response.apiRequest } });
  138. })
  139. .catch(async err => {
  140. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  141. this.log(
  142. "ERROR",
  143. "YOUTUBE_GET_API_REQUEST",
  144. `Getting api request with id ${apiRequestId} failed. "${err}"`
  145. );
  146. return cb({ status: "error", message: err });
  147. });
  148. }),
  149. /**
  150. * Reset stored API requests
  151. *
  152. * @returns {{status: string, data: object}}
  153. */
  154. resetStoredApiRequests: isAdminRequired(async function resetStoredApiRequests(session, cb) {
  155. async.waterfall(
  156. [
  157. next => {
  158. YouTubeModule.youtubeApiRequestModel.find({}, next);
  159. },
  160. (apiRequests, next) => {
  161. YouTubeModule.runJob("RESET_STORED_API_REQUESTS", {}, this)
  162. .then(() => next(null, apiRequests))
  163. .catch(err => next(err));
  164. },
  165. (apiRequests, next) => {
  166. async.eachLimit(
  167. apiRequests.map(apiRequest => apiRequest._id),
  168. 1,
  169. (requestId, next) => {
  170. CacheModule.runJob(
  171. "PUB",
  172. {
  173. channel: "youtube.removeYoutubeApiRequest",
  174. value: requestId
  175. },
  176. this
  177. )
  178. .then(() => {
  179. next();
  180. })
  181. .catch(err => {
  182. next(err);
  183. });
  184. },
  185. err => {
  186. if (err) next(err);
  187. else next();
  188. }
  189. );
  190. }
  191. ],
  192. async err => {
  193. if (err) {
  194. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  195. this.log(
  196. "ERROR",
  197. "YOUTUBE_RESET_STORED_API_REQUESTS",
  198. `Resetting stored API requests failed. "${err}"`
  199. );
  200. return cb({ status: "error", message: err });
  201. }
  202. this.log(
  203. "SUCCESS",
  204. "YOUTUBE_RESET_STORED_API_REQUESTS",
  205. `Resetting stored API requests was successful.`
  206. );
  207. return cb({ status: "success", message: "Successfully reset stored YouTube API requests" });
  208. }
  209. );
  210. }),
  211. /**
  212. * Remove stored API requests
  213. *
  214. * @returns {{status: string, data: object}}
  215. */
  216. removeStoredApiRequest: isAdminRequired(function removeStoredApiRequest(session, requestId, cb) {
  217. YouTubeModule.runJob("REMOVE_STORED_API_REQUEST", { requestId }, this)
  218. .then(() => {
  219. this.log(
  220. "SUCCESS",
  221. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  222. `Removing stored API request "${requestId}" was successful.`
  223. );
  224. CacheModule.runJob("PUB", {
  225. channel: "youtube.removeYoutubeApiRequest",
  226. value: requestId
  227. });
  228. return cb({ status: "success", message: "Successfully removed stored YouTube API request" });
  229. })
  230. .catch(async err => {
  231. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  232. this.log(
  233. "ERROR",
  234. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  235. `Removing stored API request "${requestId}" failed. "${err}"`
  236. );
  237. return cb({ status: "error", message: err });
  238. });
  239. }),
  240. /**
  241. * Gets videos, used in the admin youtube page by the AdvancedTable component
  242. *
  243. * @param {object} session - the session object automatically added by the websocket
  244. * @param page - the page
  245. * @param pageSize - the size per page
  246. * @param properties - the properties to return for each news item
  247. * @param sort - the sort object
  248. * @param queries - the queries array
  249. * @param operator - the operator for queries
  250. * @param cb
  251. */
  252. getVideos: isAdminRequired(async function getVideos(
  253. session,
  254. page,
  255. pageSize,
  256. properties,
  257. sort,
  258. queries,
  259. operator,
  260. cb
  261. ) {
  262. async.waterfall(
  263. [
  264. next => {
  265. DBModule.runJob(
  266. "GET_DATA",
  267. {
  268. page,
  269. pageSize,
  270. properties,
  271. sort,
  272. queries,
  273. operator,
  274. modelName: "youtubeVideo",
  275. blacklistedProperties: [],
  276. specialProperties: {},
  277. specialQueries: {}
  278. },
  279. this
  280. )
  281. .then(response => {
  282. next(null, response);
  283. })
  284. .catch(err => {
  285. next(err);
  286. });
  287. }
  288. ],
  289. async (err, response) => {
  290. if (err && err !== true) {
  291. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  292. this.log("ERROR", "YOUTUBE_GET_VIDEOS", `Failed to get YouTube videos. "${err}"`);
  293. return cb({ status: "error", message: err });
  294. }
  295. this.log("SUCCESS", "YOUTUBE_GET_VIDEOS", `Fetched YouTube videos successfully.`);
  296. return cb({
  297. status: "success",
  298. message: "Successfully fetched YouTube videos.",
  299. data: response
  300. });
  301. }
  302. );
  303. }),
  304. /**
  305. * Get a YouTube video
  306. *
  307. * @returns {{status: string, data: object}}
  308. */
  309. getVideo: isLoginRequired(function getVideo(session, identifier, createMissing, cb) {
  310. YouTubeModule.runJob("GET_VIDEO", { identifier, createMissing }, this)
  311. .then(res => {
  312. this.log("SUCCESS", "YOUTUBE_GET_VIDEO", `Fetching video was successful.`);
  313. return cb({ status: "success", message: "Successfully fetched YouTube video", data: res.video });
  314. })
  315. .catch(async err => {
  316. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  317. this.log("ERROR", "YOUTUBE_GET_VIDEO", `Fetching video failed. "${err}"`);
  318. return cb({ status: "error", message: err });
  319. });
  320. }),
  321. /**
  322. * Remove YouTube videos
  323. *
  324. * @returns {{status: string, data: object}}
  325. */
  326. removeVideos: isAdminRequired(function removeVideos(session, videoIds, cb) {
  327. YouTubeModule.runJob("REMOVE_VIDEOS", { videoIds }, this)
  328. .then(() => {
  329. this.log("SUCCESS", "YOUTUBE_REMOVE_VIDEOS", `Removing videos was successful.`);
  330. CacheModule.runJob("PUB", {
  331. channel: "youtube.removeVideos",
  332. value: videoIds
  333. });
  334. return cb({ status: "success", message: "Successfully removed YouTube videos" });
  335. })
  336. .catch(async err => {
  337. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  338. this.log("ERROR", "YOUTUBE_REMOVE_VIDEOS", `Removing videos failed. "${err}"`);
  339. return cb({ status: "error", message: err });
  340. });
  341. }),
  342. /**
  343. * Requests a set of YouTube videos
  344. *
  345. * @param {object} session - the session object automatically added by the websocket
  346. * @param {string} url - the url of the the YouTube playlist
  347. * @param {boolean} musicOnly - whether to only get music from the playlist
  348. * @param {boolean} musicOnly - whether to return videos
  349. * @param {Function} cb - gets called with the result
  350. */
  351. requestSet: isLoginRequired(function requestSet(session, url, musicOnly, returnVideos, cb) {
  352. YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
  353. .then(response => {
  354. this.log(
  355. "SUCCESS",
  356. "REQUEST_SET",
  357. `Successfully imported a YouTube playlist to be requested for user "${session.userId}".`
  358. );
  359. return cb({
  360. status: "success",
  361. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`,
  362. videos: returnVideos ? response.videos : null
  363. });
  364. })
  365. .catch(async err => {
  366. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  367. this.log(
  368. "ERROR",
  369. "REQUEST_SET",
  370. `Importing a YouTube playlist to be requested failed for user "${session.userId}". "${err}"`
  371. );
  372. return cb({ status: "error", message: err });
  373. });
  374. })
  375. };