1- use std:: { collections:: BTreeMap , fmt :: Write as _ } ;
1+ use std:: collections:: BTreeMap ;
22
33use futures:: { FutureExt , StreamExt } ;
44use ruma:: {
@@ -13,10 +13,9 @@ use ruma::{
1313 } ,
1414} ;
1515use tuwunel_core:: {
16- Err , Result , debug , debug_warn, error , info, is_equal_to ,
16+ Err , Result , debug_warn, info,
1717 matrix:: { Event , pdu:: PduBuilder } ,
1818 utils:: { self , ReadyExt } ,
19- warn,
2019} ;
2120use tuwunel_service:: Services ;
2221
@@ -62,148 +61,11 @@ pub(super) async fn create_user(&self, username: String, password: Option<String
6261
6362 let password = password. unwrap_or_else ( || utils:: random_string ( AUTO_GEN_PASSWORD_LENGTH ) ) ;
6463
65- // Create user
6664 self . services
6765 . users
68- . create ( & user_id, Some ( password. as_str ( ) ) , None )
66+ . full_register ( & user_id, Some ( & password) , None , None , false , true )
6967 . await ?;
7068
71- // Default to pretty displayname
72- let mut displayname = user_id. localpart ( ) . to_owned ( ) ;
73-
74- // If `new_user_displayname_suffix` is set, registration will push whatever
75- // content is set to the user's display name with a space before it
76- if !self
77- . services
78- . server
79- . config
80- . new_user_displayname_suffix
81- . is_empty ( )
82- {
83- write ! (
84- displayname,
85- " {}" ,
86- self . services
87- . server
88- . config
89- . new_user_displayname_suffix
90- ) ?;
91- }
92-
93- self . services
94- . users
95- . set_displayname ( & user_id, Some ( displayname) ) ;
96-
97- // Initial account data
98- self . services
99- . account_data
100- . update (
101- None ,
102- & user_id,
103- ruma:: events:: GlobalAccountDataEventType :: PushRules
104- . to_string ( )
105- . into ( ) ,
106- & serde_json:: to_value ( ruma:: events:: push_rules:: PushRulesEvent {
107- content : ruma:: events:: push_rules:: PushRulesEventContent {
108- global : ruma:: push:: Ruleset :: server_default ( & user_id) ,
109- } ,
110- } ) ?,
111- )
112- . await ?;
113-
114- if !self
115- . services
116- . server
117- . config
118- . auto_join_rooms
119- . is_empty ( )
120- {
121- for room in & self . services . server . config . auto_join_rooms {
122- let Ok ( room_id) = self . services . alias . maybe_resolve ( room) . await else {
123- error ! (
124- %user_id,
125- "Failed to resolve room alias to room ID when attempting to auto join {room}, skipping"
126- ) ;
127- continue ;
128- } ;
129-
130- if !self
131- . services
132- . state_cache
133- . server_in_room ( self . services . globals . server_name ( ) , & room_id)
134- . await
135- {
136- warn ! (
137- "Skipping room {room} to automatically join as we have never joined before."
138- ) ;
139- continue ;
140- }
141-
142- let state_lock = self . services . state . mutex . lock ( & room_id) . await ;
143-
144- if let Some ( room_server_name) = room. server_name ( ) {
145- match self
146- . services
147- . membership
148- . join (
149- & user_id,
150- & room_id,
151- Some ( "Automatically joining this room upon registration" . to_owned ( ) ) ,
152- & [
153- self . services . globals . server_name ( ) . to_owned ( ) ,
154- room_server_name. to_owned ( ) ,
155- ] ,
156- & None ,
157- & state_lock,
158- )
159- . await
160- {
161- | Ok ( _response) => {
162- info ! ( "Automatically joined room {room} for user {user_id}" ) ;
163- } ,
164- | Err ( e) => {
165- // don't return this error so we don't fail registrations
166- error ! (
167- "Failed to automatically join room {room} for user {user_id}: {e}"
168- ) ;
169- self . services
170- . admin
171- . send_text ( & format ! (
172- "Failed to automatically join room {room} for user {user_id}: \
173- {e}"
174- ) )
175- . await ;
176- } ,
177- }
178-
179- drop ( state_lock) ;
180- }
181- }
182- }
183-
184- // we dont add a device since we're not the user, just the creator
185-
186- // if this account creation is from the CLI / --execute, invite the first user
187- // to admin room
188- if let Ok ( admin_room) = self . services . admin . get_admin_room ( ) . await {
189- if self
190- . services
191- . state_cache
192- . room_joined_count ( & admin_room)
193- . await
194- . is_ok_and ( is_equal_to ! ( 1 ) )
195- {
196- self . services
197- . admin
198- . make_user_admin ( & user_id)
199- . boxed ( )
200- . await ?;
201- warn ! ( "Granting {user_id} admin privileges as the first user" ) ;
202- }
203- } else {
204- debug ! ( "create_user admin command called without an admin room being available" ) ;
205- }
206-
20769 self . write_str ( & format ! ( "Created user with user_id: {user_id} and password: `{password}`" ) )
20870 . await
20971}
@@ -403,7 +265,7 @@ pub(super) async fn list_joined_rooms(&self, user_id: String) -> Result {
403265#[ admin_command]
404266pub ( super ) async fn force_join_list_of_local_users (
405267 & self ,
406- room_id : OwnedRoomOrAliasId ,
268+ room : OwnedRoomOrAliasId ,
407269 yes_i_want_to_do_this : bool ,
408270) -> Result {
409271 if self . body . len ( ) < 2
@@ -415,7 +277,7 @@ pub(super) async fn force_join_list_of_local_users(
415277
416278 if !yes_i_want_to_do_this {
417279 return Err ! (
418- "You must pass the --yes-i-want-to-do-this- flag to ensure you really want to force \
280+ "You must pass the --yes-i-want-to-do-this flag to ensure you really want to force \
419281 bulk join all specified local users.",
420282 ) ;
421283 }
@@ -427,7 +289,7 @@ pub(super) async fn force_join_list_of_local_users(
427289 let ( room_id, servers) = self
428290 . services
429291 . alias
430- . maybe_resolve_with_servers ( & room_id , None )
292+ . maybe_resolve_with_servers ( & room , None )
431293 . await ?;
432294
433295 if !self
@@ -505,9 +367,10 @@ pub(super) async fn force_join_list_of_local_users(
505367 . join (
506368 & user_id,
507369 & room_id,
370+ Some ( & room) ,
508371 Some ( String :: from ( BULK_JOIN_REASON ) ) ,
509372 & servers,
510- & None ,
373+ false ,
511374 & state_lock,
512375 )
513376 . await
@@ -534,7 +397,7 @@ pub(super) async fn force_join_list_of_local_users(
534397#[ admin_command]
535398pub ( super ) async fn force_join_all_local_users (
536399 & self ,
537- room_id : OwnedRoomOrAliasId ,
400+ room : OwnedRoomOrAliasId ,
538401 yes_i_want_to_do_this : bool ,
539402) -> Result {
540403 if !yes_i_want_to_do_this {
@@ -551,7 +414,7 @@ pub(super) async fn force_join_all_local_users(
551414 let ( room_id, servers) = self
552415 . services
553416 . alias
554- . maybe_resolve_with_servers ( & room_id , None )
417+ . maybe_resolve_with_servers ( & room , None )
555418 . await ?;
556419
557420 if !self
@@ -600,9 +463,10 @@ pub(super) async fn force_join_all_local_users(
600463 . join (
601464 user_id,
602465 & room_id,
466+ Some ( & room) ,
603467 Some ( String :: from ( BULK_JOIN_REASON ) ) ,
604468 & servers,
605- & None ,
469+ false ,
606470 & state_lock,
607471 )
608472 . await
@@ -627,16 +491,12 @@ pub(super) async fn force_join_all_local_users(
627491}
628492
629493#[ admin_command]
630- pub ( super ) async fn force_join_room (
631- & self ,
632- user_id : String ,
633- room_id : OwnedRoomOrAliasId ,
634- ) -> Result {
494+ pub ( super ) async fn force_join_room ( & self , user_id : String , room : OwnedRoomOrAliasId ) -> Result {
635495 let user_id = parse_local_user_id ( self . services , & user_id) ?;
636496 let ( room_id, servers) = self
637497 . services
638498 . alias
639- . maybe_resolve_with_servers ( & room_id , None )
499+ . maybe_resolve_with_servers ( & room , None )
640500 . await ?;
641501
642502 assert ! (
@@ -648,7 +508,7 @@ pub(super) async fn force_join_room(
648508
649509 self . services
650510 . membership
651- . join ( & user_id, & room_id, None , & servers, & None , & state_lock)
511+ . join ( & user_id, & room_id, Some ( & room ) , None , & servers, false , & state_lock)
652512 . await ?;
653513
654514 drop ( state_lock) ;
0 commit comments