Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added FlairRichText on SubmissionData to better support emojis from reddit #89

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 63 additions & 56 deletions src/models/comment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>,
pub total_awards_received: i32,
/// Approved at (UTC)
pub approved_at_utc: Option<f64>,
pub approved_at_utc: f64,
/// Link id
pub link_id: Option<String>,
pub link_id: String,
/// What is this
pub author_flair_template_id: Option<String>,
/// Likes
pub author_flair_template_id: String,
/// Likes, can be null in json --> Option
pub likes: Option<bool>,
/// Saved
pub saved: Option<bool>,
/// ID
pub id: Option<String>,
pub saved: bool,
/// ID, every basic thing has an id
pub id: String,
/// Gilded
pub gilded: Option<i32>,
pub gilded: i32,
/// Archived
pub archived: Option<bool>,
pub archived: bool,
/// No follow
pub no_follow: Option<bool>,
pub no_follow: bool,
/// Auuthor
pub author: Option<String>,
pub author: String,
/// Can mod post
pub can_mod_post: Option<bool>,
pub can_mod_post: bool,
/// Created (UTC)
pub created_utc: Option<f64>,
pub created_utc: f64,
/// Send replies
pub send_replies: Option<bool>,
/// Parent ID
pub parent_id: Option<String>,
pub send_replies: bool,
/// Parent ID, full name of parent (so f.e t3_1fj3fj)
pub parent_id: String,
/// Score
pub score: Option<i32>,
pub score: i32,
/// Author fullname
pub author_fullname: Option<String>,
/// Over 18
pub author_fullname: String,
/// Over 18, only for latest comments (url/comments)
pub over_18: Option<bool>,
/// Approved by
pub approved_by: Option<String>,
/// Subreddit ID
pub approved_by: String,
/// Subreddit ID f.e "t5_2qh33"
pub subreddit_id: Option<String>,
/// Body
pub body: Option<String>,
/// Link title
pub body: String,
/// Link title, only for latest comments (url/comments)
pub link_title: Option<String>,
/// Name
pub name: Option<String>,
/// 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<bool>,
pub author_patreon_flair: bool,
/// Downs?
pub downs: Option<i32>,
pub downs: i32,
/// Is submitter
pub is_submitter: Option<bool>,
pub is_submitter: bool,
/// HTML
pub body_html: Option<String>,
/// Distinguished
pub body_html: String,
/// Distinguished, can be null in json --> Option
pub distinguished: Option<String>,
/// if the comment was eddited
pub edited: bool,
/// Stickied
pub stickied: Option<bool>,
pub stickied: bool,
/// Premium
pub author_premium: Option<bool>,
pub author_premium: bool,
/// Can guild
pub can_gild: Option<bool>,
/// Subreddit
pub subreddit: Option<String>,
pub can_gild: bool,
/// Subreddit, name of subreddit (f.e. "funny")
pub subreddit: String,
/// Flair color
pub author_flair_text_color: Option<String>,
pub author_flair_text_color: String,
/// Score hidden
pub score_hidden: Option<bool>,
/// Permalink
pub permalink: Option<String>,
/// 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<i32>,
/// Permalink
/// only for latest comments (url/comments)
pub num_comments: Option<i32>,
/// depth of comment, only available in get_article_comments (url/comments/article_id)
pub depth: Option<i32>,
/// Permalink, only for latest comments (url/comments)
pub link_permalink: Option<String>,
/// Author link
/// Only for latest comments (url/comments and not url/comments/article_id)
pub link_author: Option<String>,
/// Sub name
pub subreddit_name_prefixed: Option<String>,
/// Sub name, prefixed with "r/" (f.e. "r/funny")
pub subreddit_name_prefixed: String,
/// Author flair
pub author_flair_text: Option<String>,
/// Link url
pub author_flair_text: String,
/// Link url, only for latest comments (url/comments)
pub link_url: Option<String>,
/// Created
pub created: Option<f64>,
pub created: f64,
/// Collapsed
pub collapsed: Option<bool>,
pub collapsed: bool,
/// Controversiality
pub controversiality: Option<i32>,
pub controversiality: i32,
/// Locked
pub locked: Option<bool>,
/// Quarantine
pub locked: bool,
/// Quarantine, only for latest comments (url/comments)
pub quarantine: Option<bool>,
/// Subreddit type
pub subreddit_type: Option<String>,
pub subreddit_type: String,
/// UPS?
pub ups: Option<i32>,
pub ups: i32,
/// Replies
pub replies: Option<MaybeReplies>,
}
Expand Down
40 changes: 40 additions & 0 deletions src/models/link_flair/mod.rs
Original file line number Diff line number Diff line change
@@ -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<FlairRichText>,
#[serde(rename = "background_color")]
/// The background color of the flair
pub background_color: String,
/// The id of the flair
pub id: String,
}
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 25 additions & 1 deletion src/models/submission/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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<Vec<FlairRichText>>,
/// The CSS class set for the link's flair (if available), otherwise `None`.
pub link_flair_css_class: Option<String>,
/// The CSS class set for the author's flair (if available). If there is no flair, this is
Expand Down Expand Up @@ -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<SubmissionData>;
17 changes: 16 additions & 1 deletion src/models/subreddit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -221,7 +222,7 @@ impl Subreddit {
.await?
.json::<Vec<Comments>>()
.await?;

//first item in vec is just the link/ comment itself (from which the articles where requested)
Ok(comments.pop().unwrap())
} else {
Ok(self
Expand Down Expand Up @@ -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<Vec<LinkFlairData>, RouxError> {
let url = format!("{}/api/link_flair_v2.json", self.url);

Ok(self
.client
.get(&url)
.send()
.await?
.json::<Vec<LinkFlairData>>()
.await?)
}
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions src/models/subreddit/response.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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<Vec<RichtextFlair>>,
pub user_flair_richtext: Option<Vec<LinkFlairData>>,
/// Unknown. If set, the value appears to max out at 100.
pub videostream_links_count: Option<u8>,
/// The fullname identifier of this subreddit. This is a combination of the thing kind (t5) and the id,
Expand Down
Loading