@@ -23,6 +23,9 @@ import { config } from '@/config';
2323import { ApiErrorCode , badRequest , serverError } from '@/errors' ;
2424import type { PostRepliesResult } from '@/types/miiverse/post' ;
2525import type { HydratedPostDocument } from '@/types/mongoose/post' ;
26+ import type { HydratedCommunityDocument } from '@/types/mongoose/community' ;
27+ import type { HydratedSettingsDocument } from '@/types/mongoose/settings' ;
28+ import type { GetUserDataResponse } from '@pretendonetwork/grpc/account/get_user_data_rpc' ;
2629
2730const newPostSchema = z . object ( {
2831 community_id : z . string ( ) . optional ( ) ,
@@ -190,6 +193,32 @@ router.get('/', async function (request: express.Request, response: express.Resp
190193 } ) . end ( { pretty : true , allowEmpty : true } ) ) ;
191194} ) ;
192195
196+ function canPost ( community : HydratedCommunityDocument , userSettings : HydratedSettingsDocument , parentPost : HydratedPostDocument | null , user : GetUserDataResponse ) : boolean {
197+ const isReply = ! ! parentPost ;
198+ const isPublicPostableCommunity = community . type >= 0 && community . type < 2 ;
199+ const isOpenCommunity = community . permissions . open ;
200+
201+ const isCommunityAdmin = ( community . admins ?? [ ] ) . includes ( user . pid ) ;
202+ const isUserLimitedFromPosting = userSettings . account_status !== 0 ;
203+ const hasAccessLevelRequirement = isReply
204+ ? user . accessLevel >= community . permissions . minimum_new_comment_access_level
205+ : user . accessLevel >= community . permissions . minimum_new_post_access_level ;
206+
207+ if ( isUserLimitedFromPosting ) {
208+ return false ;
209+ }
210+
211+ if ( isCommunityAdmin ) {
212+ return true ; // admins can always post (if not limited)
213+ }
214+
215+ if ( ! hasAccessLevelRequirement ) {
216+ return false ;
217+ }
218+
219+ return isReply ? isOpenCommunity : isPublicPostableCommunity ;
220+ }
221+
193222async function newPost ( request : express . Request , response : express . Response ) : Promise < void > {
194223 response . type ( 'application/xml' ) ;
195224
@@ -264,14 +293,8 @@ async function newPost(request: express.Request, response: express.Response): Pr
264293 }
265294 }
266295
267- // TODO - Clean this up
268- // * Nesting this because of how many checks there are, extremely unreadable otherwise
269- if ( ! ( community . admins && community . admins . indexOf ( request . pid ) !== - 1 && userSettings . account_status === 0 ) ) {
270- if ( community . type >= 2 || request . user . accessLevel < community . permissions . minimum_new_post_access_level ) {
271- if ( ! ( parentPost && request . user . accessLevel >= community . permissions . minimum_new_comment_access_level && community . permissions . open ) ) {
272- return badRequest ( response , ApiErrorCode . NOT_ALLOWED , 403 ) ;
273- }
274- }
296+ if ( ! canPost ( community , userSettings , parentPost , request . user ) ) {
297+ return badRequest ( response , ApiErrorCode . NOT_ALLOWED , 403 ) ;
275298 }
276299
277300 let miiFace = 'normal_face.png' ;
0 commit comments