Refactor message::send()

This commit is contained in:
bitfl0wer 2023-05-29 23:15:05 +02:00
parent 072d99e879
commit c43e861586
1 changed files with 22 additions and 23 deletions

View File

@ -4,7 +4,6 @@ pub mod messages {
use reqwest::{multipart, Client};
use serde_json::to_string;
use crate::api::limits::Limits;
use crate::instance::UserMeta;
use crate::limit::LimitedRequester;
use crate::types::{Message, MessageSendSchema, PartialDiscordFileAttachment};
@ -22,27 +21,26 @@ pub mod messages {
* [`InstanceServerError`] - If the message cannot be sent.
*/
pub async fn send<'a>(
url_api: String,
user: &mut UserMeta,
channel_id: String,
message: &mut MessageSendSchema,
files: Option<Vec<PartialDiscordFileAttachment>>,
token: String,
limits_user: &mut Limits,
limits_instance: &mut Limits,
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
let mut belongs_to = user.belongs_to.borrow_mut();
let url_api = belongs_to.urls.get_api();
let mut requester = LimitedRequester::new().await;
if files.is_none() {
let message_request = Client::new()
.post(format!("{}/channels/{}/messages/", url_api, channel_id))
.bearer_auth(token)
.bearer_auth(user.token())
.body(to_string(message).unwrap());
requester
.send_request(
message_request,
crate::api::limits::LimitType::Channel,
limits_instance,
limits_user,
&mut belongs_to.limits,
&mut user.limits,
)
.await
} else {
@ -75,15 +73,15 @@ pub mod messages {
let message_request = Client::new()
.post(format!("{}/channels/{}/messages/", url_api, channel_id))
.bearer_auth(token)
.bearer_auth(user.token())
.multipart(form);
requester
.send_request(
message_request,
crate::api::limits::LimitType::Channel,
limits_instance,
limits_user,
&mut belongs_to.limits,
&mut user.limits,
)
.await
}
@ -91,24 +89,25 @@ pub mod messages {
}
impl UserMeta {
/// Shorthand call for Message::send()
/**
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_message(
&mut self,
message: &mut MessageSendSchema,
channel_id: String,
files: Option<Vec<PartialDiscordFileAttachment>>,
) -> Result<reqwest::Response, crate::errors::InstanceServerError> {
let token = self.token().clone();
let mut belongs_to = self.belongs_to.borrow_mut();
Message::send(
belongs_to.urls.get_api().to_string(),
channel_id,
message,
files,
token,
&mut self.limits,
&mut belongs_to.limits,
)
.await
Message::send(self, channel_id, message, files).await
}
}
}