Redirect addToHistory for inline bot results to SendData member.

This commit is contained in:
John Preston 2016-04-09 12:29:34 +04:00
parent 0894931fa1
commit c3c82eac2d
4 changed files with 77 additions and 99 deletions

View File

@ -114,11 +114,9 @@ UniquePointer<Result> Result::create(uint64 queryId, const MTPBotInlineResult &m
if (r.has_title()) result->_title = qs(r.vtitle); if (r.has_title()) result->_title = qs(r.vtitle);
if (r.has_description()) result->_description = qs(r.vdescription); if (r.has_description()) result->_description = qs(r.vdescription);
if (r.has_photo()) { if (r.has_photo()) {
result->_mtpPhoto = r.vphoto;
result->_photo = App::feedPhoto(r.vphoto); result->_photo = App::feedPhoto(r.vphoto);
} }
if (r.has_document()) { if (r.has_document()) {
result->_mtpDocument = r.vdocument;
result->_document = App::feedDocument(r.vdocument); result->_document = App::feedDocument(r.vdocument);
} }
message = &r.vsend_message; message = &r.vsend_message;
@ -136,6 +134,9 @@ UniquePointer<Result> Result::create(uint64 queryId, const MTPBotInlineResult &m
if (result->_type == Type::Photo) { if (result->_type == Type::Photo) {
result->sendData.reset(new internal::SendPhoto(result->_photo, result->_content_url, qs(r.vcaption))); result->sendData.reset(new internal::SendPhoto(result->_photo, result->_content_url, qs(r.vcaption)));
} else { } else {
if (!result->_document) {
}
result->sendData.reset(new internal::SendFile(result->_document, result->_content_url, qs(r.vcaption))); result->sendData.reset(new internal::SendFile(result->_document, result->_content_url, qs(r.vcaption)));
} }
if (r.has_reply_markup()) { 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; flags |= MTPDmessage::Flag::f_reply_markup;
markup = *_mtpKeyboard; markup = *_mtpKeyboard;
} }
if (DocumentData *document = sendData->getSentDocument()) { sendData->addToHistory(this, history, flags, msgId, fromId, mtpDate, viaBotId, replyToId, markup);
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);
}
} }
bool Result::getLocationCoords(LocationCoords *outLocation) const { bool Result::getLocationCoords(LocationCoords *outLocation) const {

View File

@ -86,6 +86,8 @@ public:
~Result(); ~Result();
private: private:
void createDocument();
enum class Type { enum class Type {
Unknown, Unknown,
Photo, Photo,
@ -115,10 +117,7 @@ private:
int _height = 0; int _height = 0;
int _duration = 0; int _duration = 0;
mutable MTPDocument _mtpDocument = MTP_documentEmpty(MTP_long(0));
DocumentData *_document = nullptr; DocumentData *_document = nullptr;
mutable MTPPhoto _mtpPhoto = MTP_photoEmpty(MTP_long(0));
PhotoData *_photo = nullptr; PhotoData *_photo = nullptr;
UniquePointer<MTPReplyMarkup> _mtpKeyboard; UniquePointer<MTPReplyMarkup> _mtpKeyboard;

View File

@ -73,42 +73,36 @@ QVector<MTPDocumentAttribute> SendData::prepareResultAttributes(const Result *ow
return result; return result;
} }
void SendData::setResultDocument(const Result *owner, const MTPDocument &document) const { void SendDataCommon::addToHistory(const Result *owner, History *history,
owner->_mtpDocument = document; 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 { SendDataCommon::SentMTPMessageFields SendText::getSentMessageFields() 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 {
SentMTPMessageFields result; SentMTPMessageFields result;
result.text = MTP_string(_message); result.text = MTP_string(_message);
result.entities = linksToMTP(_entities); result.entities = linksToMTP(_entities);
return result; return result;
} }
SendData::SentMTPMessageFields SendGeo::getSentMessageFields(const Result*) const { SendDataCommon::SentMTPMessageFields SendGeo::getSentMessageFields() const {
SentMTPMessageFields result; SentMTPMessageFields result;
result.media = MTP_messageMediaGeo(MTP_geoPoint(MTP_double(_location.lon), MTP_double(_location.lat))); result.media = MTP_messageMediaGeo(MTP_geoPoint(MTP_double(_location.lon), MTP_double(_location.lat)));
return result; return result;
} }
SendData::SentMTPMessageFields SendVenue::getSentMessageFields(const Result*) const { SendDataCommon::SentMTPMessageFields SendVenue::getSentMessageFields() const {
SentMTPMessageFields result; 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)); 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; return result;
} }
SendData::SentMTPMessageFields SendContact::getSentMessageFields(const Result*) const { SendDataCommon::SentMTPMessageFields SendContact::getSentMessageFields() const {
SentMTPMessageFields result; SentMTPMessageFields result;
result.media = MTP_messageMediaContact(MTP_string(_phoneNumber), MTP_string(_firstName), MTP_string(_lastName), MTP_int(0)); result.media = MTP_messageMediaContact(MTP_string(_phoneNumber), MTP_string(_firstName), MTP_string(_lastName), MTP_int(0));
return result; return result;
@ -122,8 +116,13 @@ QString SendContact::getLayoutDescription(const Result *owner) const {
return result; return result;
} }
SendData::SentMTPMessageFields SendPhoto::getSentMessageFields(const Result *owner) const { void SendPhoto::addToHistory(const Result *owner, History *history,
SentMTPMessageFields result; 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); ImagePtr resultThumb = getResultThumb(owner);
QImage fileThumb(resultThumb->pix().toImage()); 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))); 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<MTPPhotoSize>(photoSizes)); MTPPhoto photo = MTP_photo(MTP_long(photoId), MTP_long(0), MTP_int(ph->date), MTP_vector<MTPPhotoSize>(photoSizes));
result.media = MTP_messageMediaPhoto(photo, MTP_string(_caption)); 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);
return result;
} }
void SendFile::prepareDocument(const Result *owner) const { void SendFile::addToHistory(const Result *owner, History *history,
if (getResultDocument(owner).type() != mtpc_documentEmpty) return; 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<uint64>(); uint64 docId = rand_value<uint64>();
@ -176,24 +179,16 @@ void SendFile::prepareDocument(const Result *owner) const {
QVector<MTPDocumentAttribute> attributes = prepareResultAttributes(owner); QVector<MTPDocumentAttribute> 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<MTPDocumentAttribute>(attributes)); 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<MTPDocumentAttribute>(attributes));
if (tw > 0 && th > 0) {
App::feedDocument(document, thumb);
}
if (!owner->data().isEmpty()) { if (!owner->data().isEmpty()) {
Local::writeStickerImage(mediaKey(DocumentFileLocation, MTP::maindc(), docId), owner->data()); Local::writeStickerImage(mediaKey(DocumentFileLocation, MTP::maindc(), docId), owner->data());
} }
setResultDocument(owner, document);
}
SendData::SentMTPMessageFields SendFile::getSentMessageFields(const Result *owner) const { if (tw > 0 && th > 0) {
SentMTPMessageFields result; App::feedDocument(document, thumb);
}
prepareDocument(owner); 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);
MTPDocument document = getResultDocument(owner);
result.media = MTP_messageMediaDocument(document, MTP_string(_caption));
return result;
} }
} // namespace internal } // namespace internal

