@@ -643,8 +643,45 @@ impl Message {
643
643
None
644
644
}
645
645
646
+ // add room to a webrtc_instance as defined by the corresponding config-value;
647
+ // the result may still be prefixed by the type
648
+ pub fn create_webrtc_instance ( instance : & str , room : & str ) -> String {
649
+ let ( videochat_type, mut url) = Message :: parse_webrtc_instance ( instance) ;
650
+
651
+ // make sure, there is a scheme in the url
652
+ if !url. contains ( ':' ) {
653
+ url = format ! ( "https://{}" , url) ;
654
+ }
655
+
656
+ // add/replace room
657
+ let url = if url. contains ( "$ROOM" ) {
658
+ url. replace ( "$ROOM" , & room)
659
+ } else {
660
+ // if there nothing that would separate the room, add a slash as a separator;
661
+ // this way, urls can be given as "https://meet.jit.si" as well as "https://meet.jit.si/"
662
+ let maybe_slash = if url. ends_with ( '/' )
663
+ || url. ends_with ( '?' )
664
+ || url. ends_with ( '#' )
665
+ || url. ends_with ( '=' )
666
+ {
667
+ ""
668
+ } else {
669
+ "/"
670
+ } ;
671
+ format ! ( "{}{}{}" , url, maybe_slash, room)
672
+ } ;
673
+
674
+ // re-add and normalize type
675
+ match videochat_type {
676
+ VideochatType :: BasicWebrtc => format ! ( "basicwebrtc:{}" , url) ,
677
+ VideochatType :: Jitsi => format ! ( "jitsi:{}" , url) ,
678
+ VideochatType :: Unknown => url,
679
+ }
680
+ }
681
+
646
682
/// split a webrtc_instance as defined by the corresponding config-value into a type and a url
647
683
pub fn parse_webrtc_instance ( instance : & str ) -> ( VideochatType , String ) {
684
+ let instance: String = instance. split_whitespace ( ) . collect ( ) ;
648
685
let mut split = instance. splitn ( 2 , ':' ) ;
649
686
let type_str = split. next ( ) . unwrap_or_default ( ) . to_lowercase ( ) ;
650
687
let url = split. next ( ) ;
@@ -1877,6 +1914,43 @@ mod tests {
1877
1914
assert_eq ! ( url, "https://j.si/foo" ) ;
1878
1915
}
1879
1916
1917
+ #[ async_std:: test]
1918
+ async fn test_create_webrtc_instance ( ) {
1919
+ // webrtc_instance may come from an input field of the ui, be pretty tolerant on input
1920
+ let instance = Message :: create_webrtc_instance ( "https://meet.jit.si/" , "123" ) ;
1921
+ assert_eq ! ( instance, "https://meet.jit.si/123" ) ;
1922
+
1923
+ let instance = Message :: create_webrtc_instance ( "https://meet.jit.si" , "456" ) ;
1924
+ assert_eq ! ( instance, "https://meet.jit.si/456" ) ;
1925
+
1926
+ let instance = Message :: create_webrtc_instance ( "meet.jit.si" , "789" ) ;
1927
+ assert_eq ! ( instance, "https://meet.jit.si/789" ) ;
1928
+
1929
+ let instance = Message :: create_webrtc_instance ( "bla.foo?" , "123" ) ;
1930
+ assert_eq ! ( instance, "https://bla.foo?123" ) ;
1931
+
1932
+ let instance = Message :: create_webrtc_instance ( "jitsi:bla.foo#" , "456" ) ;
1933
+ assert_eq ! ( instance, "jitsi:https://bla.foo#456" ) ;
1934
+
1935
+ let instance = Message :: create_webrtc_instance ( "bla.foo#room=" , "789" ) ;
1936
+ assert_eq ! ( instance, "https://bla.foo#room=789" ) ;
1937
+
1938
+ let instance = Message :: create_webrtc_instance ( "https://bla.foo#room" , "123" ) ;
1939
+ assert_eq ! ( instance, "https://bla.foo#room/123" ) ;
1940
+
1941
+ let instance = Message :: create_webrtc_instance ( "bla.foo#room$ROOM" , "123" ) ;
1942
+ assert_eq ! ( instance, "https://bla.foo#room123" ) ;
1943
+
1944
+ let instance = Message :: create_webrtc_instance ( "bla.foo#room=$ROOM&after=cont" , "234" ) ;
1945
+ assert_eq ! ( instance, "https://bla.foo#room=234&after=cont" ) ;
1946
+
1947
+ let instance = Message :: create_webrtc_instance ( " meet.jit .si " , "789" ) ;
1948
+ assert_eq ! ( instance, "https://meet.jit.si/789" ) ;
1949
+
1950
+ let instance = Message :: create_webrtc_instance ( " basicwebrtc: basic . stuff\n " , "12345ab" ) ;
1951
+ assert_eq ! ( instance, "basicwebrtc:https://basic.stuff/12345ab" ) ;
1952
+ }
1953
+
1880
1954
#[ async_std:: test]
1881
1955
async fn test_get_width_height ( ) {
1882
1956
let t = test:: TestContext :: new ( ) . await ;
0 commit comments