From 6ebd2ed2a46324c0f12ab0ab3005e5ade0570dfb Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Sun, 27 Nov 2016 14:05:02 -0800 Subject: Make send_file use the CreateMessage builder --- src/client/context.rs | 32 +++++++++++++++++++++----------- src/client/rest/mod.rs | 21 +++++++++++++++++---- 2 files changed, 38 insertions(+), 15 deletions(-) (limited to 'src/client') diff --git a/src/client/context.rs b/src/client/context.rs index c0fe43f..ddfe64d 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -1084,7 +1084,11 @@ impl Context { /// Sends a file along with optional message contents. The filename _must_ /// be specified. /// - /// Pass an empty string to send no message contents. + /// Message contents may be passed by using the [`CreateMessage::content`] + /// method. + /// + /// An embed can _not_ be sent when sending a file. If you set one, it will + /// be automatically removed. /// /// **Note**: Message contents must be under 2000 unicode code points. /// @@ -1095,18 +1099,24 @@ impl Context { /// of unicode code points over the limit. /// /// [`ClientError::MessageTooLong`]: enum.ClientError.html#variant.MessageTooLong - pub fn send_file(&self, - channel_id: C, - content: &str, - file: R, - filename: &str) - -> Result where C: Into, - R: Read { - if let Some(length_over) = Message::overflow_length(content) { - return Err(Error::Client(ClientError::MessageTooLong(length_over))); + /// [`CreateMessage::content`]: ../utils/builder/struct.CreateMessage.html#method.content + pub fn send_file(&self, channel_id: C, file: R, filename: &str, f: F) + -> Result where C: Into, + F: FnOnce(CreateMessage) -> CreateMessage, + R: Read { + let mut map = f(CreateMessage::default()).0; + + if let Some(content) = map.get("content") { + if let Value::String(ref content) = *content { + if let Some(length_over) = Message::overflow_length(content) { + return Err(Error::Client(ClientError::MessageTooLong(length_over))); + } + } } - rest::send_file(channel_id.into().0, content, file, filename) + let _ = map.remove("embed"); + + rest::send_file(channel_id.into().0, file, filename, map) } /// Sends a message to a [`Channel`]. diff --git a/src/client/rest/mod.rs b/src/client/rest/mod.rs index 5624ab7..3387d5d 100644 --- a/src/client/rest/mod.rs +++ b/src/client/rest/mod.rs @@ -39,6 +39,7 @@ use multipart::client::Multipart; use self::ratelimiting::Route; use serde_json::builder::ObjectBuilder; use serde_json; +use std::collections::BTreeMap; use std::default::Default; use std::io::{ErrorKind as IoErrorKind, Read}; use std::sync::{Arc, Mutex}; @@ -1224,9 +1225,9 @@ pub fn remove_group_recipient(group_id: u64, user_id: u64) } pub fn send_file(channel_id: u64, - content: &str, mut file: R, - filename: &str) + filename: &str, + map: BTreeMap) -> Result { let uri = format!(api_concat!("/channels/{}/messages"), channel_id); let url = match Url::parse(&uri) { @@ -1235,14 +1236,26 @@ pub fn send_file(channel_id: u64, }; let mut request = try!(Request::new(Method::Post, url)); - request.headers_mut().set(header::Authorization(TOKEN.lock().unwrap().clone())); + request.headers_mut() + .set(header::Authorization(TOKEN.lock().unwrap().clone())); request.headers_mut() .set(header::UserAgent(constants::USER_AGENT.to_owned())); let mut request = try!(Multipart::from_request(request)); - try!(request.write_text("content", content)); + try!(request.write_stream("file", &mut file, Some(&filename), None)); + for (k, v) in map { + let val = match v { + Value::I64(v) => v.to_string(), + Value::String(v) => v, + Value::U64(v) => v.to_string(), + _ => continue, + }; + + try!(request.write_text(&k, val)); + } + Message::decode(try!(serde_json::from_reader(try!(request.send())))) } -- cgit v1.2.3