youtube.js 9.6 KB

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