use crate::{
client::Client,
error::Error,
request::{
attachment::{AttachmentManager, PartialAttachment},
channel::webhook::ExecuteWebhookAndWait,
Nullable, Request, TryIntoRequest,
},
response::{marker::EmptyBody, Response, ResponseFuture},
routing::Route,
};
use serde::Serialize;
use std::future::IntoFuture;
use twilight_model::{
channel::message::{AllowedMentions, Component, Embed, MessageFlags},
http::attachment::Attachment,
id::{
marker::{ChannelMarker, WebhookMarker},
Id,
},
};
use twilight_validate::{
message::{
attachment as validate_attachment, components as validate_components,
content as validate_content, embeds as validate_embeds, MessageValidationError,
MessageValidationErrorType,
},
request::webhook_username as validate_webhook_username,
};
#[derive(Serialize)]
pub(crate) struct ExecuteWebhookFields<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
allowed_mentions: Option<Nullable<&'a AllowedMentions>>,
#[serde(skip_serializing_if = "Option::is_none")]
attachments: Option<Vec<PartialAttachment<'a>>>,
#[serde(skip_serializing_if = "Option::is_none")]
avatar_url: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
components: Option<&'a [Component]>,
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
embeds: Option<&'a [Embed]>,
#[serde(skip_serializing_if = "Option::is_none")]
flags: Option<MessageFlags>,
#[serde(skip_serializing_if = "Option::is_none")]
payload_json: Option<&'a [u8]>,
#[serde(skip_serializing_if = "Option::is_none")]
thread_name: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
tts: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
username: Option<&'a str>,
}
#[must_use = "requests must be configured and executed"]
pub struct ExecuteWebhook<'a> {
attachment_manager: AttachmentManager<'a>,
fields: ExecuteWebhookFields<'a>,
http: &'a Client,
thread_id: Option<Id<ChannelMarker>>,
token: &'a str,
wait: bool,
webhook_id: Id<WebhookMarker>,
}
impl<'a> ExecuteWebhook<'a> {
pub(crate) const fn new(
http: &'a Client,
webhook_id: Id<WebhookMarker>,
token: &'a str,
) -> Self {
Self {
attachment_manager: AttachmentManager::new(),
fields: ExecuteWebhookFields {
attachments: None,
avatar_url: None,
components: None,
content: None,
embeds: None,
flags: None,
payload_json: None,
thread_name: None,
tts: None,
username: None,
allowed_mentions: None,
},
http,
thread_id: None,
token,
wait: false,
webhook_id,
}
}
pub const fn allowed_mentions(mut self, allowed_mentions: Option<&'a AllowedMentions>) -> Self {
self.fields.allowed_mentions = Some(Nullable(allowed_mentions));
self
}
pub fn attachments(
mut self,
attachments: &'a [Attachment],
) -> Result<Self, MessageValidationError> {
attachments.iter().try_for_each(validate_attachment)?;
self.attachment_manager = self
.attachment_manager
.set_files(attachments.iter().collect());
Ok(self)
}
pub const fn avatar_url(mut self, avatar_url: &'a str) -> Self {
self.fields.avatar_url = Some(avatar_url);
self
}
pub fn components(
mut self,
components: &'a [Component],
) -> Result<Self, MessageValidationError> {
validate_components(components)?;
self.fields.components = Some(components);
Ok(self)
}
pub fn content(mut self, content: &'a str) -> Result<Self, MessageValidationError> {
validate_content(content)?;
self.fields.content = Some(content);
Ok(self)
}
pub fn embeds(mut self, embeds: &'a [Embed]) -> Result<Self, MessageValidationError> {
validate_embeds(embeds)?;
self.fields.embeds = Some(embeds);
Ok(self)
}
pub const fn flags(mut self, flags: MessageFlags) -> Self {
self.fields.flags = Some(flags);
self
}
pub const fn payload_json(mut self, payload_json: &'a [u8]) -> Self {
self.fields.payload_json = Some(payload_json);
self
}
pub fn thread_id(mut self, thread_id: Id<ChannelMarker>) -> Self {
self.thread_id.replace(thread_id);
self
}
pub const fn thread_name(mut self, thread_name: &'a str) -> Self {
self.fields.thread_name = Some(thread_name);
self
}
pub const fn tts(mut self, tts: bool) -> Self {
self.fields.tts = Some(tts);
self
}
pub fn username(mut self, username: &'a str) -> Result<Self, MessageValidationError> {
validate_webhook_username(username).map_err(|source| {
MessageValidationError::from_validation_error(
MessageValidationErrorType::WebhookUsername,
source,
)
})?;
self.fields.username = Some(username);
Ok(self)
}
pub const fn wait(mut self) -> ExecuteWebhookAndWait<'a> {
self.wait = true;
ExecuteWebhookAndWait::new(self.http, self)
}
#[deprecated(since = "0.14.0", note = "use `.await` or `into_future` instead")]
pub fn exec(self) -> ResponseFuture<EmptyBody> {
self.into_future()
}
}
impl IntoFuture for ExecuteWebhook<'_> {
type Output = Result<Response<EmptyBody>, Error>;
type IntoFuture = ResponseFuture<EmptyBody>;
fn into_future(self) -> Self::IntoFuture {
let http = self.http;
match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}
impl TryIntoRequest for ExecuteWebhook<'_> {
fn try_into_request(mut self) -> Result<Request, Error> {
let mut request = Request::builder(&Route::ExecuteWebhook {
thread_id: self.thread_id.map(Id::get),
token: self.token,
wait: Some(self.wait),
webhook_id: self.webhook_id.get(),
});
request = request.use_authorization_token(false);
if self.fields.allowed_mentions.is_none() {
if let Some(allowed_mentions) = self.http.default_allowed_mentions() {
self.fields.allowed_mentions = Some(Nullable(Some(allowed_mentions)));
}
}
if !self.attachment_manager.is_empty() {
let form = if let Some(payload_json) = self.fields.payload_json {
self.attachment_manager.build_form(payload_json)
} else {
self.fields.attachments = Some(self.attachment_manager.get_partial_attachments());
let fields = crate::json::to_vec(&self.fields).map_err(Error::json)?;
self.attachment_manager.build_form(fields.as_ref())
};
request = request.form(form);
} else if let Some(payload_json) = self.fields.payload_json {
request = request.body(payload_json.to_vec());
} else {
request = request.json(&self.fields)?;
}
Ok(request.build())
}
}