1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use rtdlib::types as rtd_types;

use crate::api::Api;
use crate::listener::Lout;
use crate::errors::TGError;
use crate::tip;

pub struct Handler<'a> {
  api: &'a Api,
  lout: &'a Lout,
}

macro_rules! event_handler {
  ($event_name:ident, $td_type:ident) => {
    |api: &Api, lout: &Lout, json: &String| {
      if let Some(ev) = lout.$event_name() {
        match rtd_types::from_json::<rtd_types::$td_type>(json) {
          Ok(t) => {
            if let Err(_e) = ev((api, &t)) {
              if let Some(ev) = lout.exception() { ev((api, &TGError::new("EVENT_HANDLER_ERROR"))); }
            }
          }
          Err(e) => {
            error!("{}", tip::data_fail_with_json(json));
            eprintln!("{:?}", e);
            if let Some(ev) = lout.exception() { ev((api, &TGError::new("DESERIALIZE_JSON_FAIL"))); }
          }
        }
        return;
      }
      warn!("{}", tip::un_register_listener(stringify!($event_name)));
    }
  };
}

impl<'a> Handler<'a> {
  pub(crate) fn new(api: &'a Api, lout: &'a Lout) -> Self {
    Self {
      api,
      lout,
    }
  }

  pub fn handle(&self, json: &'a String) {
    let td_type = match rtd_types::detect_td_type(json) {
      Some(t) => t,
      None => {
        warn!("{}", tip::data_fail_with_json(json));
        return;
      }
    };
    if !self.lout.is_support(&td_type) {
      warn!("{}", tip::not_have_listener(td_type));
      return;
    }

    if let Some(ev) = self.lout.receive() {
      if let Err(e) = ev((self.api, json)) {
        if let Some(ev) = self.lout.exception() { ev((self.api, &e)); }
      }
    }

    match &td_type[..] {
      
      "updateAuthorizationState" => event_handler!(authorization_state, UpdateAuthorizationState)(self.api, self.lout, json),
      
      "updateNewMessage" => event_handler!(new_message, UpdateNewMessage)(self.api, self.lout, json),
      
      "updateMessageSendAcknowledged" => event_handler!(message_send_acknowledged, UpdateMessageSendAcknowledged)(self.api, self.lout, json),
      
      "updateMessageSendSucceeded" => event_handler!(message_send_succeeded, UpdateMessageSendSucceeded)(self.api, self.lout, json),
      
      "updateMessageSendFailed" => event_handler!(message_send_failed, UpdateMessageSendFailed)(self.api, self.lout, json),
      
      "updateMessageContent" => event_handler!(message_content, UpdateMessageContent)(self.api, self.lout, json),
      
      "updateMessageEdited" => event_handler!(message_edited, UpdateMessageEdited)(self.api, self.lout, json),
      
      "updateMessageViews" => event_handler!(message_views, UpdateMessageViews)(self.api, self.lout, json),
      
      "updateMessageContentOpened" => event_handler!(message_content_opened, UpdateMessageContentOpened)(self.api, self.lout, json),
      
      "updateMessageMentionRead" => event_handler!(message_mention_read, UpdateMessageMentionRead)(self.api, self.lout, json),
      
      "updateNewChat" => event_handler!(new_chat, UpdateNewChat)(self.api, self.lout, json),
      
      "updateChatTitle" => event_handler!(chat_title, UpdateChatTitle)(self.api, self.lout, json),
      
      "updateChatPhoto" => event_handler!(chat_photo, UpdateChatPhoto)(self.api, self.lout, json),
      
      "updateChatPermissions" => event_handler!(chat_permissions, UpdateChatPermissions)(self.api, self.lout, json),
      
      "updateChatLastMessage" => event_handler!(chat_last_message, UpdateChatLastMessage)(self.api, self.lout, json),
      
      "updateChatOrder" => event_handler!(chat_order, UpdateChatOrder)(self.api, self.lout, json),
      
      "updateChatIsPinned" => event_handler!(chat_is_pinned, UpdateChatIsPinned)(self.api, self.lout, json),
      
      "updateChatIsMarkedAsUnread" => event_handler!(chat_is_marked_as_unread, UpdateChatIsMarkedAsUnread)(self.api, self.lout, json),
      
      "updateChatIsSponsored" => event_handler!(chat_is_sponsored, UpdateChatIsSponsored)(self.api, self.lout, json),
      
      "updateChatDefaultDisableNotification" => event_handler!(chat_default_disable_notification, UpdateChatDefaultDisableNotification)(self.api, self.lout, json),
      
      "updateChatReadInbox" => event_handler!(chat_read_inbox, UpdateChatReadInbox)(self.api, self.lout, json),
      
      "updateChatReadOutbox" => event_handler!(chat_read_outbox, UpdateChatReadOutbox)(self.api, self.lout, json),
      
      "updateChatUnreadMentionCount" => event_handler!(chat_unread_mention_count, UpdateChatUnreadMentionCount)(self.api, self.lout, json),
      
      "updateChatNotificationSettings" => event_handler!(chat_notification_settings, UpdateChatNotificationSettings)(self.api, self.lout, json),
      
      "updateScopeNotificationSettings" => event_handler!(scope_notification_settings, UpdateScopeNotificationSettings)(self.api, self.lout, json),
      
      "updateChatPinnedMessage" => event_handler!(chat_pinned_message, UpdateChatPinnedMessage)(self.api, self.lout, json),
      
      "updateChatReplyMarkup" => event_handler!(chat_reply_markup, UpdateChatReplyMarkup)(self.api, self.lout, json),
      
      "updateChatDraftMessage" => event_handler!(chat_draft_message, UpdateChatDraftMessage)(self.api, self.lout, json),
      
      "updateChatOnlineMemberCount" => event_handler!(chat_online_member_count, UpdateChatOnlineMemberCount)(self.api, self.lout, json),
      
      "updateNotification" => event_handler!(notification, UpdateNotification)(self.api, self.lout, json),
      
      "updateNotificationGroup" => event_handler!(notification_group, UpdateNotificationGroup)(self.api, self.lout, json),
      
      "updateActiveNotifications" => event_handler!(active_notifications, UpdateActiveNotifications)(self.api, self.lout, json),
      
      "updateHavePendingNotifications" => event_handler!(have_pending_notifications, UpdateHavePendingNotifications)(self.api, self.lout, json),
      
      "updateDeleteMessages" => event_handler!(delete_messages, UpdateDeleteMessages)(self.api, self.lout, json),
      
      "updateUserChatAction" => event_handler!(user_chat_action, UpdateUserChatAction)(self.api, self.lout, json),
      
      "updateUserStatus" => event_handler!(user_status, UpdateUserStatus)(self.api, self.lout, json),
      
      "updateUser" => event_handler!(user, UpdateUser)(self.api, self.lout, json),
      
      "updateBasicGroup" => event_handler!(basic_group, UpdateBasicGroup)(self.api, self.lout, json),
      
      "updateSupergroup" => event_handler!(supergroup, UpdateSupergroup)(self.api, self.lout, json),
      
      "updateSecretChat" => event_handler!(secret_chat, UpdateSecretChat)(self.api, self.lout, json),
      
      "updateUserFullInfo" => event_handler!(user_full_info, UpdateUserFullInfo)(self.api, self.lout, json),
      
      "updateBasicGroupFullInfo" => event_handler!(basic_group_full_info, UpdateBasicGroupFullInfo)(self.api, self.lout, json),
      
      "updateSupergroupFullInfo" => event_handler!(supergroup_full_info, UpdateSupergroupFullInfo)(self.api, self.lout, json),
      
      "updateServiceNotification" => event_handler!(service_notification, UpdateServiceNotification)(self.api, self.lout, json),
      
      "updateFile" => event_handler!(file, UpdateFile)(self.api, self.lout, json),
      
      "updateFileGenerationStart" => event_handler!(file_generation_start, UpdateFileGenerationStart)(self.api, self.lout, json),
      
      "updateFileGenerationStop" => event_handler!(file_generation_stop, UpdateFileGenerationStop)(self.api, self.lout, json),
      
      "updateCall" => event_handler!(call, UpdateCall)(self.api, self.lout, json),
      
      "updateUserPrivacySettingRules" => event_handler!(user_privacy_setting_rules, UpdateUserPrivacySettingRules)(self.api, self.lout, json),
      
      "updateUnreadMessageCount" => event_handler!(unread_message_count, UpdateUnreadMessageCount)(self.api, self.lout, json),
      
      "updateUnreadChatCount" => event_handler!(unread_chat_count, UpdateUnreadChatCount)(self.api, self.lout, json),
      
      "updateOption" => event_handler!(option, UpdateOption)(self.api, self.lout, json),
      
      "updateInstalledStickerSets" => event_handler!(installed_sticker_sets, UpdateInstalledStickerSets)(self.api, self.lout, json),
      
      "updateTrendingStickerSets" => event_handler!(trending_sticker_sets, UpdateTrendingStickerSets)(self.api, self.lout, json),
      
      "updateRecentStickers" => event_handler!(recent_stickers, UpdateRecentStickers)(self.api, self.lout, json),
      
      "updateFavoriteStickers" => event_handler!(favorite_stickers, UpdateFavoriteStickers)(self.api, self.lout, json),
      
      "updateSavedAnimations" => event_handler!(saved_animations, UpdateSavedAnimations)(self.api, self.lout, json),
      
      "updateSelectedBackground" => event_handler!(selected_background, UpdateSelectedBackground)(self.api, self.lout, json),
      
      "updateLanguagePackStrings" => event_handler!(language_pack_strings, UpdateLanguagePackStrings)(self.api, self.lout, json),
      
      "updateConnectionState" => event_handler!(connection_state, UpdateConnectionState)(self.api, self.lout, json),
      
      "updateTermsOfService" => event_handler!(terms_of_service, UpdateTermsOfService)(self.api, self.lout, json),
      
      "updateNewInlineQuery" => event_handler!(new_inline_query, UpdateNewInlineQuery)(self.api, self.lout, json),
      
      "updateNewChosenInlineResult" => event_handler!(new_chosen_inline_result, UpdateNewChosenInlineResult)(self.api, self.lout, json),
      
      "updateNewCallbackQuery" => event_handler!(new_callback_query, UpdateNewCallbackQuery)(self.api, self.lout, json),
      
      "updateNewInlineCallbackQuery" => event_handler!(new_inline_callback_query, UpdateNewInlineCallbackQuery)(self.api, self.lout, json),
      
      "updateNewShippingQuery" => event_handler!(new_shipping_query, UpdateNewShippingQuery)(self.api, self.lout, json),
      
      "updateNewPreCheckoutQuery" => event_handler!(new_pre_checkout_query, UpdateNewPreCheckoutQuery)(self.api, self.lout, json),
      
      "updateNewCustomEvent" => event_handler!(new_custom_event, UpdateNewCustomEvent)(self.api, self.lout, json),
      
      "updateNewCustomQuery" => event_handler!(new_custom_query, UpdateNewCustomQuery)(self.api, self.lout, json),
      
      "updatePoll" => event_handler!(poll, UpdatePoll)(self.api, self.lout, json),
      
      "testUseUpdate" => event_handler!(test_use_update, TestUseUpdate)(self.api, self.lout, json),
      
      
      "error" => event_handler!(error, Error)(self.api, self.lout, json),
      
      "ok" => event_handler!(ok, Ok)(self.api, self.lout, json),
      
      "proxy" => event_handler!(proxy, Proxy)(self.api, self.lout, json),
      
      _ => {
        warn!("{}", tip::data_fail_with_json(json))
      }
    }
  }

}