@@ -8,7 +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' ;
11+ import { cacheMiddleware , CacheManager } from '../cache' ;
1212
1313const allowedTags = [ "Font" , "Decoration" , "Gameplay" , "Art" , "Structure" , "Custom" , "Icon" , "Meme" , "Technical" , "Particles" , "Triggers" , "SFX" , "Effects" , "Auto Build" , "Recreation" ] ;
1414
@@ -286,7 +286,7 @@ oRouter.post('/objects/:id/overwrite',
286286 }
287287) ;
288288
289- oRouter . get ( '/objects/:id' , param ( "id" ) . isInt ( { min : 0 , max : 2147483647 } ) . notEmpty ( ) , cacheMiddleware ( 600 , ( req ) => {
289+ oRouter . get ( '/objects/:id' , param ( "id" ) . isInt ( { min : 0 , max : 2147483647 } ) . notEmpty ( ) , cacheMiddleware ( 120 , ( req ) => {
290290 const category = parseInt ( req . query . category as string ) || 0 ;
291291 return ! req . query [ "no-cache" ] || category != 4 ;
292292} ) , ( req : Request , res : Response ) => {
@@ -358,6 +358,7 @@ oRouter.post('/objects/:id/rate',
358358 ` ;
359359 await pool . query ( query , [ objectID , accountID , stars ] ) ;
360360 res . status ( 200 ) . json ( { message : `Sent rating of ${ stars } stars!` } ) ;
361+ CacheManager . delete ( `GET:/objects/${ objectID } ` ) ;
361362 } catch ( e ) {
362363 console . error ( e ) ;
363364 res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
@@ -398,6 +399,7 @@ oRouter.post('/objects/:id/comment',
398399 if ( ! objExists . rows [ 0 ] . exists ) return res . status ( 404 ) . json ( { error : "Object not found." } ) ;
399400 await pool . query ( "INSERT INTO comments (object_id, account_id, content) VALUES ($1, $2, $3)" , [ objectID , accountID , data ] ) ;
400401 res . status ( 200 ) . json ( { message : `Sent!` } ) ;
402+ CacheManager . delete ( `GET:/objects/${ objectID } /comments` ) ;
401403 } catch ( e ) {
402404 console . error ( e ) ;
403405 res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
@@ -442,6 +444,7 @@ oRouter.post('/objects/:id/comments/:commentid/pin',
442444 }
443445 await pool . query ( "UPDATE comments SET pinned = FALSE WHERE object_id = $1" , [ objectID ] ) ;
444446 await pool . query ( "UPDATE comments SET pinned = TRUE WHERE id = $1" , [ commentID ] ) ;
447+ CacheManager . delete ( `GET:/objects/${ objectID } /comments` ) ;
445448 res . status ( 200 ) . json ( { message : `Pinned!` } ) ;
446449 } catch ( e ) {
447450 console . error ( e ) ;
@@ -493,6 +496,7 @@ oRouter.post('/objects/:id/comments/:commentid/vote',
493496 await pool . query ( "UPDATE users SET c_likes = ARRAY_APPEND(c_likes, $1) WHERE account_id = $2" , [ commentID , accountID ] ) ;
494497 await pool . query ( "UPDATE comments SET likes = likes + 2 WHERE id = $1" , [ commentID ] ) ;
495498 }
499+ CacheManager . delete ( `GET:/objects/${ objectID } /comments` ) ;
496500 res . status ( 200 ) . json ( { message : "Voted!" } ) ;
497501 } else {
498502 if ( like == 0 ) { // dislike
@@ -502,6 +506,7 @@ oRouter.post('/objects/:id/comments/:commentid/vote',
502506 await pool . query ( "UPDATE users SET c_likes = ARRAY_APPEND(c_likes, $1) WHERE account_id = $2" , [ commentID , accountID ] ) ;
503507 await pool . query ( "UPDATE comments SET likes = likes + 1 WHERE id = $1" , [ commentID ] ) ;
504508 }
509+ CacheManager . delete ( `GET:/objects/${ objectID } /comments` ) ;
505510 res . status ( 200 ) . json ( { message : "Voted!" } ) ;
506511 }
507512 } catch ( e ) {
@@ -549,6 +554,7 @@ oRouter.post('/objects/:id/comments/:commentid/delete',
549554 if ( ! commentExists . rows [ 0 ] . exists ) return res . status ( 404 ) . json ( { error : "Comment not found." } ) ;
550555 }
551556 await pool . query ( "DELETE FROM comments WHERE id = $1" , [ commentID ] ) ;
557+ CacheManager . delete ( `GET:/objects/${ objectID } /comments` ) ;
552558 res . status ( 200 ) . json ( { message : `Deleted!` } ) ;
553559 } catch ( e ) {
554560 console . error ( e ) ;
@@ -722,6 +728,8 @@ oRouter.post('/objects/:id/delete',
722728 }
723729 await pool . query ( "DELETE FROM objects WHERE id = $1" , [ objectID ] ) ;
724730 res . status ( 200 ) . json ( { message : "Deleted object!" } ) ;
731+ CacheManager . delete ( `GET:/objects/${ objectID } ` ) ;
732+ CacheManager . deletePattern ( `GET:/objects` ) ;
725733 } catch ( e ) {
726734 console . error ( e ) ;
727735 res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
@@ -788,6 +796,7 @@ oRouter.post('/objects/:id/update',
788796 if ( ! objExists . rows [ 0 ] . exists ) return res . status ( 404 ) . json ( { error : "Object not found." } ) ;
789797 await pool . query ( query , [ name , description , tags , objectID ] ) ;
790798 }
799+ CacheManager . delete ( `GET:/objects/${ objectID } ` ) ;
791800 return res . status ( 200 ) . json ( { message : "Updated object!" } ) ;
792801 } catch ( e ) {
793802 console . error ( e )
@@ -942,6 +951,8 @@ oRouter.post('/objects/:id/feature',
942951 await pool . query ( "UPDATE objects SET featured = 1 WHERE id = $1" , [ objectID ] ) ;
943952 res . status ( 200 ) . json ( { message : "Featured!" } ) ;
944953 }
954+ CacheManager . delete ( `GET:/objects/${ objectID } ` ) ;
955+ CacheManager . deletePattern ( `GET:/objects` ) ;
945956 } catch ( e ) {
946957 console . error ( e ) ;
947958 res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
@@ -959,7 +970,7 @@ oRouter.post('/objects/:id/feature',
959970oRouter . get ( '/user/:id' ,
960971 param ( 'id' ) . isInt ( { min : 0 , max : 2147483647 } ) . notEmpty ( ) ,
961972 query ( 'page' ) . isInt ( { min : 0 , max : 2147483647 } ) . optional ( ) ,
962- cacheMiddleware ( 300 , ( req ) => {
973+ cacheMiddleware ( 60 , ( req ) => {
963974 return ! req . query [ "no-cache" ] ;
964975 } ) ,
965976 async ( req : Request , res : Response ) => {
@@ -1306,9 +1317,6 @@ oRouter.post('/user/@me/favorites',
13061317 body ( 'token' ) . notEmpty ( ) . isString ( ) ,
13071318 query ( 'page' ) . isInt ( { min : 1 , max : 2147483647 } ) . optional ( ) ,
13081319 query ( 'limit' ) . isInt ( { min : 1 , max : 9 } ) . optional ( ) ,
1309- cacheMiddleware ( 300 , ( req ) => {
1310- return ! req . query [ "no-cache" ] ;
1311- } ) ,
13121320 async ( req : Request , res : Response ) => {
13131321 const result = validationResult ( req ) ;
13141322 if ( ! result . isEmpty ( ) ) return res . status ( 400 ) . json ( { errors : result . array ( ) } )
@@ -1379,7 +1387,7 @@ oRouter.get('/objects',
13791387 query ( 'category' ) . isInt ( { min : 0 , max : 2147483647 } ) . optional ( ) ,
13801388 query ( 'limit' ) . isInt ( { min : 1 , max : 9 } ) . optional ( ) ,
13811389 body ( 'tags' ) . optional ( ) . isString ( ) ,
1382- cacheMiddleware ( 300 , ( req ) => {
1390+ cacheMiddleware ( 60 , ( req ) => {
13831391 const category = parseInt ( req . query . category as string ) || 0 ;
13841392 return ! req . query [ "no-cache" ] || category == 4 ;
13851393 } ) ,
@@ -1517,6 +1525,9 @@ oRouter.post('/objects/search',
15171525 query ( 'page' ) . isInt ( { min : 1 , max : 2147483647 } ) . optional ( ) ,
15181526 query ( 'limit' ) . isInt ( { min : 1 , max : 9 } ) . optional ( ) ,
15191527 body ( 'tags' ) . optional ( ) . isString ( ) ,
1528+ cacheMiddleware ( 10 , ( req ) => {
1529+ return ! req . query [ "no-cache" ]
1530+ } ) ,
15201531 async ( req : Request , res : Response ) => {
15211532 const result = validationResult ( req ) ;
15221533 if ( ! result . isEmpty ( ) ) return res . status ( 400 ) . json ( { errors : result . array ( ) } )
0 commit comments