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