chore: code cleanup

next
Timo Kösters 2021-04-14 10:43:31 +02:00
parent abddfc2d2a
commit c1953efa6b
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
5 changed files with 59 additions and 56 deletions

View File

@ -568,9 +568,7 @@ async fn join_room_by_id_helper(
serde_json::from_value::<member::MembershipState>(
pdu.content
.get("membership")
.ok_or_else(|| {
Error::BadServerResponse("Invalid member event content")
})?
.ok_or(Error::BadServerResponse("Invalid member event content"))?
.clone(),
)
.map_err(|_| {
@ -578,8 +576,7 @@ async fn join_room_by_id_helper(
})?,
&pdu.sender,
Some(invite_state),
&db.account_data,
&db.globals,
db,
)?;
}
state.insert((pdu.kind.clone(), state_key.clone()), pdu.event_id.clone());

View File

@ -222,7 +222,7 @@ pub fn get_actions<'a>(
user_display_name: db
.users
.displayname(&user)?
.unwrap_or(user.localpart().to_owned()),
.unwrap_or_else(|| user.localpart().to_owned()),
users_power_levels: power_levels.users,
default_power_level: power_levels.users_default,
notification_power_levels: power_levels.notifications,

View File

@ -706,14 +706,14 @@ impl Rooms {
userroom_id.extend_from_slice(pdu.room_id.as_bytes());
if notify {
&self
self
.userroomid_notificationcount
.update_and_fetch(&userroom_id, utils::increment)?
.expect("utils::increment will always put in a value");
}
if highlight {
&self
self
.userroomid_highlightcount
.update_and_fetch(&userroom_id, utils::increment)?
.expect("utils::increment will always put in a value");
@ -743,12 +743,10 @@ impl Rooms {
let membership = serde_json::from_value::<member::MembershipState>(
pdu.content
.get("membership")
.ok_or_else(|| {
Error::BadRequest(
ErrorKind::InvalidParam,
"Invalid member event content",
)
})?
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Invalid member event content",
))?
.clone(),
)
.map_err(|_| {
@ -807,8 +805,7 @@ impl Rooms {
membership,
&pdu.sender,
invite_state,
&db.account_data,
&db.globals,
db,
)?;
}
}
@ -1205,7 +1202,7 @@ impl Rooms {
.iter()
.filter_map(|event_id| Some(self.get_pdu(event_id).ok()??.depth))
.max()
.unwrap_or(uint!(0))
.unwrap_or_else(|| uint!(0))
+ uint!(1);
let mut unsigned = unsigned.unwrap_or_default();
@ -1542,8 +1539,7 @@ impl Rooms {
membership: member::MembershipState,
sender: &UserId,
last_state: Option<Vec<Raw<AnyStrippedStateEvent>>>,
account_data: &super::account_data::AccountData,
globals: &super::globals::Globals,
db: &Database,
) -> Result<()> {
let mut roomserver_id = room_id.as_bytes().to_vec();
roomserver_id.push(0xff);
@ -1603,23 +1599,32 @@ impl Rooms {
// .ok();
// Copy old tags to new room
if let Some(tag_event) = account_data.get::<ruma::events::tag::TagEvent>(
Some(&predecessor.room_id),
user_id,
EventType::Tag,
)? {
account_data
.update(Some(room_id), user_id, EventType::Tag, &tag_event, globals)
if let Some(tag_event) =
db.account_data.get::<ruma::events::tag::TagEvent>(
Some(&predecessor.room_id),
user_id,
EventType::Tag,
)?
{
db.account_data
.update(
Some(room_id),
user_id,
EventType::Tag,
&tag_event,
&db.globals,
)
.ok();
};
// Copy direct chat flag
if let Some(mut direct_event) = account_data
.get::<ruma::events::direct::DirectEvent>(
None,
user_id,
EventType::Direct,
)? {
if let Some(mut direct_event) =
db.account_data.get::<ruma::events::direct::DirectEvent>(
None,
user_id,
EventType::Direct,
)?
{
let mut room_ids_updated = false;
for room_ids in direct_event.content.0.values_mut() {
@ -1630,12 +1635,12 @@ impl Rooms {
}
if room_ids_updated {
account_data.update(
db.account_data.update(
None,
user_id,
EventType::Direct,
&direct_event,
globals,
&db.globals,
)?;
}
};
@ -1652,7 +1657,8 @@ impl Rooms {
}
member::MembershipState::Invite => {
// We want to know if the sender is ignored by the receiver
let is_ignored = account_data
let is_ignored = db
.account_data
.get::<ignored_user_list::IgnoredUserListEvent>(
None, // Ignored users are in global account data
&user_id, // Receiver
@ -1673,7 +1679,7 @@ impl Rooms {
.expect("state to bytes always works"),
)?;
self.roomuserid_invitecount
.insert(&roomuser_id, &globals.next_count()?.to_be_bytes())?;
.insert(&roomuser_id, &db.globals.next_count()?.to_be_bytes())?;
self.userroomid_joined.remove(&userroom_id)?;
self.roomuserid_joined.remove(&roomuser_id)?;
self.userroomid_leftstate.remove(&userroom_id)?;
@ -1693,7 +1699,7 @@ impl Rooms {
serde_json::to_vec(&Vec::<Raw<AnySyncStateEvent>>::new()).unwrap(),
)?; // TODO
self.roomuserid_leftcount
.insert(&roomuser_id, &globals.next_count()?.to_be_bytes())?;
.insert(&roomuser_id, &db.globals.next_count()?.to_be_bytes())?;
self.userroomid_joined.remove(&userroom_id)?;
self.roomuserid_joined.remove(&roomuser_id)?;
self.userroomid_invitestate.remove(&userroom_id)?;
@ -1729,8 +1735,7 @@ impl Rooms {
MembershipState::Leave,
user_id,
last_state,
&db.account_data,
&db.globals,
db,
)?;
} else {
let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>(

View File

@ -1413,15 +1413,16 @@ pub fn get_missing_events_route<'a>(
let mut i = 0;
while i < queued_events.len() && events.len() < u64::from(body.limit) as usize {
if let Some(pdu) = db.rooms.get_pdu_json(&queued_events[i])? {
if body.earliest_events.contains(
&serde_json::from_value(
let event_id =
serde_json::from_value(
serde_json::to_value(pdu.get("event_id").cloned().ok_or_else(|| {
Error::bad_database("Event in db has no event_id field.")
})?)
.expect("canonical json is valid json value"),
)
.map_err(|_| Error::bad_database("Invalid event_id field in pdu in db."))?,
) {
.map_err(|_| Error::bad_database("Invalid event_id field in pdu in db."))?;
if body.earliest_events.contains(&event_id) {
i += 1;
continue;
}
@ -1541,9 +1542,10 @@ pub async fn create_invite_route<'a>(
serde_json::to_value(
signed_event
.get("sender")
.ok_or_else(|| {
Error::BadRequest(ErrorKind::InvalidParam, "Event had no sender field.")
})?
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Event had no sender field.",
))?
.clone(),
)
.expect("CanonicalJsonValue to serde_json::Value always works"),
@ -1553,9 +1555,10 @@ pub async fn create_invite_route<'a>(
serde_json::to_value(
signed_event
.get("state_key")
.ok_or_else(|| {
Error::BadRequest(ErrorKind::InvalidParam, "Event had no state_key field.")
})?
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Event had no state_key field.",
))?
.clone(),
)
.expect("CanonicalJsonValue to serde_json::Value always works"),
@ -1586,8 +1589,7 @@ pub async fn create_invite_route<'a>(
MembershipState::Invite,
&sender,
Some(invite_state),
&db.account_data,
&db.globals,
&db,
)?;
}
@ -1638,10 +1640,9 @@ pub async fn fetch_required_signing_keys(
) -> Result<()> {
// We go through all the signatures we see on the value and fetch the corresponding signing
// keys
for (signature_server, signature) in match event
.get("signatures")
.ok_or_else(|| Error::BadServerResponse("No signatures in server response pdu."))?
{
for (signature_server, signature) in match event.get("signatures").ok_or(
Error::BadServerResponse("No signatures in server response pdu."),
)? {
CanonicalJsonValue::Object(map) => map,
_ => {
return Err(Error::BadServerResponse(

View File

@ -69,7 +69,7 @@ pub fn calculate_hash(password: &str) -> Result<String, argon2::Error> {
argon2::hash_encoded(password.as_bytes(), salt.as_bytes(), &hashing_config)
}
pub fn common_elements<'a>(
pub fn common_elements(
mut iterators: impl Iterator<Item = impl Iterator<Item = Vec<u8>>>,
check_order: impl Fn(&[u8], &[u8]) -> Ordering,
) -> Option<impl Iterator<Item = Vec<u8>>> {