Skip to content

Commit b5f8995

Browse files
committed
change update.notify to a dict of addr:text_to_notify and allow to notify all using the special addr '*'
1 parent c6dd035 commit b5f8995

File tree

1 file changed

+106
-23
lines changed

1 file changed

+106
-23
lines changed

src/webxdc.rs

Lines changed: 106 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod integration;
1919
mod maps_integration;
2020

2121
use std::cmp::max;
22+
use std::collections::HashMap;
2223
use std::path::Path;
2324

2425
use anyhow::{anyhow, bail, ensure, format_err, Context as _, Result};
@@ -202,7 +203,7 @@ pub struct StatusUpdateItem {
202203

203204
/// Array of other users `selfAddr` that should be notified about this update.
204205
#[serde(skip_serializing_if = "Option::is_none")]
205-
pub notify: Option<Vec<String>>,
206+
pub notify: Option<HashMap<String, String>>,
206207
}
207208

208209
/// Update items as passed to the UIs.
@@ -336,7 +337,6 @@ impl Context {
336337
};
337338

338339
let mut notify_msg_id = instance.id;
339-
let mut notify_text = "".to_string();
340340
let mut param_changed = false;
341341

342342
let mut instance = instance.clone();
@@ -358,7 +358,6 @@ impl Context {
358358
let summary = sanitize_bidi_characters(summary);
359359
instance.param.set(Param::WebxdcSummary, summary.clone());
360360
param_changed = true;
361-
notify_text = summary;
362361
}
363362
}
364363

@@ -393,7 +392,6 @@ impl Context {
393392
)
394393
.await?;
395394
}
396-
notify_text = info.to_string();
397395

398396
if let Some(href) = status_update_item.href {
399397
let mut notify_msg = Message::load_from_db(self, notify_msg_id).await?;
@@ -415,14 +413,20 @@ impl Context {
415413
});
416414
}
417415

