diff --git a/src/zulip.rs b/src/zulip.rs index c9b2f62f9..cf58cc0cb 100644 --- a/src/zulip.rs +++ b/src/zulip.rs @@ -71,7 +71,7 @@ pub struct Request { } #[derive(Clone, Debug, serde::Deserialize)] -struct Message { +pub(crate) struct Message { id: u64, sender_id: u64, /// A unique ID for the set of users receiving the message (either a @@ -375,10 +375,15 @@ async fn handle_command<'a>( StreamCommand::AssignPriority { issue_num, prio } => { let _ = match assign_issue_prio(&ctx, message_data, issue_num, prio).await { // give user feedback - Ok(_) => ctx.zulip.add_reaction(message_data.id, "check").await, + Ok(_) => { + // emoji reaction for the triagebot command + ctx.zulip.add_reaction(message_data.id, "check").await?; + // resolve the Zulip topic + ctx.zulip.resolve_topic(message_data.clone()).await?; + } Err(err) => { log::error!("Could not assign priority to #{}: {:?}", issue_num, err); - ctx.zulip.add_reaction(message_data.id, "scream").await + ctx.zulip.add_reaction(message_data.id, "scream").await?; } }; Ok(None) diff --git a/src/zulip/client.rs b/src/zulip/client.rs index bb92773b5..5a6c815ba 100644 --- a/src/zulip/client.rs +++ b/src/zulip/client.rs @@ -201,6 +201,45 @@ impl ZulipClient { .into() }) } + + // https://zulip.com/api/update-message#parameter-topic + pub(crate) async fn resolve_topic(&self, message_data: super::Message) -> anyhow::Result<()> { + let message_id = message_data.id; + let topic = message_data + .subject + .unwrap_or_default() + .replacen("", "✔ ", 1); + #[derive(serde::Serialize)] + struct ResolveTopic<'a> { + topic: &'a str, + propagate_mode: &'a str, + send_notification_to_old_thread: bool, + send_notification_to_new_thread: bool, + } + + let resp = self + .make_request(Method::PATCH, &format!("messages/{message_id}")) + .form(&ResolveTopic { + topic: &topic, + propagate_mode: "change_all", + send_notification_to_old_thread: false, + send_notification_to_new_thread: false, + }) + .send() + .await + .context("failed to add reaction to a Zulip message")?; + + let status = resp.status(); + if !status.is_success() { + let body = resp + .text() + .await + .context("fail receiving Zulip API response (when adding a reaction)")?; + anyhow::bail!(body) + } + + Ok(()) + } } async fn deserialize_response(response: Response) -> anyhow::Result