@@ -8,6 +8,7 @@ import { verifyToken, banCheck } from './user';
88import axios from 'axios' ;
99import moment from 'moment'
1010import { UserData } from '@/Components/User' ;
11+ import { cacheMiddleware } from '../cache' ;
1112
1213const allowedTags = [ "Font" , "Decoration" , "Gameplay" , "Art" , "Structure" , "Custom" , "Icon" , "Meme" , "Technical" , "Particles" , "Triggers" , "SFX" , "Effects" , "Auto Build" , "Recreation" ] ;
1314
@@ -57,6 +58,7 @@ enum ReviewStatus {
5758interface WebhookObjData {
5859 account_name : string ;
5960 name : string ;
61+ id : number ;
6062 description : string ;
6163 tags : Array < string > ;
6264 version : number ;
@@ -81,6 +83,11 @@ function sendWebhook(object: WebhookObjData, status: ReviewStatus, reviewer?: st
8183 "value" : object . account_name ,
8284 "inline" : true
8385 } ,
86+ {
87+ "name" : "Object ID" ,
88+ "value" : object . id ,
89+ "inline" : true
90+ } ,
8491 {
8592 "name" : "Description" ,
8693 "value" : object . description
@@ -186,6 +193,7 @@ oRouter.post('/objects/upload',
186193 timestamp : object . timestamp
187194 } ) ;
188195 sendWebhook ( {
196+ id : object . id ,
189197 name,
190198 description,
191199 tags,
@@ -256,6 +264,7 @@ oRouter.post('/objects/:id/overwrite',
256264 }
257265 res . status ( 200 ) . json ( { message : "Object updated!" } ) ;
258266 sendWebhook ( {
267+ id : objData . id ,
259268 name : objData . name ,
260269 description : objData . description ,
261270 tags : objData . tags ,
@@ -277,7 +286,10 @@ oRouter.post('/objects/:id/overwrite',
277286 }
278287) ;
279288
280- oRouter . get ( '/objects/:id' , param ( "id" ) . isInt ( { min : 0 , max : 2147483647 } ) . notEmpty ( ) , ( req : Request , res : Response ) => {
289+ oRouter . get ( '/objects/:id' , param ( "id" ) . isInt ( { min : 0 , max : 2147483647 } ) . notEmpty ( ) , cacheMiddleware ( 600 , ( req ) => {
290+ const category = parseInt ( req . query . category as string ) || 0 ;
291+ return ! req . query [ "no-cache" ] || category != 4 ;
292+ } ) , ( req : Request , res : Response ) => {
281293 const result = validationResult ( req ) ;
282294 if ( ! result . isEmpty ( ) ) return res . status ( 400 ) . json ( { errors : result . array ( ) } )
283295 const objectID = req . params . id ;
@@ -615,19 +627,18 @@ oRouter.post('/objects/:id/favorite',
615627 const objExists = await pool . query ( "SELECT EXISTS (SELECT 1 FROM objects WHERE id = $1 AND status = 1)" , [ objectID ] )
616628 if ( ! objExists . rows [ 0 ] . exists ) return res . status ( 404 ) . json ( { error : "Object not found." } ) ;
617629 let query = "" ;
618- let query2 = "" ;
630+
619631 let message = ""
620632 if ( verifyRes . user ?. favorites . includes ( parseInt ( objectID ) ) ) {
621633 query = "UPDATE users SET favorites = ARRAY_REMOVE(favorites, $1) WHERE account_id = $2" ;
622- query2 = "UPDATE objects SET favorites = favorites - 1 WHERE id = $1" ;
623634 message = "Unfavorited object!" ;
624635 } else {
625636 query = "UPDATE users SET favorites = ARRAY_APPEND(favorites, $1) WHERE account_id = $2" ;
626- query2 = "UPDATE objects SET favorites = favorites + 1 WHERE id = $1" ;
627637 message = "Favorited object!" ;
628638 }
629639 await pool . query ( query , [ objectID , accountID ] ) ;
630- await pool . query ( query2 , [ objectID ] ) ;
640+ const favoriteCount = await pool . query ( "SELECT COUNT(*) as favorites FROM users WHERE favorites @> ARRAY[$1::integer]" , [ objectID ] ) ;
641+ await pool . query ( "UPDATE objects SET favorites = $1 WHERE id = $2" , [ favoriteCount . rows [ 0 ] . favorites , objectID ] ) ;
631642 res . status ( 200 ) . json ( { message } ) ;
632643 } catch ( e ) {
633644 console . error ( e ) ;
@@ -823,6 +834,7 @@ oRouter.post('/objects/:id/accept',
823834 }
824835 await pool . query ( "UPDATE objects SET status = 1 WHERE id = $1" , [ objectID ] ) ;
825836 sendWebhook ( {
837+ id : objData . id ,
826838 name : objData . name ,
827839 description : objData . description ,
828840 tags : objData . tags ,
@@ -876,6 +888,7 @@ oRouter.post('/objects/:id/reject',
876888 await pool . query ( "DELETE FROM objects WHERE id = $1" , [ objectID ] ) ;
877889 res . status ( 200 ) . json ( { message : "Rejected!" } ) ;
878890 sendWebhook ( {
891+ id : objData . id ,
879892 name : objData . name ,
880893 description : objData . description ,
881894 tags : objData . tags ,
@@ -946,6 +959,9 @@ oRouter.post('/objects/:id/feature',
946959oRouter . get ( '/user/:id' ,
947960 param ( 'id' ) . isInt ( { min : 0 , max : 2147483647 } ) . notEmpty ( ) ,
948961 query ( 'page' ) . isInt ( { min : 0 , max : 2147483647 } ) . optional ( ) ,
962+ cacheMiddleware ( 300 , ( req ) => {
963+ return ! req . query [ "no-cache" ] ;
964+ } ) ,
949965 async ( req : Request , res : Response ) => {
950966 const result = validationResult ( req ) ;
951967 if ( ! result . isEmpty ( ) ) return res . status ( 400 ) . json ( { errors : result . array ( ) } )
@@ -1098,6 +1114,9 @@ oRouter.get('/objects/:id/comments',
10981114 query ( 'page' ) . isInt ( { min : 1 , max : 2147483647 } ) . optional ( ) ,
10991115 query ( 'limit' ) . isInt ( { min : 1 , max : 10 } ) . optional ( ) ,
11001116 query ( 'filter' ) . isInt ( { min : 1 , max : 2 } ) . optional ( ) ,
1117+ cacheMiddleware ( 30 , ( req ) => {
1118+ return ! req . query [ "no-cache" ] ;
1119+ } ) ,
11011120 async ( req : Request , res : Response ) => {
11021121 const result = validationResult ( req ) ;
11031122 if ( ! result . isEmpty ( ) ) return res . status ( 400 ) . json ( { errors : result . array ( ) } )
@@ -1287,6 +1306,9 @@ oRouter.post('/user/@me/favorites',
12871306 body ( 'token' ) . notEmpty ( ) . isString ( ) ,
12881307 query ( 'page' ) . isInt ( { min : 1 , max : 2147483647 } ) . optional ( ) ,
12891308 query ( 'limit' ) . isInt ( { min : 1 , max : 9 } ) . optional ( ) ,
1309+ cacheMiddleware ( 300 , ( req ) => {
1310+ return ! req . query [ "no-cache" ] ;
1311+ } ) ,
12901312 async ( req : Request , res : Response ) => {
12911313 const result = validationResult ( req ) ;
12921314 if ( ! result . isEmpty ( ) ) return res . status ( 400 ) . json ( { errors : result . array ( ) } )
@@ -1357,6 +1379,10 @@ oRouter.get('/objects',
13571379 query ( 'category' ) . isInt ( { min : 0 , max : 2147483647 } ) . optional ( ) ,
13581380 query ( 'limit' ) . isInt ( { min : 1 , max : 9 } ) . optional ( ) ,
13591381 body ( 'tags' ) . optional ( ) . isString ( ) ,
1382+ cacheMiddleware ( 300 , ( req ) => {
1383+ const category = parseInt ( req . query . category as string ) || 0 ;
1384+ return ! req . query [ "no-cache" ] || category == 4 ;
1385+ } ) ,
13601386 async ( req : Request , res : Response ) => {
13611387 const result = validationResult ( req ) ;
13621388 if ( ! result . isEmpty ( ) ) return res . status ( 400 ) . json ( { errors : result . array ( ) } )
0 commit comments