From c3c82eac2d171b9fbeb755398ad0bd6fbb86bdbc Mon Sep 17 00:00:00 2001 From: John Preston Date: Sat, 9 Apr 2016 12:29:34 +0400 Subject: [PATCH] Redirect addToHistory for inline bot results to SendData member. --- .../inline_bots/inline_bot_result.cpp | 18 +---- .../inline_bots/inline_bot_result.h | 5 +- .../inline_bots/inline_bot_send_data.cpp | 73 ++++++++--------- .../inline_bots/inline_bot_send_data.h | 80 +++++++++---------- 4 files changed, 77 insertions(+), 99 deletions(-) diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp index 2fa4e65aa..ce5cf253f 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_result.cpp @@ -114,11 +114,9 @@ UniquePointer Result::create(uint64 queryId, const MTPBotInlineResult &m if (r.has_title()) result->_title = qs(r.vtitle); if (r.has_description()) result->_description = qs(r.vdescription); if (r.has_photo()) { - result->_mtpPhoto = r.vphoto; result->_photo = App::feedPhoto(r.vphoto); } if (r.has_document()) { - result->_mtpDocument = r.vdocument; result->_document = App::feedDocument(r.vdocument); } message = &r.vsend_message; @@ -136,6 +134,9 @@ UniquePointer Result::create(uint64 queryId, const MTPBotInlineResult &m if (result->_type == Type::Photo) { result->sendData.reset(new internal::SendPhoto(result->_photo, result->_content_url, qs(r.vcaption))); } else { + if (!result->_document) { + + } result->sendData.reset(new internal::SendFile(result->_document, result->_content_url, qs(r.vcaption))); } if (r.has_reply_markup()) { @@ -460,18 +461,7 @@ void Result::addToHistory(History *history, MTPDmessage::Flags flags, MsgId msgI flags |= MTPDmessage::Flag::f_reply_markup; markup = *_mtpKeyboard; } - if (DocumentData *document = sendData->getSentDocument()) { - history->addNewDocument(msgId, flags, viaBotId, replyToId, date(mtpDate), fromId, document, sendData->getSentCaption(), markup); - } else if (PhotoData *photo = sendData->getSentPhoto()) { - history->addNewPhoto(msgId, flags, viaBotId, replyToId, date(mtpDate), fromId, photo, sendData->getSentCaption(), markup); - } else { - internal::SendData::SentMTPMessageFields fields = sendData->getSentMessageFields(this); - if (!fields.entities.c_vector().v.isEmpty()) { - flags |= MTPDmessage::Flag::f_entities; - } - history->addNewMessage(MTP_message(MTP_flags(flags), MTP_int(msgId), MTP_int(fromId), peerToMTP(history->peer->id), MTPnullFwdHeader, MTP_int(viaBotId), MTP_int(replyToId), mtpDate, fields.text, fields.media, markup, fields.entities, MTP_int(1), MTPint()), NewMessageUnread); - } - + sendData->addToHistory(this, history, flags, msgId, fromId, mtpDate, viaBotId, replyToId, markup); } bool Result::getLocationCoords(LocationCoords *outLocation) const { diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_result.h b/Telegram/SourceFiles/inline_bots/inline_bot_result.h index 9cd4a004f..22f60e91d 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_result.h +++ b/Telegram/SourceFiles/inline_bots/inline_bot_result.h @@ -86,6 +86,8 @@ public: ~Result(); private: + void createDocument(); + enum class Type { Unknown, Photo, @@ -115,10 +117,7 @@ private: int _height = 0; int _duration = 0; - mutable MTPDocument _mtpDocument = MTP_documentEmpty(MTP_long(0)); DocumentData *_document = nullptr; - - mutable MTPPhoto _mtpPhoto = MTP_photoEmpty(MTP_long(0)); PhotoData *_photo = nullptr; UniquePointer _mtpKeyboard; diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_send_data.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_send_data.cpp index 9c06b9bf6..4636fa8d9 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_send_data.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_send_data.cpp @@ -73,42 +73,36 @@ QVector SendData::prepareResultAttributes(const Result *ow return result; } -void SendData::setResultDocument(const Result *owner, const MTPDocument &document) const { - owner->_mtpDocument = document; +void SendDataCommon::addToHistory(const Result *owner, History *history, +MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate, +UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const { + SentMTPMessageFields fields = getSentMessageFields(); + if (!fields.entities.c_vector().v.isEmpty()) { + flags |= MTPDmessage::Flag::f_entities; + } + history->addNewMessage(MTP_message(MTP_flags(flags), MTP_int(msgId), MTP_int(fromId), peerToMTP(history->peer->id), MTPnullFwdHeader, MTP_int(viaBotId), MTP_int(replyToId), mtpDate, fields.text, fields.media, markup, fields.entities, MTP_int(1), MTPint()), NewMessageUnread); } -void SendData::setResultPhoto(const Result *owner, const MTPPhoto &photo) const { - owner->_mtpPhoto = photo; -} - -MTPDocument SendData::getResultDocument(const Result *owner) const { - return owner->_mtpDocument; -} - -MTPPhoto SendData::getResultPhoto(const Result *owner) const { - return owner->_mtpPhoto; -} - -SendData::SentMTPMessageFields SendText::getSentMessageFields(const Result*) const { +SendDataCommon::SentMTPMessageFields SendText::getSentMessageFields() const { SentMTPMessageFields result; result.text = MTP_string(_message); result.entities = linksToMTP(_entities); return result; } -SendData::SentMTPMessageFields SendGeo::getSentMessageFields(const Result*) const { +SendDataCommon::SentMTPMessageFields SendGeo::getSentMessageFields() const { SentMTPMessageFields result; result.media = MTP_messageMediaGeo(MTP_geoPoint(MTP_double(_location.lon), MTP_double(_location.lat))); return result; } -SendData::SentMTPMessageFields SendVenue::getSentMessageFields(const Result*) const { +SendDataCommon::SentMTPMessageFields SendVenue::getSentMessageFields() const { SentMTPMessageFields result; result.media = MTP_messageMediaVenue(MTP_geoPoint(MTP_double(_location.lon), MTP_double(_location.lat)), MTP_string(_title), MTP_string(_address), MTP_string(_provider), MTP_string(_venueId)); return result; } -SendData::SentMTPMessageFields SendContact::getSentMessageFields(const Result*) const { +SendDataCommon::SentMTPMessageFields SendContact::getSentMessageFields() const { SentMTPMessageFields result; result.media = MTP_messageMediaContact(MTP_string(_phoneNumber), MTP_string(_firstName), MTP_string(_lastName), MTP_int(0)); return result; @@ -122,8 +116,13 @@ QString SendContact::getLayoutDescription(const Result *owner) const { return result; } -SendData::SentMTPMessageFields SendPhoto::getSentMessageFields(const Result *owner) const { - SentMTPMessageFields result; +void SendPhoto::addToHistory(const Result *owner, History *history, +MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate, +UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const { + if (_photo) { + history->addNewPhoto(msgId, flags, viaBotId, replyToId, date(mtpDate), fromId, _photo, _caption, markup); + return; + } ImagePtr resultThumb = getResultThumb(owner); QImage fileThumb(resultThumb->pix().toImage()); @@ -143,13 +142,17 @@ SendData::SentMTPMessageFields SendPhoto::getSentMessageFields(const Result *own PhotoData *ph = App::photoSet(photoId, 0, 0, unixtime(), thumbPtr, ImagePtr(medium.width(), medium.height()), ImagePtr(getResultWidth(owner), getResultHeight(owner))); MTPPhoto photo = MTP_photo(MTP_long(photoId), MTP_long(0), MTP_int(ph->date), MTP_vector(photoSizes)); - result.media = MTP_messageMediaPhoto(photo, MTP_string(_caption)); - - return result; + MTPMessageMedia media = MTP_messageMediaPhoto(photo, MTP_string(_caption)); + history->addNewMessage(MTP_message(MTP_flags(flags), MTP_int(msgId), MTP_int(fromId), peerToMTP(history->peer->id), MTPnullFwdHeader, MTP_int(viaBotId), MTP_int(replyToId), mtpDate, MTP_string(""), media, markup, MTPnullEntities, MTP_int(1), MTPint()), NewMessageUnread); } -void SendFile::prepareDocument(const Result *owner) const { - if (getResultDocument(owner).type() != mtpc_documentEmpty) return; +void SendFile::addToHistory(const Result *owner, History *history, +MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate, +UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const { + if (_document) { + history->addNewDocument(msgId, flags, viaBotId, replyToId, date(mtpDate), fromId, _document, _caption, markup); + return; + } uint64 docId = rand_value(); @@ -176,24 +179,16 @@ void SendFile::prepareDocument(const Result *owner) const { QVector attributes = prepareResultAttributes(owner); MTPDocument document = MTP_document(MTP_long(docId), MTP_long(0), MTP_int(unixtime()), MTP_string(getResultMime(owner)), MTP_int(owner->data().size()), thumbSize, MTP_int(MTP::maindc()), MTP_vector(attributes)); - if (tw > 0 && th > 0) { - App::feedDocument(document, thumb); - } + if (!owner->data().isEmpty()) { Local::writeStickerImage(mediaKey(DocumentFileLocation, MTP::maindc(), docId), owner->data()); } - setResultDocument(owner, document); -} -SendData::SentMTPMessageFields SendFile::getSentMessageFields(const Result *owner) const { - SentMTPMessageFields result; - - prepareDocument(owner); - - MTPDocument document = getResultDocument(owner); - result.media = MTP_messageMediaDocument(document, MTP_string(_caption)); - - return result; + if (tw > 0 && th > 0) { + App::feedDocument(document, thumb); + } + MTPMessageMedia media = MTP_messageMediaDocument(document, MTP_string(_caption)); + history->addNewMessage(MTP_message(MTP_flags(flags), MTP_int(msgId), MTP_int(fromId), peerToMTP(history->peer->id), MTPnullFwdHeader, MTP_int(viaBotId), MTP_int(replyToId), mtpDate, MTP_string(""), media, markup, MTPnullEntities, MTP_int(1), MTPint()), NewMessageUnread); } } // namespace internal diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_send_data.h b/Telegram/SourceFiles/inline_bots/inline_bot_send_data.h index 09de13dae..47c655905 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_send_data.h +++ b/Telegram/SourceFiles/inline_bots/inline_bot_send_data.h @@ -42,21 +42,9 @@ public: virtual bool isValid() const = 0; - virtual DocumentData *getSentDocument() const { - return nullptr; - } - virtual PhotoData *getSentPhoto() const { - return nullptr; - } - virtual QString getSentCaption() const { - return QString(); - } - struct SentMTPMessageFields { - MTPString text = MTP_string(""); - MTPVector entities = MTPnullEntities; - MTPMessageMedia media = MTP_messageMediaEmpty(); - }; - virtual SentMTPMessageFields getSentMessageFields(const Result *owner) const = 0; + virtual void addToHistory(const Result *owner, History *history, + MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate, + UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const = 0; virtual bool hasLocationCoords() const { return false; @@ -75,16 +63,32 @@ protected: QString getResultMime(const Result *owner) const; QVector prepareResultAttributes(const Result *owner) const; - void setResultDocument(const Result *owner, const MTPDocument &document) const; - void setResultPhoto(const Result *owner, const MTPPhoto &photo) const; + void setResultDocument(const Result *owner, DocumentData *document) const; + void setResultPhoto(const Result *owner, PhotoData *photo) const; - MTPDocument getResultDocument(const Result *owner) const; - MTPPhoto getResultPhoto(const Result *owner) const; +}; + +// This class implements addHistory() for most of the types hiding +// the differences in getSentMessageFields() method. +// Only SendFile and SendPhoto work by their own. +class SendDataCommon : public SendData { +public: + + struct SentMTPMessageFields { + MTPString text = MTP_string(""); + MTPVector entities = MTPnullEntities; + MTPMessageMedia media = MTP_messageMediaEmpty(); + }; + virtual SentMTPMessageFields getSentMessageFields() const = 0; + + void addToHistory(const Result *owner, History *history, + MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate, + UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const override; }; // Plain text message. -class SendText : public SendData { +class SendText : public SendDataCommon { public: SendText(const QString &message, const EntitiesInText &entities, bool/* noWebPage*/) : _message(message) @@ -95,7 +99,7 @@ public: return !_message.isEmpty(); } - SentMTPMessageFields getSentMessageFields(const Result *owner) const override; + SentMTPMessageFields getSentMessageFields() const override; private: QString _message; @@ -104,7 +108,7 @@ private: }; // Message with geo location point media. -class SendGeo : public SendData { +class SendGeo : public SendDataCommon { public: SendGeo(const MTPDgeoPoint &point) : _location(point) { } @@ -113,7 +117,7 @@ public: return true; } - SentMTPMessageFields getSentMessageFields(const Result *owner) const override; + SentMTPMessageFields getSentMessageFields() const override; bool hasLocationCoords() const override { return true; @@ -130,7 +134,7 @@ private: }; // Message with venue media. -class SendVenue : public SendData { +class SendVenue : public SendDataCommon { public: SendVenue(const MTPDgeoPoint &point, const QString &venueId, const QString &provider, const QString &title, const QString &address) @@ -145,7 +149,7 @@ public: return true; } - SentMTPMessageFields getSentMessageFields(const Result *owner) const override; + SentMTPMessageFields getSentMessageFields() const override; bool hasLocationCoords() const override { return true; @@ -163,7 +167,7 @@ private: }; // Message with shared contact media. -class SendContact : public SendData { +class SendContact : public SendDataCommon { public: SendContact(const QString &firstName, const QString &lastName, const QString &phoneNumber) : _firstName(firstName) @@ -175,7 +179,7 @@ public: return (!_firstName.isEmpty() || !_lastName.isEmpty()) && !_phoneNumber.isEmpty(); } - SentMTPMessageFields getSentMessageFields(const Result *owner) const override; + SentMTPMessageFields getSentMessageFields() const override; QString getLayoutDescription(const Result *owner) const override; @@ -197,13 +201,9 @@ public: return _photo || !_url.isEmpty(); } - PhotoData *getSentPhoto() const override { - return _photo; - } - QString getSentCaption() const override { - return _caption; - } - SentMTPMessageFields getSentMessageFields(const Result *owner) const override; + void addToHistory(const Result *owner, History *history, + MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate, + UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const override; private: PhotoData *_photo; @@ -224,17 +224,11 @@ public: return _document || !_url.isEmpty(); } - DocumentData *getSentDocument() const override { - return _document; - } - QString getSentCaption() const override { - return _caption; - } - SentMTPMessageFields getSentMessageFields(const Result *owner) const override; + void addToHistory(const Result *owner, History *history, + MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate, + UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const override; private: - void prepareDocument(const Result *owner) const; - DocumentData *_document; QString _url, _caption;