optimize with SmallString; consolidate related re-exports

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-02-08 00:16:37 +00:00 committed by strawberry
parent ecc9099127
commit b872f8e593
39 changed files with 113 additions and 96 deletions

View file

@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use axum::extract::State;
use conduwuit::{
debug_info, debug_warn, err, error, info, pdu::PduBuilder, warn, Err, Error, Result,
debug_info, debug_warn, err, error, info, pdu::PduBuilder, warn, Err, Error, Result, StateKey,
};
use futures::FutureExt;
use ruma::{
@ -198,7 +198,7 @@ pub(crate) async fn create_room_route(
event_type: TimelineEventType::RoomCreate,
content: to_raw_value(&create_content)
.expect("create event content serialization"),
state_key: Some(String::new()),
state_key: Some(StateKey::new()),
..Default::default()
},
sender_user,
@ -267,7 +267,7 @@ pub(crate) async fn create_room_route(
event_type: TimelineEventType::RoomPowerLevels,
content: to_raw_value(&power_levels_content)
.expect("serialized power_levels event content"),
state_key: Some(String::new()),
state_key: Some(StateKey::new()),
..Default::default()
},
sender_user,
@ -371,7 +371,7 @@ pub(crate) async fn create_room_route(
}
// Implicit state key defaults to ""
pdu_builder.state_key.get_or_insert_with(String::new);
pdu_builder.state_key.get_or_insert_with(StateKey::new);
// Silently skip encryption events if they are not allowed
if pdu_builder.event_type == TimelineEventType::RoomEncryption

View file

@ -1,7 +1,7 @@
use std::cmp::max;
use axum::extract::State;
use conduwuit::{err, info, pdu::PduBuilder, Error, Result};
use conduwuit::{err, info, pdu::PduBuilder, Error, Result, StateKey};
use futures::StreamExt;
use ruma::{
api::client::{error::ErrorKind, room::upgrade_room},
@ -77,7 +77,7 @@ pub(crate) async fn upgrade_room_route(
.rooms
.timeline
.build_and_append_pdu(
PduBuilder::state(String::new(), &RoomTombstoneEventContent {
PduBuilder::state(StateKey::new(), &RoomTombstoneEventContent {
body: "This room has been replaced".to_owned(),
replacement_room: replacement_room.clone(),
}),
@ -159,7 +159,7 @@ pub(crate) async fn upgrade_room_route(
content: to_raw_value(&create_event_content)
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
state_key: Some(StateKey::new()),
redacts: None,
timestamp: None,
},
@ -188,7 +188,7 @@ pub(crate) async fn upgrade_room_route(
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(sender_user.to_string()),
state_key: Some(sender_user.as_str().into()),
redacts: None,
timestamp: None,
},
@ -217,7 +217,7 @@ pub(crate) async fn upgrade_room_route(
PduBuilder {
event_type: event_type.to_string().into(),
content: event_content,
state_key: Some(String::new()),
state_key: Some(StateKey::new()),
..Default::default()
},
sender_user,
@ -272,7 +272,7 @@ pub(crate) async fn upgrade_room_route(
.rooms
.timeline
.build_and_append_pdu(
PduBuilder::state(String::new(), &RoomPowerLevelsEventContent {
PduBuilder::state(StateKey::new(), &RoomPowerLevelsEventContent {
events_default: new_level,
invite: new_level,
..power_levels_event_content

View file

@ -172,7 +172,7 @@ async fn send_state_event_for_key_helper(
PduBuilder {
event_type: event_type.to_string().into(),
content: serde_json::from_str(json.json().get())?,
state_key: Some(String::from(state_key)),
state_key: Some(state_key.into()),
timestamp,
..Default::default()
},

View file

@ -441,7 +441,7 @@ async fn handle_left_room(
kind: RoomMember,
content: serde_json::from_str(r#"{"membership":"leave"}"#)
.expect("this is valid JSON"),
state_key: Some(sender_user.to_string()),
state_key: Some(sender_user.as_str().into()),
unsigned: None,
// The following keys are dropped on conversion
room_id: room_id.clone(),

View file

@ -29,7 +29,7 @@ use ruma::{
TimelineEventType::*,
},
serde::Raw,
uint, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UInt,
uint, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, RoomId, UInt, UserId,
};
use service::rooms::read_receipt::pack_receipts;
@ -258,12 +258,9 @@ pub(crate) async fn sync_events_v4_route(
continue;
};
if pdu.kind == RoomMember {
if let Some(state_key) = &pdu.state_key {
let user_id =
OwnedUserId::parse(state_key.clone()).map_err(|_| {
Error::bad_database("Invalid UserId in member PDU.")
})?;
if let Some(Ok(user_id)) =
pdu.state_key.as_deref().map(UserId::parse)
{
if user_id == *sender_user {
continue;
}
@ -275,18 +272,18 @@ pub(crate) async fn sync_events_v4_route(
if !share_encrypted_room(
&services,
sender_user,
&user_id,
user_id,
Some(room_id),
)
.await
{
device_list_changes.insert(user_id);
device_list_changes.insert(user_id.to_owned());
}
},
| MembershipState::Leave => {
// Write down users that have left encrypted rooms we
// are in
left_encrypted_users.insert(user_id);
left_encrypted_users.insert(user_id.to_owned());
},
| _ => {},
}

View file

@ -25,7 +25,7 @@ use ruma::{
},
serde::Raw,
state_res::TypeStateKey,
uint, DeviceId, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UInt, UserId,
uint, DeviceId, OwnedEventId, OwnedRoomId, RoomId, UInt, UserId,
};
use service::{rooms::read_receipt::pack_receipts, PduCount};
@ -765,13 +765,9 @@ async fn collect_e2ee<'a>(
continue;
};
if pdu.kind == TimelineEventType::RoomMember {
if let Some(state_key) = &pdu.state_key {
let user_id =
OwnedUserId::parse(state_key.clone()).map_err(|_| {
Error::bad_database("Invalid UserId in member PDU.")
})?;
if user_id == *sender_user {
if let Some(Ok(user_id)) = pdu.state_key.as_deref().map(UserId::parse)
{
if user_id == sender_user {
continue;
}
@ -782,18 +778,18 @@ async fn collect_e2ee<'a>(
if !share_encrypted_room(
&services,
sender_user,
&user_id,
user_id,
Some(room_id),
)
.await
{
device_list_changes.insert(user_id);
device_list_changes.insert(user_id.to_owned());
}
},
| MembershipState::Leave => {
// Write down users that have left encrypted rooms we
// are in
left_encrypted_users.insert(user_id);
left_encrypted_users.insert(user_id.to_owned());
},
| _ => {},
}