Browse Source

refactor(YouTube): Move quota definitions to config

Owen Diffey 1 year ago
parent
commit
b34f89c464
3 changed files with 23 additions and 26 deletions
  1. 4 3
      .wiki/Configuration.md
  2. 17 5
      backend/config/template.json
  3. 2 18
      backend/logic/youtube.js

+ 4 - 3
.wiki/Configuration.md

@@ -17,9 +17,10 @@ Location: `backend/config/default.json`
 | `apis.youtube.rateLimit` | Minimum interval between YouTube API requests in milliseconds. |
 | `apis.youtube.requestTimeout` | YouTube API requests timeout in milliseconds. |
 | `apis.youtube.retryAmount` | The amount of retries to perform of a failed YouTube API request. |
-| `apis.youtube.quotas.perDay` | Configure YouTube API quota per day. |
-| `apis.youtube.quotas.perMinute` | Configure YouTube API quota per minute. |
-| `apis.youtube.quotas.per100Seconds` | Configure YouTube API quota per day. |
+| `apis.youtube.quotas` | Array of YouTube API quotas. |
+| `apis.youtube.quotas.type` | YouTube API quota type, should be one of `QUERIES_PER_DAY`, `QUERIES_PER_MINUTE` or `QUERIES_PER_100_SECONDS`. |
+| `apis.youtube.quotas.title` | YouTube API quota title. |
+| `apis.youtube.quotas.limit` | YouTube API quota limit. |
 | `apis.recaptcha.secret` | ReCaptcha Site v3 secret, obtained from [here](https://www.google.com/recaptcha/admin). |
 | `apis.recaptcha.enabled` | Whether to enable ReCaptcha at email registration. |
 | `apis.github.client` | GitHub OAuth Application client, obtained from [here](https://github.com/settings/developers). |

+ 17 - 5
backend/config/template.json

@@ -14,11 +14,23 @@
 			"rateLimit": 500,
 			"requestTimeout": 5000,
 			"retryAmount": 2,
-			"quotas": {
-				"perDay": 10000,
-				"perMinute": 1800000,
-				"per100Seconds": 3000000
-			}
+			"quotas": [
+				{
+					"type": "QUERIES_PER_DAY",
+					"title": "Queries Per Day",
+					"limit": 10000
+				},
+				{
+					"type": "QUERIES_PER_MINUTE",
+					"title": "Queries Per Minute",
+					"limit": 1800000
+				},
+				{
+					"type": "QUERIES_PER_100_SECONDS",
+					"title": "Queries Per 100 Seconds",
+					"limit": 3000000
+				}
+			]
 		},
 		"recaptcha": {
 			"secret": "",

+ 2 - 18
backend/logic/youtube.js

@@ -43,26 +43,9 @@ let YouTubeModule;
 let CacheModule;
 let DBModule;
 
-const quotas = [
-	{
-		type: "QUERIES_PER_DAY",
-		title: "Queries Per Day",
-		limit: config.get("apis.youtube.quotas.perDay")
-	},
-	{
-		type: "QUERIES_PER_MINUTE",
-		title: "Queries Per Minute",
-		limit: config.get("apis.youtube.quotas.perMinute")
-	},
-	{
-		type: "QUERIES_PER_100_SECONDS",
-		title: "Queries Per 100 Seconds",
-		limit: config.get("apis.youtube.quotas.per100Seconds")
-	}
-];
-
 const isQuotaExceeded = apiCalls => {
 	const reversedApiCalls = apiCalls.slice().reverse();
+	const quotas = config.get("apis.youtube.quotas");
 	const sortedQuotas = quotas.sort((a, b) => a.limit > b.limit);
 
 	let quotaExceeded = false;
@@ -197,6 +180,7 @@ class _YouTubeModule extends CoreClass {
 					if (err) reject(new Error("Couldn't load YouTube API requests."));
 					else {
 						const reversedApiCalls = youtubeApiRequests.slice().reverse();
+						const quotas = config.get("apis.youtube.quotas");
 						const sortedQuotas = quotas.sort((a, b) => a.limit > b.limit);
 						const status = {};