mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
wrap payload to a json-structure that can be extended as needed
This commit is contained in:
@@ -1163,9 +1163,7 @@ impl<'a> MimeFactory<'a> {
|
|||||||
let json = self.msg.param.get(Param::Arg).unwrap_or_default();
|
let json = self.msg.param.get(Param::Arg).unwrap_or_default();
|
||||||
parts.push(context.build_status_update_part(json).await);
|
parts.push(context.build_status_update_part(json).await);
|
||||||
} else if self.msg.viewtype == Viewtype::W30 {
|
} else if self.msg.viewtype == Viewtype::W30 {
|
||||||
let json = context
|
let json = context.get_w30_status_updates(self.msg.id, None).await?;
|
||||||
.get_w30_status_updates_with_format(self.msg.id, None, true)
|
|
||||||
.await?;
|
|
||||||
if json != "[]" {
|
if json != "[]" {
|
||||||
parts.push(context.build_status_update_part(&json).await);
|
parts.push(context.build_status_update_part(&json).await);
|
||||||
}
|
}
|
||||||
|
|||||||
82
src/w30.rs
82
src/w30.rs
@@ -42,7 +42,8 @@ impl rusqlite::types::ToSql for StatusUpdateId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
/// Update items as sent on the wire and as stored in the database.
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub(crate) struct StatusUpdateItem {
|
pub(crate) struct StatusUpdateItem {
|
||||||
payload: Value,
|
payload: Value,
|
||||||
}
|
}
|
||||||
@@ -65,13 +66,14 @@ impl Context {
|
|||||||
if payload.is_empty() {
|
if payload.is_empty() {
|
||||||
bail!("create_status_update_record: empty payload");
|
bail!("create_status_update_record: empty payload");
|
||||||
}
|
}
|
||||||
let _test: Value = serde_json::from_str(payload)?; // checks if input data are valid json
|
let payload: Value = serde_json::from_str(payload)?; // checks if input data are valid json
|
||||||
|
let status_update_item = StatusUpdateItem { payload };
|
||||||
|
|
||||||
let rowid = self
|
let rowid = self
|
||||||
.sql
|
.sql
|
||||||
.insert(
|
.insert(
|
||||||
"INSERT INTO msgs_status_updates (msg_id, payload) VALUES(?, ?);",
|
"INSERT INTO msgs_status_updates (msg_id, payload) VALUES(?, ?);",
|
||||||
paramsv![instance_msg_id, payload],
|
paramsv![instance_msg_id, serde_json::to_string(&status_update_item)?],
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(StatusUpdateId(u32::try_from(rowid)?))
|
Ok(StatusUpdateId(u32::try_from(rowid)?))
|
||||||
@@ -118,12 +120,8 @@ impl Context {
|
|||||||
status_update.param.set_cmd(SystemMessage::W30StatusUpdate);
|
status_update.param.set_cmd(SystemMessage::W30StatusUpdate);
|
||||||
status_update.param.set(
|
status_update.param.set(
|
||||||
Param::Arg,
|
Param::Arg,
|
||||||
self.get_w30_status_updates_with_format(
|
self.get_w30_status_updates(instance_msg_id, Some(status_update_id))
|
||||||
instance_msg_id,
|
.await?,
|
||||||
Some(status_update_id),
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await?,
|
|
||||||
);
|
);
|
||||||
status_update.set_quote(self, &instance).await?;
|
status_update.set_quote(self, &instance).await?;
|
||||||
let status_update_msg_id =
|
let status_update_msg_id =
|
||||||
@@ -184,33 +182,13 @@ impl Context {
|
|||||||
|
|
||||||
/// Returns status updates as an JSON-array.
|
/// Returns status updates as an JSON-array.
|
||||||
///
|
///
|
||||||
|
/// Example: `[{"payload":"any update data"},{"payload":"another update data"}]`
|
||||||
/// The updates may be filtered by a given status_update_id;
|
/// The updates may be filtered by a given status_update_id;
|
||||||
/// if no updates are available, an empty JSON-array is returned.
|
/// if no updates are available, an empty JSON-array is returned.
|
||||||
pub async fn get_w30_status_updates(
|
pub async fn get_w30_status_updates(
|
||||||
&self,
|
&self,
|
||||||
instance_msg_id: MsgId,
|
instance_msg_id: MsgId,
|
||||||
status_update_id: Option<StatusUpdateId>,
|
status_update_id: Option<StatusUpdateId>,
|
||||||
) -> Result<String> {
|
|
||||||
self.get_w30_status_updates_with_format(instance_msg_id, status_update_id, false)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns status updates as JSON-array.
|
|
||||||
///
|
|
||||||
/// If `for_wire` is `false`, the result is suitable to be passed to the app,
|
|
||||||
/// that get back exactly the payloads as sent:
|
|
||||||
/// `["any update data", "another update data"]`
|
|
||||||
/// get_w30_status_updates() returns this format.
|
|
||||||
///
|
|
||||||
/// If `for_wire` is `true`, the payload is wrapped to an object
|
|
||||||
/// and can be enhanced in the future:
|
|
||||||
/// `[{"payload":"any update data"},{"payload":"another update data"}]`
|
|
||||||
/// This is suitable for sending objects that are not visible to apps:
|
|
||||||
pub(crate) async fn get_w30_status_updates_with_format(
|
|
||||||
&self,
|
|
||||||
instance_msg_id: MsgId,
|
|
||||||
status_update_id: Option<StatusUpdateId>,
|
|
||||||
for_wire: bool,
|
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let json = self
|
let json = self
|
||||||
.sql
|
.sql
|
||||||
@@ -229,13 +207,7 @@ impl Context {
|
|||||||
if !json.is_empty() {
|
if !json.is_empty() {
|
||||||
json.push_str(",\n");
|
json.push_str(",\n");
|
||||||
}
|
}
|
||||||
if for_wire {
|
|
||||||
json.push_str("{\"payload\":");
|
|
||||||
}
|
|
||||||
json.push_str(&payload);
|
json.push_str(&payload);
|
||||||
if for_wire {
|
|
||||||
json.push('}');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(json)
|
Ok(json)
|
||||||
},
|
},
|
||||||
@@ -359,7 +331,7 @@ mod tests {
|
|||||||
t.send_w30_status_update(instance.id, "descr", "42").await?;
|
t.send_w30_status_update(instance.id, "descr", "42").await?;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
t.get_w30_status_updates(instance.id, None).await?,
|
t.get_w30_status_updates(instance.id, None).await?,
|
||||||
"[42]".to_string()
|
r#"[{"payload":42}]"#.to_string()
|
||||||
);
|
);
|
||||||
|
|
||||||
// set_draft(None) deletes the message without the need to simulate network
|
// set_draft(None) deletes the message without the need to simulate network
|
||||||
@@ -391,7 +363,7 @@ mod tests {
|
|||||||
.await?;
|
.await?;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
t.get_w30_status_updates(instance.id, Some(id)).await?,
|
t.get_w30_status_updates(instance.id, Some(id)).await?,
|
||||||
r#"[{"foo":"bar"}]"#
|
r#"[{"payload":{"foo":"bar"}}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
assert!(t
|
assert!(t
|
||||||
@@ -404,11 +376,11 @@ mod tests {
|
|||||||
.is_err());
|
.is_err());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
t.get_w30_status_updates(instance.id, Some(id)).await?,
|
t.get_w30_status_updates(instance.id, Some(id)).await?,
|
||||||
r#"[{"foo":"bar"}]"#
|
r#"[{"payload":{"foo":"bar"}}]"#
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
t.get_w30_status_updates(instance.id, None).await?,
|
t.get_w30_status_updates(instance.id, None).await?,
|
||||||
r#"[{"foo":"bar"}]"#
|
r#"[{"payload":{"foo":"bar"}}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
let id = t
|
let id = t
|
||||||
@@ -416,14 +388,14 @@ mod tests {
|
|||||||
.await?;
|
.await?;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
t.get_w30_status_updates(instance.id, Some(id)).await?,
|
t.get_w30_status_updates(instance.id, Some(id)).await?,
|
||||||
r#"[{"foo2":"bar2"}]"#
|
r#"[{"payload":{"foo2":"bar2"}}]"#
|
||||||
);
|
);
|
||||||
t.create_status_update_record(instance.id, "true").await?;
|
t.create_status_update_record(instance.id, "true").await?;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
t.get_w30_status_updates(instance.id, None).await?,
|
t.get_w30_status_updates(instance.id, None).await?,
|
||||||
r#"[{"foo":"bar"},
|
r#"[{"payload":{"foo":"bar"}},
|
||||||
{"foo2":"bar2"},
|
{"payload":{"foo2":"bar2"}},
|
||||||
true]"#
|
{"payload":true}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -452,16 +424,16 @@ true]"#
|
|||||||
.await?;
|
.await?;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
t.get_w30_status_updates(instance.id, None).await?,
|
t.get_w30_status_updates(instance.id, None).await?,
|
||||||
r#"[{"foo":"bar"}]"#
|
r#"[{"payload":{"foo":"bar"}}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
t.receive_status_update(instance.id, r#" [ {"payload" :42} , {"payload": 23} ] "#)
|
t.receive_status_update(instance.id, r#" [ {"payload" :42} , {"payload": 23} ] "#)
|
||||||
.await?;
|
.await?;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
t.get_w30_status_updates(instance.id, None).await?,
|
t.get_w30_status_updates(instance.id, None).await?,
|
||||||
r#"[{"foo":"bar"},
|
r#"[{"payload":{"foo":"bar"}},
|
||||||
42,
|
{"payload":42},
|
||||||
23]"#
|
{"payload":23}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -501,7 +473,7 @@ true]"#
|
|||||||
alice
|
alice
|
||||||
.get_w30_status_updates(alice_instance.id, None)
|
.get_w30_status_updates(alice_instance.id, None)
|
||||||
.await?,
|
.await?,
|
||||||
r#"[{"foo":"bar"}]"#
|
r#"[{"payload":{"foo":"bar"}}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
alice
|
alice
|
||||||
@@ -512,8 +484,8 @@ true]"#
|
|||||||
alice
|
alice
|
||||||
.get_w30_status_updates(alice_instance.id, None)
|
.get_w30_status_updates(alice_instance.id, None)
|
||||||
.await?,
|
.await?,
|
||||||
r#"[{"foo":"bar"},
|
r#"[{"payload":{"foo":"bar"}},
|
||||||
{"snipp":"snapp"}]"#
|
{"payload":{"snipp":"snapp"}}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
// Bob receives all messages
|
// Bob receives all messages
|
||||||
@@ -549,7 +521,7 @@ true]"#
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
bob.get_w30_status_updates(msg_id, Some(status_update_id))
|
bob.get_w30_status_updates(msg_id, Some(status_update_id))
|
||||||
.await?,
|
.await?,
|
||||||
r#"[{"foo":"bar"}]"#
|
r#"[{"payload":{"foo":"bar"}}]"#
|
||||||
);
|
);
|
||||||
assert_eq!(msg_id, bob_instance.id);
|
assert_eq!(msg_id, bob_instance.id);
|
||||||
}
|
}
|
||||||
@@ -559,7 +531,7 @@ true]"#
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
bob.get_w30_status_updates(bob_instance.id, None).await?,
|
bob.get_w30_status_updates(bob_instance.id, None).await?,
|
||||||
r#"[{"foo":"bar"}]"#
|
r#"[{"payload":{"foo":"bar"}}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
// Alice has a second device and also receives messages there
|
// Alice has a second device and also receives messages there
|
||||||
@@ -616,8 +588,8 @@ true]"#
|
|||||||
assert!(sent1.payload().contains(r#""payload":{"foo":"bar"}"#));
|
assert!(sent1.payload().contains(r#""payload":{"foo":"bar"}"#));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
bob.get_w30_status_updates(bob_instance.id, None).await?,
|
bob.get_w30_status_updates(bob_instance.id, None).await?,
|
||||||
r#"[{"foo":"bar"},
|
r#"[{"payload":{"foo":"bar"}},
|
||||||
42]"#
|
{"payload":42}]"#
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user