youtube.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(session, page, pageSize, properties, sort, queries, operator, cb) {
  40. async.waterfall(
  41. [
  42. next => {
  43. DBModule.runJob(
  44. "GET_DATA",
  45. {
  46. page,
  47. pageSize,
  48. properties,
  49. sort,
  50. queries,
  51. operator,
  52. modelName: "youtubeApiRequest",
  53. blacklistedProperties: [],
  54. specialProperties: {},
  55. specialQueries: {}
  56. },
  57. this
  58. )
  59. .then(response => {
  60. next(null, response);
  61. })
  62. .catch(err => {
  63. next(err);
  64. });
  65. }
  66. ],
  67. async (err, response) => {
  68. if (err && err !== true) {
  69. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  70. this.log("ERROR", "YOUTUBE_GET_API_REQUESTS", `Failed to get YouTube api requests. "${err}"`);
  71. return cb({ status: "error", message: err });
  72. }
  73. this.log("SUCCESS", "YOUTUBE_GET_API_REQUESTS", `Fetched YouTube api requests successfully.`);
  74. return cb({
  75. status: "success",
  76. message: "Successfully fetched YouTube api requests.",
  77. data: response
  78. });
  79. }
  80. );
  81. }),
  82. /**
  83. * Returns a specific api request
  84. *
  85. * @returns {{status: string, data: object}}
  86. */
  87. getApiRequest: isAdminRequired(function getApiRequest(session, apiRequestId, cb) {
  88. if (!mongoose.Types.ObjectId.isValid(apiRequestId))
  89. return cb({ status: "error", message: "Api request id is not a valid ObjectId." });
  90. return YouTubeModule.runJob("GET_API_REQUEST", { apiRequestId }, this)
  91. .then(response => {
  92. this.log(
  93. "SUCCESS",
  94. "YOUTUBE_GET_API_REQUEST",
  95. `Getting api request with id ${apiRequestId} was successful.`
  96. );
  97. return cb({ status: "success", data: { apiRequest: response.apiRequest } });
  98. })
  99. .catch(async err => {
  100. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  101. this.log(
  102. "ERROR",
  103. "YOUTUBE_GET_API_REQUEST",
  104. `Getting api request with id ${apiRequestId} failed. "${err}"`
  105. );
  106. return cb({ status: "error", message: err });
  107. });
  108. })
  109. };