Skip to main content

fishpi_sdk/domain/
notice.rs

1use crate::ids::{ArticleId, UserId};
2use crate::wire::notice::{
3    WireNoticeAt, WireNoticeComment, WireNoticeCount, WireNoticeFollow, WireNoticeMsg,
4    WireNoticePoint, WireNoticeReply, WireNoticeSystem, WireNoticeTag,
5};
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum NoticeType {
9    Point,
10    Commented,
11    Reply,
12    At,
13    Following,
14    Broadcast,
15    System,
16}
17
18impl NoticeType {
19    pub fn as_str(&self) -> &'static str {
20        match self {
21            Self::Point => "point",
22            Self::Commented => "commented",
23            Self::Reply => "reply",
24            Self::At => "at",
25            Self::Following => "following",
26            Self::Broadcast => "broadcast",
27            Self::System => "sys-announce",
28        }
29    }
30}
31
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33pub enum NoticeMsgType {
34    Refresh,
35    WarnBroadcast,
36}
37
38impl NoticeMsgType {
39    pub fn as_str(&self) -> &'static str {
40        match self {
41            Self::Refresh => "refreshNotification",
42            Self::WarnBroadcast => "warnBroadcast",
43        }
44    }
45
46    pub fn values() -> [&'static str; 2] {
47        ["refreshNotification", "warnBroadcast"]
48    }
49
50    pub fn from_command(command: &str) -> Option<Self> {
51        match command {
52            "refreshNotification" => Some(Self::Refresh),
53            "warnBroadcast" => Some(Self::WarnBroadcast),
54            _ => None,
55        }
56    }
57}
58
59#[derive(Clone, Debug)]
60#[non_exhaustive]
61pub struct NoticeCount {
62    /// 用户是否启用 Web 通知
63    pub notify_status: bool,
64    /// 未读通知数
65    pub count: u64,
66    /// 未读回复通知数
67    pub reply: u64,
68    /// 未读积分通知数
69    pub point: u64,
70    /// 未读 @ 通知数
71    pub at: u64,
72    /// 未读同城通知数
73    pub broadcast: u64,
74    /// 未读系统通知数
75    pub sys_announce: u64,
76    /// 未读关注者通知数
77    pub new_follower: u64,
78    /// 未读关注通知数
79    pub following: u64,
80    /// 未读评论通知数
81    pub commented: u64,
82}
83
84impl From<WireNoticeCount> for NoticeCount {
85    fn from(w: WireNoticeCount) -> Self {
86        Self {
87            notify_status: w.notify_status,
88            count: w.count,
89            reply: w.reply,
90            point: w.point,
91            at: w.at,
92            broadcast: w.broadcast,
93            sys_announce: w.sys_announce,
94            new_follower: w.new_follower,
95            following: w.following,
96            commented: w.commented,
97        }
98    }
99}
100
101#[derive(Clone, Debug)]
102#[non_exhaustive]
103pub struct NoticePoint {
104    /// 通知 ID
105    pub id: String,
106    /// 数据 ID
107    pub data_id: String,
108    /// 用户 ID
109    pub user_id: UserId,
110    /// 数据类型
111    pub data_type: u32,
112    /// 通知描述
113    pub description: String,
114    /// 是否已读
115    pub has_read: bool,
116    /// 创建时间
117    pub create_time: String,
118}
119
120impl From<WireNoticePoint> for NoticePoint {
121    fn from(w: WireNoticePoint) -> Self {
122        Self {
123            id: w.o_id,
124            data_id: w.data_id,
125            user_id: w.user_id.into(),
126            data_type: w.data_type,
127            description: w.description,
128            has_read: w.has_read,
129            create_time: w.create_time,
130        }
131    }
132}
133
134#[derive(Clone, Debug)]
135#[non_exhaustive]
136pub struct NoticeComment {
137    /// 通知 ID
138    pub id: String,
139    /// 文章标题
140    pub title: String,
141    /// 文章作者
142    pub author: String,
143    /// 作者头像
144    pub thumbnail_url: String,
145    /// 文章类型
146    pub type_: u32,
147    /// 是否精选
148    pub perfect: bool,
149    /// 评论内容
150    pub content: String,
151    /// 评论地址
152    pub sharp_url: String,
153    /// 是否已读
154    pub has_read: bool,
155    /// 评论时间
156    pub create_time: String,
157}
158
159impl From<WireNoticeComment> for NoticeComment {
160    fn from(w: WireNoticeComment) -> Self {
161        Self {
162            id: w.o_id,
163            title: w.title,
164            author: w.author,
165            thumbnail_url: w.thumbnail_url,
166            type_: w.type_,
167            perfect: w.perfect,
168            content: w.content,
169            sharp_url: w.sharp_url,
170            has_read: w.has_read,
171            create_time: w.create_time,
172        }
173    }
174}
175
176#[derive(Clone, Debug)]
177#[non_exhaustive]
178pub struct NoticeReply {
179    /// 通知 ID
180    pub id: String,
181    /// 文章标题
182    pub title: String,
183    /// 文章作者
184    pub author: String,
185    /// 作者头像
186    pub thumbnail_url: String,
187    /// 文章类型
188    pub type_: u32,
189    /// 是否精选
190    pub perfect: bool,
191    /// 回复内容
192    pub content: String,
193    /// 回复地址
194    pub sharp_url: String,
195    /// 是否已读
196    pub has_read: bool,
197    /// 回复时间
198    pub create_time: String,
199    /// 数据类型
200    pub data_type: u32,
201}
202
203impl From<WireNoticeReply> for NoticeReply {
204    fn from(w: WireNoticeReply) -> Self {
205        Self {
206            id: w.o_id,
207            title: w.title,
208            author: w.author,
209            thumbnail_url: w.thumbnail_url,
210            type_: w.type_,
211            perfect: w.perfect,
212            content: w.content,
213            sharp_url: w.sharp_url,
214            has_read: w.has_read,
215            create_time: w.create_time,
216            data_type: w.data_type,
217        }
218    }
219}
220
221#[derive(Clone, Debug)]
222#[non_exhaustive]
223pub struct NoticeAt {
224    /// 通知 ID
225    pub id: String,
226    /// 数据类型
227    pub data_type: u32,
228    /// 用户名
229    pub user_name: String,
230    /// 文章 @ 的作者用户名
231    pub author_name: String,
232    /// 文章 @ 的文章标题
233    pub article_title: String,
234    /// 文章 @ 的文章 ID
235    pub article_id: ArticleId,
236    /// 文章 @ 的跳转地址
237    pub url: String,
238    /// 是否是文章内 @
239    pub at_in_article: bool,
240    /// 用户头像(点赞类消息)
241    pub avatar_url: String,
242    /// 用户头像(@ 消息/红包)
243    pub user_avatar_url: String,
244    /// 通知内容
245    pub content: String,
246    /// 通知描述
247    pub description: String,
248    /// 是否已读
249    pub has_read: bool,
250    /// 创建时间
251    pub create_time: String,
252    /// 数据 ID
253    pub data_id: String,
254    /// 是否已删除
255    pub deleted: bool,
256}
257
258impl From<WireNoticeAt> for NoticeAt {
259    fn from(w: WireNoticeAt) -> Self {
260        Self {
261            id: w.o_id,
262            data_type: w.data_type,
263            user_name: w.user_name,
264            author_name: w.author_name,
265            article_title: w.article_title,
266            article_id: w.article_id.into(),
267            url: w.url,
268            at_in_article: w.at_in_article,
269            avatar_url: w.avatar_url,
270            user_avatar_url: w.user_avatar_url,
271            content: w.content,
272            description: w.description,
273            has_read: w.has_read,
274            create_time: w.create_time,
275            data_id: w.data_id,
276            deleted: w.deleted,
277        }
278    }
279}
280
281#[derive(Clone, Debug)]
282#[non_exhaustive]
283pub struct NoticeTag {
284    /// 标签 ID
285    pub id: String,
286    /// 标签名
287    pub title: String,
288    /// icon 地址
289    pub icon_path: String,
290    /// 标签地址
291    pub uri: String,
292}
293
294impl From<WireNoticeTag> for NoticeTag {
295    fn from(w: WireNoticeTag) -> Self {
296        Self {
297            id: w.o_id,
298            title: w.title,
299            icon_path: w.icon_path,
300            uri: w.uri,
301        }
302    }
303}
304
305#[derive(Clone, Debug)]
306#[non_exhaustive]
307pub struct NoticeFollow {
308    /// 通知 ID
309    pub id: String,
310    /// 文章地址
311    pub url: String,
312    /// 数据类型
313    pub data_type: u32,
314    /// 文章标题
315    pub title: String,
316    /// 作者
317    pub author: String,
318    /// 通知内容
319    pub content: String,
320    /// 是否评论
321    pub is_comment: bool,
322    /// 作者头像
323    pub thumbnail_url: String,
324    /// 文章评论数
325    pub comment_count: u32,
326    /// 是否精选
327    pub perfect: bool,
328    /// 文章标签列表
329    pub tag_objs: Vec<NoticeTag>,
330    /// 文章标签
331    pub tags: String,
332    /// 文章类型
333    pub type_: u32,
334    /// 是否已读
335    pub has_read: bool,
336    /// 通知创建时间
337    pub create_time: String,
338}
339
340impl From<WireNoticeFollow> for NoticeFollow {
341    fn from(w: WireNoticeFollow) -> Self {
342        Self {
343            id: w.o_id,
344            url: w.url,
345            data_type: w.data_type,
346            title: w.title,
347            author: w.author,
348            content: w.content,
349            is_comment: w.is_comment,
350            thumbnail_url: w.thumbnail_url,
351            comment_count: w.comment_count,
352            perfect: w.perfect,
353            tag_objs: w.tag_objs.into_iter().map(NoticeTag::from).collect(),
354            tags: w.tags,
355            type_: w.type_,
356            has_read: w.has_read,
357            create_time: w.create_time,
358        }
359    }
360}
361
362#[derive(Clone, Debug)]
363#[non_exhaustive]
364pub struct NoticeSystem {
365    /// 通知 ID
366    pub id: String,
367    /// 用户 ID
368    pub user_id: UserId,
369    /// 数据 ID
370    pub data_id: String,
371    /// 数据类型
372    pub data_type: u32,
373    /// 消息描述
374    pub description: String,
375    /// 消息内容
376    pub content: String,
377    /// 是否已读
378    pub has_read: bool,
379    /// 创建日期
380    pub create_time: String,
381}
382
383impl From<WireNoticeSystem> for NoticeSystem {
384    fn from(w: WireNoticeSystem) -> Self {
385        Self {
386            id: w.o_id,
387            user_id: w.user_id.into(),
388            data_id: w.data_id,
389            data_type: w.data_type,
390            description: w.description,
391            content: w.content,
392            has_read: w.has_read,
393            create_time: w.create_time,
394        }
395    }
396}
397
398#[derive(Clone, Debug)]
399#[non_exhaustive]
400pub struct NoticeMsg {
401    /// 通知类型
402    pub command: String,
403    /// 通知接收者用户 ID
404    pub user_id: UserId,
405    /// 全局公告内容,仅 `warnBroadcast` 有信息
406    pub content: Option<String>,
407    /// 全局公告发布者,仅 `warnBroadcast` 有信息
408    pub who: Option<String>,
409}
410
411impl From<WireNoticeMsg> for NoticeMsg {
412    fn from(w: WireNoticeMsg) -> Self {
413        Self {
414            command: w.command,
415            user_id: w.user_id.into(),
416            content: w.content,
417            who: w.who,
418        }
419    }
420}
421
422#[derive(Clone, Debug)]
423pub enum NoticeItem {
424    /// 积分通知
425    Point(NoticePoint),
426    /// 评论/回帖通知
427    Comment(NoticeComment),
428    /// 回复通知
429    Reply(NoticeReply),
430    /// 提到我通知
431    At(NoticeAt),
432    /// 关注通知
433    Follow(NoticeFollow),
434    /// 系统通知
435    System(NoticeSystem),
436}
437
438pub type NoticeList = Vec<NoticeItem>;