support_helper.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/timer.h"
  9. #include "support/support_templates.h"
  10. #include "mtproto/sender.h"
  11. class History;
  12. namespace Main {
  13. class Session;
  14. } // namespace Main
  15. namespace Window {
  16. class SessionController;
  17. } // namespace Window
  18. namespace Support {
  19. struct UserInfo {
  20. QString author;
  21. TimeId date = 0;
  22. TextWithEntities text;
  23. };
  24. inline bool operator==(const UserInfo &a, const UserInfo &b) {
  25. return (a.author == b.author)
  26. && (a.date == b.date)
  27. && (a.text == b.text);
  28. }
  29. inline bool operator!=(const UserInfo &a, const UserInfo &b) {
  30. return !(a == b);
  31. }
  32. class Helper final {
  33. public:
  34. explicit Helper(not_null<Main::Session*> session);
  35. static std::unique_ptr<Helper> Create(not_null<Main::Session*> session);
  36. void registerWindow(not_null<Window::SessionController*> controller);
  37. void cloudDraftChanged(not_null<History*> history);
  38. void chatOccupiedUpdated(not_null<History*> history);
  39. [[nodiscard]] bool isOccupiedByMe(History *history) const;
  40. [[nodiscard]] bool isOccupiedBySomeone(History *history) const;
  41. void refreshInfo(not_null<UserData*> user);
  42. [[nodiscard]] rpl::producer<UserInfo> infoValue(
  43. not_null<UserData*> user) const;
  44. [[nodiscard]] rpl::producer<QString> infoLabelValue(
  45. not_null<UserData*> user) const;
  46. [[nodiscard]] rpl::producer<TextWithEntities> infoTextValue(
  47. not_null<UserData*> user) const;
  48. [[nodiscard]] UserInfo infoCurrent(not_null<UserData*> user) const;
  49. void editInfo(
  50. not_null<Window::SessionController*> controller,
  51. not_null<UserData*> user);
  52. Templates &templates();
  53. private:
  54. struct SavingInfo {
  55. TextWithEntities data;
  56. mtpRequestId requestId = 0;
  57. };
  58. void checkOccupiedChats();
  59. void updateOccupiedHistory(
  60. not_null<Window::SessionController*> controller,
  61. History *history);
  62. void setSupportName(const QString &name);
  63. void occupyIfNotYet();
  64. void occupyInDraft();
  65. void reoccupy();
  66. void applyInfo(
  67. not_null<UserData*> user,
  68. const MTPhelp_UserInfo &result);
  69. void showEditInfoBox(
  70. not_null<Window::SessionController*> controller,
  71. not_null<UserData*> user);
  72. void saveInfo(
  73. not_null<UserData*> user,
  74. TextWithEntities text,
  75. Fn<void(bool success)> done);
  76. const not_null<Main::Session*> _session;
  77. MTP::Sender _api;
  78. Templates _templates;
  79. QString _supportName;
  80. QString _supportNameNormalized;
  81. History *_occupiedHistory = nullptr;
  82. base::Timer _reoccupyTimer;
  83. base::Timer _checkOccupiedTimer;
  84. base::flat_map<not_null<History*>, TimeId> _occupiedChats;
  85. base::flat_map<not_null<UserData*>, UserInfo> _userInformation;
  86. base::flat_map<
  87. not_null<UserData*>,
  88. base::weak_ptr<Window::SessionController>> _userInfoEditPending;
  89. base::flat_map<not_null<UserData*>, SavingInfo> _userInfoSaving;
  90. rpl::lifetime _lifetime;
  91. };
  92. class FastButtonsBots final {
  93. public:
  94. explicit FastButtonsBots(not_null<Main::Session*> session);
  95. [[nodiscard]] bool enabled(not_null<PeerData*> peer) const;
  96. [[nodiscard]] rpl::producer<bool> enabledValue(
  97. not_null<PeerData*> peer) const;
  98. void setEnabled(not_null<PeerData*> peer, bool value);
  99. private:
  100. void write();
  101. void read();
  102. const not_null<Main::Session*> _session;
  103. base::flat_set<PeerId> _bots;
  104. rpl::event_stream<PeerId> _changes;
  105. bool _read = false;
  106. };
  107. QString ChatOccupiedString(not_null<History*> history);
  108. QString InterpretSendPath(
  109. not_null<Window::SessionController*> window,
  110. const QString &path);
  111. } // namespace Support