Skip to content

Commit

Permalink
emit DC_EVENT_INCOMING_WEBXDC_NOTIFY instead of DC_EVENT_INCOMING_MSG
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Nov 22, 2024
1 parent f2863ed commit efc6fbb
Showing 1 changed file with 59 additions and 48 deletions.
107 changes: 59 additions & 48 deletions src/webxdc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,47 @@ impl Context {
return Ok(None);
};

let notify = if let Some(notify_list) = status_update_item.notify {
if let Ok(self_addr) = instance.get_webxdc_self_addr(self).await {
notify_list.contains(&self_addr)
} else {
false
}
} else {
false
};
let mut notify_msg_id = instance.id;
let mut notify_text = "".to_string();
let mut param_changed = false;

let mut instance = instance.clone();
if let Some(ref document) = status_update_item.document {
if instance
.param
.update_timestamp(Param::WebxdcDocumentTimestamp, timestamp)?
{
instance.param.set(Param::WebxdcDocument, document);
param_changed = true;
}
}

if let Some(ref summary) = status_update_item.summary {
if instance
.param
.update_timestamp(Param::WebxdcSummaryTimestamp, timestamp)?
{
let summary = sanitize_bidi_characters(summary);
instance.param.set(Param::WebxdcSummary, summary.clone());
param_changed = true;
notify_text = summary;
}
}

if can_info_msg {
if let Some(ref info) = status_update_item.info {
let notify_list = status_update_item.notify;
let _notify = if let Some(notify_list) = notify_list {
if let Ok(self_addr) = instance.get_webxdc_self_addr(self).await {
notify_list.contains(&self_addr)
} else {
false
}
} else {
false
};

if let Some(overwritable_info) =
self.get_overwritable_info_msg_id(instance, from_id).await?
if let Some(overwritable_info) = self
.get_overwritable_info_msg_id(&instance, from_id)
.await?
{
chat::update_msg_text_and_timestamp(
self,
Expand All @@ -342,44 +368,21 @@ impl Context {
timestamp,
)
.await?;
notify_msg_id = overwritable_info;
} else {
chat::add_info_msg_with_cmd(
notify_msg_id = chat::add_info_msg_with_cmd(
self,
instance.chat_id,
info.as_str(),
SystemMessage::WebxdcInfoMessage,
timestamp,
None,
Some(instance),
Some(&instance),
Some(from_id),
)
.await?;
}
}
}

let mut param_changed = false;

let mut instance = instance.clone();
if let Some(ref document) = status_update_item.document {
if instance
.param
.update_timestamp(Param::WebxdcDocumentTimestamp, timestamp)?
{
instance.param.set(Param::WebxdcDocument, document);
param_changed = true;
}
}

if let Some(ref summary) = status_update_item.summary {
if instance
.param
.update_timestamp(Param::WebxdcSummaryTimestamp, timestamp)?
{
instance
.param
.set(Param::WebxdcSummary, sanitize_bidi_characters(summary));
param_changed = true;
notify_text = info.to_string();
}
}

Expand All @@ -395,6 +398,14 @@ impl Context {
});
}

if notify && !notify_text.is_empty() {
self.emit_event(EventType::IncomingWebxdcNotify {
contact_id: from_id,
msg_id: notify_msg_id,
text: notify_text,
});
}

Ok(Some(status_update_serial))
}

Expand Down Expand Up @@ -2923,10 +2934,10 @@ sth_for_the = "future""#
Ok(())
}

async fn has_incoming_msg_event(t: &TestContext, msg: Message) -> bool {
async fn has_incoming_webxdc_event(t: &TestContext, msg: Message) -> bool {
t.evtracker
.get_matching_opt(t, |evt| {
if let EventType::IncomingMsg { msg_id, .. } = evt {
if let EventType::IncomingWebxdcNotify { msg_id, .. } = evt {
*msg_id == msg.id
} else {
false
Expand Down Expand Up @@ -2967,17 +2978,17 @@ sth_for_the = "future""#
let sent2 = alice.pop_sent_msg().await;
let info_msg = alice.get_last_msg().await;
assert!(info_msg.is_info());
assert!(!has_incoming_msg_event(&alice, info_msg).await);
assert!(!has_incoming_webxdc_event(&alice, info_msg).await);

bob.recv_msg_trash(&sent2).await;
let info_msg = bob.get_last_msg().await;
assert!(info_msg.is_info());
assert!(has_incoming_msg_event(&bob, info_msg).await);
assert!(has_incoming_webxdc_event(&bob, info_msg).await);

fiona.recv_msg_trash(&sent2).await;
let info_msg = fiona.get_last_msg().await;
assert!(info_msg.is_info());
assert!(!has_incoming_msg_event(&fiona, info_msg).await);
assert!(!has_incoming_webxdc_event(&fiona, info_msg).await);

Ok(())
}
Expand Down Expand Up @@ -3012,17 +3023,17 @@ sth_for_the = "future""#
let sent2 = alice.pop_sent_msg().await;
let info_msg = alice.get_last_msg().await;
assert!(info_msg.is_info());
assert!(!has_incoming_msg_event(&alice, info_msg).await);
assert!(!has_incoming_webxdc_event(&alice, info_msg).await);

bob.recv_msg_trash(&sent2).await;
let info_msg = bob.get_last_msg().await;
assert!(info_msg.is_info());
assert!(has_incoming_msg_event(&bob, info_msg).await);
assert!(has_incoming_webxdc_event(&bob, info_msg).await);

fiona.recv_msg_trash(&sent2).await;
let info_msg = fiona.get_last_msg().await;
assert!(info_msg.is_info());
assert!(has_incoming_msg_event(&fiona, info_msg).await);
assert!(has_incoming_webxdc_event(&fiona, info_msg).await);

Ok(())
}
Expand Down

0 comments on commit efc6fbb

Please sign in to comment.