This commit is contained in:
2022-07-06 14:57:33 +00:00
committed by GitHub
parent c0fa6db0cb
commit 360b57aec6
4 changed files with 179 additions and 0 deletions

103
src/fanbox/comment.rs Normal file
View File

@@ -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<u64> {
parse_u64(&self.data["id"])
}
#[inline]
pub fn is_liked(&self) -> Option<bool> {
self.data["isLiked"].as_bool()
}
#[inline]
pub fn is_own(&self) -> Option<bool> {
self.data["isOwn"].as_bool()
}
#[inline]
pub fn like_count(&self) -> Option<u64> {
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<u64> {
parse_u64(&self.data["parentCommentId"])
}
#[inline]
pub fn replies(&self) -> Option<Vec<FanboxComment>> {
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<u64> {
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<u64> {
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()
}
}

View File

@@ -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<FanboxComment>,
/// The url of the next page
next_url: Option<String>,
/// Fanbox api client
client: Arc<FanboxClientInternal>,
}
impl FanboxCommentList {
/// Create a new instance
pub fn new(value: &JsonValue, client: Arc<FanboxClientInternal>) -> Option<Self> {
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()
}
}

View File

@@ -1,3 +1,5 @@
pub mod comment;
pub mod comment_list;
pub mod creator;
pub mod error;
pub mod item;

View File

@@ -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> {
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> {
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> {
FanboxCommentList::new(&self.data["commentList"], Arc::clone(&self.client))
}
#[inline]
pub fn next_post(&self) -> Option<FanboxPostRef> {
let obj = &self.data["nextPost"];
@@ -492,6 +510,15 @@ impl FanboxPost {
self.get_json()["commentCount"].as_u64()
}
#[inline]
pub fn comment_list(&self) -> Option<FanboxCommentList> {
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()