418-
if !notify_text.is_empty() && from_id != ContactId::SELF {
416+
if from_id != ContactId::SELF {
419417
if let Some(notify_list) = status_update_item.notify {
420418
let self_addr = instance.get_webxdc_self_addr(self).await?;
421-
if notify_list.contains(&self_addr) {
419+
if let Some(notify_text) = notify_list.get(&self_addr) {
422420
self.emit_event(EventType::IncomingWebxdcNotify {
423421
contact_id: from_id,
424422
msg_id: notify_msg_id,
425-
text: notify_text,
423+
text: notify_text.clone(),
424+
});
425+
} else if let Some(notify_text) = notify_list.get("*") {
426+
self.emit_event(EventType::IncomingWebxdcNotify {
427+
contact_id: from_id,
428+
msg_id: notify_msg_id,
429+
text: notify_text.clone(),
426430
});
427431
}
428432
}
@@ -2970,7 +2974,7 @@ sth_for_the = "future""#
29702974
.send_webxdc_status_update(
29712975
alice_instance.id,
29722976
&format!(
2973-
"{{\"payload\":7,\"info\": \"Alice moved\",\"notify\":[\"{}\"]}}",
2977+
"{{\"payload\":7,\"info\": \"Alice moved\",\"notify\":{{\"{}\": \"Your move!\"}} }}",
29742978
bob_instance.get_webxdc_self_addr(&bob).await?
29752979
),
29762980
)
@@ -2979,17 +2983,20 @@ sth_for_the = "future""#
29792983
let sent2 = alice.pop_sent_msg().await;
29802984
let info_msg = alice.get_last_msg().await;
29812985
assert!(info_msg.is_info());
2982-
assert!(!has_incoming_webxdc_event(&alice, info_msg, "Alice moved").await);
2986+
assert_eq!(info_msg.text, "Alice moved");
2987+
assert!(!has_incoming_webxdc_event(&alice, info_msg, "").await);
29832988

29842989
bob.recv_msg_trash(&sent2).await;
29852990
let info_msg = bob.get_last_msg().await;
29862991
assert!(info_msg.is_info());
2987-
assert!(has_incoming_webxdc_event(&bob, info_msg, "Alice moved").await);
2992+
assert_eq!(info_msg.text, "Alice moved");
2993+
assert!(has_incoming_webxdc_event(&bob, info_msg, "Your move!").await);
29882994

29892995
fiona.recv_msg_trash(&sent2).await;
29902996
let info_msg = fiona.get_last_msg().await;
29912997
assert!(info_msg.is_info());
2992-
assert!(!has_incoming_webxdc_event(&fiona, info_msg, "Alice moved").await);
2998+
assert_eq!(info_msg.text, "Alice moved");
2999+
assert!(!has_incoming_webxdc_event(&fiona, info_msg, "").await);
29933000

29943001
Ok(())
29953002
}
@@ -3013,7 +3020,7 @@ sth_for_the = "future""#
30133020
.send_webxdc_status_update(
30143021
alice_instance.id,
30153022
&format!(
3016-
"{{\"payload\":7,\"info\": \"moved\", \"summary\": \"ignored for notify as info is set\", \"notify\":[\"{}\",\"{}\"]}}",
3023+
"{{\"payload\":7,\"info\": \"moved\", \"summary\": \"move summary\", \"notify\":{{\"{}\":\"move, Bob\",\"{}\":\"move, Fiona\"}} }}",
30173024
bob_instance.get_webxdc_self_addr(&bob).await?,
30183025
fiona_instance.get_webxdc_self_addr(&fiona).await?
30193026
),
@@ -3024,17 +3031,17 @@ sth_for_the = "future""#
30243031
let sent2 = alice.pop_sent_msg().await;
30253032
let info_msg = alice.get_last_msg().await;
30263033
assert!(info_msg.is_info());
3027-
assert!(!has_incoming_webxdc_event(&alice, info_msg, "moved").await);
3034+
assert!(!has_incoming_webxdc_event(&alice, info_msg, "").await);
30283035

30293036
bob.recv_msg_trash(&sent2).await;
30303037
let info_msg = bob.get_last_msg().await;
30313038
assert!(info_msg.is_info());
3032-
assert!(has_incoming_webxdc_event(&bob, info_msg, "moved").await);
3039+
assert!(has_incoming_webxdc_event(&bob, info_msg, "move, Bob").await);
30333040

30343041
fiona.recv_msg_trash(&sent2).await;
30353042
let info_msg = fiona.get_last_msg().await;
30363043
assert!(info_msg.is_info());
3037-
assert!(has_incoming_webxdc_event(&fiona, info_msg, "moved").await);
3044+
assert!(has_incoming_webxdc_event(&fiona, info_msg, "move, Fiona").await);
30383045

30393046
Ok(())
30403047
}
@@ -3060,7 +3067,7 @@ sth_for_the = "future""#
30603067
.send_webxdc_status_update(
30613068
alice_instance.id,
30623069
&format!(
3063-
"{{\"payload\":7,\"info\": \"moved\", \"notify\":[\"{}\"]}}",
3070+
"{{\"payload\":7,\"info\": \"moved\", \"notify\":{{\"{}\": \"bla\"}} }}",
30643071
alice2_instance.get_webxdc_self_addr(&alice2).await?
30653072
),
30663073
)
@@ -3069,44 +3076,120 @@ sth_for_the = "future""#
30693076
let sent2 = alice.pop_sent_msg().await;
30703077
let info_msg = alice.get_last_msg().await;
30713078
assert!(info_msg.is_info());
3072-
assert!(!has_incoming_webxdc_event(&alice, info_msg, "moved").await);
3079+
assert!(!has_incoming_webxdc_event(&alice, info_msg, "").await);
30733080

30743081
alice2.recv_msg_trash(&sent2).await;
30753082
let info_msg = alice2.get_last_msg().await;
30763083
assert!(info_msg.is_info());
3077-
assert!(!has_incoming_webxdc_event(&alice2, info_msg, "moved").await);
3084+
assert!(!has_incoming_webxdc_event(&alice2, info_msg, "").await);
30783085

30793086
Ok(())
30803087
}
30813088

30823089
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
3083-
async fn test_webxdc_notify_summary() -> Result<()> {
3090+
async fn test_webxdc_notify_all() -> Result<()> {
30843091
let mut tcm = TestContextManager::new();
30853092
let alice = tcm.alice().await;
30863093
let bob = tcm.bob().await;
3094+
let fiona = tcm.fiona().await;
30873095

30883096
let grp_id = alice
3089-
.create_group_with_members(ProtectionStatus::Unprotected, "grp", &[&bob])
3097+
.create_group_with_members(ProtectionStatus::Unprotected, "grp", &[&bob, &fiona])
3098+
.await;
3099+
let alice_instance = send_webxdc_instance(&alice, grp_id).await?;
3100+
let sent1 = alice.pop_sent_msg().await;
3101+
bob.recv_msg(&sent1).await;
3102+
fiona.recv_msg(&sent1).await;
3103+
3104+
alice
3105+
.send_webxdc_status_update(
3106+
alice_instance.id,
3107+
"{\"payload\":7,\"info\": \"go\", \"notify\":{\"*\":\"notify all\"} }",
3108+
)
3109+
.await?;
3110+
alice.flush_status_updates().await?;
3111+
let sent2 = alice.pop_sent_msg().await;
3112+
let info_msg = alice.get_last_msg().await;
3113+
assert_eq!(info_msg.text, "go");
3114+
assert!(!has_incoming_webxdc_event(&alice, info_msg, "").await);
3115+
3116+
bob.recv_msg_trash(&sent2).await;
3117+
let info_msg = bob.get_last_msg().await;
3118+
assert_eq!(info_msg.text, "go");
3119+
assert!(has_incoming_webxdc_event(&bob, info_msg, "notify all").await);
3120+
3121+
fiona.recv_msg_trash(&sent2).await;
3122+
let info_msg = fiona.get_last_msg().await;
3123+
assert_eq!(info_msg.text, "go");
3124+
assert!(has_incoming_webxdc_event(&fiona, info_msg, "notify all").await);
3125+
3126+
Ok(())
3127+
}
3128+
3129+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
3130+
async fn test_webxdc_notify_bob_and_all() -> Result<()> {
3131+
let mut tcm = TestContextManager::new();
3132+
let alice = tcm.alice().await;
3133+
let bob = tcm.bob().await;
3134+
let fiona = tcm.fiona().await;
3135+
3136+
let grp_id = alice
3137+
.create_group_with_members(ProtectionStatus::Unprotected, "grp", &[&bob, &fiona])
30903138
.await;
30913139
let alice_instance = send_webxdc_instance(&alice, grp_id).await?;
30923140
let sent1 = alice.pop_sent_msg().await;
30933141
let bob_instance = bob.recv_msg(&sent1).await;
3142+
let fiona_instance = fiona.recv_msg(&sent1).await;
30943143

30953144
alice
30963145
.send_webxdc_status_update(
30973146
alice_instance.id,
30983147
&format!(
3099-
"{{\"payload\":7,\"summary\": \"4 moves done\",\"notify\":[\"{}\"]}}",
3148+
"{{\"payload\":7, \"notify\":{{\"{}\": \"notify bob\",\"*\": \"notify all\"}} }}",
31003149
bob_instance.get_webxdc_self_addr(&bob).await?
31013150
),
31023151
)
31033152
.await?;
31043153
alice.flush_status_updates().await?;
31053154
let sent2 = alice.pop_sent_msg().await;
3106-
assert!(!has_incoming_webxdc_event(&alice, alice_instance, "4 moves done").await);
3155+
bob.recv_msg_trash(&sent2).await;
3156+
fiona.recv_msg_trash(&sent2).await;
3157+
assert!(has_incoming_webxdc_event(&bob, bob_instance, "notify bob").await);
3158+
assert!(has_incoming_webxdc_event(&fiona, fiona_instance, "notify all").await);
3159+
3160+
Ok(())
3161+
}
3162+
3163+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
3164+
async fn test_webxdc_notify_all_and_bob() -> Result<()> {
3165+
let mut tcm = TestContextManager::new();
3166+
let alice = tcm.alice().await;
3167+
let bob = tcm.bob().await;
3168+
let fiona = tcm.fiona().await;
3169+
3170+
let grp_id = alice
3171+
.create_group_with_members(ProtectionStatus::Unprotected, "grp", &[&bob, &fiona])
3172+
.await;
3173+
let alice_instance = send_webxdc_instance(&alice, grp_id).await?;
3174+
let sent1 = alice.pop_sent_msg().await;
3175+
let bob_instance = bob.recv_msg(&sent1).await;
3176+
let fiona_instance = fiona.recv_msg(&sent1).await;
31073177

3178+
alice
3179+
.send_webxdc_status_update(
3180+
alice_instance.id,
3181+
&format!(
3182+
"{{\"payload\":7, \"notify\":{{\"*\": \"notify all\", \"{}\": \"notify bob\"}} }}",
3183+
bob_instance.get_webxdc_self_addr(&bob).await?
3184+
),
3185+
)
3186+
.await?;
3187+
alice.flush_status_updates().await?;
3188+
let sent2 = alice.pop_sent_msg().await;
31083189
bob.recv_msg_trash(&sent2).await;
3109-
assert!(has_incoming_webxdc_event(&bob, bob_instance, "4 moves done").await);
3190+
fiona.recv_msg_trash(&sent2).await;
3191+
assert!(has_incoming_webxdc_event(&bob, bob_instance, "notify bob").await);
3192+
assert!(has_incoming_webxdc_event(&fiona, fiona_instance, "notify all").await);
31103193

31113194
Ok(())
31123195
}

0 commit comments

Comments
 (0)