tdesktop/Telegram/SourceFiles/inline_bots/inline_bot_send_data.cpp

246 lines
5.6 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "inline_bots/inline_bot_send_data.h"
2019-09-16 11:14:06 +00:00
#include "api/api_text_entities.h"
#include "data/data_document.h"
#include "inline_bots/inline_bot_result.h"
2017-03-04 10:23:56 +00:00
#include "storage/localstorage.h"
#include "lang/lang_keys.h"
#include "history/history.h"
#include "data/data_channel.h"
#include "app.h"
namespace InlineBots {
namespace internal {
QString SendData::getLayoutTitle(const Result *owner) const {
return owner->_title;
}
QString SendData::getLayoutDescription(const Result *owner) const {
return owner->_description;
}
void SendDataCommon::addToHistory(
const Result *owner,
not_null<History*> history,
MTPDmessage::Flags flags,
MTPDmessage_ClientFlags clientFlags,
MsgId msgId,
UserId fromId,
MTPint mtpDate,
UserId viaBotId,
MsgId replyToId,
const QString &postAuthor,
const MTPReplyMarkup &markup) const {
auto fields = getSentMessageFields();
if (!fields.entities.v.isEmpty()) {
flags |= MTPDmessage::Flag::f_entities;
}
2017-12-03 14:38:05 +00:00
history->addNewMessage(
MTP_message(
MTP_flags(flags),
MTP_int(msgId),
MTP_int(fromId),
peerToMTP(history->peer->id),
MTPMessageFwdHeader(),
2017-12-03 14:38:05 +00:00
MTP_int(viaBotId),
MTP_int(replyToId),
mtpDate,
fields.text,
fields.media,
markup,
fields.entities,
MTP_int(1),
MTPint(),
MTP_string(postAuthor),
2019-08-08 10:15:59 +00:00
MTPlong(),
//MTPMessageReactions(),
MTPVector<MTPRestrictionReason>()),
clientFlags,
2019-04-25 12:45:15 +00:00
NewMessageType::Unread);
}
QString SendDataCommon::getErrorOnSend(
const Result *owner,
not_null<History*> history) const {
2019-06-19 15:09:03 +00:00
const auto error = Data::RestrictionError(
history->peer,
ChatRestriction::f_send_messages);
2019-06-19 15:09:03 +00:00
return error.value_or(QString());
}
SendDataCommon::SentMTPMessageFields SendText::getSentMessageFields() const {
SentMTPMessageFields result;
result.text = MTP_string(_message);
2019-09-16 11:14:06 +00:00
result.entities = Api::EntitiesToMTP(_entities);
return result;
}
SendDataCommon::SentMTPMessageFields SendGeo::getSentMessageFields() const {
SentMTPMessageFields result;
result.media = MTP_messageMediaGeo(_location.toMTP());
return result;
}
SendDataCommon::SentMTPMessageFields SendVenue::getSentMessageFields() const {
SentMTPMessageFields result;
2017-11-20 19:54:05 +00:00
auto venueType = QString();
result.media = MTP_messageMediaVenue(
_location.toMTP(),
MTP_string(_title),
MTP_string(_address),
MTP_string(_provider),
MTP_string(_venueId),
MTP_string(venueType));
return result;
}
SendDataCommon::SentMTPMessageFields SendContact::getSentMessageFields() const {
SentMTPMessageFields result;
2018-06-26 13:58:29 +00:00
const auto userId = 0;
const auto vcard = QString();
result.media = MTP_messageMediaContact(
MTP_string(_phoneNumber),
MTP_string(_firstName),
MTP_string(_lastName),
2018-06-26 13:58:29 +00:00
MTP_string(vcard),
MTP_int(userId));
return result;
}
QString SendContact::getLayoutDescription(const Result *owner) const {
auto result = SendData::getLayoutDescription(owner);
if (result.isEmpty()) {
return App::formatPhone(_phoneNumber);
}
return result;
}
void SendPhoto::addToHistory(
const Result *owner,
not_null<History*> history,
MTPDmessage::Flags flags,
MTPDmessage_ClientFlags clientFlags,
MsgId msgId,
UserId fromId,
MTPint mtpDate,
UserId viaBotId,
MsgId replyToId,
const QString &postAuthor,
const MTPReplyMarkup &markup) const {
2019-07-17 14:34:39 +00:00
history->addNewLocalMessage(
msgId,
flags,
clientFlags,
viaBotId,
replyToId,
mtpDate.v,
fromId,
postAuthor,
_photo,
2018-01-18 13:59:22 +00:00
{ _message, _entities },
markup);
}
QString SendPhoto::getErrorOnSend(
const Result *owner,
not_null<History*> history) const {
2019-06-19 15:09:03 +00:00
const auto error = Data::RestrictionError(
history->peer,
ChatRestriction::f_send_media);
2019-06-19 15:09:03 +00:00
return error.value_or(QString());
}
void SendFile::addToHistory(
const Result *owner,
not_null<History*> history,
MTPDmessage::Flags flags,
MTPDmessage_ClientFlags clientFlags,
MsgId msgId,
UserId fromId,
MTPint mtpDate,
UserId viaBotId,
MsgId replyToId,
const QString &postAuthor,
const MTPReplyMarkup &markup) const {
2019-07-17 14:34:39 +00:00
history->addNewLocalMessage(
msgId,
flags,
clientFlags,
viaBotId,
replyToId,
mtpDate.v,
fromId,
postAuthor,
_document,
2018-01-18 13:59:22 +00:00
{ _message, _entities },
markup);
}
QString SendFile::getErrorOnSend(
const Result *owner,
not_null<History*> history) const {
2019-06-19 15:09:03 +00:00
const auto errorMedia = Data::RestrictionError(
history->peer,
ChatRestriction::f_send_media);
2019-06-19 15:09:03 +00:00
const auto errorStickers = Data::RestrictionError(
history->peer,
ChatRestriction::f_send_stickers);
2019-06-19 15:09:03 +00:00
const auto errorGifs = Data::RestrictionError(
history->peer,
ChatRestriction::f_send_gifs);
return errorMedia
2019-06-19 15:09:03 +00:00
? *errorMedia
: (errorStickers && (_document->sticker() != nullptr))
2019-06-19 15:09:03 +00:00
? *errorStickers
: (errorGifs
&& _document->isAnimation()
&& !_document->isVideoMessage())
2019-06-19 15:09:03 +00:00
? *errorGifs
: QString();
}
void SendGame::addToHistory(
const Result *owner,
not_null<History*> history,
MTPDmessage::Flags flags,
MTPDmessage_ClientFlags clientFlags,
MsgId msgId,
UserId fromId,
MTPint mtpDate,
UserId viaBotId,
MsgId replyToId,
const QString &postAuthor,
const MTPReplyMarkup &markup) const {
2019-07-17 14:34:39 +00:00
history->addNewLocalMessage(
msgId,
flags,
clientFlags,
viaBotId,
replyToId,
mtpDate.v,
fromId,
postAuthor,
_game,
markup);
}
QString SendGame::getErrorOnSend(
const Result *owner,
not_null<History*> history) const {
2019-06-19 15:09:03 +00:00
const auto error = Data::RestrictionError(
history->peer,
ChatRestriction::f_send_games);
2019-06-19 15:09:03 +00:00
return error.value_or(QString());
}
} // namespace internal
} // namespace InlineBots