youtube.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. export default {
  25. /**
  26. * Returns details about the YouTube quota usage
  27. *
  28. * @returns {{status: string, data: object}}
  29. */
  30. getQuotaStatus: isAdminRequired(function getQuotaStatus(session, fromDate, cb) {
  31. YouTubeModule.runJob("GET_QUOTA_STATUS", { fromDate }, this)
  32. .then(response => {
  33. this.log("SUCCESS", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status was successful.`);
  34. return cb({ status: "success", data: { status: response.status } });
  35. })
  36. .catch(async err => {
  37. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  38. this.log("ERROR", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status failed. "${err}"`);
  39. return cb({ status: "error", message: err });
  40. });
  41. }),
  42. /**
  43. * Gets api requests, used in the admin youtube page by the AdvancedTable component
  44. *
  45. * @param {object} session - the session object automatically added by the websocket
  46. * @param page - the page
  47. * @param pageSize - the size per page
  48. * @param properties - the properties to return for each news item
  49. * @param sort - the sort object
  50. * @param queries - the queries array
  51. * @param operator - the operator for queries
  52. * @param cb
  53. */
  54. getApiRequests: isAdminRequired(async function getApiRequests(
  55. session,
  56. page,
  57. pageSize,
  58. properties,
  59. sort,
  60. queries,
  61. operator,
  62. cb
  63. ) {
  64. async.waterfall(
  65. [
  66. next => {
  67. DBModule.runJob(
  68. "GET_DATA",
  69. {
  70. page,
  71. pageSize,
  72. properties,
  73. sort,
  74. queries,
  75. operator,
  76. modelName: "youtubeApiRequest",
  77. blacklistedProperties: [],
  78. specialProperties: {},
  79. specialQueries: {}
  80. },
  81. this
  82. )
  83. .then(response => {
  84. next(null, response);
  85. })
  86. .catch(err => {
  87. next(err);
  88. });
  89. }
  90. ],
  91. async (err, response) => {
  92. if (err && err !== true) {
  93. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  94. this.log("ERROR", "YOUTUBE_GET_API_REQUESTS", `Failed to get YouTube api requests. "${err}"`);
  95. return cb({ status: "error", message: err });
  96. }
  97. this.log("SUCCESS", "YOUTUBE_GET_API_REQUESTS", `Fetched YouTube api requests successfully.`);
  98. return cb({
  99. status: "success",
  100. message: "Successfully fetched YouTube api requests.",
  101. data: response
  102. });
  103. }
  104. );
  105. }),
  106. /**
  107. * Returns a specific api request
  108. *
  109. * @returns {{status: string, data: object}}
  110. */
  111. getApiRequest: isAdminRequired(function getApiRequest(session, apiRequestId, cb) {
  112. if (!mongoose.Types.ObjectId.isValid(apiRequestId))
  113. return cb({ status: "error", message: "Api request id is not a valid ObjectId." });
  114. return YouTubeModule.runJob("GET_API_REQUEST", { apiRequestId }, this)
  115. .then(response => {
  116. this.log(
  117. "SUCCESS",
  118. "YOUTUBE_GET_API_REQUEST",
  119. `Getting api request with id ${apiRequestId} was successful.`
  120. );
  121. return cb({ status: "success", data: { apiRequest: response.apiRequest } });
  122. })
  123. .catch(async err => {
  124. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  125. this.log(
  126. "ERROR",
  127. "YOUTUBE_GET_API_REQUEST",
  128. `Getting api request with id ${apiRequestId} failed. "${err}"`
  129. );
  130. return cb({ status: "error", message: err });
  131. });
  132. }),
  133. /**
  134. * Reset stored API requests
  135. *
  136. * @returns {{status: string, data: object}}
  137. */
  138. resetStoredApiRequests: isAdminRequired(async function resetStoredApiRequests(session, cb) {
  139. async.waterfall(
  140. [
  141. next => {
  142. YouTubeModule.youtubeApiRequestModel.find({}, next);
  143. },
  144. (apiRequests, next) => {
  145. YouTubeModule.runJob("RESET_STORED_API_REQUESTS", {}, this)
  146. .then(() => next(null, apiRequests))
  147. .catch(err => next(err));
  148. },
  149. (apiRequests, next) => {
  150. async.eachLimit(
  151. apiRequests.map(apiRequest => apiRequest._id),
  152. 1,
  153. (requestId, next) => {
  154. CacheModule.runJob(
  155. "PUB",
  156. {
  157. channel: "youtube.removeYoutubeApiRequest",
  158. value: requestId
  159. },
  160. this
  161. )
  162. .then(() => {
  163. next();
  164. })
  165. .catch(err => {
  166. next(err);
  167. });
  168. },
  169. err => {
  170. if (err) next(err);
  171. else next();
  172. }
  173. );
  174. }
  175. ],
  176. async err => {
  177. if (err) {
  178. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  179. this.log(
  180. "ERROR",
  181. "YOUTUBE_RESET_STORED_API_REQUESTS",
  182. `Resetting stored API requests failed. "${err}"`
  183. );
  184. return cb({ status: "error", message: err });
  185. }
  186. this.log(
  187. "SUCCESS",
  188. "YOUTUBE_RESET_STORED_API_REQUESTS",
  189. `Resetting stored API requests was successful.`
  190. );
  191. return cb({ status: "success", message: "Successfully reset stored YouTube API requests" });
  192. }
  193. );
  194. }),
  195. /**
  196. * Remove stored API requests
  197. *
  198. * @returns {{status: string, data: object}}
  199. */
  200. removeStoredApiRequest: isAdminRequired(function removeStoredApiRequest(session, requestId, cb) {
  201. YouTubeModule.runJob("REMOVE_STORED_API_REQUEST", { requestId }, this)
  202. .then(() => {
  203. this.log(
  204. "SUCCESS",
  205. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  206. `Removing stored API request "${requestId}" was successful.`
  207. );
  208. CacheModule.runJob("PUB", {
  209. channel: "youtube.removeYoutubeApiRequest",
  210. value: requestId
  211. });
  212. return cb({ status: "success", message: "Successfully removed stored YouTube API request" });
  213. })
  214. .catch(async err => {
  215. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  216. this.log(
  217. "ERROR",
  218. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  219. `Removing stored API request "${requestId}" failed. "${err}"`
  220. );
  221. return cb({ status: "error", message: err });
  222. });
  223. })
  224. };