let render_webxdc_status_update_object() return an Option; add a test for that

This commit is contained in:
B. Petersen
2022-01-10 13:26:33 +01:00
committed by bjoern
parent 42f9ef00b9
commit 327328412a
2 changed files with 36 additions and 6 deletions

View File

@@ -1163,10 +1163,10 @@ 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::Webxdc { } else if self.msg.viewtype == Viewtype::Webxdc {
let json = context if let Some(json) = context
.render_webxdc_status_update_object(self.msg.id, None) .render_webxdc_status_update_object(self.msg.id, None)
.await?; .await?
if json != r#"{{"updates":[]}}"# { {
parts.push(context.build_status_update_part(&json).await); parts.push(context.build_status_update_part(&json).await);
} }
} }

View File

@@ -157,7 +157,8 @@ impl Context {
instance_msg_id, instance_msg_id,
Some(status_update_id), Some(status_update_id),
) )
.await?, .await?
.ok_or_else(|| format_err!("Status object expected."))?,
); );
status_update.set_quote(self, Some(&instance)).await?; status_update.set_quote(self, Some(&instance)).await?;
let status_update_msg_id = let status_update_msg_id =
@@ -257,11 +258,15 @@ impl Context {
&self, &self,
instance_msg_id: MsgId, instance_msg_id: MsgId,
status_update_id: Option<StatusUpdateId>, status_update_id: Option<StatusUpdateId>,
) -> Result<String> { ) -> Result<Option<String>> {
let updates_array = self let updates_array = self
.get_webxdc_status_updates(instance_msg_id, status_update_id) .get_webxdc_status_updates(instance_msg_id, status_update_id)
.await?; .await?;
Ok(format!(r#"{{"updates":{}}}"#, updates_array)) if updates_array == "[]" {
Ok(None)
} else {
Ok(Some(format!(r#"{{"updates":{}}}"#, updates_array)))
}
} }
} }
@@ -717,6 +722,31 @@ mod tests {
Ok(()) Ok(())
} }
#[async_std::test]
async fn test_render_webxdc_status_update_object() -> Result<()> {
let t = TestContext::new_alice().await;
let chat_id = create_group_chat(&t, ProtectionStatus::Unprotected, "a chat").await?;
let mut instance = create_webxdc_instance(
&t,
"minimal.xdc",
include_bytes!("../test-data/webxdc/minimal.xdc"),
)
.await?;
chat_id.set_draft(&t, Some(&mut instance)).await?;
assert!(t
.render_webxdc_status_update_object(instance.id, None)
.await?
.is_none());
t.send_webxdc_status_update(instance.id, "1", "bla").await?;
assert!(t
.render_webxdc_status_update_object(instance.id, None)
.await?
.is_some());
Ok(())
}
#[async_std::test] #[async_std::test]
async fn test_draft_and_send_webxdc_status_update() -> Result<()> { async fn test_draft_and_send_webxdc_status_update() -> Result<()> {
let alice = TestContext::new_alice().await; let alice = TestContext::new_alice().await;