@@ -170,6 +170,24 @@ export class Provisioner extends ProvisioningApi {
170170 return ( currentCount >= this . main . config . provisioning ?. limits ?. room_count ) ;
171171 }
172172
173+ /**
174+ * Checks if a Matrix room is public.
175+ * @param roomId Matrix room ID to check.
176+ * @returns Promise resolving to true if the room is public, otherwise false.
177+ * @private
178+ */
179+ private async getIsRoomPublic ( roomId : string ) : Promise < boolean > {
180+ try {
181+ const joinRulesEvent = await this . main . botIntent . getStateEvent ( roomId , 'm.room.join_rules' , '' ) ;
182+ return joinRulesEvent . join_rule === 'public' ;
183+ } catch ( e ) {
184+ throw new ApiError (
185+ "Could not check if room is public" ,
186+ ErrCode . Unknown ,
187+ ) ;
188+ }
189+ }
190+
173191 private async getBotId ( req : ProvisioningRequest , res : Response ) {
174192 const hasRoomLimit = this . main . config . provisioning ?. limits ?. room_count ;
175193 const hasTeamLimit = this . main . config . provisioning ?. limits ?. team_count ;
@@ -363,43 +381,54 @@ export class Provisioner extends ProvisioningApi {
363381 const matrixRoomId = body . matrix_room_id ;
364382
365383 const room = this . main . rooms . getByMatrixRoomId ( matrixRoomId ) ;
366- if ( ! room ) {
367- throw new SlackProvisioningError (
368- "Link not found" ,
369- SlackErrCode . UnknownLink ,
370- ) ;
384+ if ( room ) {
385+ // Convert the room 'status' into an integration manager 'status'
386+ let status = room . getStatus ( ) ;
387+ if ( status . match ( / ^ r e a d y / ) ) {
388+ // OK
389+ } else if ( status === "pending-params" ) {
390+ status = "partial" ;
391+ } else if ( status === "pending-name" ) {
392+ status = "pending" ;
393+ } else {
394+ status = "unknown" ;
395+ }
396+
397+ return res . json ( {
398+ inbound_uri : this . main . getInboundUrlForRoom ( room ) ,
399+ isWebhook : room . SlackWebhookUri !== undefined ,
400+ matrix_room_id : matrixRoomId ,
401+ slack_channel_id : room . SlackChannelId ,
402+ slack_channel_name : room . SlackChannelName ,
403+ slack_webhook_uri : room . SlackWebhookUri ,
404+ status,
405+ team_id : room . SlackTeamId ,
406+ } ) ;
371407 }
372408
373- const allowed = await this . main . checkLinkPermission ( matrixRoomId , userId ) ;
374- if ( ! allowed ) {
409+ if ( this . config . require_public_room ) {
410+ // Check if the room is public
411+ const isRoomPublic = await this . getIsRoomPublic ( matrixRoomId ) ;
412+ if ( ! isRoomPublic ) {
413+ throw new SlackProvisioningError (
414+ "Only allowed to link public rooms" ,
415+ SlackErrCode . NotPublic ,
416+ ) ;
417+ }
418+ }
419+
420+ const hasPermissions = await this . main . checkLinkPermission ( matrixRoomId , userId ) ;
421+ if ( ! hasPermissions ) {
375422 throw new SlackProvisioningError (
376423 "Not allowed to provision links in this room" ,
377424 SlackErrCode . NotEnoughPower ,
378425 ) ;
379426 }
380427
381- // Convert the room 'status' into an integration manager 'status'
382- let status = room . getStatus ( ) ;
383- if ( status . match ( / ^ r e a d y / ) ) {
384- // OK
385- } else if ( status === "pending-params" ) {
386- status = "partial" ;
387- } else if ( status === "pending-name" ) {
388- status = "pending" ;
389- } else {
390- status = "unknown" ;
391- }
392-
393- return res . json ( {
394- inbound_uri : this . main . getInboundUrlForRoom ( room ) ,
395- isWebhook : room . SlackWebhookUri !== undefined ,
396- matrix_room_id : matrixRoomId ,
397- slack_channel_id : room . SlackChannelId ,
398- slack_channel_name : room . SlackChannelName ,
399- slack_webhook_uri : room . SlackWebhookUri ,
400- status,
401- team_id : room . SlackTeamId ,
402- } ) ;
428+ throw new SlackProvisioningError (
429+ "Link not found" ,
430+ SlackErrCode . UnknownLink ,
431+ ) ;
403432 }
404433
405434 private async getChannelInfo ( req : ProvisioningRequest , res : Response ) {
@@ -475,19 +504,8 @@ export class Provisioner extends ProvisioningApi {
475504 }
476505
477506 if ( this . config . require_public_room ) {
478- // Check if the room is public
479- let joinRulesEvent ;
480- try {
481- joinRulesEvent = await this . main . botIntent . getStateEvent ( matrixRoomId , 'm.room.join_rules' , '' ) ;
482- } catch ( e ) {
483- req . log . error ( e ) ;
484- throw new ApiError (
485- "Could not check if room is public" ,
486- ErrCode . Unknown ,
487- ) ;
488- }
489-
490- if ( joinRulesEvent . join_rule !== 'public' ) {
507+ const isRoomPublic = await this . getIsRoomPublic ( matrixRoomId ) ;
508+ if ( ! isRoomPublic ) {
491509 throw new SlackProvisioningError (
492510 "Only allowed to link public rooms" ,
493511 SlackErrCode . NotPublic ,
0 commit comments