youtube.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. export default {
  10. /**
  11. * Returns details about the YouTube quota usage
  12. *
  13. * @returns {{status: string, data: object}}
  14. */
  15. getQuotaStatus: isAdminRequired(function getQuotaStatus(session, fromDate, cb) {
  16. YouTubeModule.runJob("GET_QUOTA_STATUS", { fromDate }, this)
  17. .then(response => {
  18. this.log("SUCCESS", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status was successful.`);
  19. return cb({ status: "success", data: { status: response.status } });
  20. })
  21. .catch(async err => {
  22. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  23. this.log("ERROR", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status failed. "${err}"`);
  24. return cb({ status: "error", message: err });
  25. });
  26. }),
  27. /**
  28. * Gets api requests, used in the admin youtube page by the AdvancedTable component
  29. *
  30. * @param {object} session - the session object automatically added by the websocket
  31. * @param page - the page
  32. * @param pageSize - the size per page
  33. * @param properties - the properties to return for each news item
  34. * @param sort - the sort object
  35. * @param queries - the queries array
  36. * @param operator - the operator for queries
  37. * @param cb
  38. */
  39. getApiRequests: isAdminRequired(async function getApiRequests(
  40. session,
  41. page,
  42. pageSize,
  43. properties,
  44. sort,
  45. queries,
  46. operator,
  47. cb
  48. ) {
  49. async.waterfall(
  50. [
  51. next => {
  52. DBModule.runJob(
  53. "GET_DATA",
  54. {
  55. page,
  56. pageSize,
  57. properties,
  58. sort,
  59. queries,
  60. operator,
  61. modelName: "youtubeApiRequest",
  62. blacklistedProperties: [],
  63. specialProperties: {},
  64. specialQueries: {}
  65. },
  66. this
  67. )
  68. .then(response => {
  69. next(null, response);
  70. })
  71. .catch(err => {
  72. next(err);
  73. });
  74. }
  75. ],
  76. async (err, response) => {
  77. if (err && err !== true) {
  78. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  79. this.log("ERROR", "YOUTUBE_GET_API_REQUESTS", `Failed to get YouTube api requests. "${err}"`);
  80. return cb({ status: "error", message: err });
  81. }
  82. this.log("SUCCESS", "YOUTUBE_GET_API_REQUESTS", `Fetched YouTube api requests successfully.`);
  83. return cb({
  84. status: "success",
  85. message: "Successfully fetched YouTube api requests.",
  86. data: response
  87. });
  88. }
  89. );
  90. }),
  91. /**
  92. * Returns a specific api request
  93. *
  94. * @returns {{status: string, data: object}}
  95. */
  96. getApiRequest: isAdminRequired(function getApiRequest(session, apiRequestId, cb) {
  97. if (!mongoose.Types.ObjectId.isValid(apiRequestId))
  98. return cb({ status: "error", message: "Api request id is not a valid ObjectId." });
  99. return YouTubeModule.runJob("GET_API_REQUEST", { apiRequestId }, this)
  100. .then(response => {
  101. this.log(
  102. "SUCCESS",
  103. "YOUTUBE_GET_API_REQUEST",
  104. `Getting api request with id ${apiRequestId} was successful.`
  105. );
  106. return cb({ status: "success", data: { apiRequest: response.apiRequest } });
  107. })
  108. .catch(async err => {
  109. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  110. this.log(
  111. "ERROR",
  112. "YOUTUBE_GET_API_REQUEST",
  113. `Getting api request with id ${apiRequestId} failed. "${err}"`
  114. );
  115. return cb({ status: "error", message: err });
  116. });
  117. }),
  118. /**
  119. * Reset stored API requests
  120. *
  121. * @returns {{status: string, data: object}}
  122. */
  123. resetStoredApiRequests: isAdminRequired(function resetStoredApiRequests(session, cb) {
  124. YouTubeModule.runJob("RESET_STORED_API_REQUESTS", {}, this)
  125. .then(response => {
  126. this.log("SUCCESS", "YOUTUBE_RESET_STORED_API_REQUESTS", `Resetting stored API requests was successful.`);
  127. return cb({ status: "success", message: "Successfully reset stored YouTube API requests" });
  128. })
  129. .catch(async err => {
  130. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  131. this.log("ERROR", "YOUTUBE_RESET_STORED_API_REQUESTS", `Resetting stored API requests failed. "${err}"`);
  132. return cb({ status: "error", message: err });
  133. });
  134. })
  135. };