diff --git a/src/models/comment/mod.rs b/src/models/comment/mod.rs index 77991f8..77a15ba 100644 --- a/src/models/comment/mod.rs +++ b/src/models/comment/mod.rs @@ -3,106 +3,113 @@ use serde::{Deserialize, Serialize}; use crate::models::{reply::MaybeReplies, response::BasicListing}; -/// SubredditCommentsData -/// Everything is an option to deal with both `latest_comments` and `article_comments` +/// SubredditCommentsData, kind t3 +/// Option for diff of `latest_comments` and `article_comments`, +/// see here: https://www.diffchecker.com/6qdlZr5a/ #[derive(Serialize, Debug, Deserialize)] pub struct CommentData { /// Total awards - pub total_awards_received: Option, + pub total_awards_received: i32, /// Approved at (UTC) - pub approved_at_utc: Option, + pub approved_at_utc: f64, /// Link id - pub link_id: Option, + pub link_id: String, /// What is this - pub author_flair_template_id: Option, - /// Likes + pub author_flair_template_id: String, + /// Likes, can be null in json --> Option pub likes: Option, /// Saved - pub saved: Option, - /// ID - pub id: Option, + pub saved: bool, + /// ID, every basic thing has an id + pub id: String, /// Gilded - pub gilded: Option, + pub gilded: i32, /// Archived - pub archived: Option, + pub archived: bool, /// No follow - pub no_follow: Option, + pub no_follow: bool, /// Auuthor - pub author: Option, + pub author: String, /// Can mod post - pub can_mod_post: Option, + pub can_mod_post: bool, /// Created (UTC) - pub created_utc: Option, + pub created_utc: f64, /// Send replies - pub send_replies: Option, - /// Parent ID - pub parent_id: Option, + pub send_replies: bool, + /// Parent ID, full name of parent (so f.e t3_1fj3fj) + pub parent_id: String, /// Score - pub score: Option, + pub score: i32, /// Author fullname - pub author_fullname: Option, - /// Over 18 + pub author_fullname: String, + /// Over 18, only for latest comments (url/comments) pub over_18: Option, /// Approved by - pub approved_by: Option, - /// Subreddit ID + pub approved_by: String, + /// Subreddit ID f.e "t5_2qh33" pub subreddit_id: Option, /// Body - pub body: Option, - /// Link title + pub body: String, + /// Link title, only for latest comments (url/comments) pub link_title: Option, - /// Name - pub name: Option, + /// Name, type (in this case t1 appended with "_" and then the id,so for example t1_lc6y7am) + pub name: String, /// Patreon flair - pub author_patreon_flair: Option, + pub author_patreon_flair: bool, /// Downs? - pub downs: Option, + pub downs: i32, /// Is submitter - pub is_submitter: Option, + pub is_submitter: bool, /// HTML - pub body_html: Option, - /// Distinguished + pub body_html: String, + /// Distinguished, can be null in json --> Option pub distinguished: Option, + /// if the comment was eddited + pub edited: bool, /// Stickied - pub stickied: Option, + pub stickied: bool, /// Premium - pub author_premium: Option, + pub author_premium: bool, /// Can guild - pub can_gild: Option, - /// Subreddit - pub subreddit: Option, + pub can_gild: bool, + /// Subreddit, name of subreddit (f.e. "funny") + pub subreddit: String, /// Flair color - pub author_flair_text_color: Option, + pub author_flair_text_color: String, /// Score hidden - pub score_hidden: Option, - /// Permalink - pub permalink: Option, - /// Number of reports + pub score_hidden: bool, + /// Permalink, url ending, used to request the comment, so f.e "/r/funny/comments/1dxyka2/let_me_do_it/lc6yg5s/" + pub permalink: String, + /// Number of reports, can be null in json --> Option pub num_reports: Option, - /// Permalink + /// only for latest comments (url/comments) + pub num_comments: Option, + /// depth of comment, only available in get_article_comments (url/comments/article_id) + pub depth: Option, + /// Permalink, only for latest comments (url/comments) pub link_permalink: Option, - /// Author link + /// Only for latest comments (url/comments and not url/comments/article_id) pub link_author: Option, - /// Sub name - pub subreddit_name_prefixed: Option, + /// Sub name, prefixed with "r/" (f.e. "r/funny") + pub subreddit_name_prefixed: String, /// Author flair - pub author_flair_text: Option, - /// Link url + pub author_flair_text: String, + /// Link url, only for latest comments (url/comments) pub link_url: Option, /// Created - pub created: Option, + pub created: f64, /// Collapsed - pub collapsed: Option, + pub collapsed: bool, /// Controversiality - pub controversiality: Option, + pub controversiality: i32, /// Locked - pub locked: Option, - /// Quarantine + pub locked: bool, + /// Quarantine, only for latest comments (url/comments) pub quarantine: Option, /// Subreddit type - pub subreddit_type: Option, + pub subreddit_type: String, /// UPS? - pub ups: Option, + pub ups: i32, /// Replies pub replies: Option, } diff --git a/src/models/link_flair/mod.rs b/src/models/link_flair/mod.rs new file mode 100644 index 0000000..3ecd287 --- /dev/null +++ b/src/models/link_flair/mod.rs @@ -0,0 +1,40 @@ +//!All infos about a link flair. +use serde::Deserialize; + +use crate::submission::FlairRichText; + + +/// All infos about a link flair. +#[derive(Debug, Deserialize)] +pub struct LinkFlairData{ + /// The type of the flair. Can be text, richtext or emoji. + #[serde(rename = "type")] + pub type_field: String, + /// Indicate if the flair text can be modified for each redditor that sets it + #[serde(rename = "text_editable")] + pub text_editable: bool, + /// Idk there is no documentation about this field + #[serde(rename = "allowable_content")] + pub allowable_content: String, + /// The text of the flair, If type set to "emoji" then the "text" param must be a valid emoji string, for example, ":snoo:" + pub text: String, + /// The maximum number of emojis that can be used in the flair. Reddit defaults this value to 10 + #[serde(rename = "max_emojis")] + pub max_emojis: i64, + /// The text color of the flair + #[serde(rename = "text_color")] + pub text_color: String, + /// Indicate if the flair can only be used by moderators + #[serde(rename = "mod_only")] + pub mod_only: bool, + #[serde(rename = "css_class")] + /// The flair template’s css_class (default: "") + pub css_class: String, + /// The flairs richtext, empty if type is text + pub richtext: Vec, + #[serde(rename = "background_color")] + /// The background color of the flair + pub background_color: String, + /// The id of the flair + pub id: String, +} \ No newline at end of file diff --git a/src/models/mod.rs b/src/models/mod.rs index 81fdc12..966a2fe 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -11,6 +11,7 @@ pub mod saved; pub mod submission; pub mod subreddit; pub mod user; +pub mod link_flair; pub use about::About; pub use comment::Comments; diff --git a/src/models/submission/mod.rs b/src/models/submission/mod.rs index 8a96eec..939493c 100644 --- a/src/models/submission/mod.rs +++ b/src/models/submission/mod.rs @@ -4,7 +4,7 @@ use serde_json::Value; use crate::models::response::BasicListing; -/// SubmissionsData +/// SubmissionsData, kind: t3 #[derive(Debug, Serialize, Deserialize)] pub struct SubmissionData { /// The domain of the link (if link post) or self.subreddit (if self post). @@ -79,6 +79,8 @@ pub struct SubmissionData { /// This is `false` if the submission is not edited and is the edit timestamp if it is edited. /// Access through the functions of `Submission` instead. pub edited: Value, + /// A Vector of FlairRichText objects, which can be either text or emoji. + pub link_flair_richtext: Option>, /// The CSS class set for the link's flair (if available), otherwise `None`. pub link_flair_css_class: Option, /// The CSS class set for the author's flair (if available). If there is no flair, this is @@ -171,5 +173,27 @@ pub struct SubmissionDataPreviewImageSource { pub height: u64, } +#[derive(Serialize, Deserialize, Debug)] +#[serde(untagged)] +/// FlairRichText +pub enum FlairRichText { + /// Emoji + Emoji { + ///The emoji name always starts and ends with : + a: String, + /// The emoji URL, can be used to request an image of the emoji. + u: String, + /// The type, in this case always `emoji`. + e: String, + }, + /// Text + Text { + /// The text of the flair. + e: String, + /// The type, in this case always `text`. + t: String, + }, +} + /// Submissions pub type Submissions = BasicListing; diff --git a/src/models/subreddit/mod.rs b/src/models/subreddit/mod.rs index 1b94f9b..3ae3b5c 100644 --- a/src/models/subreddit/mod.rs +++ b/src/models/subreddit/mod.rs @@ -66,6 +66,7 @@ pub mod response; extern crate serde_json; +use crate::link_flair::LinkFlairData; use crate::models::subreddit::response::{SubredditData, SubredditResponse, SubredditsData}; use crate::client::Client; @@ -221,7 +222,7 @@ impl Subreddit { .await? .json::>() .await?; - + //first item in vec is just the link/ comment itself (from which the articles where requested) Ok(comments.pop().unwrap()) } else { Ok(self @@ -295,6 +296,20 @@ impl Subreddit { self.get_comment_feed(&format!("comments/{}", article), depth, limit) .await } + + /// Return list of available link flair for the current subreddit. + //Will not return flair if the user cannot set their own link flair and they are not a moderator that can set flair. + pub async fn link_flairs(&self) -> Result, RouxError> { + let url = format!("{}/api/link_flair_v2.json", self.url); + + Ok(self + .client + .get(&url) + .send() + .await? + .json::>() + .await?) + } } #[cfg(test)] diff --git a/src/models/subreddit/response.rs b/src/models/subreddit/response.rs index cd843b1..547259d 100644 --- a/src/models/subreddit/response.rs +++ b/src/models/subreddit/response.rs @@ -1,7 +1,7 @@ //! # Subreddit Responses use serde::Deserialize; -use crate::models::response::BasicListing; +use crate::{link_flair::LinkFlairData, models::response::BasicListing}; /// SubredditResponse #[derive(Debug, Deserialize)] @@ -115,7 +115,7 @@ pub struct SubredditData { /// containing two string elements which define the user's flair. The first element, named e, contains the string text. /// The second element, named t, contains the literal string that comprises the user's flair. For example: /// [{"e":"text","t":"Proud Shitposter"}] - pub user_flair_richtext: Option>, + pub user_flair_richtext: Option>, /// Unknown. If set, the value appears to max out at 100. pub videostream_links_count: Option, /// The fullname identifier of this subreddit. This is a combination of the thing kind (t5) and the id,