Skip to content

Commit 353545a

Browse files
Fix serialization failure for Zulip API
Fixes bug(s) introduced in #653.
1 parent 2f0fddb commit 353545a

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/zulip.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ async fn execute_for_other_user(
280280
id: user.user_id,
281281
email: &user.email,
282282
},
283-
content: &message
283+
content: &message,
284284
}
285285
.send(ctx.github.raw())
286286
.await;
@@ -380,10 +380,34 @@ impl<'a> MessageApiRequest<'a> {
380380
pub async fn send(&self, client: &reqwest::Client) -> anyhow::Result<reqwest::Response> {
381381
let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
382382

383+
#[derive(serde::Serialize)]
384+
struct SerializedApi<'a> {
385+
#[serde(rename = "type")]
386+
type_: &'static str,
387+
to: String,
388+
#[serde(skip_serializing_if = "Option::is_none")]
389+
topic: Option<&'a str>,
390+
content: &'a str,
391+
}
392+
383393
Ok(client
384394
.post("https://rust-lang.zulipchat.com/api/v1/messages")
385395
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
386-
.form(&self)
396+
.form(&SerializedApi {
397+
type_: match self.recipient {
398+
Recipient::Stream { .. } => "stream",
399+
Recipient::Private { .. } => "private",
400+
},
401+
to: match self.recipient {
402+
Recipient::Stream { id, .. } => id.to_string(),
403+
Recipient::Private { email, .. } => email.to_string(),
404+
},
405+
topic: match self.recipient {
406+
Recipient::Stream { topic, .. } => Some(topic),
407+
Recipient::Private { .. } => None,
408+
},
409+
content: self.content,
410+
})
387411
.send()
388412
.await?)
389413
}

0 commit comments

Comments
 (0)