Skip to main content

fishpi_sdk/domain/
article.rs

1//! 文章相关的干净对外类型。
2
3use serde::{Serialize, Serializer};
4
5use crate::domain::user::Metal;
6use crate::ids::{ArticleId, UserId};
7use crate::impl_str_enum;
8use crate::wire::article::{
9    WireArticleAuthor, WireArticleComment, WireArticleDetail, WireArticleDraftDetail,
10    WireArticleDraftSummary, WireArticleList, WireArticleTag, WirePagination,
11};
12
13/// 帖子类型。
14#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
15#[repr(u8)]
16pub enum ArticleType {
17    Normal = 0,
18    Private = 1,
19    Broadcast = 2,
20    Thought = 3,
21    #[default]
22    Unknown = 4,
23    Question = 5,
24}
25
26impl ArticleType {
27    pub fn from_index(index: i64) -> Self {
28        match index {
29            0 => ArticleType::Normal,
30            1 => ArticleType::Private,
31            2 => ArticleType::Broadcast,
32            3 => ArticleType::Thought,
33            5 => ArticleType::Question,
34            _ => ArticleType::Unknown,
35        }
36    }
37}
38
39impl Serialize for ArticleType {
40    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
41    where
42        S: Serializer,
43    {
44        serializer.serialize_u8(*self as u8)
45    }
46}
47
48/// 帖子列表查询类型。
49#[derive(Clone, Copy, Debug)]
50pub enum ArticleListType {
51    Recent,
52    Hot,
53    Good,
54    Reply,
55    Long,
56    Perfect,
57}
58
59impl_str_enum! {
60    ArticleListType {
61        Recent => "recent",
62        Hot => "hot",
63        Good => "good",
64        Reply => "reply",
65        Long => "long",
66        Perfect => "perfect",
67    }
68}
69
70impl ArticleListType {
71    pub fn to_code(&self) -> &'static str {
72        match self {
73            ArticleListType::Recent => "",
74            ArticleListType::Hot => "/hot",
75            ArticleListType::Good => "/good",
76            ArticleListType::Reply => "/reply",
77            ArticleListType::Long => "/long",
78            ArticleListType::Perfect => "/perfect",
79        }
80    }
81
82    pub fn values() -> Vec<Self> {
83        vec![
84            ArticleListType::Recent,
85            ArticleListType::Hot,
86            ArticleListType::Good,
87            ArticleListType::Reply,
88            ArticleListType::Long,
89            ArticleListType::Perfect,
90        ]
91    }
92}
93
94/// 投票状态。
95#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
96pub enum VoteStatus {
97    #[default]
98    Normal,
99    Up,
100    Down,
101}
102
103impl VoteStatus {
104    pub fn from_index(index: usize) -> Self {
105        match index {
106            1 => VoteStatus::Up,
107            2 => VoteStatus::Down,
108            _ => VoteStatus::Normal,
109        }
110    }
111
112    fn from_wire(value: Option<i64>) -> Self {
113        match value {
114            Some(v) => VoteStatus::from_index((v + 1).max(0) as usize),
115            None => VoteStatus::Normal,
116        }
117    }
118}
119
120/// 文章状态。
121#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
122pub enum ArticleStatus {
123    #[default]
124    Normal,
125    Ban,
126    Lock,
127}
128
129impl ArticleStatus {
130    pub fn from_index(index: usize) -> Self {
131        match index {
132            0 => ArticleStatus::Normal,
133            1 => ArticleStatus::Ban,
134            _ => ArticleStatus::Lock,
135        }
136    }
137}
138
139/// 文章标签。
140#[derive(Clone, Debug)]
141#[non_exhaustive]
142pub struct ArticleTag {
143    pub id: String,
144    pub title: String,
145    pub description: String,
146    pub icon_path: String,
147    pub uri: String,
148    pub diy_css: String,
149    pub bad_count: u64,
150    pub comment_count: u32,
151    pub follower_count: u32,
152    pub good_count: u64,
153    pub reference_count: u32,
154    pub link_count: u32,
155    pub seo_desc: String,
156    pub seo_keywords: String,
157    pub seo_title: String,
158    pub tag_ad: String,
159    pub show_side_ad: u32,
160    pub status: u32,
161    pub random_double: f64,
162}
163
164impl From<WireArticleTag> for ArticleTag {
165    fn from(w: WireArticleTag) -> Self {
166        ArticleTag {
167            id: w.o_id,
168            title: w.title,
169            description: w.description,
170            icon_path: w.icon_path,
171            uri: w.uri,
172            diy_css: w.diy_css,
173            bad_count: w.bad_count,
174            comment_count: w.comment_count as u32,
175            follower_count: w.follower_count as u32,
176            good_count: w.good_count,
177            reference_count: w.reference_count as u32,
178            link_count: w.link_count as u32,
179            seo_desc: w.seo_desc,
180            seo_keywords: w.seo_keywords,
181            seo_title: w.seo_title,
182            tag_ad: w.tag_ad,
183            show_side_ad: w.show_side_ad as u32,
184            status: w.status as u32,
185            random_double: w.random_double,
186        }
187    }
188}
189
190/// 分页信息。
191#[derive(Clone, Debug, Default)]
192pub struct Pagination {
193    pub count: u32,
194    pub page_nums: Vec<u32>,
195}
196
197impl From<WirePagination> for Pagination {
198    fn from(w: WirePagination) -> Self {
199        Pagination {
200            count: w.count as u32,
201            page_nums: w.page_nums,
202        }
203    }
204}
205
206/// 文章/评论作者。
207#[derive(Clone, Debug, Default)]
208#[non_exhaustive]
209pub struct ArticleAuthor {
210    pub id: String,
211    pub user_name: String,
212    pub nickname: String,
213    pub avatar_url: String,
214    pub is_online: bool,
215    pub online_minute: u64,
216    pub point_status: bool,
217    pub follower_status: bool,
218    pub guide_step: u64,
219    pub online_status: bool,
220    pub current_checkin_streak_start: u64,
221    pub is_auto_blur: bool,
222    pub tags: String,
223    pub comment_status: bool,
224    pub timezone: String,
225    pub home_page: String,
226    pub is_enable_forward_page: bool,
227    pub user_ua_status: bool,
228    pub user_index_redirect_url: String,
229    pub latest_article_time: u64,
230    pub tag_count: u64,
231    pub nickname_list_view_mode: u64,
232    pub longest_checkin_streak: u64,
233    pub avatar_type: String,
234    pub sub_mail_send_time: u64,
235    pub update_time: u64,
236    pub sub_mail_status: bool,
237    pub is_join_point_rank: bool,
238    pub latest_login_time: u64,
239    pub user_app_role: u64,
240    pub user_avatar_view_mode: u64,
241    pub user_status: u64,
242    pub longest_checkin_streak_end: u64,
243    pub watching_article_status: bool,
244    pub latest_comment_time: u64,
245    pub province: String,
246    pub current_checkin_streak: u64,
247    pub user_no: u64,
248    pub following_tag_status: bool,
249    pub user_language: String,
250    pub is_join_used_point_rank: bool,
251    pub current_checkin_streak_end: u64,
252    pub following_article_status: bool,
253    pub keyboard_shortcuts_status: bool,
254    pub reply_watch_article_status: bool,
255    pub comment_view_mode: u64,
256    pub breezemoon_status: bool,
257    pub user_checkin_time: u64,
258    pub used_point: u64,
259    pub article_status: bool,
260    pub user_point: u64,
261    pub comment_count: u64,
262    pub user_intro: String,
263    pub user_mobile_skin: String,
264    pub list_page_size: u64,
265    pub geo_status: bool,
266    pub longest_checkin_streak_start: u64,
267    pub user_skin: String,
268    pub notify_status: bool,
269    pub following_user_status: bool,
270    pub article_count: u64,
271    pub user_role: String,
272    pub sys_metal: Vec<Metal>,
273}
274
275impl From<WireArticleAuthor> for ArticleAuthor {
276    fn from(w: WireArticleAuthor) -> Self {
277        ArticleAuthor {
278            id: w.o_id,
279            user_name: w.user_name,
280            nickname: w.nickname,
281            avatar_url: w.avatar_url,
282            is_online: w.is_online,
283            online_minute: w.online_minute,
284            point_status: w.point_status,
285            follower_status: w.follower_status,
286            guide_step: w.guide_step,
287            online_status: w.online_status,
288            current_checkin_streak_start: w.current_checkin_streak_start,
289            is_auto_blur: w.is_auto_blur,
290            tags: w.tags,
291            comment_status: w.comment_status,
292            timezone: w.timezone,
293            home_page: w.home_page,
294            is_enable_forward_page: w.is_enable_forward_page,
295            user_ua_status: w.user_ua_status,
296            user_index_redirect_url: w.user_index_redirect_url,
297            latest_article_time: w.latest_article_time,
298            tag_count: w.tag_count,
299            nickname_list_view_mode: w.list_view_mode,
300            longest_checkin_streak: w.longest_checkin_streak,
301            avatar_type: w.avatar_type,
302            sub_mail_send_time: w.sub_mail_send_time,
303            update_time: w.update_time,
304            sub_mail_status: w.sub_mail_status,
305            is_join_point_rank: w.is_join_point_rank,
306            latest_login_time: w.latest_login_time,
307            user_app_role: w.user_app_role,
308            user_avatar_view_mode: w.user_avatar_view_mode,
309            user_status: w.user_status,
310            longest_checkin_streak_end: w.longest_checkin_streak_end,
311            watching_article_status: w.watching_article_status,
312            latest_comment_time: w.latest_comment_time,
313            province: w.province,
314            current_checkin_streak: w.current_checkin_streak,
315            user_no: w.user_no,
316            following_tag_status: w.following_tag_status,
317            user_language: w.user_language,
318            is_join_used_point_rank: w.is_join_used_point_rank,
319            current_checkin_streak_end: w.current_checkin_streak_end,
320            following_article_status: w.following_article_status,
321            keyboard_shortcuts_status: w.keyboard_shortcuts_status,
322            reply_watch_article_status: w.reply_watch_article_status,
323            comment_view_mode: w.comment_view_mode,
324            breezemoon_status: w.breezemoon_status,
325            user_checkin_time: w.user_checkin_time,
326            used_point: w.used_point,
327            article_status: w.article_status,
328            user_point: w.user_point,
329            comment_count: w.comment_count,
330            user_intro: w.user_intro,
331            user_mobile_skin: w.user_mobile_skin,
332            list_page_size: w.list_page_size,
333            geo_status: w.geo_status,
334            longest_checkin_streak_start: w.longest_checkin_streak_start,
335            user_skin: w.user_skin,
336            notify_status: w.notify_status,
337            following_user_status: w.following_user_status,
338            article_count: w.article_count,
339            user_role: w.user_role,
340            sys_metal: w.sys_metal.into_iter().map(Metal::from).collect(),
341        }
342    }
343}
344
345/// 评论作者。
346pub type CommentAuthor = ArticleAuthor;
347
348/// 文章评论。
349#[derive(Clone, Debug, Default)]
350#[non_exhaustive]
351pub struct ArticleComment {
352    pub id: String,
353    pub is_nice: bool,
354    pub create_time_str: String,
355    pub author_id: UserId,
356    pub score: String,
357    pub create_time: String,
358    pub author_url: String,
359    pub vote: VoteStatus,
360    pub revision_count: u64,
361    pub time_ago: String,
362    pub reply_id: String,
363    pub sys_metal: Vec<Metal>,
364    pub good_count: u64,
365    pub visible: bool,
366    pub article_id: ArticleId,
367    pub rewarded_count: u64,
368    pub sharp_url: String,
369    pub is_anonymous: bool,
370    pub reply_count: u64,
371    pub content: String,
372    pub status: ArticleStatus,
373    pub commenter: CommentAuthor,
374    pub author: String,
375    pub thank_count: u64,
376    pub bad_count: u64,
377    pub rewarded: bool,
378    pub thumbnail_url: String,
379    pub audio_url: String,
380    pub offered: u64,
381}
382
383impl From<WireArticleComment> for ArticleComment {
384    fn from(w: WireArticleComment) -> Self {
385        ArticleComment {
386            id: w.o_id,
387            is_nice: w.is_nice,
388            create_time_str: w.create_time_str,
389            author_id: w.author_id.into(),
390            score: w.score.to_string(),
391            create_time: w.create_time,
392            author_url: w.author_url,
393            vote: VoteStatus::from_wire(w.vote),
394            revision_count: w.revision_count,
395            time_ago: w.time_ago,
396            reply_id: w.reply_id,
397            sys_metal: w.sys_metal.into_iter().map(Metal::from).collect(),
398            good_count: w.good_count,
399            visible: w.visible,
400            article_id: w.article_id.into(),
401            rewarded_count: w.rewarded_count,
402            sharp_url: w.sharp_url,
403            is_anonymous: w.is_anonymous,
404            reply_count: w.reply_count,
405            content: w.content,
406            status: ArticleStatus::from_index(w.status as usize),
407            commenter: w.commenter.into(),
408            author: w.author,
409            thank_count: w.thank_count,
410            bad_count: w.bad_count,
411            rewarded: w.rewarded,
412            thumbnail_url: w.thumbnail_url,
413            audio_url: w.audio_url,
414            offered: w.offered,
415        }
416    }
417}
418
419/// 文章详情。
420#[derive(Clone, Debug, Default)]
421#[non_exhaustive]
422pub struct ArticleDetail {
423    pub id: String,
424    pub title: String,
425    pub title_emoji: String,
426    pub title_emoji_unicode: String,
427    pub type_: ArticleType,
428    pub status: ArticleStatus,
429    pub vote: VoteStatus,
430    pub author_id: UserId,
431    pub author_name: String,
432    pub author_intro: String,
433    pub author_url: String,
434    pub author: ArticleAuthor,
435    pub thumbnail_url: String,
436    pub thumbnail_url20: String,
437    pub thumbnail_url48: String,
438    pub thumbnail_url210: String,
439    pub content: String,
440    pub source: String,
441    pub preview_content: String,
442    pub table: String,
443    pub tags: String,
444    pub tag_objs: Vec<ArticleTag>,
445    pub permalink: String,
446    pub img1_url: String,
447    pub audio_url: String,
448    pub city: String,
449    pub ip: String,
450    pub create_time: String,
451    pub create_time_str: String,
452    pub update_time: String,
453    pub update_time_str: String,
454    pub time_ago: String,
455    pub comment_time_ago: String,
456    pub latest_comment_time: String,
457    pub latest_comment_time_str: String,
458    pub latest_commenter_name: String,
459    pub show_in_list: bool,
460    pub anonymous: bool,
461    pub offered: bool,
462    pub offer_point: u64,
463    pub perfect: bool,
464    pub commentable: bool,
465    pub comment_viewable: bool,
466    pub rewarded: bool,
467    pub rewarded_count: u64,
468    pub reward_point: u64,
469    pub reward_content: String,
470    pub thanked: bool,
471    pub thank_count: u64,
472    pub thanked_count: u64,
473    pub is_following: bool,
474    pub is_watching: bool,
475    pub is_my_article: bool,
476    pub bad_count: u64,
477    pub good_count: u64,
478    pub view_count: u64,
479    pub view_count_format: String,
480    pub watch_count: u64,
481    pub collect_count: u64,
482    pub comment_count: u64,
483    pub anonymous_view: u64,
484    pub heat: u64,
485    pub stick: u64,
486    pub stick_remains: u64,
487    pub editor_type: u64,
488    pub push_order: u64,
489    pub reddit_score: String,
490    pub revision_count: u64,
491    pub random_double: f64,
492    pub pagination: Option<Pagination>,
493    pub comments: Vec<ArticleComment>,
494    pub nice_comments: Vec<ArticleComment>,
495}
496
497impl ArticleDetail {
498    /// 帖子 Markdown 源文本。
499    pub fn markdown_content(&self) -> &str {
500        if self.source.trim().is_empty() {
501            &self.content
502        } else {
503            &self.source
504        }
505    }
506
507    /// 帖子 HTML 内容。
508    pub fn html_content(&self) -> &str {
509        &self.content
510    }
511
512    /// 推荐展示正文。
513    pub fn display_content(&self) -> &str {
514        self.markdown_content()
515    }
516
517    /// 是否存在 Markdown 原文。
518    pub fn has_markdown_source(&self) -> bool {
519        !self.source.trim().is_empty()
520    }
521}
522
523impl From<WireArticleDetail> for ArticleDetail {
524    fn from(w: WireArticleDetail) -> Self {
525        ArticleDetail {
526            id: w.o_id,
527            title: w.title,
528            title_emoji: w.title_emoji,
529            title_emoji_unicode: w.title_emoji_unicode,
530            type_: ArticleType::from_index(w.type_index),
531            status: ArticleStatus::from_index(w.status as usize),
532            vote: VoteStatus::from_wire(w.vote),
533            author_id: w.author_id.into(),
534            author_name: w.author_name,
535            author_intro: w.author_intro,
536            author_url: w.author_url,
537            author: w.author.into(),
538            thumbnail_url: w.thumbnail_url,
539            thumbnail_url20: w.thumbnail_url20,
540            thumbnail_url48: w.thumbnail_url48,
541            thumbnail_url210: w.thumbnail_url210,
542            content: w.content,
543            source: w.source,
544            preview_content: w.preview_content,
545            table: w.table,
546            tags: w.tags,
547            tag_objs: w.tag_objs.into_iter().map(ArticleTag::from).collect(),
548            permalink: w.permalink,
549            img1_url: w.img1_url,
550            audio_url: w.audio_url,
551            city: w.city,
552            ip: w.ip,
553            create_time: w.create_time,
554            create_time_str: w.create_time_str,
555            update_time: w.update_time,
556            update_time_str: w.update_time_str,
557            time_ago: w.time_ago,
558            comment_time_ago: w.comment_time_ago,
559            latest_comment_time: w.latest_comment_time,
560            latest_comment_time_str: w.latest_comment_time_str,
561            latest_commenter_name: w.latest_commenter_name,
562            show_in_list: w.show_in_list,
563            anonymous: w.anonymous,
564            offered: w.offered,
565            offer_point: w.offer_point,
566            perfect: w.perfect,
567            commentable: w.commentable,
568            comment_viewable: w.comment_viewable,
569            rewarded: w.rewarded,
570            rewarded_count: w.rewarded_count,
571            reward_point: w.reward_point,
572            reward_content: w.reward_content,
573            thanked: w.thanked,
574            thank_count: w.thank_count,
575            thanked_count: w.thanked_count,
576            is_following: w.is_following,
577            is_watching: w.is_watching,
578            is_my_article: w.is_my_article,
579            bad_count: w.bad_count,
580            good_count: w.good_count,
581            view_count: w.view_count,
582            view_count_format: w.view_count_format,
583            watch_count: w.watch_count,
584            collect_count: w.collect_count,
585            comment_count: w.comment_count,
586            anonymous_view: w.anonymous_view,
587            heat: w.heat,
588            stick: w.stick,
589            stick_remains: w.stick_remains,
590            editor_type: w.editor_type,
591            push_order: w.push_order,
592            reddit_score: w.reddit_score.to_string(),
593            revision_count: w.revision_count,
594            random_double: w.random_double,
595            pagination: None,
596            comments: w.comments.into_iter().map(ArticleComment::from).collect(),
597            nice_comments: w
598                .nice_comments
599                .into_iter()
600                .map(ArticleComment::from)
601                .collect(),
602        }
603    }
604}
605
606/// 文章列表。
607#[derive(Clone, Debug, Default)]
608#[non_exhaustive]
609pub struct ArticleList {
610    pub list: Vec<ArticleDetail>,
611    pub pagination: Pagination,
612    pub tag: Option<ArticleTag>,
613}
614
615impl From<WireArticleList> for ArticleList {
616    fn from(w: WireArticleList) -> Self {
617        ArticleList {
618            list: w.articles.into_iter().map(ArticleDetail::from).collect(),
619            pagination: w.pagination.into(),
620            tag: w.tag.map(ArticleTag::from),
621        }
622    }
623}
624
625/// 文章草稿列表项。
626#[derive(Clone, Debug, Default)]
627#[non_exhaustive]
628pub struct ArticleDraftSummary {
629    pub id: String,
630    pub title: String,
631    pub summary: String,
632    pub tags: String,
633    pub type_: u32,
634    pub column_id: String,
635    pub column_title: String,
636    pub chapter_no: String,
637    pub updated_time: u64,
638    pub created_time: u64,
639    pub user_id: UserId,
640    pub reward_point: u32,
641    pub qna_offer_point: u32,
642    pub show_in_list: u32,
643    pub statement: u32,
644    pub commentable: bool,
645    pub anonymous: bool,
646    pub notify_followers: bool,
647}
648
649impl From<WireArticleDraftSummary> for ArticleDraftSummary {
650    fn from(w: WireArticleDraftSummary) -> Self {
651        ArticleDraftSummary {
652            id: w.o_id,
653            title: w.title,
654            summary: w.summary,
655            tags: w.tags,
656            type_: w.type_ as u32,
657            column_id: w.column_id,
658            column_title: w.column_title,
659            chapter_no: w.chapter_no,
660            updated_time: w.updated_time,
661            created_time: w.created_time,
662            user_id: w.user_id.into(),
663            reward_point: w.reward_point as u32,
664            qna_offer_point: w.qna_offer_point as u32,
665            show_in_list: w.show_in_list as u32,
666            statement: w.statement as u32,
667            commentable: w.commentable,
668            anonymous: w.anonymous,
669            notify_followers: w.notify_followers,
670        }
671    }
672}
673
674/// 文章草稿详情。
675#[derive(Clone, Debug, Default)]
676#[non_exhaustive]
677pub struct ArticleDraftDetail {
678    pub id: String,
679    pub title: String,
680    pub content: String,
681    pub thought_content: String,
682    pub tags: String,
683    pub type_: u32,
684    pub column_id: String,
685    pub column_title: String,
686    pub chapter_no: String,
687    pub reward_content: String,
688    pub reward_point: u32,
689    pub qna_offer_point: u32,
690    pub commentable: bool,
691    pub anonymous: bool,
692    pub notify_followers: bool,
693    pub show_in_list: u32,
694    pub statement: u32,
695    pub updated_time: u64,
696    pub created_time: u64,
697    pub summary: String,
698    pub user_id: UserId,
699}
700
701impl From<WireArticleDraftDetail> for ArticleDraftDetail {
702    fn from(w: WireArticleDraftDetail) -> Self {
703        ArticleDraftDetail {
704            id: w.o_id,
705            title: w.title,
706            content: w.content,
707            thought_content: w.thought_content,
708            tags: w.tags,
709            type_: w.type_ as u32,
710            column_id: w.column_id,
711            column_title: w.column_title,
712            chapter_no: w.chapter_no,
713            reward_content: w.reward_content,
714            reward_point: w.reward_point as u32,
715            qna_offer_point: w.qna_offer_point as u32,
716            commentable: w.commentable,
717            anonymous: w.anonymous,
718            notify_followers: w.notify_followers,
719            show_in_list: w.show_in_list as u32,
720            statement: w.statement as u32,
721            updated_time: w.updated_time,
722            created_time: w.created_time,
723            summary: w.summary,
724            user_id: w.user_id.into(),
725        }
726    }
727}