Simplify identifier parsing code
This commit is contained in:
parent
41fef1da64
commit
bffddbd487
15 changed files with 147 additions and 179 deletions
|
@ -1,8 +1,4 @@
|
|||
use std::{
|
||||
collections::BTreeMap,
|
||||
convert::{TryFrom, TryInto},
|
||||
sync::Arc,
|
||||
};
|
||||
use std::{collections::BTreeMap, convert::TryInto, sync::Arc};
|
||||
|
||||
use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH};
|
||||
use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma};
|
||||
|
@ -396,8 +392,7 @@ pub async fn register_route(
|
|||
)?;
|
||||
|
||||
// 6. Events implied by name and topic
|
||||
let room_name =
|
||||
Box::<RoomName>::try_from(format!("{} Admin Room", db.globals.server_name()))
|
||||
let room_name = RoomName::parse(format!("{} Admin Room", db.globals.server_name()))
|
||||
.expect("Room name is valid");
|
||||
db.rooms.build_and_append_pdu(
|
||||
PduBuilder {
|
||||
|
|
|
@ -64,7 +64,7 @@ pub async fn join_room_by_id_route(
|
|||
.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();
|
||||
|
||||
|
@ -92,16 +92,17 @@ pub async fn join_room_by_id_route(
|
|||
/// - If the server does not know about the room: asks other servers over federation
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
post("/_matrix/client/r0/join/<_>", data = "<body>")
|
||||
post("/_matrix/client/r0/join/<_>", data = "<req>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
#[tracing::instrument(skip(db, req))]
|
||||
pub async fn join_room_by_id_or_alias_route(
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<join_room_by_id_or_alias::Request<'_>>,
|
||||
req: Ruma<join_room_by_id_or_alias::Request<'_>>,
|
||||
) -> ConduitResult<join_room_by_id_or_alias::Response> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
let body = req.body;
|
||||
let sender_user = req.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let (servers, room_id) = match Box::<RoomId>::try_from(body.room_id_or_alias.clone()) {
|
||||
let (servers, room_id) = match Box::<RoomId>::try_from(body.room_id_or_alias) {
|
||||
Ok(room_id) => {
|
||||
let mut servers: HashSet<_> = db
|
||||
.rooms
|
||||
|
@ -111,7 +112,7 @@ pub async fn join_room_by_id_or_alias_route(
|
|||
.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();
|
||||
|
||||
|
@ -127,7 +128,7 @@ pub async fn join_room_by_id_or_alias_route(
|
|||
|
||||
let join_room_response = join_room_by_id_helper(
|
||||
&db,
|
||||
body.sender_user.as_deref(),
|
||||
req.sender_user.as_deref(),
|
||||
&room_id,
|
||||
&servers,
|
||||
body.third_party_signed.as_ref(),
|
||||
|
@ -619,11 +620,12 @@ async fn join_room_by_id_helper(
|
|||
.expect("event is valid, we just created it");
|
||||
|
||||
// Generate event id
|
||||
let event_id = Box::<EventId>::try_from(&*format!(
|
||||
let event_id = format!(
|
||||
"${}",
|
||||
ruma::signatures::reference_hash(&join_event_stub, &room_version)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
))
|
||||
);
|
||||
let event_id = <&EventId>::try_from(event_id.as_str())
|
||||
.expect("ruma's reference hashes are valid event ids");
|
||||
|
||||
// Add event_id back
|
||||
|
@ -642,7 +644,7 @@ async fn join_room_by_id_helper(
|
|||
remote_server,
|
||||
federation::membership::create_join_event::v2::Request {
|
||||
room_id,
|
||||
event_id: &event_id,
|
||||
event_id,
|
||||
pdu: &PduEvent::convert_to_outgoing_federation_event(join_event.clone()),
|
||||
},
|
||||
)
|
||||
|
@ -650,7 +652,7 @@ async fn join_room_by_id_helper(
|
|||
|
||||
db.rooms.get_or_create_shortroomid(room_id, &db.globals)?;
|
||||
|
||||
let pdu = PduEvent::from_id_val(&event_id, join_event.clone())
|
||||
let pdu = PduEvent::from_id_val(event_id, join_event.clone())
|
||||
.map_err(|_| Error::BadServerResponse("Invalid join event PDU."))?;
|
||||
|
||||
let mut state = HashMap::new();
|
||||
|
@ -788,7 +790,7 @@ fn validate_and_add_event_id(
|
|||
error!("Invalid PDU in server response: {:?}: {:?}", pdu, e);
|
||||
Error::BadServerResponse("Invalid PDU in server response")
|
||||
})?;
|
||||
let event_id = Box::<EventId>::try_from(&*format!(
|
||||
let event_id = EventId::parse(format!(
|
||||
"${}",
|
||||
ruma::signatures::reference_hash(&value, room_version)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
|
@ -1011,11 +1013,12 @@ pub(crate) async fn invite_helper<'a>(
|
|||
};
|
||||
|
||||
// Generate event id
|
||||
let expected_event_id = Box::<EventId>::try_from(&*format!(
|
||||
let expected_event_id = format!(
|
||||
"${}",
|
||||
ruma::signatures::reference_hash(&pdu_json, &room_version_id)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
))
|
||||
);
|
||||
let expected_event_id = <&EventId>::try_from(expected_event_id.as_str())
|
||||
.expect("ruma's reference hashes are valid event ids");
|
||||
|
||||
let response = db
|
||||
|
@ -1025,7 +1028,7 @@ pub(crate) async fn invite_helper<'a>(
|
|||
user_id.server_name(),
|
||||
create_invite::v2::Request {
|
||||
room_id,
|
||||
event_id: &expected_event_id,
|
||||
event_id: expected_event_id,
|
||||
room_version: &room_version_id,
|
||||
event: &PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()),
|
||||
invite_room_state: &invite_room_state,
|
||||
|
|
|
@ -5,13 +5,8 @@ use ruma::{
|
|||
r0::message::{get_message_events, send_message_event},
|
||||
},
|
||||
events::EventType,
|
||||
EventId,
|
||||
};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
convert::{TryFrom, TryInto},
|
||||
sync::Arc,
|
||||
};
|
||||
use std::{collections::BTreeMap, convert::TryInto, sync::Arc};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
use rocket::{get, put};
|
||||
|
@ -67,10 +62,9 @@ pub async fn send_message_event_route(
|
|||
));
|
||||
}
|
||||
|
||||
let event_id = Box::<EventId>::try_from(
|
||||
utils::string_from_bytes(&response)
|
||||
.map_err(|_| Error::bad_database("Invalid txnid bytes in database."))?,
|
||||
)
|
||||
let event_id = utils::string_from_bytes(&response)
|
||||
.map_err(|_| Error::bad_database("Invalid txnid bytes in database."))?
|
||||
.try_into()
|
||||
.map_err(|_| Error::bad_database("Invalid event id in txnid data."))?;
|
||||
return Ok(send_message_event::Response { event_id }.into());
|
||||
}
|
||||
|
|
|
@ -26,12 +26,7 @@ use ruma::{
|
|||
RoomAliasId, RoomId, RoomVersionId,
|
||||
};
|
||||
use serde_json::{json, value::to_raw_value};
|
||||
use std::{
|
||||
cmp::max,
|
||||
collections::BTreeMap,
|
||||
convert::{TryFrom, TryInto},
|
||||
sync::Arc,
|
||||
};
|
||||
use std::{cmp::max, collections::BTreeMap, convert::TryInto, sync::Arc};
|
||||
use tracing::{info, warn};
|
||||
|
||||
#[cfg(feature = "conduit_bin")]
|
||||
|
@ -93,12 +88,11 @@ pub async fn create_room_route(
|
|||
.as_ref()
|
||||
.map_or(Ok(None), |localpart| {
|
||||
// TODO: Check for invalid characters and maximum length
|
||||
let alias = Box::<RoomAliasId>::try_from(format!(
|
||||
"#{}:{}",
|
||||
localpart,
|
||||
db.globals.server_name(),
|
||||
))
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
|
||||
let alias =
|
||||
RoomAliasId::parse(format!("#{}:{}", localpart, db.globals.server_name()))
|
||||
.map_err(|_| {
|
||||
Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias.")
|
||||
})?;
|
||||
|
||||
if db.rooms.id_from_alias(&alias)?.is_some() {
|
||||
Err(Error::BadRequest(
|
||||
|
|
|
@ -10,7 +10,7 @@ use ruma::{
|
|||
};
|
||||
use std::{
|
||||
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
convert::TryInto,
|
||||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
|
@ -298,8 +298,7 @@ async fn sync_helper(
|
|||
})?;
|
||||
|
||||
if let Some(state_key) = &pdu.state_key {
|
||||
let user_id =
|
||||
Box::<UserId>::try_from(state_key.clone()).map_err(|_| {
|
||||
let user_id = UserId::parse(state_key.clone()).map_err(|_| {
|
||||
Error::bad_database("Invalid UserId in member PDU.")
|
||||
})?;
|
||||
|
||||
|
@ -427,7 +426,7 @@ async fn sync_helper(
|
|||
}
|
||||
|
||||
if let Some(state_key) = &state_event.state_key {
|
||||
let user_id = Box::<UserId>::try_from(state_key.clone())
|
||||
let user_id = UserId::parse(state_key.clone())
|
||||
.map_err(|_| Error::bad_database("Invalid UserId in member PDU."))?;
|
||||
|
||||
if user_id == sender_user {
|
||||
|
|
|
@ -476,11 +476,9 @@ impl Database {
|
|||
if db.globals.database_version()? < 6 {
|
||||
// Set room member count
|
||||
for (roomid, _) in db.rooms.roomid_shortstatehash.iter() {
|
||||
let room_id =
|
||||
Box::<RoomId>::try_from(utils::string_from_bytes(&roomid).unwrap())
|
||||
.unwrap();
|
||||
|
||||
db.rooms.update_joined_count(&room_id, &db)?;
|
||||
let string = utils::string_from_bytes(&roomid).unwrap();
|
||||
let room_id = <&RoomId>::try_from(string.as_str()).unwrap();
|
||||
db.rooms.update_joined_count(room_id, &db)?;
|
||||
}
|
||||
|
||||
db.globals.bump_database_version(6)?;
|
||||
|
@ -587,10 +585,9 @@ impl Database {
|
|||
.get(&seventid)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let event_id =
|
||||
Box::<EventId>::try_from(utils::string_from_bytes(&event_id).unwrap())
|
||||
.unwrap();
|
||||
let pdu = db.rooms.get_pdu(&event_id).unwrap().unwrap();
|
||||
let string = utils::string_from_bytes(&event_id).unwrap();
|
||||
let event_id = <&EventId>::try_from(string.as_str()).unwrap();
|
||||
let pdu = db.rooms.get_pdu(event_id).unwrap().unwrap();
|
||||
|
||||
if Some(&pdu.room_id) != current_room.as_ref() {
|
||||
current_room = Some(pdu.room_id.clone());
|
||||
|
|
|
@ -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,17 +33,15 @@ impl Admin {
|
|||
|
||||
let guard = db.read().await;
|
||||
|
||||
let conduit_user =
|
||||
Box::<UserId>::try_from(format!("@conduit:{}", guard.globals.server_name()))
|
||||
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()
|
||||
))
|
||||
format!("#admins:{}", guard.globals.server_name())
|
||||
.as_str()
|
||||
.try_into()
|
||||
.expect("#admins:server_name is a valid room alias"),
|
||||
)
|
||||
.unwrap();
|
||||
|
|
|
@ -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.")
|
||||
})?)
|
||||
|
|
|
@ -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(|_| {
|
||||
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."))?,
|
||||
);
|
||||
.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)
|
||||
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()
|
||||
|
|
|
@ -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(|_| {
|
||||
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?);
|
||||
.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()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::{
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
convert::{TryFrom, TryInto},
|
||||
convert::TryInto,
|
||||
fmt::Debug,
|
||||
sync::Arc,
|
||||
time::{Duration, Instant},
|
||||
|
@ -583,8 +583,7 @@ impl Sending {
|
|||
}
|
||||
}
|
||||
|
||||
let userid =
|
||||
Box::<UserId>::try_from(utils::string_from_bytes(user).map_err(|_| {
|
||||
let userid = UserId::parse(utils::string_from_bytes(user).map_err(|_| {
|
||||
(
|
||||
kind.clone(),
|
||||
Error::bad_database("Invalid push user string in db."),
|
||||
|
@ -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() {
|
||||
|
|
|
@ -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,8 +62,7 @@ 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()))
|
||||
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();
|
||||
|
||||
|
@ -98,11 +97,9 @@ impl Users {
|
|||
})?;
|
||||
|
||||
Ok(Some((
|
||||
Box::<UserId>::try_from(utils::string_from_bytes(user_bytes).map_err(
|
||||
|_| {
|
||||
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."))
|
||||
|
|
|
@ -13,7 +13,7 @@ use serde_json::{
|
|||
json,
|
||||
value::{to_raw_value, RawValue as RawJsonValue},
|
||||
};
|
||||
use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom, ops::Deref};
|
||||
use std::{cmp::Ordering, collections::BTreeMap, convert::TryInto, ops::Deref};
|
||||
use tracing::warn;
|
||||
|
||||
/// Content hashes of a PDU.
|
||||
|
@ -337,12 +337,13 @@ pub(crate) fn gen_event_id_canonical_json(
|
|||
Error::BadServerResponse("Invalid PDU in server response")
|
||||
})?;
|
||||
|
||||
let event_id = Box::<EventId>::try_from(&*format!(
|
||||
let event_id = format!(
|
||||
"${}",
|
||||
// Anything higher than version3 behaves the same
|
||||
ruma::signatures::reference_hash(&value, &RoomVersionId::V6)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
))
|
||||
)
|
||||
.try_into()
|
||||
.expect("ruma's reference hashes are valid event ids");
|
||||
|
||||
Ok((event_id, value))
|
||||
|
|
|
@ -20,7 +20,6 @@ use {
|
|||
},
|
||||
ruma::api::{AuthScheme, IncomingRequest},
|
||||
std::collections::BTreeMap,
|
||||
std::convert::TryFrom,
|
||||
std::io::Cursor,
|
||||
tracing::{debug, warn},
|
||||
};
|
||||
|
@ -103,8 +102,7 @@ where
|
|||
.unwrap()
|
||||
},
|
||||
|string| {
|
||||
Box::<UserId>::try_from(string.expect("parsing to string always works"))
|
||||
.unwrap()
|
||||
UserId::parse(string.expect("parsing to string always works")).unwrap()
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -171,7 +169,7 @@ where
|
|||
}
|
||||
};
|
||||
|
||||
let origin = match Box::<ServerName>::try_from(origin_str) {
|
||||
let origin = match ServerName::parse(origin_str) {
|
||||
Ok(s) => s,
|
||||
_ => {
|
||||
warn!(
|
||||
|
|
|
@ -544,11 +544,10 @@ pub fn get_server_keys_route(db: DatabaseGuard) -> Json<String> {
|
|||
return Json("Federation is disabled.".to_owned());
|
||||
}
|
||||
|
||||
let mut verify_keys = BTreeMap::new();
|
||||
let mut verify_keys: BTreeMap<Box<ServerSigningKeyId>, VerifyKey> = BTreeMap::new();
|
||||
verify_keys.insert(
|
||||
Box::<ServerSigningKeyId>::try_from(
|
||||
format!("ed25519:{}", db.globals.keypair().version()).as_str(),
|
||||
)
|
||||
format!("ed25519:{}", db.globals.keypair().version())
|
||||
.try_into()
|
||||
.expect("found invalid server signing keys in DB"),
|
||||
VerifyKey {
|
||||
key: base64::encode_config(db.globals.keypair().public_key(), base64::STANDARD_NO_PAD),
|
||||
|
@ -730,7 +729,7 @@ pub async fn send_transaction_message_route(
|
|||
// 0. Check the server is in the room
|
||||
let room_id = match value
|
||||
.get("room_id")
|
||||
.and_then(|id| Box::<RoomId>::try_from(id.as_str()?).ok())
|
||||
.and_then(|id| RoomId::parse(id.as_str()?).ok())
|
||||
{
|
||||
Some(id) => id,
|
||||
None => {
|
||||
|
@ -2354,10 +2353,10 @@ pub fn get_event_route(
|
|||
.and_then(|val| val.as_str())
|
||||
.ok_or_else(|| Error::bad_database("Invalid event in database"))?;
|
||||
|
||||
let room_id = Box::<RoomId>::try_from(room_id_str)
|
||||
let room_id = <&RoomId>::try_from(room_id_str)
|
||||
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
|
||||
|
||||
if !db.rooms.server_in_room(sender_servername, &room_id)? {
|
||||
if !db.rooms.server_in_room(sender_servername, room_id)? {
|
||||
return Err(Error::BadRequest(ErrorKind::NotFound, "Event not found."));
|
||||
}
|
||||
|
||||
|
@ -2408,7 +2407,7 @@ pub fn get_missing_events_route(
|
|||
.and_then(|val| val.as_str())
|
||||
.ok_or_else(|| Error::bad_database("Invalid event in database"))?;
|
||||
|
||||
let event_room_id = Box::<RoomId>::try_from(room_id_str)
|
||||
let event_room_id = <&RoomId>::try_from(room_id_str)
|
||||
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
|
||||
|
||||
if event_room_id != body.room_id {
|
||||
|
@ -2476,14 +2475,14 @@ pub fn get_event_authorization_route(
|
|||
.and_then(|val| val.as_str())
|
||||
.ok_or_else(|| Error::bad_database("Invalid event in database"))?;
|
||||
|
||||
let room_id = Box::<RoomId>::try_from(room_id_str)
|
||||
let room_id = <&RoomId>::try_from(room_id_str)
|
||||
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
|
||||
|
||||
if !db.rooms.server_in_room(sender_servername, &room_id)? {
|
||||
if !db.rooms.server_in_room(sender_servername, room_id)? {
|
||||
return Err(Error::BadRequest(ErrorKind::NotFound, "Event not found."));
|
||||
}
|
||||
|
||||
let auth_chain_ids = get_auth_chain(&room_id, vec![Arc::from(&*body.event_id)], &db)?;
|
||||
let auth_chain_ids = get_auth_chain(room_id, vec![Arc::from(&*body.event_id)], &db)?;
|
||||
|
||||
Ok(get_event_authorization::v1::Response {
|
||||
auth_chain: auth_chain_ids
|
||||
|
@ -2948,7 +2947,7 @@ pub async fn create_invite_route(
|
|||
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Failed to sign event."))?;
|
||||
|
||||
// Generate event id
|
||||
let event_id = Box::<EventId>::try_from(&*format!(
|
||||
let event_id = EventId::parse(format!(
|
||||
"${}",
|
||||
ruma::signatures::reference_hash(&signed_event, &body.room_version)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
|
@ -3224,7 +3223,7 @@ pub(crate) async fn fetch_required_signing_keys(
|
|||
|
||||
let fetch_res = fetch_signing_keys(
|
||||
db,
|
||||
&Box::<ServerName>::try_from(&**signature_server).map_err(|_| {
|
||||
signature_server.as_str().try_into().map_err(|_| {
|
||||
Error::BadServerResponse("Invalid servername in signatures of server response pdu.")
|
||||
})?,
|
||||
signature_ids,
|
||||
|
@ -3262,11 +3261,12 @@ fn get_server_keys_from_cache(
|
|||
Error::BadServerResponse("Invalid PDU in server response")
|
||||
})?;
|
||||
|
||||
let event_id = Box::<EventId>::try_from(&*format!(
|
||||
let event_id = format!(
|
||||
"${}",
|
||||
ruma::signatures::reference_hash(&value, room_version)
|
||||
.expect("ruma can calculate reference hashes")
|
||||
))
|
||||
);
|
||||
let event_id = <&EventId>::try_from(event_id.as_str())
|
||||
.expect("ruma's reference hashes are valid event ids");
|
||||
|
||||
if let Some((time, tries)) = db
|
||||
|
@ -3274,7 +3274,7 @@ fn get_server_keys_from_cache(
|
|||
.bad_event_ratelimiter
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&event_id)
|
||||
.get(event_id)
|
||||
{
|
||||
// Exponential backoff
|
||||
let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries);
|
||||
|
@ -3308,7 +3308,7 @@ fn get_server_keys_from_cache(
|
|||
let contains_all_ids =
|
||||
|keys: &BTreeMap<String, String>| signature_ids.iter().all(|id| keys.contains_key(id));
|
||||
|
||||
let origin = &Box::<ServerName>::try_from(&**signature_server).map_err(|_| {
|
||||
let origin = <&ServerName>::try_from(signature_server.as_str()).map_err(|_| {
|
||||
Error::BadServerResponse("Invalid servername in signatures of server response pdu.")
|
||||
})?;
|
||||
|
||||
|
@ -3327,7 +3327,7 @@ fn get_server_keys_from_cache(
|
|||
|
||||
if !contains_all_ids(&result) {
|
||||
trace!("Signing key not loaded for {}", origin);
|
||||
servers.insert(origin.clone(), BTreeMap::new());
|
||||
servers.insert(origin.to_owned(), BTreeMap::new());
|
||||
}
|
||||
|
||||
pub_key_map.insert(origin.to_string(), result);
|
||||
|
|
Loading…
Reference in a new issue