api_single_message_search.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #pragma once
  8. namespace Main {
  9. class Session;
  10. } // namespace Main
  11. namespace Api {
  12. namespace details {
  13. struct SingleMessageSearchKey {
  14. QString domainOrId;
  15. MsgId postId = 0;
  16. [[nodiscard]] bool empty() const {
  17. return domainOrId.isEmpty() || !postId;
  18. }
  19. [[nodiscard]] explicit operator bool() const {
  20. return !empty();
  21. }
  22. [[nodiscard]] bool operator<(const SingleMessageSearchKey &other) const {
  23. return std::tie(domainOrId, postId)
  24. < std::tie(other.domainOrId, other.postId);
  25. }
  26. [[nodiscard]] bool operator==(
  27. const SingleMessageSearchKey &other) const {
  28. return std::tie(domainOrId, postId)
  29. == std::tie(other.domainOrId, other.postId);
  30. }
  31. };
  32. } // namespace details
  33. class SingleMessageSearch {
  34. public:
  35. explicit SingleMessageSearch(not_null<Main::Session*> session);
  36. ~SingleMessageSearch();
  37. void clear();
  38. // If 'ready' callback is empty, the result must not be 'nullopt'.
  39. [[nodiscard]] std::optional<HistoryItem*> lookup(
  40. const QString &query,
  41. Fn<void()> ready = nullptr);
  42. private:
  43. using Key = details::SingleMessageSearchKey;
  44. [[nodiscard]] std::optional<HistoryItem*> performLookup(
  45. Fn<void()> ready);
  46. [[nodiscard]] std::optional<HistoryItem*> performLookupById(
  47. ChannelId channelId,
  48. Fn<void()> ready);
  49. [[nodiscard]] std::optional<HistoryItem*> performLookupByUsername(
  50. const QString &username,
  51. Fn<void()> ready);
  52. [[nodiscard]] std::optional<HistoryItem*> performLookupByChannel(
  53. not_null<ChannelData*> channel,
  54. Fn<void()> ready);
  55. const not_null<Main::Session*> _session;
  56. std::map<Key, FullMsgId> _cache;
  57. mtpRequestId _requestId = 0;
  58. Key _requestKey;
  59. };
  60. [[nodiscard]] QString ConvertPeerSearchQuery(const QString &query);
  61. } // namespace Api