Merge branch 'server-user-globals' into 'next'

refactor: add server_user to globals

See merge request famedly/conduit!686
This commit is contained in:
Matthias Ahouansou 2024-05-31 21:27:26 +00:00
commit 7b19618136
4 changed files with 36 additions and 37 deletions

View file

@ -406,11 +406,9 @@ impl KeyValueDatabase {
// Matrix resource ownership is based on the server name; changing it
// requires recreating the database from scratch.
if services().users.count()? > 0 {
let conduit_user =
UserId::parse_with_server_name("conduit", services().globals.server_name())
.expect("@conduit:server_name is valid");
let conduit_user = services().globals.server_user();
if !services().users.exists(&conduit_user)? {
if !services().users.exists(conduit_user)? {
error!(
"The {} server user does not exist, and the database is not new.",
conduit_user
@ -1104,22 +1102,21 @@ impl KeyValueDatabase {
/// Sets the emergency password and push rules for the @conduit account in case emergency password is set
fn set_emergency_access() -> Result<bool> {
let conduit_user = UserId::parse_with_server_name("conduit", services().globals.server_name())
.expect("@conduit:server_name is a valid UserId");
let conduit_user = services().globals.server_user();
services().users.set_password(
&conduit_user,
conduit_user,
services().globals.emergency_password().as_deref(),
)?;
let (ruleset, res) = match services().globals.emergency_password() {
Some(_) => (Ruleset::server_default(&conduit_user), Ok(true)),
Some(_) => (Ruleset::server_default(conduit_user), Ok(true)),
None => (Ruleset::new(), Ok(false)),
};
services().account_data.update(
None,
&conduit_user,
conduit_user,
GlobalAccountDataEventType::PushRules.to_string().into(),
&serde_json::to_value(&GlobalAccountDataEvent {
content: PushRulesEventContent { global: ruleset },

View file

@ -217,8 +217,7 @@ impl Service {
// TODO: Use futures when we have long admin commands
//let mut futures = FuturesUnordered::new();
let conduit_user = UserId::parse(format!("@conduit:{}", services().globals.server_name()))
.expect("@conduit:server_name is valid");
let conduit_user = services().globals.server_user();
if let Ok(Some(conduit_room)) = services().admin.get_admin_room() {
loop {
@ -252,7 +251,7 @@ impl Service {
state_key: None,
redacts: None,
},
&conduit_user,
conduit_user,
&conduit_room,
&state_lock,
)
@ -1037,11 +1036,9 @@ impl Service {
let state_lock = mutex_state.lock().await;
// Create a user for the server
let conduit_user =
UserId::parse_with_server_name("conduit", services().globals.server_name())
.expect("@conduit:server_name is valid");
let conduit_user = services().globals.server_user();
services().users.create(&conduit_user, None)?;
services().users.create(conduit_user, None)?;
let room_version = services().globals.default_room_version();
let mut content = match room_version {
@ -1054,7 +1051,7 @@ impl Service {
| RoomVersionId::V7
| RoomVersionId::V8
| RoomVersionId::V9
| RoomVersionId::V10 => RoomCreateEventContent::new_v1(conduit_user.clone()),
| RoomVersionId::V10 => RoomCreateEventContent::new_v1(conduit_user.to_owned()),
RoomVersionId::V11 => RoomCreateEventContent::new_v11(),
_ => unreachable!("Validity of room version already checked"),
};
@ -1074,7 +1071,7 @@ impl Service {
state_key: Some("".to_owned()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1102,7 +1099,7 @@ impl Service {
state_key: Some(conduit_user.to_string()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1110,7 +1107,7 @@ impl Service {
// 3. Power levels
let mut users = BTreeMap::new();
users.insert(conduit_user.clone(), 100.into());
users.insert(conduit_user.to_owned(), 100.into());
services()
.rooms
@ -1127,7 +1124,7 @@ impl Service {
state_key: Some("".to_owned()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1146,7 +1143,7 @@ impl Service {
state_key: Some("".to_owned()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1167,7 +1164,7 @@ impl Service {
state_key: Some("".to_owned()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1188,7 +1185,7 @@ impl Service {
state_key: Some("".to_owned()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1208,7 +1205,7 @@ impl Service {
state_key: Some("".to_owned()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1228,7 +1225,7 @@ impl Service {
state_key: Some("".to_owned()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1254,7 +1251,7 @@ impl Service {
state_key: Some("".to_owned()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1301,9 +1298,7 @@ impl Service {
let state_lock = mutex_state.lock().await;
// Use the server user to grant the new admin's power level
let conduit_user =
UserId::parse_with_server_name("conduit", services().globals.server_name())
.expect("@conduit:server_name is valid");
let conduit_user = services().globals.server_user();
// Invite and join the real user
services()
@ -1327,7 +1322,7 @@ impl Service {
state_key: Some(user_id.to_string()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1379,7 +1374,7 @@ impl Service {
state_key: Some("".to_owned()),
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
)
@ -1398,7 +1393,7 @@ impl Service {
state_key: None,
redacts: None,
},
&conduit_user,
conduit_user,
&room_id,
&state_lock,
).await?;

View file

@ -72,6 +72,7 @@ pub struct Service {
pub roomid_mutex_state: RwLock<HashMap<OwnedRoomId, Arc<Mutex<()>>>>,
pub roomid_mutex_federation: RwLock<HashMap<OwnedRoomId, Arc<Mutex<()>>>>, // this lock will be held longer
pub roomid_federationhandletime: RwLock<HashMap<OwnedRoomId, (OwnedEventId, Instant)>>,
server_user: OwnedUserId,
pub stateres_mutex: Arc<Mutex<()>>,
pub rotate: RotationHandler,
@ -186,6 +187,8 @@ impl Service {
let mut s = Self {
allow_registration: RwLock::new(config.allow_registration),
server_user: UserId::parse(format!("@conduit:{}", &config.server_name))
.expect("@conduit:server_name is valid"),
db,
config,
keypair: Arc::new(keypair),
@ -279,6 +282,10 @@ impl Service {
self.config.server_name.as_ref()
}
pub fn server_user(&self) -> &UserId {
self.server_user.as_ref()
}
pub fn max_request_size(&self) -> u32 {
self.config.max_request_size
}

View file

@ -483,16 +483,16 @@ impl Service {
.search
.index_pdu(shortroomid, &pdu_id, &body)?;
let server_user = format!("@conduit:{}", services().globals.server_name());
let server_user = services().globals.server_user();
let to_conduit = body.starts_with(&format!("{server_user}: "))
|| body.starts_with(&format!("{server_user} "))
|| body == format!("{server_user}:")
|| body == server_user;
|| body == server_user.as_str();
// This will evaluate to false if the emergency password is set up so that
// the administrator can execute commands as conduit
let from_conduit = pdu.sender == server_user
let from_conduit = pdu.sender == *server_user
&& services().globals.emergency_password().is_none();
if let Some(admin_room) = services().admin.get_admin_room()? {
@ -857,7 +857,7 @@ impl Service {
.filter(|v| v.starts_with('@'))
.unwrap_or(sender.as_str());
let server_name = services().globals.server_name();
let server_user = format!("@conduit:{}", server_name);
let server_user = services().globals.server_user().as_str();
let content = serde_json::from_str::<ExtractMembership>(pdu.content.get())
.map_err(|_| Error::bad_database("Invalid content in pdu."))?;