@@ -16,6 +16,25 @@ import Stats from '../models/schemas/Stat.js';
1616 */
1717const authorize = requiredRole => async ( req , res , next ) => {
1818 try {
19+ /**
20+ * Determine the endpoint based on the request URL.
21+ */
22+ const endpoint = getEndpointFromUrl ( req . originalUrl ) ;
23+
24+ /**
25+ * Check if the requested endpoint is disabled.
26+ */
27+ const isEndpointEnabled = await isEndpointEnabledInStats ( endpoint ) ;
28+
29+ if ( ! isEndpointEnabled ) {
30+ return next (
31+ createError (
32+ 403 ,
33+ `The endpoint '${ endpoint } ' is currently disabled. Go to https://discord.gg/yyW389c for support.` ,
34+ ) ,
35+ ) ;
36+ }
37+
1938 /**
2039 * Extract API key from request headers.
2140 *
@@ -49,6 +68,7 @@ const authorize = requiredRole => async (req, res, next) => {
4968 const updateData = {
5069 $inc : {
5170 req_quota : userData && userData . req_quota > 0 ? - 1 : 0 ,
71+ req_consumed : userData && userData . req_quota > 0 ? 1 : 0 ,
5272 req_count : userData ? 1 : 0 ,
5373 } ,
5474 } ;
@@ -92,6 +112,11 @@ const authorize = requiredRole => async (req, res, next) => {
92112 return next ( createError ( 403 , 'Insufficient privileges to access this endpoint.' ) ) ;
93113 }
94114
115+ /**
116+ * Log the user request.
117+ */
118+ await logUserRequest ( userData . _id , endpoint ) ;
119+
95120 /**
96121 * Increment system stats for successful requests.
97122 */
@@ -113,6 +138,46 @@ const authorize = requiredRole => async (req, res, next) => {
113138 }
114139} ;
115140
141+ /**
142+ * Helper function to extract endpoint from the request URL.
143+ *
144+ * @param {string } url - The request URL.
145+ * @returns {string } - The extracted endpoint.
146+ */
147+ const getEndpointFromUrl = url => {
148+ const urlSegments = url . split ( '/' ) ;
149+ return urlSegments [ urlSegments . length - 1 ] ; // Last segment is assumed to be the endpoint
150+ } ;
151+
152+ /**
153+ * Helper function to check if the endpoint is enabled in the Stats collection.
154+ *
155+ * @param {string } endpoint - The endpoint to check.
156+ * @returns {Promise<boolean> } - Promise resolving to true if enabled, false otherwise.
157+ */
158+ const isEndpointEnabledInStats = async endpoint => {
159+ try {
160+ // Assuming 'Stats' is the correct model for endpoint settings
161+ const settings = await Stats . findOne ( ) ;
162+
163+ // Handle case where settings are not found
164+ if ( ! settings ) {
165+ return false ;
166+ }
167+
168+ // Check if endpoint exists in settings and isEnabled is defined
169+ if ( settings [ endpoint ] && typeof settings [ endpoint ] . isEnabled !== 'undefined' ) {
170+ return settings [ endpoint ] . isEnabled ;
171+ }
172+
173+ // Default to true if isEnabled is not defined or endpoint doesn't exist
174+ return true ;
175+ } catch ( error ) {
176+ console . error ( 'Error fetching endpoint settings:' , error ) ;
177+ return true ;
178+ }
179+ } ;
180+
116181/**
117182 * Increment the specified statistics in the system stats collection.
118183 *
@@ -125,4 +190,28 @@ const incrementSystemStats = async stats => {
125190 await Stats . findByIdAndUpdate ( { _id : 'systemstats' } , { $inc : stats } ) ;
126191} ;
127192
193+ /**
194+ * Log the number of requests made by a user to a specific endpoint.
195+ *
196+ * @param {string } userId - The ID of the user.
197+ * @param {string } endpoint - The endpoint being accessed.
198+ * @returns {Promise<void> } - Resolves when the log is updated.
199+ */
200+ const logUserRequest = async ( userId , endpoint ) => {
201+ try {
202+ // Find the user and update the request count for the specific endpoint
203+ await Users . findByIdAndUpdate (
204+ userId ,
205+ {
206+ $inc : {
207+ [ `statistics.requests.${ endpoint } ` ] : 1 ,
208+ } ,
209+ } ,
210+ { new : true , upsert : true } , // Create a new document if it doesn't exist
211+ ) ;
212+ } catch ( error ) {
213+ console . error ( 'Error logging user request:' , error ) ;
214+ }
215+ } ;
216+
128217export default authorize ;
0 commit comments