From 57acc4f6551b13e6ef7836d5f3e1ce248e99fd31 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 28 Jun 2024 23:23:59 +0000 Subject: [PATCH] fix needless pass by value Signed-off-by: Jason Volk --- src/admin/media/commands.rs | 6 +++--- src/api/client/media.rs | 14 +++++++------- src/api/client/push.rs | 4 +--- src/api/client/read_marker.rs | 4 ++-- src/api/server/send.rs | 2 +- src/service/appservice/data.rs | 2 +- src/service/appservice/mod.rs | 2 +- src/service/media/data.rs | 8 ++++---- src/service/media/mod.rs | 20 ++++++++++---------- src/service/pusher/data.rs | 6 +++--- src/service/pusher/mod.rs | 2 +- src/service/rooms/read_receipt/data.rs | 4 ++-- src/service/rooms/read_receipt/mod.rs | 2 +- src/service/rooms/state_compressor/data.rs | 2 +- src/service/rooms/state_compressor/mod.rs | 4 ++-- 15 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/admin/media/commands.rs b/src/admin/media/commands.rs index c1026ae2..d29d5f47 100644 --- a/src/admin/media/commands.rs +++ b/src/admin/media/commands.rs @@ -15,7 +15,7 @@ pub(super) async fn delete( if let Some(mxc) = mxc { debug!("Got MXC URL: {mxc}"); - services().media.delete(mxc.to_string()).await?; + services().media.delete(mxc.as_ref()).await?; return Ok(RoomMessageEventContent::text_plain( "Deleted the MXC from our database and on our filesystem.", @@ -123,7 +123,7 @@ pub(super) async fn delete( } for mxc_url in mxc_urls { - services().media.delete(mxc_url).await?; + services().media.delete(&mxc_url).await?; mxc_deletion_count = mxc_deletion_count.saturating_add(1); } @@ -154,7 +154,7 @@ pub(super) async fn delete_list(body: Vec<&str>) -> Result) -> R content_type, file, content_disposition, - }) = services().media.get(mxc.clone()).await? + }) = services().media.get(&mxc).await? { let content_disposition = Some(make_content_disposition(&content_type, content_disposition, None)); @@ -264,7 +264,7 @@ pub(crate) async fn get_content_as_filename_route( content_type, file, content_disposition, - }) = services().media.get(mxc.clone()).await? + }) = services().media.get(&mxc).await? { let content_disposition = Some(make_content_disposition( &content_type, @@ -352,7 +352,7 @@ pub(crate) async fn get_content_thumbnail_route( }) = services() .media .get_thumbnail( - mxc.clone(), + &mxc, body.width .try_into() .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, @@ -405,7 +405,7 @@ pub(crate) async fn get_content_thumbnail_route( .media .upload_thumbnail( None, - mxc, + &mxc, None, get_thumbnail_response.content_type.as_deref(), body.width.try_into().expect("all UInts are valid u32s"), @@ -494,7 +494,7 @@ async fn get_remote_content( .media .create( None, - mxc.to_owned(), + mxc, content_disposition.as_deref(), content_response.content_type.as_deref(), &content_response.file, @@ -520,7 +520,7 @@ async fn download_image(client: &reqwest::Client, url: &str) -> Result) -> R pub(crate) async fn set_pushers_route(body: Ruma) -> Result { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); - services() - .pusher - .set_pusher(sender_user, body.action.clone())?; + services().pusher.set_pusher(sender_user, &body.action)?; Ok(set_pusher::v3::Response::default()) } diff --git a/src/api/client/read_marker.rs b/src/api/client/read_marker.rs index 0f43eeef..cbb36b81 100644 --- a/src/api/client/read_marker.rs +++ b/src/api/client/read_marker.rs @@ -85,7 +85,7 @@ pub(crate) async fn set_read_marker_route( services().rooms.read_receipt.readreceipt_update( sender_user, &body.room_id, - ruma::events::receipt::ReceiptEvent { + &ruma::events::receipt::ReceiptEvent { content: ruma::events::receipt::ReceiptEventContent(receipt_content), room_id: body.room_id.clone(), }, @@ -145,7 +145,7 @@ pub(crate) async fn create_receipt_route( services().rooms.read_receipt.readreceipt_update( sender_user, &body.room_id, - ruma::events::receipt::ReceiptEvent { + &ruma::events::receipt::ReceiptEvent { content: ruma::events::receipt::ReceiptEventContent(receipt_content), room_id: body.room_id.clone(), }, diff --git a/src/api/server/send.rs b/src/api/server/send.rs index 1168dd82..5fad790f 100644 --- a/src/api/server/send.rs +++ b/src/api/server/send.rs @@ -208,7 +208,7 @@ pub(crate) async fn send_transaction_message_route( services() .rooms .read_receipt - .readreceipt_update(&user_id, &room_id, event)?; + .readreceipt_update(&user_id, &room_id, &event)?; } } else { debug_warn!(%user_id, %room_id, %origin, "received read receipt EDU from server who does not have a single member from their server in the room"); diff --git a/src/service/appservice/data.rs b/src/service/appservice/data.rs index 59fabe96..c70a35e2 100644 --- a/src/service/appservice/data.rs +++ b/src/service/appservice/data.rs @@ -17,7 +17,7 @@ impl Data { } /// Registers an appservice and returns the ID to the caller - pub(super) fn register_appservice(&self, yaml: Registration) -> Result { + pub(super) fn register_appservice(&self, yaml: &Registration) -> Result { let id = yaml.id.as_str(); self.id_appserviceregistrations .insert(id.as_bytes(), serde_yaml::to_string(&yaml).unwrap().as_bytes())?; diff --git a/src/service/appservice/mod.rs b/src/service/appservice/mod.rs index 83023151..17f951d4 100644 --- a/src/service/appservice/mod.rs +++ b/src/service/appservice/mod.rs @@ -149,7 +149,7 @@ impl Service { .await .insert(yaml.id.clone(), yaml.clone().try_into()?); - self.db.register_appservice(yaml) + self.db.register_appservice(&yaml) } /// Remove an appservice registration diff --git a/src/service/media/data.rs b/src/service/media/data.rs index 7b8d0b5a..e52244f3 100644 --- a/src/service/media/data.rs +++ b/src/service/media/data.rs @@ -23,7 +23,7 @@ impl Data { } pub(super) fn create_file_metadata( - &self, sender_user: Option<&str>, mxc: String, width: u32, height: u32, content_disposition: Option<&str>, + &self, sender_user: Option<&str>, mxc: &str, width: u32, height: u32, content_disposition: Option<&str>, content_type: Option<&str>, ) -> Result> { let mut key = mxc.as_bytes().to_vec(); @@ -56,7 +56,7 @@ impl Data { Ok(key) } - pub(super) fn delete_file_mxc(&self, mxc: String) -> Result<()> { + pub(super) fn delete_file_mxc(&self, mxc: &str) -> Result<()> { debug!("MXC URI: {:?}", mxc); let mut prefix = mxc.as_bytes().to_vec(); @@ -82,7 +82,7 @@ impl Data { } /// Searches for all files with the given MXC - pub(super) fn search_mxc_metadata_prefix(&self, mxc: String) -> Result>> { + pub(super) fn search_mxc_metadata_prefix(&self, mxc: &str) -> Result>> { debug!("MXC URI: {:?}", mxc); let mut prefix = mxc.as_bytes().to_vec(); @@ -106,7 +106,7 @@ impl Data { } pub(super) fn search_file_metadata( - &self, mxc: String, width: u32, height: u32, + &self, mxc: &str, width: u32, height: u32, ) -> Result<(Option, Option, Vec)> { let mut prefix = mxc.as_bytes().to_vec(); prefix.push(0xFF); diff --git a/src/service/media/mod.rs b/src/service/media/mod.rs index a6e2a2a9..f631acb3 100644 --- a/src/service/media/mod.rs +++ b/src/service/media/mod.rs @@ -59,7 +59,7 @@ impl Service { /// Uploads a file. pub async fn create( - &self, sender_user: Option, mxc: String, content_disposition: Option<&str>, + &self, sender_user: Option, mxc: &str, content_disposition: Option<&str>, content_type: Option<&str>, file: &[u8], ) -> Result<()> { // Width, Height = 0 if it's not a thumbnail @@ -79,13 +79,13 @@ impl Service { } /// Deletes a file in the database and from the media directory via an MXC - pub async fn delete(&self, mxc: String) -> Result<()> { - if let Ok(keys) = self.db.search_mxc_metadata_prefix(mxc.clone()) { + pub async fn delete(&self, mxc: &str) -> Result<()> { + if let Ok(keys) = self.db.search_mxc_metadata_prefix(mxc) { for key in keys { self.remove_media_file(&key).await?; debug!("Deleting MXC {mxc} from database"); - self.db.delete_file_mxc(mxc.clone())?; + self.db.delete_file_mxc(mxc)?; } Ok(()) @@ -100,7 +100,7 @@ impl Service { /// Uploads or replaces a file thumbnail. #[allow(clippy::too_many_arguments)] pub async fn upload_thumbnail( - &self, sender_user: Option, mxc: String, content_disposition: Option<&str>, + &self, sender_user: Option, mxc: &str, content_disposition: Option<&str>, content_type: Option<&str>, width: u32, height: u32, file: &[u8], ) -> Result<()> { let key = if let Some(user) = sender_user { @@ -119,7 +119,7 @@ impl Service { } /// Downloads a file. - pub async fn get(&self, mxc: String) -> Result> { + pub async fn get(&self, mxc: &str) -> Result> { if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc, 0, 0) { let mut file = Vec::new(); let path = self.get_media_file(&key); @@ -232,7 +232,7 @@ impl Service { for mxc in remote_mxcs { debug!("Deleting MXC {mxc} from database and filesystem"); - self.delete(mxc).await?; + self.delete(&mxc).await?; deletion_count = deletion_count.saturating_add(1); } @@ -265,12 +265,12 @@ impl Service { /// /// For width,height <= 96 the server uses another thumbnailing algorithm /// which crops the image afterwards. - pub async fn get_thumbnail(&self, mxc: String, width: u32, height: u32) -> Result> { + pub async fn get_thumbnail(&self, mxc: &str, width: u32, height: u32) -> Result> { let (width, height, crop) = self .thumbnail_properties(width, height) .unwrap_or((0, 0, false)); // 0, 0 because that's the original file - if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc.clone(), width, height) { + if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc, width, height) { // Using saved thumbnail let mut file = Vec::new(); let path = self.get_media_file(&key); @@ -281,7 +281,7 @@ impl Service { content_type, file: file.clone(), })) - } else if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc.clone(), 0, 0) { + } else if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc, 0, 0) { // Generate a thumbnail let mut file = Vec::new(); let path = self.get_media_file(&key); diff --git a/src/service/pusher/data.rs b/src/service/pusher/data.rs index 0892073f..a8a9366e 100644 --- a/src/service/pusher/data.rs +++ b/src/service/pusher/data.rs @@ -19,14 +19,14 @@ impl Data { } } - pub(super) fn set_pusher(&self, sender: &UserId, pusher: set_pusher::v3::PusherAction) -> Result<()> { - match &pusher { + pub(super) fn set_pusher(&self, sender: &UserId, pusher: &set_pusher::v3::PusherAction) -> Result<()> { + match pusher { set_pusher::v3::PusherAction::Post(data) => { let mut key = sender.as_bytes().to_vec(); key.push(0xFF); key.extend_from_slice(data.pusher.ids.pushkey.as_bytes()); self.senderkey_pusher - .insert(&key, &serde_json::to_vec(&pusher).expect("Pusher is valid JSON value"))?; + .insert(&key, &serde_json::to_vec(pusher).expect("Pusher is valid JSON value"))?; Ok(()) }, set_pusher::v3::PusherAction::Delete(ids) => { diff --git a/src/service/pusher/mod.rs b/src/service/pusher/mod.rs index ff167918..25faecd5 100644 --- a/src/service/pusher/mod.rs +++ b/src/service/pusher/mod.rs @@ -38,7 +38,7 @@ impl Service { }) } - pub fn set_pusher(&self, sender: &UserId, pusher: set_pusher::v3::PusherAction) -> Result<()> { + pub fn set_pusher(&self, sender: &UserId, pusher: &set_pusher::v3::PusherAction) -> Result<()> { self.db.set_pusher(sender, pusher) } diff --git a/src/service/rooms/read_receipt/data.rs b/src/service/rooms/read_receipt/data.rs index cf9d0ee3..a33c64ac 100644 --- a/src/service/rooms/read_receipt/data.rs +++ b/src/service/rooms/read_receipt/data.rs @@ -27,7 +27,7 @@ impl Data { } } - pub(super) fn readreceipt_update(&self, user_id: &UserId, room_id: &RoomId, event: ReceiptEvent) -> Result<()> { + pub(super) fn readreceipt_update(&self, user_id: &UserId, room_id: &RoomId, event: &ReceiptEvent) -> Result<()> { let mut prefix = room_id.as_bytes().to_vec(); prefix.push(0xFF); @@ -56,7 +56,7 @@ impl Data { self.readreceiptid_readreceipt.insert( &room_latest_id, - &serde_json::to_vec(&event).expect("EduEvent::to_string always works"), + &serde_json::to_vec(event).expect("EduEvent::to_string always works"), )?; Ok(()) diff --git a/src/service/rooms/read_receipt/mod.rs b/src/service/rooms/read_receipt/mod.rs index 56401fe4..5da374ef 100644 --- a/src/service/rooms/read_receipt/mod.rs +++ b/src/service/rooms/read_receipt/mod.rs @@ -22,7 +22,7 @@ impl Service { } /// Replaces the previous read receipt. - pub fn readreceipt_update(&self, user_id: &UserId, room_id: &RoomId, event: ReceiptEvent) -> Result<()> { + pub fn readreceipt_update(&self, user_id: &UserId, room_id: &RoomId, event: &ReceiptEvent) -> Result<()> { self.db.readreceipt_update(user_id, room_id, event)?; services().sending.flush_room(room_id)?; diff --git a/src/service/rooms/state_compressor/data.rs b/src/service/rooms/state_compressor/data.rs index 2bf93f38..454a3b57 100644 --- a/src/service/rooms/state_compressor/data.rs +++ b/src/service/rooms/state_compressor/data.rs @@ -60,7 +60,7 @@ impl Data { }) } - pub(super) fn save_statediff(&self, shortstatehash: u64, diff: StateDiff) -> Result<()> { + pub(super) fn save_statediff(&self, shortstatehash: u64, diff: &StateDiff) -> Result<()> { let mut value = diff.parent.unwrap_or(0).to_be_bytes().to_vec(); for new in diff.added.iter() { value.extend_from_slice(&new[..]); diff --git a/src/service/rooms/state_compressor/mod.rs b/src/service/rooms/state_compressor/mod.rs index 8dde2890..ee0e4cc9 100644 --- a/src/service/rooms/state_compressor/mod.rs +++ b/src/service/rooms/state_compressor/mod.rs @@ -200,7 +200,7 @@ impl Service { // There is no parent layer, create a new state self.db.save_statediff( shortstatehash, - StateDiff { + &StateDiff { parent: None, added: statediffnew, removed: statediffremoved, @@ -251,7 +251,7 @@ impl Service { // Diff small enough, we add diff as layer on top of parent self.db.save_statediff( shortstatehash, - StateDiff { + &StateDiff { parent: Some(parent.0), added: statediffnew, removed: statediffremoved,