use crate::{
client::Client,
error::Error as HttpError,
request::{validate, Form, Pending, Request},
routing::Route,
};
use serde::Serialize;
use std::{
error::Error,
fmt::{Display, Formatter, Result as FmtResult},
};
use twilight_model::{
channel::{embed::Embed, message::AllowedMentions, Attachment},
id::{ApplicationId, MessageId},
};
#[derive(Debug)]
pub struct UpdateFollowupMessageError {
kind: UpdateFollowupMessageErrorType,
source: Option<Box<dyn Error + Send + Sync>>,
}
impl UpdateFollowupMessageError {
#[must_use = "retrieving the type has no effect if left unused"]
pub const fn kind(&self) -> &UpdateFollowupMessageErrorType {
&self.kind
}
#[must_use = "consuming the error and retrieving the source has no effect if left unused"]
pub fn into_source(self) -> Option<Box<dyn Error + Send + Sync>> {
self.source
}
#[must_use = "consuming the error into its parts has no effect if left unused"]
pub fn into_parts(
self,
) -> (
UpdateFollowupMessageErrorType,
Option<Box<dyn Error + Send + Sync>>,
) {
(self.kind, self.source)
}
}
impl Display for UpdateFollowupMessageError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match &self.kind {
UpdateFollowupMessageErrorType::ContentInvalid { .. } => {
f.write_str("message content is invalid")
}
UpdateFollowupMessageErrorType::EmbedTooLarge { .. } => {
f.write_str("length of one of the embeds is too large")
}
UpdateFollowupMessageErrorType::TooManyEmbeds { embeds } => f.write_fmt(format_args!(
"{} embeds were provided, but only 10 may be provided",
embeds.len()
)),
}
}
}
impl Error for UpdateFollowupMessageError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source
.as_ref()
.map(|source| &**source as &(dyn Error + 'static))
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum UpdateFollowupMessageErrorType {
ContentInvalid {
content: String,
},
EmbedTooLarge {
embeds: Vec<Embed>,
index: usize,
},
TooManyEmbeds {
embeds: Vec<Embed>,
},
}
#[derive(Default, Serialize)]
struct UpdateFollowupMessageFields {
#[serde(skip_serializing_if = "Option::is_none")]
allowed_mentions: Option<AllowedMentions>,
#[serde(skip_serializing_if = "Vec::is_empty")]
attachments: Vec<Attachment>,
#[allow(clippy::option_option)]
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<Option<String>>,
#[allow(clippy::option_option)]
#[serde(skip_serializing_if = "Option::is_none")]
embeds: Option<Option<Vec<Embed>>>,
#[serde(skip_serializing_if = "Option::is_none")]
payload_json: Option<Vec<u8>>,
}
pub struct UpdateFollowupMessage<'a> {
fields: UpdateFollowupMessageFields,
files: Vec<(String, Vec<u8>)>,
fut: Option<Pending<'a, ()>>,
http: &'a Client,
message_id: MessageId,
token: String,
application_id: ApplicationId,
}
impl<'a> UpdateFollowupMessage<'a> {
pub const EMBED_COUNT_LIMIT: usize = 10;
pub(crate) fn new(
http: &'a Client,
application_id: ApplicationId,
token: impl Into<String>,
message_id: MessageId,
) -> Self {
Self {
fields: UpdateFollowupMessageFields {
allowed_mentions: http.default_allowed_mentions(),
..UpdateFollowupMessageFields::default()
},
files: Vec::new(),
fut: None,
http,
message_id,
token: token.into(),
application_id,
}
}
pub fn allowed_mentions(mut self, allowed: AllowedMentions) -> Self {
self.fields.allowed_mentions.replace(allowed);
self
}
pub fn attachment(mut self, attachment: Attachment) -> Self {
self.fields.attachments.push(attachment);
self
}
pub fn attachments(mut self, attachments: impl IntoIterator<Item = Attachment>) -> Self {
self.fields
.attachments
.extend(attachments.into_iter().collect::<Vec<Attachment>>());
self
}
pub fn content(mut self, content: Option<String>) -> Result<Self, UpdateFollowupMessageError> {
if let Some(content_ref) = content.as_ref() {
if !validate::content_limit(content_ref) {
return Err(UpdateFollowupMessageError {
kind: UpdateFollowupMessageErrorType::ContentInvalid {
content: content.expect("content is known to be some"),
},
source: None,
});
}
}
self.fields.content.replace(content);
Ok(self)
}
pub fn embeds(
mut self,
embeds: Option<Vec<Embed>>,
) -> Result<Self, UpdateFollowupMessageError> {
if let Some(embeds_present) = embeds.as_deref() {
if embeds_present.len() > Self::EMBED_COUNT_LIMIT {
return Err(UpdateFollowupMessageError {
kind: UpdateFollowupMessageErrorType::TooManyEmbeds {
embeds: embeds.expect("embeds are known to be present"),
},
source: None,
});
}
for (idx, embed) in embeds_present.iter().enumerate() {
if let Err(source) = validate::embed(&embed) {
return Err(UpdateFollowupMessageError {
kind: UpdateFollowupMessageErrorType::EmbedTooLarge {
embeds: embeds.expect("embeds are known to be present"),
index: idx,
},
source: Some(Box::new(source)),
});
}
}
}
self.fields.embeds.replace(embeds);
Ok(self)
}
pub fn file(mut self, name: impl Into<String>, file: impl Into<Vec<u8>>) -> Self {
self.files.push((name.into(), file.into()));
self
}
pub fn files<N: Into<String>, F: Into<Vec<u8>>>(
mut self,
attachments: impl IntoIterator<Item = (N, F)>,
) -> Self {
for (name, file) in attachments {
self = self.file(name, file);
}
self
}
pub fn payload_json(mut self, payload_json: impl Into<Vec<u8>>) -> Self {
self.fields.payload_json.replace(payload_json.into());
self
}
fn request(&mut self) -> Result<Request, HttpError> {
let mut request = Request::builder(Route::UpdateWebhookMessage {
message_id: self.message_id.0,
token: self.token.clone(),
webhook_id: self.application_id.0,
});
if !self.files.is_empty() || self.fields.payload_json.is_some() {
let mut form = Form::new();
for (index, (name, file)) in self.files.drain(..).enumerate() {
form.file(format!("{}", index).as_bytes(), name.as_bytes(), &file);
}
if let Some(payload_json) = &self.fields.payload_json {
form.payload_json(&payload_json);
} else {
let body = crate::json::to_vec(&self.fields).map_err(HttpError::json)?;
form.payload_json(&body);
}
request = request.form(form);
} else {
request = request.json(&self.fields)?;
}
Ok(request.build())
}
fn start(&mut self) -> Result<(), HttpError> {
let request = self.request()?;
self.fut.replace(Box::pin(self.http.verify(request)));
Ok(())
}
}
poll_req!(UpdateFollowupMessage<'_>, ());