From 6858b1eb0d5e1c72960961f12059a0992c5aa67c Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 3 May 2023 16:27:04 +0200 Subject: [PATCH] Add Message send() --- src/api/channels/messages.rs | 45 +++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index c9211ef..a00fa79 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -1,24 +1,53 @@ pub mod messages { - use reqwest::Client; + use reqwest::{Client, Response}; use serde_json::to_string; use crate::api::limits::Limits; use crate::api::types::Message; + use crate::api::User; + use crate::errors::InstanceServerError; use crate::instance::Instance; use crate::limit::LimitedRequester; impl Message { + /** + Sends a message to the Spacebar server. + # Arguments + * `url_api` - The URL of the Spacebar server's API. + * `message` - The [`Message`] that will be sent to the Spacebar server. + * `limits_user` - The [`Limits`] of the user. + * `limits_instance` - The [`Limits`] of the instance. + * `requester` - The [`LimitedRequester`] that will be used to make requests to the Spacebar server. + # Errors + * [`InstanceServerError`] - If the message cannot be sent. + */ pub async fn send( url_api: &String, message: &Message, - limits: &Limits, + limits_user: &mut Limits, + limits_instance: &mut Limits, requester: &mut LimitedRequester, - ) { - let client = Client::new() - .post(url_api) - .body(to_string(message).unwrap()) - .send() - .await; + ) -> Result { + let request = Client::new() + .post(format!( + "{}/channels/{}/messages", + url_api, message.channel_id + )) + .body(to_string(message).unwrap()); + match requester + .send_request( + request, + crate::api::limits::LimitType::Channel, + limits_instance, + limits_user, + ) + .await + { + Ok(result) => return Ok(result), + Err(e) => return Err(e), + }; } } + + impl User {} }