#include "typeconverter.h" #include "message.h" //converts between tdlib types and own types ChatType convertChatType(const td_api::ChatType& in) { switch(in.get_id()) { case td_api::chatTypeBasicGroup::ID: return ChatType::GROUP; case td_api::chatTypePrivate::ID: return ChatType::PRIVATE; case td_api::chatTypeSecret::ID: return ChatType::SECRET; case td_api::chatTypeSupergroup::ID: { if(((const td_api::chatTypeSupergroup&) in).is_channel_) return ChatType::CHANNEL; return ChatType::SUPERGROUP; } } return ChatType::UNKNOWN; //unknown } void convertUser(const td_api::user& in, User& out) { if(in.usernames_ && in.usernames_->active_usernames_.empty()) { out.username = in.usernames_->active_usernames_.at(0); } else { out.username = {}; } out.tgid = in.id_; out.firstname = in.first_name_; out.lastname = in.last_name_; out.phonenumber = in.phone_number_; out.isContact = in.is_contact_; out.isMutalContact = in.is_mutual_contact_; out.isVerified = in.is_verified_; out.isSupport = in.is_support_; out.restriction = in.restriction_reason_; out.isScam = in.is_scam_; out.haveAccess = in.have_access_; out.type = convertUserType(*in.type_); } UserType convertUserType(const td_api::UserType& in) { switch(in.get_id()) { case td_api::userTypeBot::ID: return UserType::BOT; case td_api::userTypeDeleted::ID: return UserType::DELETED; case td_api::userTypeRegular::ID: return UserType::REGULAR; } return UserType::UNKNOWN; //unknown } const std::string& convertUserType(UserType type) { switch(type) { case UserType::BOT: return UserTypeTypes[0]; case UserType::DELETED: return UserTypeTypes[1]; case UserType::REGULAR: return UserTypeTypes[2]; default: break; } return UserTypeTypes[3]; } UserType convertUserType(const std::string& in) { if(in == UserTypeTypes[0]) return UserType::BOT; if(in == UserTypeTypes[1]) return UserType::DELETED; if(in == UserTypeTypes[2]) return UserType::REGULAR; return UserType::UNKNOWN; } const std::string& convertMessageType(MessageType type) { uint32_t i = (uint32_t) type; if(i > 14) return MessageTypeTypes[0]; //unknown return MessageTypeTypes[i]; } MessageType convertMessageType(const std::string& in) { for(int32_t i = 0; i < 14; ++i) { if(MessageTypeTypes[i] == in) { return (MessageType) i; } } return MessageType::UNKNOWN; } static int64_t getSender(const td_api::MessageSender& s) { if(s.get_id() == td_api::messageSenderUser::ID) { return static_cast(s).user_id_; } if(s.get_id() == td_api::messageSenderChat::ID) { return static_cast(s).chat_id_; } return 0; } void convertMessage(const td_api::message& in, Message& out) { out.id = in.id_; out.chat = in.chat_id_; out.sender = getSender(*in.sender_id_); out.text = ""; out.type = convertMessageTypeandText(*in.content_, out.text); out.isDeleted = false; out.isOutgoing = in.is_outgoing_; out.isPinned = in.is_pinned_; out.isEditable = in.can_be_edited_; out.isForwardable = in.can_be_forwarded_; out.sendDate = in.date_; out.editDate = in.edit_date_; out.messageThreadId = in.message_thread_id_; out.viaBotid = in.via_bot_user_id_; out.mediaAlbumId = in.media_album_id_; ReplyMarkup* markup = convertReplyMarkup(in.reply_markup_.get()); if(markup) { out.replyMarkup = std::shared_ptr(markup); } } #define CHECKCAST(TYPE, MTYPE, ACCESS) case td_api::TYPE::ID: \ textout = static_cast(cont).ACCESS; \ return MessageType::MTYPE #define ECHECKCAST(TYPE, MTYPE) case td_api::TYPE::ID: \ return MessageType::MTYPE MessageType convertMessageTypeandText(const td_api::MessageContent& cont, std::string& textout) { switch(cont.get_id()) { CHECKCAST(messageText, TEXT, text_->text_); CHECKCAST(messageAnimation, ANIMATION, caption_->text_); CHECKCAST(messageAudio, AUDIO, caption_->text_); CHECKCAST(messageContact, CONTACT, contact_->first_name_); CHECKCAST(messageDocument, DOCUMENT, caption_->text_); ECHECKCAST(messageLocation, LOCATION); CHECKCAST(messagePhoto, PHOTO, caption_->text_); ECHECKCAST(messageExpiredPhoto, PHOTO); CHECKCAST(messagePoll, POLL, poll_->question_); ECHECKCAST(messageSticker, STICKER); ECHECKCAST(messageDice, STICKER); CHECKCAST(messageVideo, VIDEO, caption_->text_); ECHECKCAST(messageExpiredVideo, VIDEO); ECHECKCAST(messageVideoNote, VIDEO); CHECKCAST(messageVenue, VENUE, venue_->title_); CHECKCAST(messageVoiceNote, VOICE, caption_->text_); ECHECKCAST(messageUnsupported, UNSUPPORTED); default: return MessageType::SERVICE; } // unreachable return MessageType::UNKNOWN; } #undef CHECKCAST #undef ECHECKCAST ReplyMarkup* convertReplyMarkup(const td_api::ReplyMarkup* in) { if(!in) return nullptr; if(in->get_id() == td_api::replyMarkupInlineKeyboard::ID) { const td_api::replyMarkupInlineKeyboard* kyb = (const td_api::replyMarkupInlineKeyboard*) in; InlineReplyMarkup* out = new InlineReplyMarkup(); // convertbuttons for(uint32_t i = 0; i < kyb->rows_.size(); ++i) { const std::vector>& row = kyb->rows_.at(i); for(uint32_t j = 0; j < row.size(); ++j) { InlineButton* btn = convertInlineButton(row.at(j).get()); if(btn) out->buttons.push_back(btn); } } return out; } return nullptr; } InlineButton* convertInlineButton(const td_api::inlineKeyboardButton* in) { //construct button InlineButton* btn = nullptr; const td_api::InlineKeyboardButtonType* type = in->type_.get(); if(type->get_id() == td_api::inlineKeyboardButtonTypeCallback::ID) { btn = new InlineCallbackButton(); ((InlineCallbackButton*) btn)->data = ((const td_api::inlineKeyboardButtonTypeCallback*) type)->data_; } else if(type->get_id() == td_api::inlineKeyboardButtonTypeCallbackWithPassword::ID) { btn = new InlineCallbackPasswordButton(); ((InlineCallbackPasswordButton*) btn)->data = ((const td_api::inlineKeyboardButtonTypeCallbackWithPassword*) type)->data_; } else if(type->get_id() == td_api::inlineKeyboardButtonTypeLoginUrl::ID) { InlineLoginURLButton* lurlbtn = new InlineLoginURLButton(); const td_api::inlineKeyboardButtonTypeLoginUrl* casted = (const td_api::inlineKeyboardButtonTypeLoginUrl*) type; lurlbtn->url = casted->url_; lurlbtn->id = casted->id_; lurlbtn->forwardText = casted->forward_text_; btn = lurlbtn; } else if(type->get_id() == td_api::inlineKeyboardButtonTypeSwitchInline::ID) { InlineSwitchInlineButton* lsbtn = new InlineSwitchInlineButton(); const td_api::inlineKeyboardButtonTypeSwitchInline* casted = (const td_api::inlineKeyboardButtonTypeSwitchInline*) type; lsbtn->query = casted->query_; lsbtn->inCurrentChat = (casted->target_chat_->get_id() == td_api::targetChatCurrent::ID); btn = lsbtn; } else if(type->get_id() == td_api::inlineKeyboardButtonTypeUrl::ID) { btn = new InlineURLButton(); ((InlineURLButton*) btn)->url = ((const td_api::inlineKeyboardButtonTypeUrl*) type)->url_; } if(btn) { btn->text = in->text_; } return btn; } bool User::operator==(const User& u) const { return (tgid == u.tgid && firstname == u.firstname && lastname == u.lastname && username == u.username && phonenumber == u.phonenumber && isContact == u.isContact && isMutalContact == u.isMutalContact && isVerified == u.isVerified && isSupport == u.isSupport && restriction == u.restriction && isScam == u.isScam && haveAccess == u.haveAccess && type == u.type); } bool User::operator!=(const User& u) const { return !operator==(u); } bool Message::operator==(const Message& m) const { return (id == m.id && chat == m.chat && sender == m.sender && type == m.type && text == m.text && isDeleted == m.isDeleted && isOutgoing == m.isOutgoing && isPinned == m.isPinned && isForwardable == m.isForwardable && sendDate == m.sendDate && editDate == m.sendDate && viaBotid == m.viaBotid && mediaAlbumId == m.mediaAlbumId); } bool Message::operator!=(const Message& m) const { return !operator==(m); } const std::string& getMessageText(const td_api::object_ptr& msg) { if(msg->content_->get_id() == td_api::messageText::ID) { const std::string& text = ((const td_api::messageText&) *msg->content_).text_->text_; return text; } static const std::string EMPTY = ""; return EMPTY; } #define CHECKCAST(TYPE) case td_api::TYPE::ID: \ return &*static_cast(cont).caption_; td_api::formattedText* getMessageFormattedText(td_api::message& msg, bool& isText) { if(!msg.content_) return nullptr; td_api::MessageContent& cont = *msg.content_; isText = false; switch(cont.get_id()) { CHECKCAST(messageAnimation); CHECKCAST(messageAudio); CHECKCAST(messageDocument); CHECKCAST(messagePhoto); CHECKCAST(messageVideo); CHECKCAST(messageVoiceNote); case td_api::messageText::ID: isText = true; return &*static_cast(cont).text_; } //default return nullptr; } #undef CHECKCAST