api_single_message_search.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "api/api_single_message_search.h"
  8. #include "main/main_session.h"
  9. #include "data/data_session.h"
  10. #include "data/data_channel.h"
  11. #include "data/data_search_controller.h"
  12. #include "core/local_url_handlers.h"
  13. #include "history/history_item.h"
  14. #include "base/qthelp_url.h"
  15. #include "apiwrap.h"
  16. namespace Api {
  17. namespace {
  18. using Key = details::SingleMessageSearchKey;
  19. Key ExtractKey(const QString &query) {
  20. const auto trimmed = query.trimmed();
  21. const auto local = Core::TryConvertUrlToLocal(trimmed);
  22. const auto check = local.isEmpty() ? trimmed : local;
  23. const auto parse = [&] {
  24. const auto delimeter = check.indexOf('?');
  25. return (delimeter > 0)
  26. ? qthelp::url_parse_params(
  27. check.mid(delimeter + 1),
  28. qthelp::UrlParamNameTransform::ToLower)
  29. : QMap<QString, QString>();
  30. };
  31. if (check.startsWith(u"tg://privatepost"_q, Qt::CaseInsensitive)) {
  32. const auto params = parse();
  33. const auto channel = params.value("channel");
  34. const auto post = params.value("post").toInt();
  35. return (channel.toULongLong() && post) ? Key{ channel, post } : Key();
  36. } else if (check.startsWith(u"tg://resolve"_q, Qt::CaseInsensitive)) {
  37. const auto params = parse();
  38. const auto domain = params.value("domain");
  39. const auto post = params.value("post").toInt();
  40. return (!domain.isEmpty() && post) ? Key{ domain, post } : Key();
  41. }
  42. return Key();
  43. }
  44. } // namespace
  45. SingleMessageSearch::SingleMessageSearch(not_null<Main::Session*> session)
  46. : _session(session) {
  47. }
  48. SingleMessageSearch::~SingleMessageSearch() {
  49. clear();
  50. }
  51. void SingleMessageSearch::clear() {
  52. _cache.clear();
  53. _requestKey = Key();
  54. _session->api().request(base::take(_requestId)).cancel();
  55. }
  56. std::optional<HistoryItem*> SingleMessageSearch::lookup(
  57. const QString &query,
  58. Fn<void()> ready) {
  59. const auto key = ExtractKey(query);
  60. if (!key) {
  61. return nullptr;
  62. }
  63. const auto i = _cache.find(key);
  64. if (i != end(_cache)) {
  65. return _session->data().message(i->second);
  66. }
  67. if (!(_requestKey == key)) {
  68. _session->api().request(base::take(_requestId)).cancel();
  69. _requestKey = key;
  70. }
  71. return performLookup(ready);
  72. }
  73. std::optional<HistoryItem*> SingleMessageSearch::performLookupByChannel(
  74. not_null<ChannelData*> channel,
  75. Fn<void()> ready) {
  76. Expects(!_requestKey.empty());
  77. const auto postId = _requestKey.postId;
  78. if (const auto item = _session->data().message(channel->id, postId)) {
  79. _cache.emplace(_requestKey, item->fullId());
  80. return item;
  81. } else if (!ready) {
  82. return nullptr;
  83. }
  84. const auto fail = [=] {
  85. _cache.emplace(_requestKey, FullMsgId());
  86. ready();
  87. };
  88. _requestId = _session->api().request(MTPchannels_GetMessages(
  89. channel->inputChannel,
  90. MTP_vector<MTPInputMessage>(1, MTP_inputMessageID(MTP_int(postId)))
  91. )).done([=](const MTPmessages_Messages &result) {
  92. const auto received = Api::ParseSearchResult(
  93. channel,
  94. Storage::SharedMediaType::kCount,
  95. postId,
  96. Data::LoadDirection::Around,
  97. result);
  98. if (!received.messageIds.empty()
  99. && received.messageIds.front() == postId) {
  100. _cache.emplace(
  101. _requestKey,
  102. FullMsgId(channel->id, postId));
  103. ready();
  104. } else {
  105. fail();
  106. }
  107. }).fail([=] {
  108. fail();
  109. }).send();
  110. return std::nullopt;
  111. }
  112. std::optional<HistoryItem*> SingleMessageSearch::performLookupById(
  113. ChannelId channelId,
  114. Fn<void()> ready) {
  115. Expects(!_requestKey.empty());
  116. if (const auto channel = _session->data().channelLoaded(channelId)) {
  117. return performLookupByChannel(channel, ready);
  118. } else if (!ready) {
  119. return nullptr;
  120. }
  121. const auto fail = [=] {
  122. _cache.emplace(_requestKey, FullMsgId());
  123. ready();
  124. };
  125. _requestId = _session->api().request(MTPchannels_GetChannels(
  126. MTP_vector<MTPInputChannel>(
  127. 1,
  128. MTP_inputChannel(MTP_long(channelId.bare), MTP_long(0)))
  129. )).done([=](const MTPmessages_Chats &result) {
  130. result.match([&](const auto &data) {
  131. const auto peer = _session->data().processChats(data.vchats());
  132. if (peer && peer->id == peerFromChannel(channelId)) {
  133. if (performLookupByChannel(peer->asChannel(), ready)) {
  134. ready();
  135. }
  136. } else {
  137. fail();
  138. }
  139. });
  140. }).fail([=] {
  141. fail();
  142. }).send();
  143. return std::nullopt;
  144. }
  145. std::optional<HistoryItem*> SingleMessageSearch::performLookupByUsername(
  146. const QString &username,
  147. Fn<void()> ready) {
  148. Expects(!_requestKey.empty());
  149. if (const auto peer = _session->data().peerByUsername(username)) {
  150. if (const auto channel = peer->asChannel()) {
  151. return performLookupByChannel(channel, ready);
  152. }
  153. _cache.emplace(_requestKey, FullMsgId());
  154. return nullptr;
  155. } else if (!ready) {
  156. return nullptr;
  157. }
  158. const auto fail = [=] {
  159. _cache.emplace(_requestKey, FullMsgId());
  160. ready();
  161. };
  162. _requestId = _session->api().request(MTPcontacts_ResolveUsername(
  163. MTP_flags(0),
  164. MTP_string(username),
  165. MTP_string()
  166. )).done([=](const MTPcontacts_ResolvedPeer &result) {
  167. result.match([&](const MTPDcontacts_resolvedPeer &data) {
  168. _session->data().processUsers(data.vusers());
  169. _session->data().processChats(data.vchats());
  170. const auto peerId = peerFromMTP(data.vpeer());
  171. const auto peer = peerId
  172. ? _session->data().peerLoaded(peerId)
  173. : nullptr;
  174. if (const auto channel = peer ? peer->asChannel() : nullptr) {
  175. if (performLookupByChannel(channel, ready)) {
  176. ready();
  177. }
  178. } else {
  179. fail();
  180. }
  181. });
  182. }).fail([=] {
  183. fail();
  184. }).send();
  185. return std::nullopt;
  186. }
  187. std::optional<HistoryItem*> SingleMessageSearch::performLookup(
  188. Fn<void()> ready) {
  189. Expects(!_requestKey.empty());
  190. if (!_requestKey.domainOrId[0].isDigit()) {
  191. return performLookupByUsername(_requestKey.domainOrId, ready);
  192. }
  193. const auto channelId = ChannelId(_requestKey.domainOrId.toULongLong());
  194. return performLookupById(channelId, ready);
  195. }
  196. QString ConvertPeerSearchQuery(const QString &query) {
  197. const auto trimmed = query.trimmed();
  198. const auto local = Core::TryConvertUrlToLocal(trimmed);
  199. const auto check = local.isEmpty() ? trimmed : local;
  200. if (!check.startsWith(u"tg://resolve"_q, Qt::CaseInsensitive)) {
  201. return query;
  202. }
  203. const auto delimeter = check.indexOf('?');
  204. const auto params = (delimeter > 0)
  205. ? qthelp::url_parse_params(
  206. check.mid(delimeter + 1),
  207. qthelp::UrlParamNameTransform::ToLower)
  208. : QMap<QString, QString>();
  209. return params.value("domain", query);
  210. }
  211. } // namespace Api