log to status update

& send webxdc update event
This commit is contained in:
Simon Laux
2022-05-05 13:06:28 +02:00
committed by Septias
parent 0538e68638
commit 948f6906cb
4 changed files with 71 additions and 19 deletions

View File

@@ -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;

View File

@@ -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,
},
});
}
}
}
}
});

View File

@@ -323,13 +323,13 @@ pub enum EventType {
},
}
impl Event {
pub fn to_json(&self) -> String {
impl EventType {
pub fn to_json(&self, timestamp: Option<i64>) -> Value {
let mut tree: serde_json::Map<String, Value> = 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)
}
}

View File

@@ -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<String>,
pub(crate) info: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
document: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
summary: Option<String>,
pub(crate) summary: Option<String>,
}
/// 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<StatusUpdateSerial> {
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,