botcat_capoo/model/event/notice/
notify.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Clone, Debug, Deserialize, Serialize)]
4#[serde(tag = "sub_type", rename_all = "snake_case")]
5pub enum NotifyEvent {
6 Poke(PokeEvent),
7 LuckyKing(LuckyKingEvent),
8 Honor(HonorEvent),
9}
10
11#[derive(Clone, Debug, Deserialize, Serialize)]
12pub struct PokeEvent {
13 #[serde(skip_serializing_if = "Option::is_none")]
14 pub group_id: Option<u64>,
15 pub user_id: u64,
16 pub target_id: u64,
17}
18
19#[derive(Clone, Debug, Deserialize, Serialize)]
20pub struct LuckyKingEvent {
21 group_id: u64,
22 user_id: u64,
23 target_id: u64,
24}
25
26#[derive(Clone, Debug, Deserialize, Serialize)]
27pub struct HonorEvent {
28 honor_type: String,
29 group_id: u64,
30 user_id: u64,
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36 use crate::model::event::{BotEvent, NoticeEvent, PostEvent};
37
38 #[test]
39 fn test_serialize_group_poke_event() {
40 let poke_event = PokeEvent {
41 group_id: Some(123456),
42 user_id: 654321,
43 target_id: 111111,
44 };
45 let notify_event = NotifyEvent::Poke(poke_event);
46 let notice_event = NoticeEvent::Notify(notify_event);
47 let post_event = PostEvent::Notice(notice_event);
48 let bot_event = BotEvent {
49 time: 1625079600,
50 self_id: 123456789,
51 post_type: post_event,
52 };
53
54 let json = serde_json::to_string(&bot_event).expect("Failed to serialize BotEvent");
55
56 println!("{json}");
57
58 assert!(json.contains("\"sub_type\":\"poke\""));
59 assert!(json.contains("\"group_id\":123456"));
60 assert!(json.contains("\"user_id\":654321"));
61 assert!(json.contains("\"target_id\":111111"));
62 }
63
64 #[test]
65 fn test_deserialize_group_poke_event() {
66 let json = r#"{"time":1625079600,"self_id":123456789,"post_type":"notice","notice_type":"notify","sub_type":"poke","group_id":123456,"user_id":654321,"target_id":111111}"#;
67 let bot_event: BotEvent =
68 serde_json::from_str(json).expect("Failed to deserialize BotEvent");
69
70 println!("{:?}", bot_event);
71
72 match bot_event.post_type {
73 PostEvent::Notice(notice_event) => match notice_event {
74 NoticeEvent::Notify(notify_event) => match notify_event {
75 NotifyEvent::Poke(poke_event) => {
76 assert_eq!(poke_event.group_id, Some(123456));
77 assert_eq!(poke_event.user_id, 654321);
78 assert_eq!(poke_event.target_id, 111111);
79 }
80 _ => panic!("Expected PokeEvent"),
81 },
82 _ => panic!("Expected NotifyEvent"),
83 },
84 _ => panic!("Expected NoticeEvent"),
85 }
86 }
87
88 #[test]
89 fn test_serialize_private_poke_event() {
90 let poke_event = PokeEvent {
91 group_id: None,
92 user_id: 654321,
93 target_id: 111111,
94 };
95 let notify_event = NotifyEvent::Poke(poke_event);
96 let notice_event = NoticeEvent::Notify(notify_event);
97 let post_event = PostEvent::Notice(notice_event);
98 let bot_event = BotEvent {
99 time: 1625079600,
100 self_id: 123456789,
101 post_type: post_event,
102 };
103
104 let json = serde_json::to_string(&bot_event).expect("Failed to serialize BotEvent");
105
106 println!("{json}");
107
108 assert!(json.contains("\"sub_type\":\"poke\""));
109 assert!(json.contains("\"user_id\":654321"));
110 assert!(!json.contains("\"group_id\""));
111 assert!(json.contains("\"target_id\":111111"));
112 }
113
114 #[test]
115 fn test_deserialize_private_poke_event() {
116 let json = r#"{"time":1625079600,"self_id":123456789,"post_type":"notice","notice_type":"notify","sub_type":"poke","user_id":654321,"target_id":111111}"#;
117 let bot_event: BotEvent =
118 serde_json::from_str(json).expect("Failed to deserialize BotEvent");
119
120 println!("{:?}", bot_event);
121
122 match bot_event.post_type {
123 PostEvent::Notice(notice_event) => match notice_event {
124 NoticeEvent::Notify(notify_event) => match notify_event {
125 NotifyEvent::Poke(poke_event) => {
126 assert_eq!(poke_event.group_id, None);
127 assert_eq!(poke_event.user_id, 654321);
128 assert_eq!(poke_event.target_id, 111111);
129 }
130 _ => panic!("Expected PokeEvent"),
131 },
132 _ => panic!("Expected NotifyEvent"),
133 },
134 _ => panic!("Expected NoticeEvent"),
135 }
136 }
137}