From 52872ffebf1bc205e9c079ccf025b72516a019e8 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 5 Jan 2022 01:26:06 +0100 Subject: [PATCH] Add - Channel Structure - API Endpoint of the list of available channels - Function who return the correct ID of the channel filled in the .env file --- src/mattermost.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/mattermost.rs b/src/mattermost.rs index 4b75c2b..481d9be 100644 --- a/src/mattermost.rs +++ b/src/mattermost.rs @@ -1,5 +1,6 @@ use chrono::prelude::*; use mattermost_api::prelude::*; +use serde::{Deserialize, Serialize}; use serde_json::json; mod erreur { @@ -106,6 +107,73 @@ impl Affichage for mattermost_api::models::TeamInformation { } } +/// Response struct from /channels +/// +/// Pour savoir ce qu'il faut mettre ici, il faut check la documentation +/// https://api.mattermost.com/#operation/GetAllChannels +/// et bien faire attention aux valeurs qui peuvent être null +/// -> on peut le savoir en regardant le détail de ce qui est envoyé à Mattermost +/// ex ici, il faut cliquer sur : 200 Channel list retrieval successful +/// pour accéder à la liste déroulante :) +#[derive(Debug, Deserialize, Serialize)] +pub struct ChannelInformation { + pub id: String, + pub create_at: i64, + pub update_at: i64, + pub delete_at: i64, + pub team_id: String, + #[serde(rename = "type")] + pub type_: String, + pub display_name: String, + pub name: String, + pub header: String, + pub purpose: String, + pub last_post_at: i64, + pub total_msg_count: i64, + pub extra_update_at: i64, + pub creator_id: String, + pub team_display_name: String, + pub team_name: String, + pub team_update_at: i64, + pub policy_id: Option, +} + +/// Récupère la liste de tous les salons disponibles +pub async fn get_channel_info( + api: &mattermost_api::client::Mattermost, +) -> Result, ApiError> { + api.query("GET", "channels", None, None).await +} + +/// Récupère l'ID du salon renseigné grâce à son nom en faisant une requête à l'API de Mattermost +#[allow(dead_code)] // je comprends pas pourquoi j'ai besoin de mettre ça, parce que j'utilises le code en dessous... +pub async fn channel_id_by_name(api: &mattermost_api::client::Mattermost, salon: String) -> String { + println!("\nTentative de récupération de l'équipe {}...", salon); + match get_channel_info(api).await { + Ok(channels) => { + for channel in &channels { + if &channel.name == &salon { + return channel.id.clone(); + } + } + + panic!( + "{}", + erreur::message_erreur("Impossible de récupérer l'ID du salon renseigné: Pas présent dans la liste récupérée") + ) + } + Err(err) => { + panic!( + "{}", + erreur::message_erreur(&format!( + "Impossible de récupérer l'ID du salon renseigné: {}", + err + )) + ) + } + } +} + /// Affiche les informations complète sur une équipe #[allow(dead_code)] // je comprends pas pourquoi j'ai besoin de mettre ça, parce que j'utilises le code en dessous... pub async fn team_info(api: &mattermost_api::client::Mattermost, equipe: &str) {