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: Result<ExecuteWebhookFields<'a>, MessageValidationError>,
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: Ok(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 fn allowed_mentions(mut self, allowed_mentions: Option<&'a AllowedMentions>) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.allowed_mentions = Some(Nullable(allowed_mentions));
}
self
}
pub fn attachments(mut self, attachments: &'a [Attachment]) -> Self {
if self.fields.is_ok() {
if let Err(source) = attachments.iter().try_for_each(validate_attachment) {
self.fields = Err(source);
} else {
self.attachment_manager = self
.attachment_manager
.set_files(attachments.iter().collect());
}
}
self
}
pub fn avatar_url(mut self, avatar_url: &'a str) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.avatar_url = Some(avatar_url);
}
self
}
pub fn components(mut self, components: &'a [Component]) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_components(components)?;
fields.components = Some(components);
Ok(fields)
});
self
}
pub fn content(mut self, content: &'a str) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_content(content)?;
fields.content = Some(content);
Ok(fields)
});
self
}
pub fn embeds(mut self, embeds: &'a [Embed]) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_embeds(embeds)?;
fields.embeds = Some(embeds);
Ok(fields)
});
self
}
pub fn flags(mut self, flags: MessageFlags) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.flags = Some(flags);
}
self
}
pub fn payload_json(mut self, payload_json: &'a [u8]) -> Self {
if let Ok(fields) = self.fields.as_mut() {
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 fn thread_name(mut self, thread_name: &'a str) -> Self {
self.fields = self.fields.map(|mut fields| {
fields.thread_name = Some(thread_name);
fields
});
self
}
pub fn tts(mut self, tts: bool) -> Self {
if let Ok(fields) = self.fields.as_mut() {
fields.tts = Some(tts);
}
self
}
pub fn username(mut self, username: &'a str) -> Self {
self.fields = self.fields.and_then(|mut fields| {
validate_webhook_username(username).map_err(|source| {
MessageValidationError::from_validation_error(
MessageValidationErrorType::WebhookUsername,
source,
)
})?;
fields.username = Some(username);
Ok(fields)
});
self
}
pub const fn wait(mut self) -> ExecuteWebhookAndWait<'a> {
self.wait = true;
ExecuteWebhookAndWait::new(self.http, self)
}
}
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(self) -> Result<Request, Error> {
let mut fields = self.fields.map_err(Error::validation)?;
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 fields.allowed_mentions.is_none() {
if let Some(allowed_mentions) = self.http.default_allowed_mentions() {
fields.allowed_mentions = Some(Nullable(Some(allowed_mentions)));
}
}
if !self.attachment_manager.is_empty() {
let form = if let Some(payload_json) = fields.payload_json {
self.attachment_manager.build_form(payload_json)
} else {
fields.attachments = Some(self.attachment_manager.get_partial_attachments());
let fields = crate::json::to_vec(&fields).map_err(Error::json)?;
self.attachment_manager.build_form(fields.as_ref())
};
request = request.form(form);
} else if let Some(payload_json) = fields.payload_json {
request = request.body(payload_json.to_vec());
} else {
request = request.json(&fields);
}
request.build()
}
}