From 948f6906cb798cd4f8e5159194813f002437747b Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Thu, 5 May 2022 13:06:28 +0200 Subject: [PATCH] log to status update & send webxdc update event --- src/config.rs | 6 +++--- src/context.rs | 30 +++++++++++++++++++++++++++++- src/events.rs | 19 +++++++++++++------ src/webxdc.rs | 35 ++++++++++++++++++++++++++--------- 4 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/config.rs b/src/config.rs index d18dd1448..8ba377737 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,13 +1,13 @@ //! # Key-value configuration management. use anyhow::{ensure, Context as _, Result}; -use strum::{EnumProperty as EnumPropertyTrait, IntoEnumIterator}; -use async_std::fs::File; -use async_std::io::WriteExt; + +use strum::{EnumProperty, IntoEnumIterator}; use strum_macros::{AsRefStr, Display, EnumIter, EnumProperty, EnumString}; use crate::blob::BlobObject; +use crate::chat; use crate::constants::DC_VERSION_STR; use crate::contact::addr_cmp; use crate::context::Context; diff --git a/src/context.rs b/src/context.rs index 585c1e573..7886e02ea 100644 --- a/src/context.rs +++ b/src/context.rs @@ -25,6 +25,7 @@ use crate::scheduler::Scheduler; use crate::sql::Sql; use crate::stock_str::StockStrings; use crate::tools::{duration_to_str, time}; +use crate::webxdc::StatusUpdateItem; /// Builder for the [`Context`]. /// @@ -434,7 +435,7 @@ impl Context { pub fn emit_event(&self, event: EventType) { self.events.emit(Event { id: self.id, - typ: event, + typ: event.clone(), }); let context = self.clone(); async_std::task::spawn(async move { @@ -448,6 +449,33 @@ impl Context { .unwrap_or_default() .as_millis() as i64; // TODO + + let webxdc_instance_id = MsgId::new(debug_logging_webxdc as u32); + + match context + .internal_write_status_update( + &webxdc_instance_id, + StatusUpdateItem { + payload: event.to_json(Some(time)), + info: None, + summary: None, + }, + ) + .await + { + Err(err) => { + eprintln!("Can't log event to webxdc status update: {:#}", err); + } + Ok(serial) => { + context.events.emit(Event { + id: context.id, + typ: EventType::WebxdcStatusUpdate { + msg_id: webxdc_instance_id, + status_update_serial: serial, + }, + }); + } + } } } }); diff --git a/src/events.rs b/src/events.rs index d43724990..4f68f420a 100644 --- a/src/events.rs +++ b/src/events.rs @@ -323,13 +323,13 @@ pub enum EventType { }, } -impl Event { - pub fn to_json(&self) -> String { +impl EventType { + pub fn to_json(&self, timestamp: Option) -> Value { let mut tree: serde_json::Map = serde_json::Map::new(); - tree.insert("id".to_string(), Value::Number(self.as_id().into())); + tree.insert("event_type".to_string(), Value::Number(self.as_id().into())); - let (data1, data2) = match &self.typ { + let (data1, data2) = match &self { EventType::Info(data1) | EventType::Warning(data1) | EventType::Error(data1) @@ -340,7 +340,9 @@ impl Event { | EventType::ImapMessageMoved(data1) | EventType::NewBlobFile(data1) | EventType::DeletedBlobFile(data1) - | EventType::ErrorSelfNotInGroup(data1) => (Value::String(data1.to_owned()), Value::Null), + | EventType::ErrorSelfNotInGroup(data1) => { + (Value::String(data1.to_owned()), Value::Null) + } EventType::MsgsChanged { chat_id, msg_id } | EventType::IncomingMsg { chat_id, msg_id } | EventType::MsgDelivered { chat_id, msg_id } @@ -400,6 +402,11 @@ impl Event { tree.insert("data1".to_string(), data1); tree.insert("data2".to_string(), data2); - Value::Object(tree).to_string() + + if let Some(ts) = timestamp { + tree.insert("ts".to_string(), Value::Number(ts.into())); + } + + Value::Object(tree) } } diff --git a/src/webxdc.rs b/src/webxdc.rs index a4f185900..bd7f2b50f 100644 --- a/src/webxdc.rs +++ b/src/webxdc.rs @@ -118,16 +118,16 @@ struct StatusUpdates { /// Update items as sent on the wire and as stored in the database. #[derive(Debug, Serialize, Deserialize)] pub(crate) struct StatusUpdateItem { - payload: Value, + pub(crate) payload: Value, #[serde(skip_serializing_if = "Option::is_none")] - info: Option, + pub(crate) info: Option, #[serde(skip_serializing_if = "Option::is_none")] document: Option, #[serde(skip_serializing_if = "Option::is_none")] - summary: Option, + pub(crate) summary: Option, } /// Update items as passed to the UIs. @@ -323,12 +323,13 @@ impl Context { self.emit_msgs_changed(instance.chat_id, instance.id); } - let rowid = self - .sql - .insert( - "INSERT INTO msgs_status_updates (msg_id, update_item) VALUES(?, ?);", - paramsv![instance.id, serde_json::to_string(&status_update_item)?], - ) + let rowid = self.sql.insert( + "INSERT INTO msgs_status_updates (msg_id, update_item) VALUES(?, ?);", + paramsv![instance.id, serde_json::to_string(&status_update_item)?], + ).await?; + + let status_update_serial = self + .internal_write_status_update(&instance.id, status_update_item) .await?; let status_update_serial = StatusUpdateSerial(u32::try_from(rowid)?); @@ -343,6 +344,22 @@ impl Context { Ok(status_update_serial) } + pub(crate) async fn internal_write_status_update( + &self, + instance_id: &MsgId, + status_update_item: StatusUpdateItem, + ) -> Result { + let rowid = self + .sql + .insert( + "INSERT INTO msgs_status_updates (msg_id, update_item) VALUES(?, ?);", + paramsv![instance_id, serde_json::to_string(&status_update_item)?], + ) + .await?; + let status_update_serial = StatusUpdateSerial(u32::try_from(rowid)?); + Ok(status_update_serial) + } + /// Sends a status update for an webxdc instance. /// /// If the instance is a draft,