From b6135dd717cbbf625e0a3a523eb14b6e4b243834 Mon Sep 17 00:00:00 2001 From: Maxime Augier Date: Sun, 18 Aug 2024 14:35:01 +0200 Subject: [PATCH] Redo mattermost support --- src/mattermost.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/mattermost.rs b/src/mattermost.rs index 003fd6f..9394e7b 100644 --- a/src/mattermost.rs +++ b/src/mattermost.rs @@ -4,15 +4,23 @@ use anyhow::Result; pub struct Context { pub base: String, - pub token: String, + pub auth_header: String, } pub struct Channel { channel_id: String, } + impl Context { + pub fn new(base: String, token: &str) -> Result { + Ok(Self { + base, + auth_header: format!("Bearer {token}"), + }) + } + fn path(&self, rel: &str) -> String { format!("{}/api/v4/{}", self.base, rel) } @@ -38,6 +46,18 @@ impl Context { self.set_custom_status(text, emoji) } - pub fn send_to_channel(&self, channel: &Channel, ) + pub fn channel(&self, id: &str) -> Channel { + Channel { channel_id: id.to_owned() } + } + + pub fn send_to_channel(&self, channel: &Channel, msg: &str) -> Result<()> { + let path = self.path("posts"); + ureq::post(&path) + .set("Authorization", &self.auth_header) + .send_json(json!( + { "channel_id": channel.channel_id, "message": msg } + ))?; + Ok(()) + } }