inline_bot_send_data.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "inline_bots/inline_bot_send_data.h"
  8. #include "api/api_text_entities.h"
  9. #include "data/data_document.h"
  10. #include "inline_bots/inline_bot_result.h"
  11. #include "storage/localstorage.h"
  12. #include "lang/lang_keys.h"
  13. #include "history/history.h"
  14. #include "history/history_item.h"
  15. #include "data/data_channel.h"
  16. #include "ui/text/format_values.h" // Ui::FormatPhone
  17. namespace InlineBots {
  18. namespace internal {
  19. QString SendData::getLayoutTitle(const Result *owner) const {
  20. return owner->_title;
  21. }
  22. QString SendData::getLayoutDescription(const Result *owner) const {
  23. return owner->_description;
  24. }
  25. not_null<HistoryItem*> SendDataCommon::makeMessage(
  26. const Result *owner,
  27. not_null<History*> history,
  28. HistoryItemCommonFields &&fields) const {
  29. auto distinct = getSentMessageFields();
  30. if (fields.replyTo) {
  31. fields.flags |= MessageFlag::HasReplyInfo;
  32. }
  33. return history->makeMessage(
  34. std::move(fields),
  35. std::move(distinct.text),
  36. std::move(distinct.media));
  37. }
  38. Data::SendError SendDataCommon::getErrorOnSend(
  39. const Result *owner,
  40. not_null<History*> history) const {
  41. const auto type = ChatRestriction::SendOther;
  42. return Data::RestrictionError(history->peer, type);
  43. }
  44. SendDataCommon::SentMessageFields SendText::getSentMessageFields() const {
  45. return { .text = { _message, _entities } };
  46. }
  47. SendDataCommon::SentMessageFields SendGeo::getSentMessageFields() const {
  48. if (_period) {
  49. using Flag = MTPDmessageMediaGeoLive::Flag;
  50. return { .media = MTP_messageMediaGeoLive(
  51. MTP_flags((_heading ? Flag::f_heading : Flag(0))
  52. | (_proximityNotificationRadius
  53. ? Flag::f_proximity_notification_radius
  54. : Flag(0))),
  55. _location.toMTP(),
  56. MTP_int(_heading.value_or(0)),
  57. MTP_int(*_period),
  58. MTP_int(_proximityNotificationRadius.value_or(0))) };
  59. }
  60. return { .media = MTP_messageMediaGeo(_location.toMTP()) };
  61. }
  62. SendDataCommon::SentMessageFields SendVenue::getSentMessageFields() const {
  63. return { .media = MTP_messageMediaVenue(
  64. _location.toMTP(),
  65. MTP_string(_title),
  66. MTP_string(_address),
  67. MTP_string(_provider),
  68. MTP_string(_venueId),
  69. MTP_string(QString())) }; // venue_type
  70. }
  71. SendDataCommon::SentMessageFields SendContact::getSentMessageFields() const {
  72. return { .media = MTP_messageMediaContact(
  73. MTP_string(_phoneNumber),
  74. MTP_string(_firstName),
  75. MTP_string(_lastName),
  76. MTP_string(), // vcard
  77. MTP_long(0)) }; // user_id
  78. }
  79. QString SendContact::getLayoutDescription(const Result *owner) const {
  80. auto result = SendData::getLayoutDescription(owner);
  81. if (result.isEmpty()) {
  82. return Ui::FormatPhone(_phoneNumber);
  83. }
  84. return result;
  85. }
  86. not_null<HistoryItem*> SendPhoto::makeMessage(
  87. const Result *owner,
  88. not_null<History*> history,
  89. HistoryItemCommonFields &&fields) const {
  90. return history->makeMessage(
  91. std::move(fields),
  92. _photo,
  93. TextWithEntities{ _message, _entities });
  94. }
  95. Data::SendError SendPhoto::getErrorOnSend(
  96. const Result *owner,
  97. not_null<History*> history) const {
  98. const auto type = ChatRestriction::SendPhotos;
  99. return Data::RestrictionError(history->peer, type);
  100. }
  101. not_null<HistoryItem*> SendFile::makeMessage(
  102. const Result *owner,
  103. not_null<History*> history,
  104. HistoryItemCommonFields &&fields) const {
  105. return history->makeMessage(
  106. std::move(fields),
  107. _document,
  108. TextWithEntities{ _message, _entities });
  109. }
  110. Data::SendError SendFile::getErrorOnSend(
  111. const Result *owner,
  112. not_null<History*> history) const {
  113. const auto type = _document->requiredSendRight();
  114. return Data::RestrictionError(history->peer, type);
  115. }
  116. not_null<HistoryItem*> SendGame::makeMessage(
  117. const Result *owner,
  118. not_null<History*> history,
  119. HistoryItemCommonFields &&fields) const {
  120. return history->makeMessage(std::move(fields), _game);
  121. }
  122. Data::SendError SendGame::getErrorOnSend(
  123. const Result *owner,
  124. not_null<History*> history) const {
  125. const auto type = ChatRestriction::SendGames;
  126. return Data::RestrictionError(history->peer, type);
  127. }
  128. SendDataCommon::SentMessageFields SendInvoice::getSentMessageFields() const {
  129. return { .media = _media };
  130. }
  131. QString SendInvoice::getLayoutDescription(const Result *owner) const {
  132. return qs(_media.c_messageMediaInvoice().vdescription());
  133. }
  134. } // namespace internal
  135. } // namespace InlineBots