Skip to content

Commit

Permalink
feat!(sdk): Accept any type that implements Into<String> as an atta…
Browse files Browse the repository at this point in the history
…chment filename

Since we need a String as final type, this can avoid to allocate a string if the provided type is already a String.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
  • Loading branch information
zecakeh committed Dec 21, 2024
1 parent adb4428 commit 53bea1c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions crates/matrix-sdk/src/room/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<'a> IntoFuture for SendRawMessageLikeEvent<'a> {
#[allow(missing_debug_implementations)]
pub struct SendAttachment<'a> {
room: &'a Room,
filename: &'a str,
filename: String,
content_type: &'a Mime,
data: Vec<u8>,
config: AttachmentConfig,
Expand All @@ -252,7 +252,7 @@ pub struct SendAttachment<'a> {
impl<'a> SendAttachment<'a> {
pub(crate) fn new(
room: &'a Room,
filename: &'a str,
filename: String,
content_type: &'a Mime,
data: Vec<u8>,
config: AttachmentConfig,
Expand Down
12 changes: 6 additions & 6 deletions crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1933,12 +1933,12 @@ impl Room {
#[instrument(skip_all)]
pub fn send_attachment<'a>(
&'a self,
filename: &'a str,
filename: impl Into<String>,
content_type: &'a Mime,
data: Vec<u8>,
config: AttachmentConfig,
) -> SendAttachment<'a> {
SendAttachment::new(self, filename, content_type, data, config)
SendAttachment::new(self, filename.into(), content_type, data, config)
}

/// Prepare and send an attachment to this room.
Expand Down Expand Up @@ -1971,7 +1971,7 @@ impl Room {
#[instrument(skip_all)]
pub(super) async fn prepare_and_send_attachment<'a>(
&'a self,
filename: &'a str,
filename: String,
content_type: &'a Mime,
data: Vec<u8>,
mut config: AttachmentConfig,
Expand Down Expand Up @@ -2076,7 +2076,7 @@ impl Room {
pub(crate) fn make_attachment_type(
&self,
content_type: &Mime,
filename: &str,
filename: String,
source: MediaSource,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
Expand All @@ -2087,8 +2087,8 @@ impl Room {
// body is the filename, and the filename is not set.
// https://github.com/matrix-org/matrix-spec-proposals/blob/main/proposals/2530-body-as-caption.md
let (body, filename) = match caption {
Some(caption) => (caption, Some(filename.to_owned())),
None => (filename.to_owned(), None),
Some(caption) => (caption, Some(filename)),
None => (filename, None),
};

let (thumbnail_source, thumbnail_info) = thumbnail.unzip();
Expand Down
3 changes: 2 additions & 1 deletion crates/matrix-sdk/src/send_queue/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl RoomSendQueue {
#[instrument(skip_all, fields(event_txn))]
pub async fn send_attachment(
&self,
filename: &str,
filename: impl Into<String>,
content_type: Mime,
data: Vec<u8>,
mut config: AttachmentConfig,
Expand All @@ -118,6 +118,7 @@ impl RoomSendQueue {
return Err(RoomSendQueueError::RoomNotJoined);
}

let filename = filename.into();
let upload_file_txn = TransactionId::new();
let send_event_txn = config.txn_id.map_or_else(ChildTransactionId::new, Into::into);

Expand Down

0 comments on commit 53bea1c

Please sign in to comment.