resolve the last few relevant pedantic clippy lints

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-03-27 16:33:09 -04:00 committed by June
parent fa71dd4b4c
commit 48d1a3af3c
20 changed files with 464 additions and 560 deletions

View file

@ -12,9 +12,8 @@ use crate::{services, Error, Result, Ruma};
/// - A user can only access their own filters /// - A user can only access their own filters
pub async fn get_filter_route(body: Ruma<get_filter::v3::Request>) -> Result<get_filter::v3::Response> { pub async fn get_filter_route(body: Ruma<get_filter::v3::Request>) -> Result<get_filter::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let filter = match services().users.get_filter(sender_user, &body.filter_id)? { let Some(filter) = services().users.get_filter(sender_user, &body.filter_id)? else {
Some(filter) => filter, return Err(Error::BadRequest(ErrorKind::NotFound, "Filter not found."));
None => return Err(Error::BadRequest(ErrorKind::NotFound, "Filter not found.")),
}; };
Ok(get_filter::v3::Response::new(filter)) Ok(get_filter::v3::Response::new(filter))

View file

@ -387,8 +387,7 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
.collect(); .collect();
while let Some((server, response)) = futures.next().await { while let Some((server, response)) = futures.next().await {
match response { if let Ok(Ok(response)) = response {
Ok(Ok(response)) => {
for (user, masterkey) in response.master_keys { for (user, masterkey) in response.master_keys {
let (master_key_id, mut master_key) = services().users.parse_master_key(&user, &masterkey)?; let (master_key_id, mut master_key) = services().users.parse_master_key(&user, &masterkey)?;
@ -412,11 +411,9 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
self_signing_keys.extend(response.self_signing_keys); self_signing_keys.extend(response.self_signing_keys);
device_keys.extend(response.device_keys); device_keys.extend(response.device_keys);
}, } else {
_ => {
back_off(server.to_owned()).await; back_off(server.to_owned()).await;
failures.insert(server.to_string(), json!({})); failures.insert(server.to_string(), json!({}));
},
} }
} }

View file

@ -788,13 +788,12 @@ async fn request_url_preview(url: &str) -> Result<UrlPreviewData> {
)); ));
} }
let content_type = match response let Some(content_type) = response
.headers() .headers()
.get(reqwest::header::CONTENT_TYPE) .get(reqwest::header::CONTENT_TYPE)
.and_then(|x| x.to_str().ok()) .and_then(|x| x.to_str().ok())
{ else {
Some(ct) => ct, return Err(Error::BadRequest(ErrorKind::Unknown, "Unknown Content-Type"));
None => return Err(Error::BadRequest(ErrorKind::Unknown, "Unknown Content-Type")),
}; };
let data = match content_type { let data = match content_type {
html if html.starts_with("text/html") => download_html(client, url).await?, html if html.starts_with("text/html") => download_html(client, url).await?,

View file

@ -669,16 +669,14 @@ pub(crate) async fn join_room_by_id_helper(
"There is a signed event. This room is probably using restricted joins. Adding signature \ "There is a signed event. This room is probably using restricted joins. Adding signature \
to our event" to our event"
); );
let (signed_event_id, signed_value) = let Ok((signed_event_id, signed_value)) =
match gen_event_id_canonical_json(signed_raw, &room_version_id) { gen_event_id_canonical_json(signed_raw, &room_version_id)
Ok(t) => t, else {
Err(_) => {
// Event could not be converted to canonical json // Event could not be converted to canonical json
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Could not convert event to canonical json.", "Could not convert event to canonical json.",
)); ));
},
}; };
if signed_event_id != event_id { if signed_event_id != event_id {
@ -753,9 +751,8 @@ pub(crate) async fn join_room_by_id_helper(
.iter() .iter()
.map(|pdu| validate_and_add_event_id(pdu, &room_version_id, &pub_key_map)) .map(|pdu| validate_and_add_event_id(pdu, &room_version_id, &pub_key_map))
{ {
let (event_id, value) = match result.await { let Ok((event_id, value)) = result.await else {
Ok(t) => t, continue;
Err(_) => continue,
}; };
let pdu = PduEvent::from_id_val(&event_id, value.clone()).map_err(|e| { let pdu = PduEvent::from_id_val(&event_id, value.clone()).map_err(|e| {
@ -783,9 +780,8 @@ pub(crate) async fn join_room_by_id_helper(
.iter() .iter()
.map(|pdu| validate_and_add_event_id(pdu, &room_version_id, &pub_key_map)) .map(|pdu| validate_and_add_event_id(pdu, &room_version_id, &pub_key_map))
{ {
let (event_id, value) = match result.await { let Ok((event_id, value)) = result.await else {
Ok(t) => t, continue;
Err(_) => continue,
}; };
services() services()
@ -1104,15 +1100,13 @@ pub(crate) async fn join_room_by_id_helper(
.await?; .await?;
if let Some(signed_raw) = send_join_response.room_state.event { if let Some(signed_raw) = send_join_response.room_state.event {
let (signed_event_id, signed_value) = match gen_event_id_canonical_json(&signed_raw, &room_version_id) { let Ok((signed_event_id, signed_value)) = gen_event_id_canonical_json(&signed_raw, &room_version_id)
Ok(t) => t, else {
Err(_) => {
// Event could not be converted to canonical json // Event could not be converted to canonical json
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Could not convert event to canonical json.", "Could not convert event to canonical json.",
)); ));
},
}; };
if signed_event_id != event_id { if signed_event_id != event_id {
@ -1311,15 +1305,12 @@ pub(crate) async fn invite_helper(
// We do not add the event_id field to the pdu here because of signature and // We do not add the event_id field to the pdu here because of signature and
// hashes checks // hashes checks
let (event_id, value) = match gen_event_id_canonical_json(&response.event, &room_version_id) { let Ok((event_id, value)) = gen_event_id_canonical_json(&response.event, &room_version_id) else {
Ok(t) => t,
Err(_) => {
// Event could not be converted to canonical json // Event could not be converted to canonical json
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Could not convert event to canonical json.", "Could not convert event to canonical json.",
)); ));
},
}; };
if *pdu.event_id != *event_id { if *pdu.event_id != *event_id {

View file

@ -21,14 +21,11 @@ pub async fn report_event_route(body: Ruma<report_content::v3::Request>) -> Resu
info!("Received /report request by user {}", sender_user); info!("Received /report request by user {}", sender_user);
// check if we know about the reported event ID or if it's invalid // check if we know about the reported event ID or if it's invalid
let pdu = match services().rooms.timeline.get_pdu(&body.event_id)? { let Some(pdu) = services().rooms.timeline.get_pdu(&body.event_id)? else {
Some(pdu) => pdu,
_ => {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"Event ID is not known to us or Event ID is invalid", "Event ID is not known to us or Event ID is invalid",
)) ));
},
}; };
// check if the room ID from the URI matches the PDU's room ID // check if the room ID from the URI matches the PDU's room ID

View file

@ -314,28 +314,23 @@ async fn sync_helper(
None => HashMap::new(), None => HashMap::new(),
}; };
let left_event_id = match services().rooms.state_accessor.room_state_get_id( let Some(left_event_id) = services().rooms.state_accessor.room_state_get_id(
&room_id, &room_id,
&StateEventType::RoomMember, &StateEventType::RoomMember,
sender_user.as_str(), sender_user.as_str(),
)? { )?
Some(e) => e, else {
None => {
error!("Left room but no left state event"); error!("Left room but no left state event");
continue; continue;
},
}; };
let left_shortstatehash = match services() let Some(left_shortstatehash) = services()
.rooms .rooms
.state_accessor .state_accessor
.pdu_shortstatehash(&left_event_id)? .pdu_shortstatehash(&left_event_id)?
{ else {
Some(s) => s,
None => {
error!("Leave event has no state"); error!("Leave event has no state");
continue; continue;
},
}; };
let mut left_state_ids = services() let mut left_state_ids = services()
@ -616,9 +611,7 @@ async fn load_joined_room(
// Database queries: // Database queries:
let current_shortstatehash = if let Some(s) = services().rooms.state.get_room_shortstatehash(room_id)? { let Some(current_shortstatehash) = services().rooms.state.get_room_shortstatehash(room_id)? else {
s
} else {
error!("Room {} has no state", room_id); error!("Room {} has no state", room_id);
return Err(Error::BadDatabase("Room has no state")); return Err(Error::BadDatabase("Room has no state"));
}; };
@ -736,12 +729,9 @@ async fn load_joined_room(
.get_statekey_from_short(shortstatekey)?; .get_statekey_from_short(shortstatekey)?;
if event_type != StateEventType::RoomMember { if event_type != StateEventType::RoomMember {
let pdu = match services().rooms.timeline.get_pdu(&id)? { let Some(pdu) = services().rooms.timeline.get_pdu(&id)? else {
Some(pdu) => pdu,
None => {
error!("Pdu in state not found: {}", id); error!("Pdu in state not found: {}", id);
continue; continue;
},
}; };
state_events.push(pdu); state_events.push(pdu);
@ -755,12 +745,9 @@ async fn load_joined_room(
// TODO: Delete the following line when this is resolved: https://github.com/vector-im/element-web/issues/22565 // TODO: Delete the following line when this is resolved: https://github.com/vector-im/element-web/issues/22565
|| (cfg!(feature = "element_hacks") && *sender_user == state_key) || (cfg!(feature = "element_hacks") && *sender_user == state_key)
{ {
let pdu = match services().rooms.timeline.get_pdu(&id)? { let Some(pdu) = services().rooms.timeline.get_pdu(&id)? else {
Some(pdu) => pdu,
None => {
error!("Pdu in state not found: {}", id); error!("Pdu in state not found: {}", id);
continue; continue;
},
}; };
// This check is in case a bad user ID made it into the database // This check is in case a bad user ID made it into the database
@ -812,12 +799,9 @@ async fn load_joined_room(
for (key, id) in current_state_ids { for (key, id) in current_state_ids {
if full_state || since_state_ids.get(&key) != Some(&id) { if full_state || since_state_ids.get(&key) != Some(&id) {
let pdu = match services().rooms.timeline.get_pdu(&id)? { let Some(pdu) = services().rooms.timeline.get_pdu(&id)? else {
Some(pdu) => pdu,
None => {
error!("Pdu in state not found: {}", id); error!("Pdu in state not found: {}", id);
continue; continue;
},
}; };
if pdu.kind == TimelineEventType::RoomMember { if pdu.kind == TimelineEventType::RoomMember {
@ -1209,9 +1193,7 @@ pub async fn sync_events_v4_route(
); );
for room_id in &all_joined_rooms { for room_id in &all_joined_rooms {
let current_shortstatehash = if let Some(s) = services().rooms.state.get_room_shortstatehash(room_id)? { let Some(current_shortstatehash) = services().rooms.state.get_room_shortstatehash(room_id)? else {
s
} else {
error!("Room {} has no state", room_id); error!("Room {} has no state", room_id);
continue; continue;
}; };
@ -1272,12 +1254,9 @@ pub async fn sync_events_v4_route(
for (key, id) in current_state_ids { for (key, id) in current_state_ids {
if since_state_ids.get(&key) != Some(&id) { if since_state_ids.get(&key) != Some(&id) {
let pdu = match services().rooms.timeline.get_pdu(&id)? { let Some(pdu) = services().rooms.timeline.get_pdu(&id)? else {
Some(pdu) => pdu,
None => {
error!("Pdu in state not found: {}", id); error!("Pdu in state not found: {}", id);
continue; continue;
},
}; };
if pdu.kind == TimelineEventType::RoomMember { if pdu.kind == TimelineEventType::RoomMember {
if let Some(state_key) = &pdu.state_key { if let Some(state_key) = &pdu.state_key {

View file

@ -84,9 +84,8 @@ where
None None
}; };
let (sender_user, sender_device, sender_servername, from_appservice) = if let Some(info) = let (sender_user, sender_device, sender_servername, from_appservice) =
appservice_registration if let Some(info) = appservice_registration {
{
match metadata.authentication { match metadata.authentication {
AuthScheme::AccessToken => { AuthScheme::AccessToken => {
let user_id = query_params.user_id.map_or_else( let user_id = query_params.user_id.map_or_else(
@ -135,9 +134,8 @@ where
} else { } else {
match metadata.authentication { match metadata.authentication {
AuthScheme::AccessToken => { AuthScheme::AccessToken => {
let token = match token { let Some(token) = token else {
Some(token) => token, return Err(Error::BadRequest(ErrorKind::MissingToken, "Missing access token."));
_ => return Err(Error::BadRequest(ErrorKind::MissingToken, "Missing access token.")),
}; };
match services().users.find_from_token(token)? { match services().users.find_from_token(token)? {
@ -254,8 +252,8 @@ where
if parts.uri.to_string().contains('@') { if parts.uri.to_string().contains('@') {
warn!( warn!(
"Request uri contained '@' character. Make sure your reverse proxy gives Conduit \ "Request uri contained '@' character. Make sure your reverse proxy gives \
the raw uri (apache: use nocanon)" Conduit the raw uri (apache: use nocanon)"
); );
} }
@ -274,9 +272,8 @@ where
.config .config
.allow_public_room_directory_without_auth .allow_public_room_directory_without_auth
{ {
let token = match token { let Some(token) = token else {
Some(token) => token, return Err(Error::BadRequest(ErrorKind::MissingToken, "Missing access token."));
_ => return Err(Error::BadRequest(ErrorKind::MissingToken, "Missing access token.")),
}; };
match services().users.find_from_token(token)? { match services().users.find_from_token(token)? {

View file

@ -429,8 +429,7 @@ async fn find_actual_destination(destination: &'_ ServerName) -> (FedDest, FedDe
FedDest::Named(host.to_owned(), port.to_owned()) FedDest::Named(host.to_owned(), port.to_owned())
} else { } else {
debug!("Requesting well known for {destination}"); debug!("Requesting well known for {destination}");
match request_well_known(destination.as_str()).await { if let Some(delegated_hostname) = request_well_known(destination.as_str()).await {
Some(delegated_hostname) => {
debug!("3: A .well-known file is available"); debug!("3: A .well-known file is available");
hostname = add_port_to_hostname(&delegated_hostname).into_uri_string(); hostname = add_port_to_hostname(&delegated_hostname).into_uri_string();
match get_ip_with_port(&delegated_hostname) { match get_ip_with_port(&delegated_hostname) {
@ -469,19 +468,13 @@ async fn find_actual_destination(destination: &'_ ServerName) -> (FedDest, FedDe
} }
}, },
} }
}, } else {
None => {
debug!("4: No .well-known or an error occured"); debug!("4: No .well-known or an error occured");
match query_srv_record(&destination_str).await { if let Some(hostname_override) = query_srv_record(&destination_str).await {
Some(hostname_override) => {
debug!("4: SRV record found"); debug!("4: SRV record found");
let force_port = hostname_override.port(); let force_port = hostname_override.port();
query_and_cache_override( query_and_cache_override(&hostname, &hostname_override.hostname(), force_port.unwrap_or(8448))
&hostname,
&hostname_override.hostname(),
force_port.unwrap_or(8448),
)
.await; .await;
if let Some(port) = force_port { if let Some(port) = force_port {
@ -489,14 +482,11 @@ async fn find_actual_destination(destination: &'_ ServerName) -> (FedDest, FedDe
} else { } else {
add_port_to_hostname(&hostname) add_port_to_hostname(&hostname)
} }
}, } else {
None => {
debug!("5: No SRV record found"); debug!("5: No SRV record found");
query_and_cache_override(&destination_str, &destination_str, 8448).await; query_and_cache_override(&destination_str, &destination_str, 8448).await;
add_port_to_hostname(&destination_str) add_port_to_hostname(&destination_str)
},
} }
},
} }
} }
}, },
@ -776,15 +766,12 @@ pub fn parse_incoming_pdu(pdu: &RawJsonValue) -> Result<(OwnedEventId, Canonical
let room_version_id = services().rooms.state.get_room_version(&room_id)?; let room_version_id = services().rooms.state.get_room_version(&room_id)?;
let (event_id, value) = match gen_event_id_canonical_json(pdu, &room_version_id) { let Ok((event_id, value)) = gen_event_id_canonical_json(pdu, &room_version_id) else {
Ok(t) => t,
Err(_) => {
// Event could not be converted to canonical json // Event could not be converted to canonical json
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Could not convert event to canonical json.", "Could not convert event to canonical json.",
)); ));
},
}; };
Ok((event_id, value, room_id)) Ok((event_id, value, room_id))
} }
@ -1379,12 +1366,13 @@ pub async fn get_room_state_route(body: Ruma<get_room_state::v1::Request>) -> Re
Ok(get_room_state::v1::Response { Ok(get_room_state::v1::Response {
auth_chain: auth_chain_ids auth_chain: auth_chain_ids
.filter_map(|id| match services().rooms.timeline.get_pdu_json(&id).ok()? { .filter_map(|id| {
Some(json) => Some(PduEvent::convert_to_outgoing_federation_event(json)), if let Some(json) = services().rooms.timeline.get_pdu_json(&id).ok()? {
None => { Some(PduEvent::convert_to_outgoing_federation_event(json))
} else {
error!("Could not find event json for {id} in db."); error!("Could not find event json for {id} in db.");
None None
}, }
}) })
.collect(), .collect(),
pdus, pdus,
@ -1623,15 +1611,12 @@ async fn create_join_event(
// We do not add the event_id field to the pdu here because of signature and // We do not add the event_id field to the pdu here because of signature and
// hashes checks // hashes checks
let room_version_id = services().rooms.state.get_room_version(room_id)?; let room_version_id = services().rooms.state.get_room_version(room_id)?;
let (event_id, value) = match gen_event_id_canonical_json(pdu, &room_version_id) { let Ok((event_id, value)) = gen_event_id_canonical_json(pdu, &room_version_id) else {
Ok(t) => t,
Err(_) => {
// Event could not be converted to canonical json // Event could not be converted to canonical json
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Could not convert event to canonical json.", "Could not convert event to canonical json.",
)); ));
},
}; };
let origin: OwnedServerName = serde_json::from_value( let origin: OwnedServerName = serde_json::from_value(

View file

@ -253,9 +253,10 @@ impl KeyValueDatabaseEngine for Arc<Engine> {
let options = BackupEngineOptions::new(path.unwrap())?; let options = BackupEngineOptions::new(path.unwrap())?;
let mut engine = BackupEngine::open(&options, &self.env)?; let mut engine = BackupEngine::open(&options, &self.env)?;
let ret = if self.config.database_backups_to_keep > 0 { let ret = if self.config.database_backups_to_keep > 0 {
match engine.create_new_backup_flush(&self.rocks, true) { if let Err(e) = engine.create_new_backup_flush(&self.rocks, true) {
Err(e) => return Err(Box::new(e)), return Err(Box::new(e));
Ok(()) => { }
let engine_info = engine.get_backup_info(); let engine_info = engine.get_backup_info();
let info = &engine_info.last().unwrap(); let info = &engine_info.last().unwrap();
info!( info!(
@ -263,8 +264,6 @@ impl KeyValueDatabaseEngine for Arc<Engine> {
info.backup_id, info.size, info.num_files, info.backup_id, info.size, info.num_files,
); );
Ok(()) Ok(())
},
}
} else { } else {
Ok(()) Ok(())
}; };

View file

@ -43,7 +43,7 @@ impl<T> Drop for NonAliasingBox<T> {
} }
} }
pub struct Engine { pub(crate) struct Engine {
writer: Mutex<Connection>, writer: Mutex<Connection>,
read_conn_tls: ThreadLocal<Connection>, read_conn_tls: ThreadLocal<Connection>,
read_iterator_conn_tls: ThreadLocal<Connection>, read_iterator_conn_tls: ThreadLocal<Connection>,

View file

@ -52,12 +52,11 @@ impl service::rooms::search::Data for KeyValueDatabase {
.map(move |(key, _)| key[prefix3.len()..].to_vec()) .map(move |(key, _)| key[prefix3.len()..].to_vec())
}); });
let common_elements = match utils::common_elements(iterators, |a, b| { let Some(common_elements) = utils::common_elements(iterators, |a, b| {
// We compare b with a because we reversed the iterator earlier // We compare b with a because we reversed the iterator earlier
b.cmp(a) b.cmp(a)
}) { }) else {
Some(it) => it, return Ok(None);
None => return Ok(None),
}; };
Ok(Some((Box::new(common_elements), words))) Ok(Some((Box::new(common_elements), words)))

View file

@ -11,18 +11,15 @@ impl service::rooms::short::Data for KeyValueDatabase {
return Ok(*short); return Ok(*short);
} }
let short = match self.eventid_shorteventid.get(event_id.as_bytes())? { let short = if let Some(shorteventid) = self.eventid_shorteventid.get(event_id.as_bytes())? {
Some(shorteventid) => {
utils::u64_from_bytes(&shorteventid).map_err(|_| Error::bad_database("Invalid shorteventid in db."))? utils::u64_from_bytes(&shorteventid).map_err(|_| Error::bad_database("Invalid shorteventid in db."))?
}, } else {
None => {
let shorteventid = services().globals.next_count()?; let shorteventid = services().globals.next_count()?;
self.eventid_shorteventid self.eventid_shorteventid
.insert(event_id.as_bytes(), &shorteventid.to_be_bytes())?; .insert(event_id.as_bytes(), &shorteventid.to_be_bytes())?;
self.shorteventid_eventid self.shorteventid_eventid
.insert(&shorteventid.to_be_bytes(), event_id.as_bytes())?; .insert(&shorteventid.to_be_bytes(), event_id.as_bytes())?;
shorteventid shorteventid
},
}; };
self.eventidshort_cache self.eventidshort_cache
@ -79,17 +76,15 @@ impl service::rooms::short::Data for KeyValueDatabase {
statekey_vec.push(0xFF); statekey_vec.push(0xFF);
statekey_vec.extend_from_slice(state_key.as_bytes()); statekey_vec.extend_from_slice(state_key.as_bytes());
let short = match self.statekey_shortstatekey.get(&statekey_vec)? { let short = if let Some(shortstatekey) = self.statekey_shortstatekey.get(&statekey_vec)? {
Some(shortstatekey) => utils::u64_from_bytes(&shortstatekey) utils::u64_from_bytes(&shortstatekey).map_err(|_| Error::bad_database("Invalid shortstatekey in db."))?
.map_err(|_| Error::bad_database("Invalid shortstatekey in db."))?, } else {
None => {
let shortstatekey = services().globals.next_count()?; let shortstatekey = services().globals.next_count()?;
self.statekey_shortstatekey self.statekey_shortstatekey
.insert(&statekey_vec, &shortstatekey.to_be_bytes())?; .insert(&statekey_vec, &shortstatekey.to_be_bytes())?;
self.shortstatekey_statekey self.shortstatekey_statekey
.insert(&shortstatekey.to_be_bytes(), &statekey_vec)?; .insert(&shortstatekey.to_be_bytes(), &statekey_vec)?;
shortstatekey shortstatekey
},
}; };
self.statekeyshort_cache self.statekeyshort_cache
@ -170,18 +165,17 @@ impl service::rooms::short::Data for KeyValueDatabase {
/// Returns (shortstatehash, already_existed) /// Returns (shortstatehash, already_existed)
fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> Result<(u64, bool)> { fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> Result<(u64, bool)> {
Ok(match self.statehash_shortstatehash.get(state_hash)? { Ok(if let Some(shortstatehash) = self.statehash_shortstatehash.get(state_hash)? {
Some(shortstatehash) => ( (
utils::u64_from_bytes(&shortstatehash) utils::u64_from_bytes(&shortstatehash)
.map_err(|_| Error::bad_database("Invalid shortstatehash in db."))?, .map_err(|_| Error::bad_database("Invalid shortstatehash in db."))?,
true, true,
), )
None => { } else {
let shortstatehash = services().globals.next_count()?; let shortstatehash = services().globals.next_count()?;
self.statehash_shortstatehash self.statehash_shortstatehash
.insert(state_hash, &shortstatehash.to_be_bytes())?; .insert(state_hash, &shortstatehash.to_be_bytes())?;
(shortstatehash, false) (shortstatehash, false)
},
}) })
} }
@ -193,16 +187,13 @@ impl service::rooms::short::Data for KeyValueDatabase {
} }
fn get_or_create_shortroomid(&self, room_id: &RoomId) -> Result<u64> { fn get_or_create_shortroomid(&self, room_id: &RoomId) -> Result<u64> {
Ok(match self.roomid_shortroomid.get(room_id.as_bytes())? { Ok(if let Some(short) = self.roomid_shortroomid.get(room_id.as_bytes())? {
Some(short) => {
utils::u64_from_bytes(&short).map_err(|_| Error::bad_database("Invalid shortroomid in db."))? utils::u64_from_bytes(&short).map_err(|_| Error::bad_database("Invalid shortroomid in db."))?
}, } else {
None => {
let short = services().globals.next_count()?; let short = services().globals.next_count()?;
self.roomid_shortroomid self.roomid_shortroomid
.insert(room_id.as_bytes(), &short.to_be_bytes())?; .insert(room_id.as_bytes(), &short.to_be_bytes())?;
short short
},
}) })
} }
} }

View file

@ -77,13 +77,12 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
fn state_get_id( fn state_get_id(
&self, shortstatehash: u64, event_type: &StateEventType, state_key: &str, &self, shortstatehash: u64, event_type: &StateEventType, state_key: &str,
) -> Result<Option<Arc<EventId>>> { ) -> Result<Option<Arc<EventId>>> {
let shortstatekey = match services() let Some(shortstatekey) = services()
.rooms .rooms
.short .short
.get_shortstatekey(event_type, state_key)? .get_shortstatekey(event_type, state_key)?
{ else {
Some(s) => s, return Ok(None);
None => return Ok(None),
}; };
let full_state = services() let full_state = services()
.rooms .rooms

View file

@ -119,9 +119,9 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
using get_alias_helper to fetch room ID remotely" using get_alias_helper to fetch room ID remotely"
); );
let room_id = match services().rooms.alias.resolve_local_alias(&room_alias)? { let room_id = if let Some(room_id) = services().rooms.alias.resolve_local_alias(&room_alias)? {
Some(room_id) => room_id, room_id
None => { } else {
debug!( debug!(
"We don't have this room alias to a room ID locally, attempting to fetch room ID over \ "We don't have this room alias to a room ID locally, attempting to fetch room ID over \
federation" federation"
@ -138,7 +138,6 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
))); )));
}, },
} }
},
}; };
services().rooms.metadata.ban_room(&room_id, true)?; services().rooms.metadata.ban_room(&room_id, true)?;
@ -352,14 +351,12 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
if disable_federation { if disable_federation {
return Ok(RoomMessageEventContent::text_plain(format!( return Ok(RoomMessageEventContent::text_plain(format!(
"Finished bulk room ban, banned {} total rooms, evicted all users, and disabled incoming \ "Finished bulk room ban, banned {room_ban_count} total rooms, evicted all users, and disabled \
federation with the room.", incoming federation with the room."
room_ban_count
))); )));
} }
return Ok(RoomMessageEventContent::text_plain(format!( return Ok(RoomMessageEventContent::text_plain(format!(
"Finished bulk room ban, banned {} total rooms and evicted all users.", "Finished bulk room ban, banned {room_ban_count} total rooms and evicted all users."
room_ban_count
))); )));
} }
@ -403,9 +400,9 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
using get_alias_helper to fetch room ID remotely" using get_alias_helper to fetch room ID remotely"
); );
let room_id = match services().rooms.alias.resolve_local_alias(&room_alias)? { let room_id = if let Some(room_id) = services().rooms.alias.resolve_local_alias(&room_alias)? {
Some(room_id) => room_id, room_id
None => { } else {
debug!( debug!(
"We don't have this room alias to a room ID locally, attempting to fetch room ID over \ "We don't have this room alias to a room ID locally, attempting to fetch room ID over \
federation" federation"
@ -422,7 +419,6 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
))); )));
}, },
} }
},
}; };
services().rooms.metadata.ban_room(&room_id, false)?; services().rooms.metadata.ban_room(&room_id, false)?;

View file

@ -214,13 +214,10 @@ impl Service {
}) })
.transpose()?; .transpose()?;
let mxc_s = match mxc { let Some(mxc_s) = mxc else {
Some(mxc) => mxc,
None => {
return Err(Error::bad_database( return Err(Error::bad_database(
"Parsed MXC URL unicode bytes from database but still is None", "Parsed MXC URL unicode bytes from database but still is None",
)); ));
},
}; };
debug!("Parsed MXC key to URL: {}", mxc_s); debug!("Parsed MXC key to URL: {}", mxc_s);

View file

@ -314,9 +314,8 @@ impl Service {
Ok(ruma::signatures::Verified::Signatures) => { Ok(ruma::signatures::Verified::Signatures) => {
// Redact // Redact
warn!("Calculated hash does not match: {}", event_id); warn!("Calculated hash does not match: {}", event_id);
let obj = match ruma::canonical_json::redact(value, room_version_id, None) { let Ok(obj) = ruma::canonical_json::redact(value, room_version_id, None) else {
Ok(obj) => obj, return Err(Error::BadRequest(ErrorKind::InvalidParam, "Redaction failed"));
Err(_) => return Err(Error::BadRequest(ErrorKind::InvalidParam, "Redaction failed")),
}; };
// Skip the PDU if it is redacted and we already have it as an outlier event // Skip the PDU if it is redacted and we already have it as an outlier event
@ -373,12 +372,9 @@ impl Service {
// Build map of auth events // Build map of auth events
let mut auth_events = HashMap::new(); let mut auth_events = HashMap::new();
for id in &incoming_pdu.auth_events { for id in &incoming_pdu.auth_events {
let auth_event = match services().rooms.timeline.get_pdu(id)? { let Some(auth_event) = services().rooms.timeline.get_pdu(id)? else {
Some(e) => e,
None => {
warn!("Could not find auth event {}", id); warn!("Could not find auth event {}", id);
continue; continue;
},
}; };
self.check_room_id(room_id, &auth_event)?; self.check_room_id(room_id, &auth_event)?;
@ -525,20 +521,16 @@ impl Service {
let mut okay = true; let mut okay = true;
for prev_eventid in &incoming_pdu.prev_events { for prev_eventid in &incoming_pdu.prev_events {
let prev_event = if let Ok(Some(pdu)) = services().rooms.timeline.get_pdu(prev_eventid) { let Ok(Some(prev_event)) = services().rooms.timeline.get_pdu(prev_eventid) else {
pdu
} else {
okay = false; okay = false;
break; break;
}; };
let sstatehash = if let Ok(Some(s)) = services() let Ok(Some(sstatehash)) = services()
.rooms .rooms
.state_accessor .state_accessor
.pdu_shortstatehash(prev_eventid) .pdu_shortstatehash(prev_eventid)
{ else {
s
} else {
okay = false; okay = false;
break; break;
}; };
@ -1072,13 +1064,11 @@ impl Service {
{ {
Ok(res) => { Ok(res) => {
info!("Got {} over federation", next_id); info!("Got {} over federation", next_id);
let (calculated_event_id, value) = let Ok((calculated_event_id, value)) =
match pdu::gen_event_id_canonical_json(&res.pdu, room_version_id) { pdu::gen_event_id_canonical_json(&res.pdu, room_version_id)
Ok(t) => t, else {
Err(_) => {
back_off((*next_id).to_owned()).await; back_off((*next_id).to_owned()).await;
continue; continue;
},
}; };
if calculated_event_id != *next_id { if calculated_event_id != *next_id {
@ -1616,20 +1606,17 @@ impl Service {
/// Returns Ok if the acl allows the server /// Returns Ok if the acl allows the server
pub fn acl_check(&self, server_name: &ServerName, room_id: &RoomId) -> Result<()> { pub fn acl_check(&self, server_name: &ServerName, room_id: &RoomId) -> Result<()> {
let acl_event = let acl_event = if let Some(acl) =
match services() services()
.rooms .rooms
.state_accessor .state_accessor
.room_state_get(room_id, &StateEventType::RoomServerAcl, "")? .room_state_get(room_id, &StateEventType::RoomServerAcl, "")?
{ {
Some(acl) => {
debug!("ACL event found: {acl:?}"); debug!("ACL event found: {acl:?}");
acl acl
}, } else {
None => { debug!("No ACL event found");
info!("No ACL event found");
return Ok(()); return Ok(());
},
}; };
let acl_event_content: RoomServerAclEventContent = match serde_json::from_str(acl_event.content.get()) { let acl_event_content: RoomServerAclEventContent = match serde_json::from_str(acl_event.content.get()) {

View file

@ -90,7 +90,7 @@ impl Arena {
/// Returns the first untraversed node, marking it as traversed in the /// Returns the first untraversed node, marking it as traversed in the
/// process /// process
pub fn first_untraversed(&mut self) -> Option<NodeId> { fn first_untraversed(&mut self) -> Option<NodeId> {
if self.nodes.is_empty() { if self.nodes.is_empty() {
None None
} else if let Some(untraversed) = self.first_untraversed { } else if let Some(untraversed) = self.first_untraversed {
@ -133,7 +133,7 @@ impl Arena {
} }
/// Adds all the given nodes as children of the parent node /// Adds all the given nodes as children of the parent node
pub fn push(&mut self, parent: NodeId, mut children: Vec<OwnedRoomId>) { fn push(&mut self, parent: NodeId, mut children: Vec<OwnedRoomId>) {
if children.is_empty() { if children.is_empty() {
self.traverse(parent); self.traverse(parent);
} else if self.nodes.get(parent.index).is_some() { } else if self.nodes.get(parent.index).is_some() {
@ -204,7 +204,7 @@ impl Arena {
} }
} }
pub fn new(root: OwnedRoomId, max_depth: usize) -> Self { fn new(root: OwnedRoomId, max_depth: usize) -> Self {
let zero_depth = max_depth == 0; let zero_depth = max_depth == 0;
Arena { Arena {
@ -229,11 +229,11 @@ impl Arena {
// Note: perhaps use some better form of token rather than just room count // Note: perhaps use some better form of token rather than just room count
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct PagnationToken { pub(crate) struct PagnationToken {
pub skip: UInt, pub(crate) skip: UInt,
pub limit: UInt, pub(crate) limit: UInt,
pub max_depth: UInt, pub(crate) max_depth: UInt,
pub suggested_only: bool, pub(crate) suggested_only: bool,
} }
impl FromStr for PagnationToken { impl FromStr for PagnationToken {

View file

@ -4,7 +4,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
pub use data::Data; pub(crate) use data::Data;
use ruma::{ use ruma::{
api::client::error::ErrorKind, api::client::error::ErrorKind,
events::{ events::{
@ -43,9 +43,8 @@ impl Service {
.ok() .ok()
.map(|(_, id)| id) .map(|(_, id)| id)
}) { }) {
let pdu = match services().rooms.timeline.get_pdu_json(&event_id)? { let Some(pdu) = services().rooms.timeline.get_pdu_json(&event_id)? else {
Some(pdu) => pdu, continue;
None => continue,
}; };
let pdu: PduEvent = match serde_json::from_str( let pdu: PduEvent = match serde_json::from_str(
@ -57,19 +56,16 @@ impl Service {
match pdu.kind { match pdu.kind {
TimelineEventType::RoomMember => { TimelineEventType::RoomMember => {
let membership_event = match serde_json::from_str::<RoomMemberEventContent>(pdu.content.get()) { let Ok(membership_event) = serde_json::from_str::<RoomMemberEventContent>(pdu.content.get()) else {
Ok(e) => e, continue;
Err(_) => continue,
}; };
let state_key = match pdu.state_key { let Some(state_key) = pdu.state_key else {
Some(k) => k, continue;
None => continue,
}; };
let user_id = match UserId::parse(state_key) { let Ok(user_id) = UserId::parse(state_key) else {
Ok(id) => id, continue;
Err(_) => continue,
}; };
services() services()
@ -355,10 +351,7 @@ impl Service {
&self, room_id: &RoomId, kind: &TimelineEventType, sender: &UserId, state_key: Option<&str>, &self, room_id: &RoomId, kind: &TimelineEventType, sender: &UserId, state_key: Option<&str>,
content: &serde_json::value::RawValue, content: &serde_json::value::RawValue,
) -> Result<StateMap<Arc<PduEvent>>> { ) -> Result<StateMap<Arc<PduEvent>>> {
let shortstatehash = let Some(shortstatehash) = services().rooms.state.get_room_shortstatehash(room_id)? else {
if let Some(current_shortstatehash) = services().rooms.state.get_room_shortstatehash(room_id)? {
current_shortstatehash
} else {
return Ok(HashMap::new()); return Ok(HashMap::new());
}; };

View file

@ -143,9 +143,8 @@ impl Service {
/// the room's history_visibility at that event's state. /// the room's history_visibility at that event's state.
#[tracing::instrument(skip(self, user_id, room_id, event_id))] #[tracing::instrument(skip(self, user_id, room_id, event_id))]
pub fn user_can_see_event(&self, user_id: &UserId, room_id: &RoomId, event_id: &EventId) -> Result<bool> { pub fn user_can_see_event(&self, user_id: &UserId, room_id: &RoomId, event_id: &EventId) -> Result<bool> {
let shortstatehash = match self.pdu_shortstatehash(event_id)? { let Some(shortstatehash) = self.pdu_shortstatehash(event_id)? else {
Some(shortstatehash) => shortstatehash, return Ok(true);
None => return Ok(true),
}; };
if let Some(visibility) = self if let Some(visibility) = self

View file

@ -473,7 +473,7 @@ impl Service {
} }
/// Ensure that a user only sees signatures from themselves and the target user /// Ensure that a user only sees signatures from themselves and the target user
pub fn clean_signatures<F: Fn(&UserId) -> bool>( pub(crate) fn clean_signatures<F: Fn(&UserId) -> bool>(
cross_signing_key: &mut serde_json::Value, sender_user: Option<&UserId>, user_id: &UserId, allowed_signatures: F, cross_signing_key: &mut serde_json::Value, sender_user: Option<&UserId>, user_id: &UserId, allowed_signatures: F,
) -> Result<(), Error> { ) -> Result<(), Error> {
if let Some(signatures) = cross_signing_key if let Some(signatures) = cross_signing_key