@@ -386,11 +386,32 @@ async def _create_and_join_rooms(self, user_id: str) -> None:
386
386
room_alias = RoomAlias .from_string (r )
387
387
388
388
if self .hs .hostname != room_alias .domain :
389
- logger .warning (
390
- "Cannot create room alias %s, "
391
- "it does not match server domain" ,
389
+ # If the alias is remote, try to join the room. This might fail
390
+ # because the room might be invite only, but we don't have any local
391
+ # user in the room to invite this one with, so at this point that's
392
+ # the best we can do.
393
+ logger .info (
394
+ "Cannot automatically create room with alias %s as it isn't"
395
+ " local, trying to join the room instead" ,
392
396
r ,
393
397
)
398
+
399
+ (
400
+ room ,
401
+ remote_room_hosts ,
402
+ ) = await room_member_handler .lookup_room_alias (room_alias )
403
+ room_id = room .to_string ()
404
+
405
+ await room_member_handler .update_membership (
406
+ requester = create_requester (
407
+ user_id , authenticated_entity = self ._server_name
408
+ ),
409
+ target = UserID .from_string (user_id ),
410
+ room_id = room_id ,
411
+ remote_room_hosts = remote_room_hosts ,
412
+ action = "join" ,
413
+ ratelimit = False ,
414
+ )
394
415
else :
395
416
# A shallow copy is OK here since the only key that is
396
417
# modified is room_alias_name.
@@ -448,22 +469,32 @@ async def _join_rooms(self, user_id: str) -> None:
448
469
)
449
470
450
471
# Calculate whether the room requires an invite or can be
451
- # joined directly. Note that unless a join rule of public exists,
452
- # it is treated as requiring an invite.
453
- requires_invite = True
454
-
455
- state = await self .store .get_filtered_current_state_ids (
456
- room_id , StateFilter .from_types ([(EventTypes .JoinRules , "" )])
472
+ # joined directly. By default, we consider the room as requiring an
473
+ # invite if the homeserver is in the room (unless told otherwise by the
474
+ # join rules). Otherwise we consider it as being joinable, at the risk of
475
+ # failing to join, but in this case there's little more we can do since
476
+ # we don't have a local user in the room to craft up an invite with.
477
+ requires_invite = await self .store .is_host_joined (
478
+ room_id ,
479
+ self .server_name ,
457
480
)
458
481
459
- event_id = state . get (( EventTypes . JoinRules , "" ))
460
- if event_id :
461
- join_rules_event = await self .store .get_event (
462
- event_id , allow_none = True
482
+ if requires_invite :
483
+ # If the server is in the room, check if the room is public.
484
+ state = await self .store .get_filtered_current_state_ids (
485
+ room_id , StateFilter . from_types ([( EventTypes . JoinRules , "" )])
463
486
)
464
- if join_rules_event :
465
- join_rule = join_rules_event .content .get ("join_rule" , None )
466
- requires_invite = join_rule and join_rule != JoinRules .PUBLIC
487
+
488
+ event_id = state .get ((EventTypes .JoinRules , "" ))
489
+ if event_id :
490
+ join_rules_event = await self .store .get_event (
491
+ event_id , allow_none = True
492
+ )
493
+ if join_rules_event :
494
+ join_rule = join_rules_event .content .get ("join_rule" , None )
495
+ requires_invite = (
496
+ join_rule and join_rule != JoinRules .PUBLIC
497
+ )
467
498
468
499
# Send the invite, if necessary.
469
500
if requires_invite :
0 commit comments