refactor for ruma identifiers optimizations

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-28 23:31:24 +00:00 committed by strawberry
commit 0a9b6c136f
19 changed files with 81 additions and 88 deletions

View file

@ -10,9 +10,7 @@ use std::{
use conduwuit::{error, Config, Result};
use data::Data;
use regex::RegexSet;
use ruma::{
OwnedEventId, OwnedRoomAliasId, OwnedServerName, OwnedUserId, RoomAliasId, ServerName, UserId,
};
use ruma::{OwnedEventId, OwnedRoomAliasId, OwnedServerName, OwnedUserId, ServerName, UserId};
use tokio::sync::Mutex;
use crate::service;
@ -73,7 +71,7 @@ impl crate::Service for Service {
jwt_decoding_key,
bad_event_ratelimiter: Arc::new(RwLock::new(HashMap::new())),
stateres_mutex: Arc::new(Mutex::new(())),
admin_alias: RoomAliasId::parse(format!("#admins:{}", &config.server_name))
admin_alias: OwnedRoomAliasId::try_from(format!("#admins:{}", &config.server_name))
.expect("#admins:server_name is valid alias name"),
server_user: UserId::parse_with_server_name(
String::from("conduit"),

View file

@ -129,10 +129,10 @@ impl Service {
servers: Option<Vec<OwnedServerName>>,
) -> Result<(OwnedRoomId, Vec<OwnedServerName>)> {
if room.is_room_id() {
let room_id = RoomId::parse(room).expect("valid RoomId");
Ok((room_id, servers.unwrap_or_default()))
let room_id: &RoomId = room.try_into().expect("valid RoomId");
Ok((room_id.to_owned(), servers.unwrap_or_default()))
} else {
let alias = &RoomAliasId::parse(room).expect("valid RoomAliasId");
let alias: &RoomAliasId = room.try_into().expect("valid RoomAliasId");
self.resolve_alias(alias, servers).await
}
}

View file

@ -1,5 +1,5 @@
use conduwuit::{err, implement, pdu::gen_event_id_canonical_json, result::FlatOk, Result};
use ruma::{CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedRoomId, RoomId};
use ruma::{CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedRoomId};
use serde_json::value::RawValue as RawJsonValue;
#[implement(super::Service)]
@ -14,7 +14,7 @@ pub async fn parse_incoming_pdu(
let room_id: OwnedRoomId = value
.get("room_id")
.and_then(CanonicalJsonValue::as_str)
.map(RoomId::parse)
.map(OwnedRoomId::parse)
.flat_ok_or(err!(Request(InvalidParam("Invalid room_id in pdu"))))?;
let room_version_id = self

View file

@ -139,7 +139,7 @@ impl Service {
.state_cache
.update_membership(
room_id,
&user_id,
user_id,
membership_event,
&pdu.sender,
None,

View file

@ -35,7 +35,7 @@ use ruma::{
push::{Action, Ruleset, Tweak},
state_res::{self, Event, RoomVersion},
uint, user_id, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, OwnedRoomId,
OwnedServerName, RoomId, RoomVersionId, ServerName, UserId,
OwnedServerName, OwnedUserId, RoomId, RoomVersionId, ServerName, UserId,
};
use serde::Deserialize;
use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
@ -424,7 +424,7 @@ impl Service {
if pdu.kind == TimelineEventType::RoomMember {
if let Some(state_key) = &pdu.state_key {
let target_user_id = UserId::parse(state_key.clone())?;
let target_user_id = OwnedUserId::parse(state_key)?;
if self.services.users.is_active_local(&target_user_id).await {
push_target.insert(target_user_id);
@ -534,7 +534,7 @@ impl Service {
| TimelineEventType::RoomMember => {
if let Some(state_key) = &pdu.state_key {
// if the state_key fails
let target_user_id = UserId::parse(state_key.clone())
let target_user_id = UserId::parse(state_key)
.expect("This state_key was previously validated");
let content: RoomMemberEventContent = pdu.get_content()?;
@ -550,7 +550,7 @@ impl Service {
.state_cache
.update_membership(
&pdu.room_id,
&target_user_id,
target_user_id,
content,
&pdu.sender,
invite_state,
@ -627,7 +627,7 @@ impl Service {
.and_then(|state_key| UserId::parse(state_key.as_str()).ok())
{
let appservice_uid = appservice.registration.sender_localpart.as_str();
if state_key_uid == appservice_uid {
if state_key_uid == &appservice_uid {
self.services
.sending
.send_pdu_appservice(appservice.registration.id.clone(), pdu_id)?;

View file

@ -7,7 +7,7 @@ use conduwuit::{
};
use database::{Database, Deserialized, Map};
use futures::{Stream, StreamExt};
use ruma::{ServerName, UserId};
use ruma::{OwnedServerName, ServerName, UserId};
use super::{Destination, SendingEvent};
use crate::{globals, Dep};
@ -209,7 +209,7 @@ fn parse_servercurrentevent(key: &[u8], value: &[u8]) -> Result<(Destination, Se
let mut parts = key[1..].splitn(3, |&b| b == 0xFF);
let user = parts.next().expect("splitn always returns one element");
let user_string = utils::string_from_bytes(user)
let user_string = utils::str_from_bytes(user)
.map_err(|_| Error::bad_database("Invalid user string in servercurrentevent"))?;
let user_id = UserId::parse(user_string)
.map_err(|_| Error::bad_database("Invalid user id in servercurrentevent"))?;
@ -225,7 +225,7 @@ fn parse_servercurrentevent(key: &[u8], value: &[u8]) -> Result<(Destination, Se
.ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?;
(
Destination::Push(user_id, pushkey_string),
Destination::Push(user_id.to_owned(), pushkey_string),
if value.is_empty() {
SendingEvent::Pdu(event.into())
} else {
@ -246,7 +246,7 @@ fn parse_servercurrentevent(key: &[u8], value: &[u8]) -> Result<(Destination, Se
})?;
(
Destination::Federation(ServerName::parse(server).map_err(|_| {
Destination::Federation(OwnedServerName::parse(&server).map_err(|_| {
Error::bad_database("Invalid server string in server_currenttransaction")
})?),
if value.is_empty() {

View file

@ -883,7 +883,7 @@ impl Service {
.get("room_id")
.and_then(|val| RoomId::parse(val.as_str()?).ok())
{
match self.services.state.get_room_version(&room_id).await {
match self.services.state.get_room_version(room_id).await {
| Ok(room_version_id) => match room_version_id {
| RoomVersionId::V1 | RoomVersionId::V2 => {},
| _ => _ = pdu_json.remove("event_id"),

View file

@ -937,7 +937,7 @@ impl Service {
let user_string = utils::string_from_bytes(user_bytes)
.map_err(|e| err!(Database("User ID in openid_userid is invalid unicode. {e}")))?;
UserId::parse(user_string)
OwnedUserId::try_from(user_string)
.map_err(|e| err!(Database("User ID in openid_userid is invalid. {e}")))
}