support_templates.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "base/binary_guard.h"
  9. #include <QtNetwork/QNetworkReply>
  10. namespace Main {
  11. class Session;
  12. } // namespace Main
  13. namespace Support {
  14. namespace details {
  15. struct TemplatesQuestion {
  16. QString question;
  17. QStringList originalKeys;
  18. QStringList normalizedKeys;
  19. QString value;
  20. };
  21. struct TemplatesFile {
  22. QString url;
  23. std::map<QString, TemplatesQuestion> questions;
  24. };
  25. struct TemplatesData {
  26. std::map<QString, TemplatesFile> files;
  27. };
  28. struct TemplatesIndex {
  29. using Id = std::pair<QString, QString>; // filename, normalized question
  30. using Term = std::pair<QString, int>; // search term, weight
  31. std::map<QChar, std::vector<Id>> first;
  32. std::map<Id, std::vector<Term>> full;
  33. };
  34. } // namespace details
  35. class Templates : public base::has_weak_ptr {
  36. public:
  37. explicit Templates(not_null<Main::Session*> session);
  38. void reload();
  39. using Question = details::TemplatesQuestion;
  40. std::vector<Question> query(const QString &text) const;
  41. auto errors() const {
  42. return _errors.events();
  43. }
  44. struct QuestionByKey {
  45. Question question;
  46. QString key;
  47. };
  48. std::optional<QuestionByKey> matchExact(QString text) const;
  49. std::optional<QuestionByKey> matchFromEnd(QString text) const;
  50. int maxKeyLength() const {
  51. return _maxKeyLength;
  52. }
  53. ~Templates();
  54. private:
  55. struct Updates;
  56. void load();
  57. void update();
  58. void ensureUpdatesCreated();
  59. void updateRequestFinished(QNetworkReply *reply);
  60. void checkUpdateFinished();
  61. void setData(details::TemplatesData &&data);
  62. not_null<Main::Session*> _session;
  63. details::TemplatesData _data;
  64. details::TemplatesIndex _index;
  65. rpl::event_stream<QStringList> _errors;
  66. base::binary_guard _reading;
  67. bool _reloadAfterRead = false;
  68. rpl::lifetime _reloadToastSubscription;
  69. int _maxKeyLength = 0;
  70. std::unique_ptr<Updates> _updates;
  71. rpl::lifetime _lifetime;
  72. };
  73. } // namespace Support