Simplify identifier parsing code

This commit is contained in:
Jonas Platte 2021-11-27 00:30:28 +01:00
parent 41fef1da64
commit bffddbd487
No known key found for this signature in database
GPG key ID: CC154DE0E30B7C67
15 changed files with 147 additions and 179 deletions

View file

@ -1,10 +1,10 @@
use std::{convert::TryFrom, sync::Arc};
use std::{convert::TryInto, sync::Arc};
use crate::{pdu::PduBuilder, Database};
use rocket::futures::{channel::mpsc, stream::StreamExt};
use ruma::{
events::{room::message::RoomMessageEventContent, EventType},
RoomAliasId, UserId,
UserId,
};
use serde_json::value::to_raw_value;
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
@ -33,18 +33,16 @@ impl Admin {
let guard = db.read().await;
let conduit_user =
Box::<UserId>::try_from(format!("@conduit:{}", guard.globals.server_name()))
.expect("@conduit:server_name is valid");
let conduit_user = UserId::parse(format!("@conduit:{}", guard.globals.server_name()))
.expect("@conduit:server_name is valid");
let conduit_room = guard
.rooms
.id_from_alias(
&Box::<RoomAliasId>::try_from(format!(
"#admins:{}",
guard.globals.server_name()
))
.expect("#admins:server_name is a valid room alias"),
format!("#admins:{}", guard.globals.server_name())
.as_str()
.try_into()
.expect("#admins:server_name is a valid room alias"),
)
.unwrap();

View file

@ -6,7 +6,7 @@ use ruma::{
},
RoomId, UserId,
};
use std::{collections::BTreeMap, convert::TryFrom, sync::Arc};
use std::{collections::BTreeMap, sync::Arc};
use super::abstraction::Tree;
@ -231,7 +231,7 @@ impl KeyBackups {
Error::bad_database("backupkeyid_backup session_id is invalid.")
})?;
let room_id = Box::<RoomId>::try_from(
let room_id = RoomId::parse(
utils::string_from_bytes(parts.next().ok_or_else(|| {
Error::bad_database("backupkeyid_backup key is invalid.")
})?)

View file

@ -434,7 +434,7 @@ impl Rooms {
None => continue,
};
let user_id = match Box::<UserId>::try_from(state_key) {
let user_id = match UserId::parse(state_key) {
Ok(id) => id,
Err(_) => continue,
};
@ -871,12 +871,10 @@ impl Rooms {
.get(&shorteventid.to_be_bytes())?
.ok_or_else(|| Error::bad_database("Shorteventid does not exist"))?;
let event_id = Arc::from(
Box::<EventId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("EventID in shorteventid_eventid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("EventId in shorteventid_eventid is invalid."))?,
);
let event_id = EventId::parse_arc(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("EventID in shorteventid_eventid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("EventId in shorteventid_eventid is invalid."))?;
self.shorteventid_cache
.lock()
@ -1169,7 +1167,7 @@ impl Rooms {
self.roomid_pduleaves
.scan_prefix(prefix)
.map(|(_, bytes)| {
Box::<EventId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
EventId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("EventID in roomid_pduleaves is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("EventId in roomid_pduleaves is invalid."))
@ -1420,7 +1418,7 @@ impl Rooms {
}
// if the state_key fails
let target_user_id = Box::<UserId>::try_from(state_key.clone())
let target_user_id = UserId::parse(state_key.clone())
.expect("This state_key was previously validated");
let content = serde_json::from_str::<ExtractMembership>(pdu.content.get())
@ -1476,10 +1474,9 @@ impl Rooms {
if body.starts_with(&format!("@conduit:{}: ", db.globals.server_name()))
&& self
.id_from_alias(
&Box::<RoomAliasId>::try_from(format!(
"#admins:{}",
db.globals.server_name()
))
<&RoomAliasId>::try_from(
format!("#admins:{}", db.globals.server_name()).as_str(),
)
.expect("#admins:server_name is a valid room alias"),
)?
.as_ref()
@ -1530,7 +1527,7 @@ impl Rooms {
}
"get_auth_chain" => {
if args.len() == 1 {
if let Ok(event_id) = Box::<EventId>::try_from(args[0]) {
if let Ok(event_id) = EventId::parse_arc(args[0]) {
if let Some(event) = db.rooms.get_pdu_json(&event_id)? {
let room_id_str = event
.get("room_id")
@ -1541,12 +1538,12 @@ impl Rooms {
)
})?;
let room_id = Box::<RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
let room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
let start = Instant::now();
let count = server_server::get_auth_chain(
&room_id,
vec![Arc::from(event_id)],
room_id,
vec![event_id],
db,
)?
.count();
@ -1569,7 +1566,7 @@ impl Rooms {
let string = body[1..body.len() - 1].join("\n");
match serde_json::from_str(&string) {
Ok(value) => {
let event_id = Box::<EventId>::try_from(&*format!(
let event_id = EventId::parse(format!(
"${}",
// Anything higher than version3 behaves the same
ruma::signatures::reference_hash(
@ -1624,7 +1621,7 @@ impl Rooms {
}
"get_pdu" => {
if args.len() == 1 {
if let Ok(event_id) = Box::<EventId>::try_from(args[0]) {
if let Ok(event_id) = EventId::parse(args[0]) {
let mut outlier = false;
let mut pdu_json =
db.rooms.get_non_outlier_pdu_json(&event_id)?;
@ -2083,7 +2080,7 @@ impl Rooms {
.expect("event is valid, we just created it");
// Generate event id
pdu.event_id = Box::<EventId>::try_from(&*format!(
pdu.event_id = EventId::parse(format!(
"${}",
ruma::signatures::reference_hash(&pdu_json, &room_version_id)
.expect("ruma can calculate reference hashes")
@ -2758,7 +2755,7 @@ impl Rooms {
.filter_map(|event| serde_json::from_str(event.json().get()).ok())
.filter_map(|event: serde_json::Value| event.get("sender").cloned())
.filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
.filter_map(|sender| Box::<UserId>::try_from(sender).ok())
.filter_map(|sender| UserId::parse(sender).ok())
.map(|user| user.server_name().to_owned())
.collect();
@ -2819,7 +2816,7 @@ impl Rooms {
.expect("event is valid, we just created it");
// Generate event id
let event_id = Box::<EventId>::try_from(&*format!(
let event_id = EventId::parse(format!(
"${}",
ruma::signatures::reference_hash(&leave_event_stub, &room_version_id)
.expect("ruma can calculate reference hashes")
@ -2908,7 +2905,7 @@ impl Rooms {
self.alias_roomid
.get(alias.alias().as_bytes())?
.map(|bytes| {
Box::<RoomId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
RoomId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Room ID in alias_roomid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("Room ID in alias_roomid is invalid."))
@ -2951,7 +2948,7 @@ impl Rooms {
#[tracing::instrument(skip(self))]
pub fn public_rooms(&self) -> impl Iterator<Item = Result<Box<RoomId>>> + '_ {
self.publicroomids.iter().map(|(bytes, _)| {
Box::<RoomId>::try_from(
RoomId::parse(
utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Room ID in publicroomids is invalid unicode.")
})?,
@ -3039,7 +3036,7 @@ impl Rooms {
Ok(utils::common_elements(iterators, Ord::cmp)
.expect("users is not empty")
.map(|bytes| {
Box::<RoomId>::try_from(utils::string_from_bytes(&*bytes).map_err(|_| {
RoomId::parse(utils::string_from_bytes(&*bytes).map_err(|_| {
Error::bad_database("Invalid RoomId bytes in userroomid_joined")
})?)
.map_err(|_| Error::bad_database("Invalid RoomId in userroomid_joined."))
@ -3056,7 +3053,7 @@ impl Rooms {
prefix.push(0xff);
self.roomserverids.scan_prefix(prefix).map(|(key, _)| {
Box::<ServerName>::try_from(
ServerName::parse(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
@ -3089,7 +3086,7 @@ impl Rooms {
prefix.push(0xff);
self.serverroomids.scan_prefix(prefix).map(|(key, _)| {
Box::<RoomId>::try_from(
RoomId::parse(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
@ -3111,7 +3108,7 @@ impl Rooms {
prefix.push(0xff);
self.roomuserid_joined.scan_prefix(prefix).map(|(key, _)| {
Box::<UserId>::try_from(
UserId::parse(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
@ -3159,7 +3156,7 @@ impl Rooms {
self.roomuseroncejoinedids
.scan_prefix(prefix)
.map(|(key, _)| {
Box::<UserId>::try_from(
UserId::parse(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
@ -3185,7 +3182,7 @@ impl Rooms {
self.roomuserid_invitecount
.scan_prefix(prefix)
.map(|(key, _)| {
Box::<UserId>::try_from(
UserId::parse(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
@ -3238,7 +3235,7 @@ impl Rooms {
self.userroomid_joined
.scan_prefix(user_id.as_bytes().to_vec())
.map(|(key, _)| {
Box::<RoomId>::try_from(
RoomId::parse(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
@ -3264,7 +3261,7 @@ impl Rooms {
self.userroomid_invitestate
.scan_prefix(prefix)
.map(|(key, state)| {
let room_id = Box::<RoomId>::try_from(
let room_id = RoomId::parse(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()
@ -3337,7 +3334,7 @@ impl Rooms {
self.userroomid_leftstate
.scan_prefix(prefix)
.map(|(key, state)| {
let room_id = Box::<RoomId>::try_from(
let room_id = RoomId::parse(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()

View file

@ -11,7 +11,7 @@ use ruma::{
};
use std::{
collections::{HashMap, HashSet},
convert::{TryFrom, TryInto},
convert::TryInto,
mem,
sync::Arc,
};
@ -97,7 +97,7 @@ impl RoomEdus {
let count =
utils::u64_from_bytes(&k[prefix.len()..prefix.len() + mem::size_of::<u64>()])
.map_err(|_| Error::bad_database("Invalid readreceiptid count in db."))?;
let user_id = Box::<UserId>::try_from(
let user_id = UserId::parse(
utils::string_from_bytes(&k[prefix.len() + mem::size_of::<u64>() + 1..])
.map_err(|_| {
Error::bad_database("Invalid readreceiptid userid bytes in db.")
@ -310,17 +310,13 @@ impl RoomEdus {
let mut user_ids = HashSet::new();
for user_id in self
.typingid_userid
.scan_prefix(prefix)
.map(|(_, user_id)| {
Box::<UserId>::try_from(utils::string_from_bytes(&user_id).map_err(|_| {
Error::bad_database("User ID in typingid_userid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("User ID in typingid_userid is invalid."))
})
{
user_ids.insert(user_id?);
for (_, user_id) in self.typingid_userid.scan_prefix(prefix) {
let user_id = UserId::parse(utils::string_from_bytes(&user_id).map_err(|_| {
Error::bad_database("User ID in typingid_userid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("User ID in typingid_userid is invalid."))?;
user_ids.insert(user_id);
}
Ok(SyncEphemeralRoomEvent {
@ -518,7 +514,7 @@ impl RoomEdus {
.iter_from(&*first_possible_edu, false)
.take_while(|(key, _)| key.starts_with(&prefix))
{
let user_id = Box::<UserId>::try_from(
let user_id = UserId::parse(
utils::string_from_bytes(
key.rsplit(|&b| b == 0xff)
.next()

View file

@ -1,6 +1,6 @@
use std::{
collections::{BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto},
convert::TryInto,
fmt::Debug,
sync::Arc,
time::{Duration, Instant},
@ -583,19 +583,18 @@ impl Sending {
}
}
let userid =
Box::<UserId>::try_from(utils::string_from_bytes(user).map_err(|_| {
(
kind.clone(),
Error::bad_database("Invalid push user string in db."),
)
})?)
.map_err(|_| {
(
kind.clone(),
Error::bad_database("Invalid push user id in db."),
)
})?;
let userid = UserId::parse(utils::string_from_bytes(user).map_err(|_| {
(
kind.clone(),
Error::bad_database("Invalid push user string in db."),
)
})?)
.map_err(|_| {
(
kind.clone(),
Error::bad_database("Invalid push user id in db."),
)
})?;
let mut senderkey = user.clone();
senderkey.push(0xff);
@ -732,7 +731,7 @@ impl Sending {
})?;
(
OutgoingKind::Appservice(Box::<ServerName>::try_from(server).map_err(|_| {
OutgoingKind::Appservice(ServerName::parse(server).map_err(|_| {
Error::bad_database("Invalid server string in server_currenttransaction")
})?),
if value.is_empty() {
@ -771,7 +770,7 @@ impl Sending {
})?;
(
OutgoingKind::Normal(Box::<ServerName>::try_from(server).map_err(|_| {
OutgoingKind::Normal(ServerName::parse(server).map_err(|_| {
Error::bad_database("Invalid server string in server_currenttransaction")
})?),
if value.is_empty() {

View file

@ -8,7 +8,7 @@ use ruma::{
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, RoomAliasId, UInt,
UserId,
};
use std::{collections::BTreeMap, convert::TryFrom, mem, sync::Arc};
use std::{collections::BTreeMap, convert::TryInto, mem, sync::Arc};
use tracing::warn;
use super::abstraction::Tree;
@ -62,9 +62,8 @@ impl Users {
rooms: &super::rooms::Rooms,
globals: &super::globals::Globals,
) -> Result<bool> {
let admin_room_alias_id =
Box::<RoomAliasId>::try_from(format!("#admins:{}", globals.server_name()))
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
let admin_room_alias_id = RoomAliasId::parse(format!("#admins:{}", globals.server_name()))
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
let admin_room_id = rooms.id_from_alias(&admin_room_alias_id)?.unwrap();
rooms.is_joined(user_id, &admin_room_id)
@ -98,11 +97,9 @@ impl Users {
})?;
Ok(Some((
Box::<UserId>::try_from(utils::string_from_bytes(user_bytes).map_err(
|_| {
Error::bad_database("User ID in token_userdeviceid is invalid unicode.")
},
)?)
UserId::parse(utils::string_from_bytes(user_bytes).map_err(|_| {
Error::bad_database("User ID in token_userdeviceid is invalid unicode.")
})?)
.map_err(|_| {
Error::bad_database("User ID in token_userdeviceid is invalid.")
})?,
@ -117,7 +114,7 @@ impl Users {
#[tracing::instrument(skip(self))]
pub fn iter(&self) -> impl Iterator<Item = Result<Box<UserId>>> + '_ {
self.userid_password.iter().map(|(bytes, _)| {
Box::<UserId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
UserId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("User ID in userid_password is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("User ID in userid_password is invalid."))
@ -189,7 +186,7 @@ impl Users {
.map(|bytes| {
let s = utils::string_from_bytes(&bytes)
.map_err(|_| Error::bad_database("Avatar URL in db is invalid."))?;
Box::<MxcUri>::try_from(s)
s.try_into()
.map_err(|_| Error::bad_database("Avatar URL in db is invalid."))
})
.transpose()
@ -686,7 +683,7 @@ impl Users {
}
})
.map(|(_, bytes)| {
Box::<UserId>::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
UserId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("User ID in devicekeychangeid_userid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("User ID in devicekeychangeid_userid is invalid."))