From e8a21402ded91d4c66cfe78575c8182f8719ca67 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 8 Jul 2022 10:55:42 +0000 Subject: [PATCH] Update --- src/fanbox/comment_list.rs | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/fanbox/comment_list.rs b/src/fanbox/comment_list.rs index 4e59c93..a7fa19d 100644 --- a/src/fanbox/comment_list.rs +++ b/src/fanbox/comment_list.rs @@ -1,6 +1,10 @@ use super::comment::FanboxComment; +#[cfg(test)] +use super::post::FanboxPost; use crate::fanbox_api::FanboxClientInternal; +use crate::gettext; use json::JsonValue; +use proc_macros::fanbox_api_test; use std::fmt::Debug; use std::sync::Arc; @@ -15,6 +19,36 @@ pub struct FanboxCommentList { } impl FanboxCommentList { + #[allow(dead_code)] + /// Get next page. + /// # Note + /// If no next page presented, will return a error. + pub async fn get_next_page(&self) -> Option { + match &self.next_url { + Some(next_url) => { + match self + .client + .get_url( + next_url, + gettext("Failed to get next page of comments:"), + gettext("Comment's data:"), + ) + .await + { + Some(s) => Self::new(&s["body"], Arc::clone(&self.client)), + None => None, + } + } + None => None, + } + } + + #[allow(dead_code)] + /// Returns true if next page is presented. + pub fn has_next_page(&self) -> bool { + self.next_url.is_some() + } + /// Create a new instance pub fn new(value: &JsonValue, client: Arc) -> Option { let oitems = &value["items"]; @@ -45,3 +79,40 @@ impl Debug for FanboxCommentList { .finish_non_exhaustive() } } + +fanbox_api_test!(test_comment_list, { + match client.get_post_info(3795935).await { + Some(data) => { + assert_eq!(data.id(), Some(3795935)); + assert_eq!( + data.title(), + Some("171🍡新刊表紙の限定差分&skebイラストラフ※R18") + ); + match data { + FanboxPost::Article(a) => { + println!("{:?}", a); + let list = a.comment_list().unwrap(); + if list.has_next_page() { + match list.get_next_page().await { + Some(list) => { + println!("{:?}", list); + } + None => { + panic!("Failed to get the next page.") + } + } + } + } + FanboxPost::Unknown(data) => { + println!("{}", data.data); + } + _ => { + println!("{:?}", data); + } + } + } + None => { + panic!("Failed to get the post info(171🍡新刊表紙の限定差分&skebイラストラフ※R18)"); + } + } +});