add_participants_box.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "boxes/peer_list_controllers.h"
  9. #include "boxes/peers/edit_participants_box.h"
  10. struct ChatAdminRightsInfo;
  11. struct ChatRestrictionsInfo;
  12. namespace Window {
  13. class SessionNavigation;
  14. } // namespace Window
  15. class AddParticipantsBoxController : public ContactsBoxController {
  16. public:
  17. static void Start(
  18. not_null<Window::SessionNavigation*> navigation,
  19. not_null<ChatData*> chat);
  20. static void Start(
  21. not_null<Window::SessionNavigation*> navigation,
  22. not_null<ChannelData*> channel);
  23. static void Start(
  24. not_null<Window::SessionNavigation*> navigation,
  25. not_null<ChannelData*> channel,
  26. base::flat_set<not_null<UserData*>> &&alreadyIn);
  27. explicit AddParticipantsBoxController(not_null<Main::Session*> session);
  28. explicit AddParticipantsBoxController(not_null<PeerData*> peer);
  29. AddParticipantsBoxController(
  30. not_null<PeerData*> peer,
  31. base::flat_set<not_null<UserData*>> &&alreadyIn);
  32. [[nodiscard]] not_null<PeerData*> peer() const {
  33. return _peer;
  34. }
  35. void rowClicked(not_null<PeerListRow*> row) override;
  36. void itemDeselectedHook(not_null<PeerData*> peer) override;
  37. protected:
  38. void prepareViewHook() override;
  39. std::unique_ptr<PeerListRow> createRow(
  40. not_null<UserData*> user) override;
  41. virtual bool needsInviteLinkButton();
  42. private:
  43. static void Start(
  44. not_null<Window::SessionNavigation*> navigation,
  45. not_null<ChannelData*> channel,
  46. base::flat_set<not_null<UserData*>> &&alreadyIn,
  47. bool justCreated);
  48. QPointer<Ui::BoxContent> showBox(object_ptr<Ui::BoxContent> box) const;
  49. void addInviteLinkButton();
  50. void inviteSelectedUsers(
  51. not_null<PeerListBox*> box,
  52. Fn<void()> done) const;
  53. void subscribeToMigration();
  54. int alreadyInCount() const;
  55. bool isAlreadyIn(not_null<UserData*> user) const;
  56. int fullCount() const;
  57. void updateTitle();
  58. PeerData *_peer = nullptr;
  59. base::flat_set<not_null<UserData*>> _alreadyIn;
  60. };
  61. struct ForbiddenInvites {
  62. std::vector<not_null<UserData*>> users;
  63. std::vector<not_null<UserData*>> premiumAllowsInvite;
  64. std::vector<not_null<UserData*>> premiumAllowsWrite;
  65. [[nodiscard]] bool empty() const {
  66. return users.empty();
  67. }
  68. };
  69. [[nodiscard]] ForbiddenInvites CollectForbiddenUsers(
  70. not_null<Main::Session*> session,
  71. const MTPmessages_InvitedUsers &result);
  72. bool ChatInviteForbidden(
  73. std::shared_ptr<Ui::Show> show,
  74. not_null<PeerData*> peer,
  75. ForbiddenInvites forbidden);
  76. // Adding an admin, banned or restricted user from channel members
  77. // with search + contacts search + global search.
  78. class AddSpecialBoxController
  79. : public PeerListController
  80. , public base::has_weak_ptr {
  81. public:
  82. using Role = ParticipantsBoxController::Role;
  83. using AdminDoneCallback = Fn<void(
  84. not_null<UserData*> user,
  85. ChatAdminRightsInfo adminRights,
  86. const QString &rank)>;
  87. using BannedDoneCallback = Fn<void(
  88. not_null<PeerData*> participant,
  89. ChatRestrictionsInfo bannedRights)>;
  90. AddSpecialBoxController(
  91. not_null<PeerData*> peer,
  92. Role role,
  93. AdminDoneCallback adminDoneCallback,
  94. BannedDoneCallback bannedDoneCallback);
  95. [[nodiscard]] not_null<PeerData*> peer() const {
  96. return _peer;
  97. }
  98. [[nodiscard]] Main::Session &session() const override;
  99. void prepare() override;
  100. void rowClicked(not_null<PeerListRow*> row) override;
  101. void loadMoreRows() override;
  102. [[nodiscard]] std::unique_ptr<PeerListRow> createSearchRow(
  103. not_null<PeerData*> peer) override;
  104. private:
  105. template <typename Callback>
  106. bool checkInfoLoaded(not_null<PeerData*> participant, Callback callback);
  107. void prepareChatRows(not_null<ChatData*> chat);
  108. void rebuildChatRows(not_null<ChatData*> chat);
  109. void showAdmin(not_null<UserData*> user, bool sure = false);
  110. void editAdminDone(
  111. not_null<UserData*> user,
  112. ChatAdminRightsInfo rights,
  113. const QString &rank);
  114. void showRestricted(not_null<UserData*> user, bool sure = false);
  115. void editRestrictedDone(
  116. not_null<PeerData*> participant,
  117. ChatRestrictionsInfo rights);
  118. void kickUser(not_null<PeerData*> participant, bool sure = false);
  119. bool appendRow(not_null<PeerData*> participant);
  120. bool prependRow(not_null<UserData*> user);
  121. std::unique_ptr<PeerListRow> createRow(
  122. not_null<PeerData*> participant) const;
  123. void subscribeToMigration();
  124. void migrate(not_null<ChatData*> chat, not_null<ChannelData*> channel);
  125. QPointer<Ui::BoxContent> showBox(object_ptr<Ui::BoxContent> box) const;
  126. not_null<PeerData*> _peer;
  127. MTP::Sender _api;
  128. Role _role = Role::Admins;
  129. int _offset = 0;
  130. mtpRequestId _loadRequestId = 0;
  131. bool _allLoaded = false;
  132. ParticipantsAdditionalData _additional;
  133. std::unique_ptr<ParticipantsOnlineSorter> _onlineSorter;
  134. Ui::BoxPointer _editBox;
  135. QPointer<Ui::BoxContent> _editParticipantBox;
  136. AdminDoneCallback _adminDoneCallback;
  137. BannedDoneCallback _bannedDoneCallback;
  138. protected:
  139. bool _excludeSelf = true;
  140. };
  141. // Finds chat/channel members, then contacts, then global search results.
  142. class AddSpecialBoxSearchController : public PeerListSearchController {
  143. public:
  144. using Role = ParticipantsBoxController::Role;
  145. AddSpecialBoxSearchController(
  146. not_null<PeerData*> peer,
  147. not_null<ParticipantsAdditionalData*> additional);
  148. void searchQuery(const QString &query) override;
  149. bool isLoading() override;
  150. bool loadMoreRows() override;
  151. private:
  152. struct CacheEntry {
  153. MTPchannels_ChannelParticipants result;
  154. int requestedCount = 0;
  155. };
  156. struct Query {
  157. QString text;
  158. int offset = 0;
  159. };
  160. void searchOnServer();
  161. bool searchParticipantsInCache();
  162. void searchParticipantsDone(
  163. mtpRequestId requestId,
  164. const MTPchannels_ChannelParticipants &result,
  165. int requestedCount);
  166. bool searchGlobalInCache();
  167. void searchGlobalDone(
  168. mtpRequestId requestId,
  169. const MTPcontacts_Found &result);
  170. void requestParticipants();
  171. void addChatMembers(not_null<ChatData*> chat);
  172. void addChatsContacts();
  173. void requestGlobal();
  174. void subscribeToMigration();
  175. not_null<PeerData*> _peer;
  176. not_null<ParticipantsAdditionalData*> _additional;
  177. MTP::Sender _api;
  178. base::Timer _timer;
  179. QString _query;
  180. mtpRequestId _requestId = 0;
  181. int _offset = 0;
  182. bool _participantsLoaded = false;
  183. bool _chatsContactsAdded = false;
  184. bool _chatMembersAdded = false;
  185. bool _globalLoaded = false;
  186. std::map<QString, CacheEntry> _participantsCache;
  187. std::map<mtpRequestId, Query> _participantsQueries;
  188. std::map<QString, MTPcontacts_Found> _globalCache;
  189. std::map<mtpRequestId, QString> _globalQueries;
  190. };