use crate::{
client::Client,
error::Error,
request::{
Nullable, Request, TryIntoRequest,
attachment::{AttachmentManager, PartialAttachment},
},
response::{Response, ResponseFuture},
routing::Route,
};
use serde::Serialize;
use std::future::IntoFuture;
use twilight_model::{
channel::{
Message,
message::{AllowedMentions, Component, Embed},
},
http::attachment::Attachment,
id::{
Id,
marker::{AttachmentMarker, ChannelMarker, MessageMarker, WebhookMarker},
},
};
use twilight_validate::message::{
MessageValidationError, attachment as validate_attachment, content as validate_content,
embeds as validate_embeds,
};
#[derive(Serialize)]
struct UpdateWebhookMessageFields<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
allowed_mentions: Option<Nullable<&'a AllowedMentions>>,
#[serde(skip_serializing_if = "Option::is_none")]
attachments: Option<Nullable<Vec<PartialAttachment<'a>>>>,
#[serde(skip_serializing_if = "Option::is_none")]
components: Option<Nullable<&'a [Component]>>,
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<Nullable<&'a str>>,
#[serde(skip_serializing_if = "Option::is_none")]
embeds: Option<Nullable<&'a [Embed]>>,
#[serde(skip_serializing_if = "Option::is_none")]
payload_json: Option<&'a [u8]>,
}
#[must_use = "requests must be configured and executed"]
pub struct UpdateWebhookMessage<'a> {
attachment_manager: AttachmentManager<'a>,
fields: Result<UpdateWebhookMessageFields<'a>, MessageValidationError>,
http: &'a Client,
message_id: Id<MessageMarker>,
thread_id: Option<Id<ChannelMarker>>,
token: &'a str,
webhook_id: Id<WebhookMarker>,
}
impl<'a> UpdateWebhookMessage<'a> {
pub(crate) const fn new(
http: &'a Client,
webhook_id: Id<WebhookMarker>,
token: &'a str,
message_id: Id<MessageMarker>,
) -> Self {
Self {
attachment_manager: AttachmentManager::new(),
fields: Ok(UpdateWebhookMessageFields {
allowed_mentions: None,
attachments: None,
components: None,
content: None,
embeds: None,
payload_json: None,
}),
http,
message_id,
thread_id: None,
token,
webhook_id,
}
}
pub const 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 components(mut self, components: Option<&'a [Component]>) -> Self {
self.fields = self.fields.map(|mut fields| {
fields.components = Some(Nullable(components));
fields
});
self
}
pub fn content(mut self, content: Option<&'a str>) -> Self {
self.fields = self.fields.and_then(|mut fields| {
if let Some(content) = content {
validate_content(content)?;
}
fields.content = Some(Nullable(content));
Ok(fields)
});
self
}
pub fn embeds(mut self, embeds: Option<&'a [Embed]>) -> Self {
self.fields = self.fields.and_then(|mut fields| {
if let Some(embeds) = embeds {
validate_embeds(embeds)?;
}
fields.embeds = Some(Nullable(embeds));
Ok(fields)
});
self
}
pub fn keep_attachment_ids(mut self, attachment_ids: &'a [Id<AttachmentMarker>]) -> Self {
if let Ok(fields) = self.fields.as_mut() {
self.attachment_manager = self.attachment_manager.set_ids(attachment_ids.to_vec());
fields.attachments = Some(Nullable(Some(Vec::new())));
}
self
}
pub const 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 const fn thread_id(mut self, thread_id: Id<ChannelMarker>) -> Self {
self.thread_id.replace(thread_id);
self
}
}
impl IntoFuture for UpdateWebhookMessage<'_> {
type Output = Result<Response<Message>, Error>;
type IntoFuture = ResponseFuture<Message>;
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 UpdateWebhookMessage<'_> {
fn try_into_request(self) -> Result<Request, Error> {
let mut fields = self.fields.map_err(Error::validation)?;
let mut request = Request::builder(&Route::UpdateWebhookMessage {
message_id: self.message_id.get(),
thread_id: self.thread_id.map(Id::get),
token: self.token,
webhook_id: self.webhook_id.get(),
});
request = request.use_authorization_token(false);
if fields.allowed_mentions.is_none()
&& 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(Nullable(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()
}
}
#[cfg(test)]
mod tests {
use super::{UpdateWebhookMessage, UpdateWebhookMessageFields};
use crate::{
client::Client,
request::{Nullable, Request, TryIntoRequest},
routing::Route,
};
use twilight_model::id::Id;
#[test]
fn request() {
let client = Client::new("token".to_owned());
let builder = UpdateWebhookMessage::new(&client, Id::new(1), "token", Id::new(2))
.content(Some("test"))
.thread_id(Id::new(3));
let actual = builder
.try_into_request()
.expect("failed to create request");
let body = UpdateWebhookMessageFields {
allowed_mentions: None,
attachments: None,
components: None,
content: Some(Nullable(Some("test"))),
embeds: None,
payload_json: None,
};
let route = Route::UpdateWebhookMessage {
message_id: 2,
thread_id: Some(3),
token: "token",
webhook_id: 1,
};
let expected = Request::builder(&route)
.json(&body)
.build()
.expect("failed to serialize body");
assert_eq!(expected.body, actual.body);
assert_eq!(expected.path, actual.path);
}
}