From 360b57aec6f057c33ff186d663740c5b5b3a60e6 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Wed, 6 Jul 2022 14:57:33 +0000 Subject: [PATCH] Update --- src/fanbox/comment.rs | 103 +++++++++++++++++++++++++++++++++++++ src/fanbox/comment_list.rs | 47 +++++++++++++++++ src/fanbox/mod.rs | 2 + src/fanbox/post.rs | 27 ++++++++++ 4 files changed, 179 insertions(+) create mode 100644 src/fanbox/comment.rs create mode 100644 src/fanbox/comment_list.rs diff --git a/src/fanbox/comment.rs b/src/fanbox/comment.rs new file mode 100644 index 0000000..f168be5 --- /dev/null +++ b/src/fanbox/comment.rs @@ -0,0 +1,103 @@ +use crate::parser::json::parse_u64; +use json::JsonValue; +use std::fmt::Debug; + +pub struct FanboxComment { + pub data: JsonValue, +} + +impl FanboxComment { + #[inline] + pub fn body(&self) -> Option<&str> { + self.data["body"].as_str() + } + + #[inline] + pub fn created_datetime(&self) -> Option<&str> { + self.data["createdDatetime"].as_str() + } + + #[inline] + pub fn id(&self) -> Option { + parse_u64(&self.data["id"]) + } + + #[inline] + pub fn is_liked(&self) -> Option { + self.data["isLiked"].as_bool() + } + + #[inline] + pub fn is_own(&self) -> Option { + self.data["isOwn"].as_bool() + } + + #[inline] + pub fn like_count(&self) -> Option { + self.data["likeCount"].as_u64() + } + + #[inline] + /// Create a new instance + pub fn new(data: &JsonValue) -> Self { + Self { data: data.clone() } + } + + #[inline] + pub fn parent_comment_id(&self) -> Option { + parse_u64(&self.data["parentCommentId"]) + } + + #[inline] + pub fn replies(&self) -> Option> { + let r = &self.data["replies"]; + if r.is_array() { + let mut list = Vec::new(); + for i in r.members() { + list.push(Self::new(i)); + } + Some(list) + } else { + None + } + } + + #[inline] + pub fn root_comment_id(&self) -> Option { + parse_u64(&self.data["rootCommentId"]) + } + + #[inline] + pub fn user_icon_url(&self) -> Option<&str> { + self.data["user"]["iconUrl"].as_str() + } + + #[inline] + pub fn user_id(&self) -> Option { + parse_u64(&self.data["user"]["userId"]) + } + + #[inline] + pub fn user_name(&self) -> Option<&str> { + self.data["user"]["name"].as_str() + } +} + +impl Debug for FanboxComment { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FanboxComment") + .field("id", &self.id()) + .field("body", &self.body()) + .field("created_datetime", &self.created_datetime()) + .field("is_liked", &self.is_liked()) + .field("is_own", &self.is_own()) + .field("like_count", &self.like_count()) + .field("parent_comment_id", &self.parent_comment_id()) + .field("replies", &self.replies()) + .field("root_comment_id", &self.root_comment_id()) + .field("user_icon_url", &self.user_icon_url()) + .field("user_id", &self.user_id()) + .field("user_name", &self.user_name()) + .finish_non_exhaustive() + } +} diff --git a/src/fanbox/comment_list.rs b/src/fanbox/comment_list.rs new file mode 100644 index 0000000..4e59c93 --- /dev/null +++ b/src/fanbox/comment_list.rs @@ -0,0 +1,47 @@ +use super::comment::FanboxComment; +use crate::fanbox_api::FanboxClientInternal; +use json::JsonValue; +use std::fmt::Debug; +use std::sync::Arc; + +/// A list of fanbox item. +pub struct FanboxCommentList { + /// Item list + pub items: Vec, + /// The url of the next page + next_url: Option, + /// Fanbox api client + client: Arc, +} + +impl FanboxCommentList { + /// Create a new instance + pub fn new(value: &JsonValue, client: Arc) -> Option { + let oitems = &value["items"]; + if !oitems.is_array() { + return None; + } + let mut items = Vec::new(); + for item in oitems.members() { + items.push(FanboxComment::new(item)); + } + let next_url = match value["nextUrl"].as_str() { + Some(next_url) => Some(next_url.to_owned()), + None => None, + }; + Some(Self { + items, + next_url, + client, + }) + } +} + +impl Debug for FanboxCommentList { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FanboxCommentList") + .field("items", &self.items) + .field("next_url", &self.next_url) + .finish_non_exhaustive() + } +} diff --git a/src/fanbox/mod.rs b/src/fanbox/mod.rs index f990e5c..538c406 100644 --- a/src/fanbox/mod.rs +++ b/src/fanbox/mod.rs @@ -1,3 +1,5 @@ +pub mod comment; +pub mod comment_list; pub mod creator; pub mod error; pub mod item; diff --git a/src/fanbox/post.rs b/src/fanbox/post.rs index 9712b13..499b140 100644 --- a/src/fanbox/post.rs +++ b/src/fanbox/post.rs @@ -1,3 +1,4 @@ +use super::comment_list::FanboxCommentList; use crate::fanbox_api::FanboxClientInternal; use crate::parser::json::parse_u64; use json::JsonValue; @@ -20,6 +21,11 @@ impl FanboxPostArticle { self.data["commentCount"].as_u64() } + #[inline] + pub fn comment_list(&self) -> Option { + FanboxCommentList::new(&self.data["commentList"], Arc::clone(&self.client)) + } + #[inline] pub fn cover_image_url(&self) -> Option<&str> { self.data["coverImageUrl"].as_str() @@ -156,6 +162,7 @@ impl Debug for FanboxPostArticle { f.debug_struct("FanboxPostArticle") .field("id", &self.id()) .field("comment_count", &self.comment_count()) + .field("comment_list", &self.comment_list()) .field("cover_image_url", &self.cover_image_url()) .field("creator_id", &self.creator_id()) .field("excerpt", &self.excerpt()) @@ -192,6 +199,11 @@ impl FanboxPostImage { self.data["commentCount"].as_u64() } + #[inline] + pub fn comment_list(&self) -> Option { + FanboxCommentList::new(&self.data["commentList"], Arc::clone(&self.client)) + } + #[inline] pub fn cover_image_url(&self) -> Option<&str> { self.data["coverImageUrl"].as_str() @@ -328,6 +340,7 @@ impl Debug for FanboxPostImage { f.debug_struct("FanboxPostImage") .field("id", &self.id()) .field("comment_count", &self.comment_count()) + .field("comment_list", &self.comment_list()) .field("cover_image_url", &self.cover_image_url()) .field("creator_id", &self.creator_id()) .field("excerpt", &self.excerpt()) @@ -426,6 +439,11 @@ impl FanboxPostUnknown { #[allow(dead_code)] impl FanboxPostUnknown { + #[inline] + pub fn comment_list(&self) -> Option { + FanboxCommentList::new(&self.data["commentList"], Arc::clone(&self.client)) + } + #[inline] pub fn next_post(&self) -> Option { let obj = &self.data["nextPost"]; @@ -492,6 +510,15 @@ impl FanboxPost { self.get_json()["commentCount"].as_u64() } + #[inline] + pub fn comment_list(&self) -> Option { + match self { + Self::Article(a) => a.comment_list(), + Self::Image(i) => i.comment_list(), + Self::Unknown(u) => u.comment_list(), + } + } + #[inline] pub fn cover_image_url(&self) -> Option<&str> { self.get_json()["coverImageUrl"].as_str()