Skip to content

Commit f826999

Browse files
author
Alfonso Grillo
authored
feature: send voice message API (#2697)
This PR adds a ffi binding for sending voice messages in the legacy format (before extensible events). It also makes minor changes in the `matrix-sdk` crate to accomodate this change. * Add send_voice_message api * Update ruma-events dependency * Fix attachment info mapping * Remove unstable dependency in attachment.rs * Bump ruma-events * Fix matrix-sdk Cargo.toml * Fix formatting issues * Refactor Voice case * Remove clone from AttachmentInfo * Remove duplicate code * Remove unused imports * Fix formatting issue * Rename update function
1 parent ba2a513 commit f826999

File tree

5 files changed

+73
-19
lines changed

5 files changed

+73
-19
lines changed

bindings/matrix-sdk-ffi/src/room.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,30 @@ impl Room {
818818
}))
819819
}
820820

821+
pub fn send_voice_message(
822+
self: Arc<Self>,
823+
url: String,
824+
audio_info: AudioInfo,
825+
waveform: Vec<u16>,
826+
progress_watcher: Option<Box<dyn ProgressWatcher>>,
827+
) -> Arc<SendAttachmentJoinHandle> {
828+
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
829+
let mime_str =
830+
audio_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
831+
let mime_type =
832+
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;
833+
834+
let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
835+
.map_err(|_| RoomError::InvalidAttachmentData)?;
836+
837+
let attachment_info =
838+
AttachmentInfo::Voice { audio_info: base_audio_info, waveform: Some(waveform) };
839+
let attachment_config = AttachmentConfig::new().info(attachment_info);
840+
841+
self.send_attachment(url, mime_type, attachment_config, progress_watcher).await
842+
}))
843+
}
844+
821845
pub fn send_file(
822846
self: Arc<Self>,
823847
url: String,

crates/matrix-sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ matrix-sdk-sqlite = { version = "0.1.0", path = "../matrix-sdk-sqlite", default-
8888
mime = "0.3.16"
8989
mime2ext = "0.1.52"
9090
rand = { version = "0.8.5", optional = true }
91-
ruma = { workspace = true, features = ["rand", "unstable-msc2448", "unstable-msc2965", "unstable-msc3930"] }
91+
ruma = { workspace = true, features = ["rand", "unstable-msc2448", "unstable-msc2965", "unstable-msc3930", "unstable-msc3245-v1-compat"] }
9292
serde = { workspace = true }
9393
serde_html_form = { workspace = true }
9494
serde_json = { workspace = true }

crates/matrix-sdk/src/attachment.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ pub enum AttachmentInfo {
8787
Audio(BaseAudioInfo),
8888
/// The metadata of a file.
8989
File(BaseFileInfo),
90+
/// The metadata of a voice message
91+
Voice {
92+
/// The audio info
93+
audio_info: BaseAudioInfo,
94+
/// The waveform of the voice message
95+
waveform: Option<Vec<u16>>,
96+
},
9097
}
9198

9299
impl From<AttachmentInfo> for ImageInfo {
@@ -125,6 +132,10 @@ impl From<AttachmentInfo> for AudioInfo {
125132
duration: info.duration,
126133
size: info.size,
127134
}),
135+
AttachmentInfo::Voice { audio_info, .. } => assign!(AudioInfo::new(), {
136+
duration: audio_info.duration,
137+
size: audio_info.size,
138+
}),
128139
_ => AudioInfo::new(),
129140
}
130141
}

crates/matrix-sdk/src/encryption/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ use ruma::{
4545
assign,
4646
events::room::{
4747
message::{
48-
AudioInfo, AudioMessageEventContent, FileInfo, FileMessageEventContent,
49-
ImageMessageEventContent, MessageType, VideoInfo, VideoMessageEventContent,
48+
AudioMessageEventContent, FileInfo, FileMessageEventContent, ImageMessageEventContent,
49+
MessageType, VideoInfo, VideoMessageEventContent,
5050
},
5151
ImageInfo, MediaSource, ThumbnailInfo,
5252
},
@@ -202,13 +202,13 @@ impl Client {
202202
MessageType::Image(content)
203203
}
204204
mime::AUDIO => {
205-
let info = assign!(info.map(AudioInfo::from).unwrap_or_default(), {
206-
mimetype: Some(content_type.as_ref().to_owned()),
207-
});
208-
let content = assign!(AudioMessageEventContent::encrypted(body.to_owned(), file), {
209-
info: Some(Box::new(info))
210-
});
211-
MessageType::Audio(content)
205+
let audio_message_event_content =
206+
AudioMessageEventContent::encrypted(body.to_owned(), file);
207+
MessageType::Audio(crate::media::update_audio_message_event(
208+
audio_message_event_content,
209+
content_type,
210+
info,
211+
))
212212
}
213213
mime::VIDEO => {
214214
let info = assign!(info.map(VideoInfo::from).unwrap_or_default(), {

crates/matrix-sdk/src/media.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ use ruma::{
3232
assign,
3333
events::room::{
3434
message::{
35-
self, AudioInfo, FileInfo, FileMessageEventContent, ImageMessageEventContent,
36-
MessageType, VideoInfo, VideoMessageEventContent,
35+
self, AudioInfo, AudioMessageEventContent, FileInfo, FileMessageEventContent,
36+
ImageMessageEventContent, MessageType, UnstableAudioDetailsContentBlock,
37+
UnstableVoiceContentBlock, VideoInfo, VideoMessageEventContent,
3738
},
3839
ImageInfo, MediaSource, ThumbnailInfo,
3940
},
@@ -438,13 +439,13 @@ impl Media {
438439
)
439440
}
440441
mime::AUDIO => {
441-
let info = assign!(info.map(AudioInfo::from).unwrap_or_default(), {
442-
mimetype: Some(content_type.as_ref().to_owned()),
443-
});
444-
MessageType::Audio(
445-
message::AudioMessageEventContent::plain(body.to_owned(), url)
446-
.info(Box::new(info)),
447-
)
442+
let audio_message_event_content =
443+
message::AudioMessageEventContent::plain(body.to_owned(), url);
444+
MessageType::Audio(update_audio_message_event(
445+
audio_message_event_content,
446+
content_type,
447+
info,
448+
))
448449
}
449450
mime::VIDEO => {
450451
let info = assign!(info.map(VideoInfo::from).unwrap_or_default(), {
@@ -495,3 +496,21 @@ impl Media {
495496
}
496497
}
497498
}
499+
500+
pub(crate) fn update_audio_message_event(
501+
mut audio_message_event_content: AudioMessageEventContent,
502+
content_type: &Mime,
503+
info: Option<AttachmentInfo>,
504+
) -> AudioMessageEventContent {
505+
if let Some(AttachmentInfo::Voice { audio_info, waveform: Some(waveform_vec) }) = &info {
506+
if let Some(duration) = audio_info.duration {
507+
let waveform = waveform_vec.iter().map(|v| (*v).into()).collect();
508+
audio_message_event_content.audio =
509+
Some(UnstableAudioDetailsContentBlock::new(duration, waveform));
510+
}
511+
audio_message_event_content.voice = Some(UnstableVoiceContentBlock::new());
512+
}
513+
514+
let audio_info = assign!(info.map(AudioInfo::from).unwrap_or_default(), {mimetype: Some(content_type.as_ref().to_owned()), });
515+
audio_message_event_content.info(Box::new(audio_info))
516+
}

0 commit comments

Comments
 (0)