View File

@ -42,21 +42,9 @@ public:
virtual bool isValid() const = 0; virtual bool isValid() const = 0;
virtual DocumentData *getSentDocument() const { virtual void addToHistory(const Result *owner, History *history,
return nullptr; MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate,
} UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const = 0;
virtual PhotoData *getSentPhoto() const {
return nullptr;
}
virtual QString getSentCaption() const {
return QString();
}
struct SentMTPMessageFields {
MTPString text = MTP_string("");
MTPVector<MTPMessageEntity> entities = MTPnullEntities;
MTPMessageMedia media = MTP_messageMediaEmpty();
};
virtual SentMTPMessageFields getSentMessageFields(const Result *owner) const = 0;
virtual bool hasLocationCoords() const { virtual bool hasLocationCoords() const {
return false; return false;
@ -75,16 +63,32 @@ protected:
QString getResultMime(const Result *owner) const; QString getResultMime(const Result *owner) const;
QVector<MTPDocumentAttribute> prepareResultAttributes(const Result *owner) const; QVector<MTPDocumentAttribute> prepareResultAttributes(const Result *owner) const;
void setResultDocument(const Result *owner, const MTPDocument &document) const; void setResultDocument(const Result *owner, DocumentData *document) const;
void setResultPhoto(const Result *owner, const MTPPhoto &photo) 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<MTPMessageEntity> 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. // Plain text message.
class SendText : public SendData { class SendText : public SendDataCommon {
public: public:
SendText(const QString &message, const EntitiesInText &entities, bool/* noWebPage*/) SendText(const QString &message, const EntitiesInText &entities, bool/* noWebPage*/)
: _message(message) : _message(message)
@ -95,7 +99,7 @@ public:
return !_message.isEmpty(); return !_message.isEmpty();
} }
SentMTPMessageFields getSentMessageFields(const Result *owner) const override; SentMTPMessageFields getSentMessageFields() const override;
private: private:
QString _message; QString _message;
@ -104,7 +108,7 @@ private:
}; };
// Message with geo location point media. // Message with geo location point media.
class SendGeo : public SendData { class SendGeo : public SendDataCommon {
public: public:
SendGeo(const MTPDgeoPoint &point) : _location(point) { SendGeo(const MTPDgeoPoint &point) : _location(point) {
} }
@ -113,7 +117,7 @@ public:
return true; return true;
} }
SentMTPMessageFields getSentMessageFields(const Result *owner) const override; SentMTPMessageFields getSentMessageFields() const override;
bool hasLocationCoords() const override { bool hasLocationCoords() const override {
return true; return true;
@ -130,7 +134,7 @@ private:
}; };
// Message with venue media. // Message with venue media.
class SendVenue : public SendData { class SendVenue : public SendDataCommon {
public: public:
SendVenue(const MTPDgeoPoint &point, const QString &venueId, SendVenue(const MTPDgeoPoint &point, const QString &venueId,
const QString &provider, const QString &title, const QString &address) const QString &provider, const QString &title, const QString &address)
@ -145,7 +149,7 @@ public:
return true; return true;
} }
SentMTPMessageFields getSentMessageFields(const Result *owner) const override; SentMTPMessageFields getSentMessageFields() const override;
bool hasLocationCoords() const override { bool hasLocationCoords() const override {
return true; return true;
@ -163,7 +167,7 @@ private:
}; };
// Message with shared contact media. // Message with shared contact media.
class SendContact : public SendData { class SendContact : public SendDataCommon {
public: public:
SendContact(const QString &firstName, const QString &lastName, const QString &phoneNumber) SendContact(const QString &firstName, const QString &lastName, const QString &phoneNumber)
: _firstName(firstName) : _firstName(firstName)
@ -175,7 +179,7 @@ public:
return (!_firstName.isEmpty() || !_lastName.isEmpty()) && !_phoneNumber.isEmpty(); 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; QString getLayoutDescription(const Result *owner) const override;
@ -197,13 +201,9 @@ public:
return _photo || !_url.isEmpty(); return _photo || !_url.isEmpty();
} }
PhotoData *getSentPhoto() const override { void addToHistory(const Result *owner, History *history,
return _photo; MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate,
} UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const override;
QString getSentCaption() const override {
return _caption;
}
SentMTPMessageFields getSentMessageFields(const Result *owner) const override;
private: private:
PhotoData *_photo; PhotoData *_photo;
@ -224,17 +224,11 @@ public:
return _document || !_url.isEmpty(); return _document || !_url.isEmpty();
} }
DocumentData *getSentDocument() const override { void addToHistory(const Result *owner, History *history,
return _document; MTPDmessage::Flags flags, MsgId msgId, UserId fromId, MTPint mtpDate,
} UserId viaBotId, MsgId replyToId, const MTPReplyMarkup &markup) const override;
QString getSentCaption() const override {
return _caption;
}
SentMTPMessageFields getSentMessageFields(const Result *owner) const override;
private: private:
void prepareDocument(const Result *owner) const;
DocumentData *_document; DocumentData *_document;
QString _url, _caption; QString _url, _caption;