|
|
@ -1,5 +1,4 @@ |
|
|
|
use isolang::Language; |
|
|
|
use isolang::Language; |
|
|
|
use std::fmt; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A builder pattern struct for constructing a status.
|
|
|
|
/// A builder pattern struct for constructing a status.
|
|
|
|
///
|
|
|
|
///
|
|
|
@ -9,36 +8,234 @@ use std::fmt; |
|
|
|
/// # extern crate elefren;
|
|
|
|
/// # extern crate elefren;
|
|
|
|
/// # use elefren::{Language, StatusBuilder};
|
|
|
|
/// # use elefren::{Language, StatusBuilder};
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// let status = StatusBuilder {
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
/// status: "a status".to_string(),
|
|
|
|
/// let status = StatusBuilder::new()
|
|
|
|
/// sensitive: Some(true),
|
|
|
|
/// .status("a status")
|
|
|
|
/// spoiler_text: Some("a CW".to_string()),
|
|
|
|
/// .sensitive(true)
|
|
|
|
/// language: Some(Language::Eng),
|
|
|
|
/// .spoiler_text("a CW")
|
|
|
|
/// ..Default::default()
|
|
|
|
/// .language(Language::Eng)
|
|
|
|
/// };
|
|
|
|
/// .build()?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
#[derive(Debug, Default, Clone, Serialize, PartialEq)] |
|
|
|
#[derive(Debug, Default, Clone, PartialEq)] |
|
|
|
pub struct StatusBuilder { |
|
|
|
pub struct StatusBuilder { |
|
|
|
/// The text of the status.
|
|
|
|
status: Option<String>, |
|
|
|
pub status: String, |
|
|
|
in_reply_to_id: Option<String>, |
|
|
|
/// Id of status being replied to.
|
|
|
|
media_ids: Option<Vec<String>>, |
|
|
|
|
|
|
|
sensitive: Option<bool>, |
|
|
|
|
|
|
|
spoiler_text: Option<String>, |
|
|
|
|
|
|
|
visibility: Option<Visibility>, |
|
|
|
|
|
|
|
language: Option<Language>, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl StatusBuilder { |
|
|
|
|
|
|
|
/// Create a StatusBuilder object
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust,no_run
|
|
|
|
|
|
|
|
/// # use elefren::prelude::*;
|
|
|
|
|
|
|
|
/// # use elefren::status_builder::Visibility;
|
|
|
|
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
|
|
|
|
/// # let data = Data {
|
|
|
|
|
|
|
|
/// # base: "".into(),
|
|
|
|
|
|
|
|
/// # client_id: "".into(),
|
|
|
|
|
|
|
|
/// # client_secret: "".into(),
|
|
|
|
|
|
|
|
/// # redirect: "".into(),
|
|
|
|
|
|
|
|
/// # token: "".into(),
|
|
|
|
|
|
|
|
/// # };
|
|
|
|
|
|
|
|
/// # let client = Mastodon::from(data);
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new()
|
|
|
|
|
|
|
|
/// .status("a status")
|
|
|
|
|
|
|
|
/// .visibility(Visibility::Public)
|
|
|
|
|
|
|
|
/// .build()?;
|
|
|
|
|
|
|
|
/// client.new_status(status)?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn new() -> StatusBuilder { |
|
|
|
|
|
|
|
StatusBuilder::default() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the text for the post
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust
|
|
|
|
|
|
|
|
/// # use elefren::prelude::*;
|
|
|
|
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new().status("awoooooo").build()?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn status<I: Into<String>>(&mut self, status: I) -> &mut Self { |
|
|
|
|
|
|
|
self.status = Some(status.into()); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the in_reply_to_id for the post
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust
|
|
|
|
|
|
|
|
/// # use elefren::prelude::*;
|
|
|
|
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new()
|
|
|
|
|
|
|
|
/// .status("awooooo")
|
|
|
|
|
|
|
|
/// .in_reply_to("12345")
|
|
|
|
|
|
|
|
/// .build()?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn in_reply_to<I: Into<String>>(&mut self, id: I) -> &mut Self { |
|
|
|
|
|
|
|
self.in_reply_to_id = Some(id.into()); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the media_ids for the post
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust
|
|
|
|
|
|
|
|
/// # use elefren::prelude::*;
|
|
|
|
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new().media_ids(&["foo", "bar"]).build()?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn media_ids<S: std::fmt::Display, I: IntoIterator<Item = S>>( |
|
|
|
|
|
|
|
&mut self, |
|
|
|
|
|
|
|
ids: I, |
|
|
|
|
|
|
|
) -> &mut Self { |
|
|
|
|
|
|
|
self.media_ids = Some(ids.into_iter().map(|s| s.to_string()).collect::<Vec<_>>()); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the sensitive attribute for the post
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust
|
|
|
|
|
|
|
|
/// # use elefren::prelude::*;
|
|
|
|
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new()
|
|
|
|
|
|
|
|
/// .media_ids(&["foo", "bar"])
|
|
|
|
|
|
|
|
/// .sensitive(true)
|
|
|
|
|
|
|
|
/// .build()?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn sensitive(&mut self, sensitive: bool) -> &mut Self { |
|
|
|
|
|
|
|
self.sensitive = Some(sensitive); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the spoiler text/CW for the post
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust
|
|
|
|
|
|
|
|
/// # use elefren::prelude::*;
|
|
|
|
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new()
|
|
|
|
|
|
|
|
/// .status("awoooo!!")
|
|
|
|
|
|
|
|
/// .spoiler_text("awoo inside")
|
|
|
|
|
|
|
|
/// .build()?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn spoiler_text<I: Into<String>>(&mut self, spoiler_text: I) -> &mut Self { |
|
|
|
|
|
|
|
self.spoiler_text = Some(spoiler_text.into()); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the visibility for the post
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust
|
|
|
|
|
|
|
|
/// # use elefren::prelude::*;
|
|
|
|
|
|
|
|
/// # use elefren::status_builder::Visibility;
|
|
|
|
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new()
|
|
|
|
|
|
|
|
/// .status("awooooooo")
|
|
|
|
|
|
|
|
/// .visibility(Visibility::Public)
|
|
|
|
|
|
|
|
/// .build()?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn visibility(&mut self, visibility: Visibility) -> &mut Self { |
|
|
|
|
|
|
|
self.visibility = Some(visibility); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Set the language for the post
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust
|
|
|
|
|
|
|
|
/// # use elefren::prelude::*;
|
|
|
|
|
|
|
|
/// # use elefren::Language;
|
|
|
|
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new()
|
|
|
|
|
|
|
|
/// .status("awoo!!!!")
|
|
|
|
|
|
|
|
/// .language(Language::Eng)
|
|
|
|
|
|
|
|
/// .build()?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn language(&mut self, language: Language) -> &mut Self { |
|
|
|
|
|
|
|
self.language = Some(language); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Constructs a NewStatus
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```rust
|
|
|
|
|
|
|
|
/// # use elefren::prelude::*;
|
|
|
|
|
|
|
|
/// # fn main() -> Result<(), elefren::Error> {
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new().status("awoo!").build()?;
|
|
|
|
|
|
|
|
/// # Ok(())
|
|
|
|
|
|
|
|
/// # }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn build(&self) -> Result<NewStatus, crate::Error> { |
|
|
|
|
|
|
|
if self.status.is_none() && self.media_ids.is_none() { |
|
|
|
|
|
|
|
return Err(crate::Error::Other( |
|
|
|
|
|
|
|
"status text or media ids are required in order to post a status".to_string(), |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Ok(NewStatus { |
|
|
|
|
|
|
|
status: self.status.clone(), |
|
|
|
|
|
|
|
in_reply_to_id: self.in_reply_to_id.clone(), |
|
|
|
|
|
|
|
media_ids: self.media_ids.clone(), |
|
|
|
|
|
|
|
sensitive: self.sensitive.clone(), |
|
|
|
|
|
|
|
spoiler_text: self.spoiler_text.clone(), |
|
|
|
|
|
|
|
visibility: self.visibility.clone(), |
|
|
|
|
|
|
|
language: self.language.clone(), |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Represents a post that can be sent to the POST /api/v1/status endpoint
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone, Serialize, PartialEq)] |
|
|
|
|
|
|
|
pub struct NewStatus { |
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
|
|
|
|
status: Option<String>, |
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
pub in_reply_to_id: Option<String>, |
|
|
|
in_reply_to_id: Option<String>, |
|
|
|
/// Ids of media attachments being attached to the status.
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
pub media_ids: Option<Vec<String>>, |
|
|
|
media_ids: Option<Vec<String>>, |
|
|
|
/// Whether current status is sensitive.
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
pub sensitive: Option<bool>, |
|
|
|
sensitive: Option<bool>, |
|
|
|
/// Text to precede the normal status text.
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
pub spoiler_text: Option<String>, |
|
|
|
spoiler_text: Option<String>, |
|
|
|
/// Visibility of the status, defaults to `Public`.
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
pub visibility: Option<Visibility>, |
|
|
|
visibility: Option<Visibility>, |
|
|
|
/// Language code of the status
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
#[serde(skip_serializing_if = "Option::is_none")] |
|
|
|
pub language: Option<Language>, |
|
|
|
language: Option<Language>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The visibility of a status.
|
|
|
|
/// The visibility of a status.
|
|
|
@ -55,21 +252,6 @@ pub enum Visibility { |
|
|
|
Public, |
|
|
|
Public, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl StatusBuilder { |
|
|
|
|
|
|
|
/// Create a new status with text.
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// use elefren::prelude::*;
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// let status = StatusBuilder::new("Hello World!");
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
pub fn new<D: fmt::Display>(status: D) -> Self { |
|
|
|
|
|
|
|
StatusBuilder { |
|
|
|
|
|
|
|
status: status.to_string(), |
|
|
|
|
|
|
|
..Self::default() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for Visibility { |
|
|
|
impl Default for Visibility { |
|
|
|
fn default() -> Self { |
|
|
|
fn default() -> Self { |
|
|
|
Visibility::Public |
|
|
|
Visibility::Public |
|
|
@ -84,9 +266,12 @@ mod tests { |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_new() { |
|
|
|
fn test_new() { |
|
|
|
let s = StatusBuilder::new("a status"); |
|
|
|
let s = StatusBuilder::new() |
|
|
|
let expected = StatusBuilder { |
|
|
|
.status("a status") |
|
|
|
status: "a status".to_string(), |
|
|
|
.build() |
|
|
|
|
|
|
|
.expect("Couldn't build status"); |
|
|
|
|
|
|
|
let expected = NewStatus { |
|
|
|
|
|
|
|
status: Some("a status".to_string()), |
|
|
|
in_reply_to_id: None, |
|
|
|
in_reply_to_id: None, |
|
|
|
media_ids: None, |
|
|
|
media_ids: None, |
|
|
|
sensitive: None, |
|
|
|
sensitive: None, |
|
|
@ -125,17 +310,20 @@ mod tests { |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_serialize_status() { |
|
|
|
fn test_serialize_status() { |
|
|
|
let status = StatusBuilder::new("a status"); |
|
|
|
let status = StatusBuilder::new() |
|
|
|
|
|
|
|
.status("a status") |
|
|
|
|
|
|
|
.build() |
|
|
|
|
|
|
|
.expect("Couldn't build status"); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
serde_json::to_string(&status).expect("Couldn't serialize status"), |
|
|
|
serde_json::to_string(&status).expect("Couldn't serialize status"), |
|
|
|
"{\"status\":\"a status\"}".to_string() |
|
|
|
"{\"status\":\"a status\"}".to_string() |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
let status = StatusBuilder { |
|
|
|
let status = StatusBuilder::new() |
|
|
|
status: "a status".into(), |
|
|
|
.status("a status") |
|
|
|
language: Some(Language::Eng), |
|
|
|
.language(Language::Eng) |
|
|
|
..Default::default() |
|
|
|
.build() |
|
|
|
}; |
|
|
|
.expect("Couldn't build status"); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
serde_json::to_string(&status).expect("Couldn't serialize status"), |
|
|
|
serde_json::to_string(&status).expect("Couldn't serialize status"), |
|
|
|
"{\"status\":\"a status\",\"language\":\"eng\"}" |
|
|
|
"{\"status\":\"a status\",\"language\":\"eng\"}" |
|
|
|