Skip to content

Commit 7115fb2

Browse files
dasha-uwujevolk
authored andcommitted
Refactor join, alias services
Split knock, user register from api into services Fix autojoin not working with v12 rooms Fix 'm.login.registration_token/validity' for reloaded registration tokens Change join servers order Move autojoin for ldap
1 parent 959c559 commit 7115fb2

25 files changed

Lines changed: 1150 additions & 1331 deletions

File tree

src/admin/user/commands.rs

Lines changed: 15 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::BTreeMap, fmt::Write as _};
1+
use std::collections::BTreeMap;
22

33
use futures::{FutureExt, StreamExt};
44
use ruma::{
@@ -13,10 +13,9 @@ use ruma::{
1313
},
1414
};
1515
use 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
};
2120
use 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]
404266
pub(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]
535398
pub(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);

src/admin/user/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(super) enum UserCommand {
7878
/// - Manually join a local user to a room.
7979
ForceJoinRoom {
8080
user_id: String,
81-
room_id: OwnedRoomOrAliasId,
81+
room: OwnedRoomOrAliasId,
8282
},
8383

8484
/// - Manually leave a local user from a room.
@@ -148,7 +148,7 @@ pub(super) enum UserCommand {
148148
///
149149
/// Requires the `--yes-i-want-to-do-this` flag.
150150
ForceJoinListOfLocalUsers {
151-
room_id: OwnedRoomOrAliasId,
151+
room: OwnedRoomOrAliasId,
152152

153153
#[arg(long)]
154154
yes_i_want_to_do_this: bool,
@@ -160,7 +160,7 @@ pub(super) enum UserCommand {
160160
///
161161
/// Requires the `--yes-i-want-to-do-this` flag.
162162
ForceJoinAllLocalUsers {
163-
room_id: OwnedRoomOrAliasId,
163+
room: OwnedRoomOrAliasId,
164164

165165
#[arg(long)]
166166
yes_i_want_to_do_this: bool,

src/api/client/alias.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,26 +121,16 @@ async fn room_available_servers(
121121

122122
// insert our server as the very first choice if in list, else check if we can
123123
// prefer the room alias server first
124-
match servers
124+
if let Some(server_index) = servers
125125
.iter()
126126
.position(|server_name| services.globals.server_is_ours(server_name))
127127
{
128-
| Some(server_index) => {
129-
servers.swap_remove(server_index);
130-
servers.insert(0, services.globals.server_name().to_owned());
131-
},
132-
| _ => {
133-
match servers
134-
.iter()
135-
.position(|server| server == room_alias.server_name())
136-
{
137-
| Some(alias_server_index) => {
138-
servers.swap_remove(alias_server_index);
139-
servers.insert(0, room_alias.server_name().into());
140-
},
141-
| _ => {},
142-
}
143-
},
128+
servers.swap(0, server_index);
129+
} else if let Some(alias_server_index) = servers
130+
.iter()
131+
.position(|server| server == room_alias.server_name())
132+
{
133+
servers.swap(0, alias_server_index);
144134
}
145135

146136
servers

src/api/client/device.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use axum::extract::State;
22
use axum_client_ip::InsecureClientIp;
33
use futures::StreamExt;
44
use ruma::{
5-
MilliSecondsSinceUnixEpoch, OwnedDeviceId,
5+
MilliSecondsSinceUnixEpoch,
66
api::client::device::{
77
self, delete_device, delete_devices, get_device, get_devices, update_device,
88
},
99
};
10-
use tuwunel_core::{Err, Result, debug, err, utils, utils::string::to_small_string};
10+
use tuwunel_core::{Err, Result, debug, err, utils::string::to_small_string};
1111

12-
use crate::{Ruma, client::DEVICE_ID_LENGTH, router::auth_uiaa};
12+
use crate::{Ruma, router::auth_uiaa};
1313

1414
/// # `GET /_matrix/client/r0/devices`
1515
///
@@ -94,13 +94,11 @@ pub(crate) async fn update_device_route(
9494
appservice.registration.id
9595
);
9696

97-
let device_id = OwnedDeviceId::from(utils::random_string(DEVICE_ID_LENGTH));
98-
9997
services
10098
.users
10199
.create_device(
102100
sender_user,
103-
&device_id,
101+
None,
104102
(Some(&appservice.registration.as_token), None),
105103
None,
106104
None,

src/api/client/membership/invite.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ pub(crate) async fn invite_user_route(
2222

2323
invite_check(&services, sender_user, room_id).await?;
2424

25-
banned_room_check(&services, sender_user, Some(room_id), room_id.server_name(), client)
26-
.await?;
25+
banned_room_check(&services, sender_user, room_id, None, client).await?;
2726

2827
let invite_user::v3::InvitationRecipient::UserId { user_id } = &body.recipient else {
2928
return Err!(Request(ThreepidDenied("Third party identifiers are not implemented")));

0 commit comments

Comments
 (0)