From bd0b352854e656ec22f657d56ac9e86fea7c09f7 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 14 Aug 2020 00:23:37 +0200 Subject: [PATCH 1/7] make webrtc-instance-creation testable --- src/chat.rs | 8 +------- src/message.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 886a0aa60..96db541e6 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -1629,13 +1629,7 @@ pub async fn send_videochat_invitation(context: &Context, chat_id: ChatId) -> Re bail!("webrtc_instance not set"); }; - let room = dc_create_id(); - - let instance = if instance.contains("$ROOM") { - instance.replace("$ROOM", &room) - } else { - format!("{}{}", instance, room) - }; + let instance = Message::create_webrtc_instance(&instance, &dc_create_id()); let mut msg = Message::new(Viewtype::VideochatInvitation); msg.param.set(Param::WebrtcRoom, &instance); diff --git a/src/message.rs b/src/message.rs index 092569785..1be8ae7a4 100644 --- a/src/message.rs +++ b/src/message.rs @@ -643,6 +643,16 @@ impl Message { None } + // add room to a webrtc_instance as defined by the corresponding config-value; + // the result may still be prefixed by the type + pub fn create_webrtc_instance(instance: &str, room: &str) -> String { + if instance.contains("$ROOM") { + instance.replace("$ROOM", &room) + } else { + format!("{}{}", instance, room) + } + } + /// split a webrtc_instance as defined by the corresponding config-value into a type and a url pub fn parse_webrtc_instance(instance: &str) -> (VideochatType, String) { let mut split = instance.splitn(2, ':'); From a66d624b8738272950a0b4e38a618c3160ff33e0 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 14 Aug 2020 00:32:30 +0200 Subject: [PATCH 2/7] add failing test --- src/message.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/message.rs b/src/message.rs index 1be8ae7a4..55fe645b5 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1886,6 +1886,36 @@ mod tests { assert_eq!(url, "https://j.si/foo"); } + #[async_std::test] + async fn test_create_webrtc_instance() { + let instance = Message::create_webrtc_instance("https://meet.jit.si/", "123"); + assert_eq!(instance, "https://meet.jit.si/123"); + + let instance = Message::create_webrtc_instance("https://meet.jit.si", "456"); + assert_eq!(instance, "https://meet.jit.si/456"); + + let instance = Message::create_webrtc_instance("meet.jit.si", "789"); + assert_eq!(instance, "https://meet.jit.si/789"); + + let instance = Message::create_webrtc_instance("bla.foo?", "123"); + assert_eq!(instance, "https://bla.foo?123"); + + let instance = Message::create_webrtc_instance("jitsi:bla.foo#", "456"); + assert_eq!(instance, "jitsi:https://bla.foo#456"); + + let instance = Message::create_webrtc_instance("bla.foo#room=", "789"); + assert_eq!(instance, "https://bla.foo#room=789"); + + let instance = Message::create_webrtc_instance("https://bla.foo#room", "123"); + assert_eq!(instance, "https://bla.foo#room/123"); + + let instance = Message::create_webrtc_instance("bla.foo#room$ROOM", "123"); + assert_eq!(instance, "https://bla.foo#room123"); + + let instance = Message::create_webrtc_instance("bla.foo#room=$ROOM&after=cont", "234"); + assert_eq!(instance, "https://bla.foo#room=234&after=cont"); + } + #[async_std::test] async fn test_get_width_height() { let t = test::TestContext::new().await; From 38a32d176bc0d0493ec46c2a625d2a5f86ea33ee Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 14 Aug 2020 00:45:21 +0200 Subject: [PATCH 3/7] add a slash before room if there is no other separator --- src/message.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/message.rs b/src/message.rs index 55fe645b5..034a3eb99 100644 --- a/src/message.rs +++ b/src/message.rs @@ -649,7 +649,18 @@ impl Message { if instance.contains("$ROOM") { instance.replace("$ROOM", &room) } else { - format!("{}{}", instance, room) + // if there nothing that would separate the room, add a slash as a separator + // this way, urls can be given as "https://meet.jit.si" as well as "https://meet.jit.si/" + let maybe_slash = if instance.ends_with("/") + || instance.ends_with("?") + || instance.ends_with("#") + || instance.ends_with("=") + { + "" + } else { + "/" + }; + format!("{}{}{}", instance, maybe_slash, room) } } From 8159141d44853c51b2dd6440f4f712788b2a0d26 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 14 Aug 2020 01:15:39 +0200 Subject: [PATCH 4/7] add https-scheme to videochat-instance, if missing in pattern --- src/message.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/message.rs b/src/message.rs index 034a3eb99..8c07bb2d2 100644 --- a/src/message.rs +++ b/src/message.rs @@ -646,21 +646,36 @@ impl Message { // add room to a webrtc_instance as defined by the corresponding config-value; // the result may still be prefixed by the type pub fn create_webrtc_instance(instance: &str, room: &str) -> String { - if instance.contains("$ROOM") { - instance.replace("$ROOM", &room) + let (videochat_type, mut url) = Message::parse_webrtc_instance(instance); + + // make sure, there is a scheme in the url + if !url.contains(":") { + url = format!("https://{}", url); + } + + // add/replace room + let url = if url.contains("$ROOM") { + url.replace("$ROOM", &room) } else { - // if there nothing that would separate the room, add a slash as a separator + // if there nothing that would separate the room, add a slash as a separator; // this way, urls can be given as "https://meet.jit.si" as well as "https://meet.jit.si/" - let maybe_slash = if instance.ends_with("/") - || instance.ends_with("?") - || instance.ends_with("#") - || instance.ends_with("=") + let maybe_slash = if url.ends_with("/") + || url.ends_with("?") + || url.ends_with("#") + || url.ends_with("=") { "" } else { "/" }; - format!("{}{}{}", instance, maybe_slash, room) + format!("{}{}{}", url, maybe_slash, room) + }; + + // re-add and normalize type + match videochat_type { + VideochatType::BasicWebrtc => format!("basicwebrtc:{}", url), + VideochatType::Jitsi => format!("jitsi:{}", url), + _ => url, } } From 0208c02ec29fcabf5fa5e49ce471ee3f564d7ba1 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 14 Aug 2020 11:55:36 +0200 Subject: [PATCH 5/7] ignore whitespace in given webrtc_instance --- src/message.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/message.rs b/src/message.rs index 8c07bb2d2..d31361dde 100644 --- a/src/message.rs +++ b/src/message.rs @@ -681,6 +681,7 @@ impl Message { /// split a webrtc_instance as defined by the corresponding config-value into a type and a url pub fn parse_webrtc_instance(instance: &str) -> (VideochatType, String) { + let instance: String = instance.split_whitespace().collect(); let mut split = instance.splitn(2, ':'); let type_str = split.next().unwrap_or_default().to_lowercase(); let url = split.next(); @@ -1914,6 +1915,7 @@ mod tests { #[async_std::test] async fn test_create_webrtc_instance() { + // webrtc_instance may come from an input field of the ui, be pretty tolerant on input let instance = Message::create_webrtc_instance("https://meet.jit.si/", "123"); assert_eq!(instance, "https://meet.jit.si/123"); @@ -1940,6 +1942,12 @@ mod tests { let instance = Message::create_webrtc_instance("bla.foo#room=$ROOM&after=cont", "234"); assert_eq!(instance, "https://bla.foo#room=234&after=cont"); + + let instance = Message::create_webrtc_instance(" meet.jit .si ", "789"); + assert_eq!(instance, "https://meet.jit.si/789"); + + let instance = Message::create_webrtc_instance(" basicwebrtc: basic . stuff\n ", "12345ab"); + assert_eq!(instance, "basicwebrtc:https://basic.stuff/12345ab"); } #[async_std::test] From 810bd514d7b724b306d6955e7d3581bc52b30d2e Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 14 Aug 2020 13:35:02 +0200 Subject: [PATCH 6/7] make clippy happy --- src/message.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/message.rs b/src/message.rs index d31361dde..78ec8af13 100644 --- a/src/message.rs +++ b/src/message.rs @@ -649,7 +649,7 @@ impl Message { let (videochat_type, mut url) = Message::parse_webrtc_instance(instance); // make sure, there is a scheme in the url - if !url.contains(":") { + if !url.contains(':') { url = format!("https://{}", url); } @@ -659,10 +659,10 @@ impl Message { } else { // if there nothing that would separate the room, add a slash as a separator; // this way, urls can be given as "https://meet.jit.si" as well as "https://meet.jit.si/" - let maybe_slash = if url.ends_with("/") - || url.ends_with("?") - || url.ends_with("#") - || url.ends_with("=") + let maybe_slash = if url.ends_with('/') + || url.ends_with('?') + || url.ends_with('#') + || url.ends_with('=') { "" } else { From 54edd4d2117087c84b1bce95d0d02b5ca0b780b2 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Fri, 14 Aug 2020 13:41:51 +0200 Subject: [PATCH 7/7] force enum-match exhaustive --- src/message.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/message.rs b/src/message.rs index 78ec8af13..1139a5c63 100644 --- a/src/message.rs +++ b/src/message.rs @@ -675,7 +675,7 @@ impl Message { match videochat_type { VideochatType::BasicWebrtc => format!("basicwebrtc:{}", url), VideochatType::Jitsi => format!("jitsi:{}", url), - _ => url, + VideochatType::Unknown => url, } }