@@ -4,12 +4,12 @@ import ObjectData from '@/Components/Object';
44import { getCache } from '../postgres' ;
55import { Router , Request , Response } from 'express'
66import { query , body , param , validationResult , CustomValidator } from 'express-validator' ;
7- import { verifyToken } from './user' ;
7+ import { verifyToken , banCheck } from './user' ;
88import axios from 'axios' ;
99import moment from 'moment'
1010import { UserData } from '@/Components/User' ;
1111
12- const allowedTags = [ "Font" , "Decoration" , "Gameplay" , "Art" , "Structure" , "Custom" , "Icon" , "Meme" , "Technical" , "Particles" , "Triggers" , "SFX" , "Effects" , "Auto Build" ] ;
12+ const allowedTags = [ "Font" , "Decoration" , "Gameplay" , "Art" , "Structure" , "Custom" , "Icon" , "Meme" , "Technical" , "Particles" , "Triggers" , "SFX" , "Effects" , "Auto Build" , "Recreation" ] ;
1313
1414const oRouter = Router ( ) ;
1515
@@ -138,7 +138,7 @@ oRouter.post('/objects/upload',
138138 const { token, name, description, data } = req . body ;
139139 let tags = req . body . tags as Array < string > ;
140140 if ( name . length > 64 ) return res . status ( 413 ) . json ( { error : "The name cannot be more than 64 characters long!" } ) ;
141- if ( description . length > 300 ) return res . status ( 413 ) . json ( { error : "The description cannot be more than 300 characters long!" } ) ;
141+ if ( description . length > 500 ) return res . status ( 413 ) . json ( { error : "The description cannot be more than 500 characters long!" } ) ;
142142 const splitData : Array < string > = data . split ( ";" ) ;
143143 if ( splitData . length > 50000 ) return res . status ( 413 ) . json ( { error : "You cannot upload a custom object with more than 50,000 objects!" } )
144144 const hasBlacklistedIDs = splitData . find ( objStr => {
@@ -164,7 +164,7 @@ oRouter.post('/objects/upload',
164164 return res . status ( 401 ) . json ( { error : verifyRes . message } ) ;
165165 }
166166 if ( ! verifyRes . user ) return res . status ( 404 ) . json ( { error : "Couldn't retrieve user." } ) ;
167- if ( verifyRes . user . role == - 1 ) return res . status ( 403 ) . json ( { error : "You are banned! Reason: " + verifyRes . user . ban_reason } ) ;
167+ if ( banCheck ( res , verifyRes . user , 1 ) ) return ;
168168 try {
169169 const dupCheck = await pool . query ( "SELECT id FROM objects WHERE data = $1 LIMIT 1;" , [ data ] ) ;
170170 if ( dupCheck . rowCount != null && dupCheck . rowCount > 0 ) return res . status ( 409 ) . json ( { error : "You cannot upload an object that already exists!" } ) ;
@@ -243,13 +243,13 @@ oRouter.post('/objects/:id/overwrite',
243243 return res . status ( 401 ) . json ( { error : verifyRes . message } ) ;
244244 }
245245 if ( ! verifyRes . user ) return res . status ( 404 ) . json ( { error : "Couldn't retrieve user." } ) ;
246- if ( verifyRes . user . role == - 1 ) return res . status ( 403 ) . json ( { error : "You are banned! Reason: " + verifyRes . user . ban_reason } ) ;
246+ if ( banCheck ( res , verifyRes . user , 1 ) ) return ;
247247 try {
248248 const query = await pool . query ( "SELECT * FROM objects WHERE id = $1 AND status != 3" , [ objectID ] )
249249 if ( ! query . rows . length ) return res . status ( 404 ) . json ( { error : "Object not found." } ) ;
250250 const objData : ObjectData = query . rows [ 0 ] ;
251251 if ( objData . account_id != verifyRes . user . account_id ) return res . status ( 403 ) . json ( { error : "This is not your object!" } ) ;
252- if ( verifyRes . user . role == 0 ) {
252+ if ( verifyRes . user . role == 0 && ! objData . featured ) {
253253 await pool . query ( "UPDATE objects SET data = $1, status = 0, updated_at = $2, version = version + 1 WHERE id = $3" , [ data , new Date ( ) , objectID ] ) ;
254254 } else {
255255 await pool . query ( "UPDATE objects SET data = $1, updated_at = $2, version = version + 1 WHERE id = $3" , [ data , new Date ( ) , objectID ] ) ;
@@ -333,7 +333,8 @@ oRouter.post('/objects/:id/rate',
333333 return res . status ( 401 ) . json ( { error : verifyRes . message } ) ;
334334 }
335335 const accountID = verifyRes . user ?. account_id ;
336- if ( verifyRes . user ?. role == - 1 ) return res . status ( 403 ) . json ( { error : "You are banned! Reason: " + verifyRes . user . ban_reason } ) ;
336+ if ( ! verifyRes . user ) return ;
337+ if ( banCheck ( res , verifyRes . user , 3 ) ) return ;
337338 try {
338339 const objExists = await pool . query ( "SELECT EXISTS (SELECT 1 FROM objects WHERE id = $1 AND status = 1)" , [ objectID ] )
339340 if ( ! objExists . rows [ 0 ] . exists ) return res . status ( 404 ) . json ( { error : "Object not found." } ) ;
@@ -377,8 +378,9 @@ oRouter.post('/objects/:id/comment',
377378 } else if ( ! verifyRes . valid ) {
378379 return res . status ( 401 ) . json ( { error : verifyRes . message } ) ;
379380 }
380- const accountID = verifyRes . user ?. account_id ;
381- if ( verifyRes . user ?. role == - 1 ) return res . status ( 403 ) . json ( { error : "You are banned! Reason: " + verifyRes . user . ban_reason } ) ;
381+ if ( ! verifyRes . user ) return ;
382+ const accountID = verifyRes . user . account_id ;
383+ if ( banCheck ( res , verifyRes . user , 2 ) ) return ;
382384 try {
383385 const objExists = await pool . query ( "SELECT EXISTS (SELECT 1 FROM objects WHERE id = $1 AND status = 1)" , [ objectID ] )
384386 if ( ! objExists . rows [ 0 ] . exists ) return res . status ( 404 ) . json ( { error : "Object not found." } ) ;
@@ -415,8 +417,9 @@ oRouter.post('/objects/:id/comments/:commentid/pin',
415417 } else if ( ! verifyRes . valid ) {
416418 return res . status ( 401 ) . json ( { error : verifyRes . message } ) ;
417419 }
418- const accountID = verifyRes . user ?. account_id ;
419- if ( verifyRes . user ?. role == - 1 ) return res . status ( 403 ) . json ( { error : "You are banned! Reason: " + verifyRes . user . ban_reason } ) ;
420+ if ( ! verifyRes . user ) return ;
421+ const accountID = verifyRes . user . account_id ;
422+ if ( banCheck ( res , verifyRes . user , 3 ) ) return ;
420423 try {
421424 if ( verifyRes . user && verifyRes . user . role == 3 ) {
422425 const objExists = await pool . query ( "SELECT EXISTS (SELECT 1 FROM objects WHERE id = $1)" , [ objectID ] )
@@ -520,8 +523,9 @@ oRouter.post('/objects/:id/comments/:commentid/delete',
520523 } else if ( ! verifyRes . valid ) {
521524 return res . status ( 401 ) . json ( { error : verifyRes . message } ) ;
522525 }
526+ if ( ! verifyRes . user ) return ;
523527 const accountID = verifyRes . user ?. account_id ;
524- if ( verifyRes . user ?. role == - 1 ) return res . status ( 403 ) . json ( { error : "You are banned! Reason: " + verifyRes . user . ban_reason } ) ;
528+ if ( banCheck ( res , verifyRes . user , 3 ) ) return ;
525529 try {
526530 const query = await pool . query ( "SELECT * FROM objects WHERE id = $1 AND status = 1" , [ objectID ] )
527531 if ( ! query . rows . length ) return res . status ( 404 ) . json ( { error : "Object not found." } ) ;
@@ -567,8 +571,9 @@ oRouter.post('/objects/:id/report',
567571 } else if ( ! verifyRes . valid ) {
568572 return res . status ( 401 ) . json ( { error : verifyRes . message } ) ;
569573 }
570- const accountID = verifyRes . user ?. account_id ;
571- if ( verifyRes . user ?. role == - 1 ) return res . status ( 403 ) . json ( { error : "You are banned! Reason: " + verifyRes . user . ban_reason } ) ;
574+ if ( ! verifyRes . user ) return ;
575+ const accountID = verifyRes . user . account_id ;
576+ if ( banCheck ( res , verifyRes . user , 1 ) ) return ;
572577 try {
573578 const objExists = await pool . query ( "SELECT EXISTS (SELECT 1 FROM objects WHERE id = $1 AND status = 1)" , [ objectID ] )
574579 if ( ! objExists . rows [ 0 ] . exists ) return res . status ( 404 ) . json ( { error : "Object not found." } ) ;
@@ -736,7 +741,7 @@ oRouter.post('/objects/:id/update',
736741 const { token, name, description } = req . body ;
737742 let tags = req . body . tags as Array < string > ;
738743 if ( name . length > 64 ) return res . status ( 413 ) . json ( { error : "The name cannot be more than 64 characters long!" } ) ;
739- if ( description . length > 300 ) return res . status ( 413 ) . json ( { error : "The description cannot be more than 300 characters long!" } ) ;
744+ if ( description . length > 500 ) return res . status ( 413 ) . json ( { error : "The description cannot be more than 500 characters long!" } ) ;
740745 if ( ! tags || ! tags . length ) tags = [ ] ;
741746 if ( tags . length > 5 ) return res . status ( 413 ) . json ( { error : "You can only add a maximum of 5 tags!" } ) ;
742747 getCache ( ) . then ( pool => { // returns a PoolClient
@@ -1219,7 +1224,7 @@ oRouter.post('/objects/reports',
12191224 } else if ( ! verifyRes . valid ) {
12201225 return res . status ( 401 ) . json ( { error : verifyRes . message } ) ;
12211226 }
1222- if ( verifyRes . user && verifyRes . user . role != 3 ) return res . status ( 403 ) . json ( { error : "No permission" } ) ;
1227+ if ( verifyRes . user && verifyRes . user . role < 2 ) return res . status ( 403 ) . json ( { error : "No permission" } ) ;
12231228 const page = parseInt ( req . query . page as string ) || 1 ;
12241229 const limit = 9 ;
12251230 const offset = ( page - 1 ) * limit ;
@@ -1231,7 +1236,12 @@ oRouter.post('/objects/reports',
12311236 COALESCE(AVG(orate.stars), 0) as rating,
12321237 COUNT(orate.stars) as rating_count,
12331238 COUNT(*) OVER() AS total_records,
1234- COUNT(rep) AS report_count
1239+ COUNT(rep) AS report_count,
1240+ COALESCE(ARRAY_AGG(DISTINCT jsonb_build_object(
1241+ 'reason', rep.reason,
1242+ 'account_id', rep.account_id,
1243+ 'timestamp', rep.timestamp
1244+ )) FILTER (WHERE rep.object_id IS NOT NULL), '{}') AS reports
12351245 FROM
12361246 objects o
12371247 LEFT JOIN
@@ -1248,7 +1258,11 @@ oRouter.post('/objects/reports',
12481258 const totalRecords = ( result . rows . length > 0 ) ? parseInt ( result . rows [ 0 ] . total_records ) : 0 ;
12491259 const totalPages = Math . ceil ( totalRecords / limit ) ;
12501260
1251- const objectData = convertRowsToObjects ( result . rows )
1261+ const objectData = ( convertRowsToObjects ( result . rows ) as Array < any > ) . map ( ( value , index ) => {
1262+ value . report_count = parseInt ( result . rows [ index ] . report_count ) ;
1263+ value . reports = result . rows [ index ] . reports ;
1264+ return value ;
1265+ } ) ;
12521266 res . json ( {
12531267 results : objectData ,
12541268 page,
0 commit comments