1use std::sync::Arc;
2
3use crate::advanced::text_handler;
4use crate::api::BotApi;
5use crate::listener::Lout;
6use crate::types::*;
7use crate::vision::*;
8
9pub fn handle(api: BotApi, lout: &Arc<Lout>, raw: &RawMessage, is_edited: bool) {
10 let message = to_message(raw, is_edited);
11
12
13 macro_rules! maybe_field {
14 ($name:ident, $variant:ident, $field:ident, $fnc:ident) => {{
15 if let Some(val) = &raw.$name {
16 let obj = $variant {
17 message,
18 $field: val.clone(),
19 };
20 if let Some(fnc) = lout.$fnc() {
21 (*fnc)((api, obj));
22 }
23 return;
24 }
25 }}
26 }
27
28 macro_rules! maybe_field_with_caption {
29 ($name:ident, $variant:ident, $field:ident, $fnc:ident) => {{
30 if let Some(fnc) = lout.$fnc() {
31 if let Some(val) = &raw.$name {
32 let obj = $variant {
33 message,
34 $field: val.clone(),
35 caption: raw.caption.clone(),
36 };
37 (*fnc)((api, obj));
38 return;
39 }
40 }
41 }}
42 }
43
44 macro_rules! maybe_field_with_caption_and_group {
45 ($name:ident, $variant:ident, $field:ident, $fnc:ident) => {{
46 if let Some(fnc) = lout.$fnc() {
47 if let Some(val) = &raw.$name {
48 let obj = $variant {
49 message,
50 $field: val.clone(),
51 caption: raw.caption.clone(),
52 media_group_id: raw.media_group_id.clone()
53 };
54 (*fnc)((api, obj));
55 return;
56 }
57 }
58 }}
59 }
60
61 macro_rules! maybe_true_field {
62 ($name:ident, $fnc:ident) => {{
63 if let Some(fnc) = lout.$fnc() {
64 if let Some(True) = &raw.$name {
65 (*fnc)((api, message));
66 return;
67 }
68 }
69 }}
70 }
71
72 if let Some(_) = &raw.text {
73 text_handler::handle_text(api, lout, raw, message);
74 return;
75 }
76
77
78 maybe_field! ( audio, VAudioMessage, audio, listen_audio );
79 maybe_field_with_caption! ( document, VDocumentMessage, document, listen_document );
80 maybe_field_with_caption_and_group!( photo, VPhotoMessage, photo, listen_photo );
81 maybe_field! ( sticker, VStickerMessage, sticker, listen_sticker );
82 maybe_field_with_caption_and_group!( video, VVideoMessage, video, listen_video );
83 maybe_field! ( voice, VVoiceMessage, voice, listen_voice );
84 maybe_field! ( video_note, VVideoNoteMessage, video_note, listen_video_note );
85 maybe_field! ( contact, VContactMessage, contact, listen_contact );
86 maybe_field! ( location, VLocationMessage, location, listen_location );
87 maybe_field! ( venue, VVenueMessage, venue, listen_venue );
88 maybe_field! ( new_chat_members, VNewChatMembersMessage, members, listen_new_chat_members );
89 maybe_field! ( left_chat_member, VLeftChatMemberMessage, member, listen_left_chat_member );
90 maybe_field! ( new_chat_title, VChatTitleMessage, title, listen_new_chat_title );
91 maybe_field! ( new_chat_photo, VChatPhotoMessage, photos, listen_new_chat_photo );
92 maybe_true_field! ( delete_chat_photo, listen_delete_chat_photo );
93 maybe_true_field! ( group_chat_created, listen_group_chat_created );
94 maybe_true_field! ( supergroup_chat_created, listen_supergroup_chat_created );
95 maybe_true_field! ( channel_chat_created, listen_channel_chat_create );
96 maybe_field! ( migrate_to_chat_id, VMigrateToChatIdMessage, migrate_to_chat_id, listen_migrate_to_chat );
97 maybe_field! ( migrate_from_chat_id, VMigrateFromChatIdMessage, migrate_from_chat_id, listen_migrate_from_chat );
98 maybe_field! ( pinned_message, VPinnedMessageMessage, pinned, listen_pinned );
99}
101
102
103pub fn to_message(raw: &RawMessage, is_edited: bool) -> Message {
104 Message {
105 id: raw.message_id,
106 from: raw.from.clone(),
107 date: raw.date,
108forward: gen_forward(&raw),
110 reply_to_message: raw.reply_to_message.clone().map(|raw| PossibilityMessage::new(*raw)),
111 edit_date: raw.edit_date,
112 chat: gen_chat(raw),
113 is_edited
114 }
115}
116
117fn gen_chat(raw: &RawMessage) -> VMessageChat {
118 match raw.chat {
119 Chat::Channel(ref channel) => VMessageChat::Channel(channel.clone()),
120 Chat::Private(ref user) => VMessageChat::Message(MessageChat::Private(user.clone())),
121 Chat::Group(ref group) => VMessageChat::Message(MessageChat::Group(group.clone())),
122 Chat::Supergroup(ref supergroup) => VMessageChat::Message(MessageChat::Supergroup(supergroup.clone())),
123 Chat::Unknown(ref rawchat) => VMessageChat::Message(MessageChat::Unknown(rawchat.clone()))
124 }
125}
126
127fn gen_forward(raw: &RawMessage) -> Option<Forward> {
128 match (raw.forward_date,
129 &raw.forward_from,
130 &raw.forward_from_chat,
131 raw.forward_from_message_id,
132 &raw.forward_sender_name) {
133 (None, &None, &None, None, &None) => None,
134 (Some(date), &Some(ref from), &None, None, &None) => {
135 Some(Forward {
136 date,
137 from: ForwardFrom::User { user: from.clone() },
138 })
139 }
140 (Some(date), &None, &Some(Chat::Channel(ref channel)), Some(message_id), &None) => {
141 Some(Forward {
142 date,
143 from: ForwardFrom::Channel {
144 channel: channel.clone(),
145 message_id,
146 },
147 })
148 }
149 (Some(date), &None, &None, None, &Some(ref sender_name)) => {
150 Some(Forward {
151 date,
152 from: ForwardFrom::ChannelHiddenUser { sender_name: sender_name.clone() },
153 })
154 }
155 _ => None,
156 }
157}
158
159
160
161