api_chat_participants.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "data/data_chat_participant_status.h"
  9. #include "mtproto/sender.h"
  10. #include "base/timer.h"
  11. class ApiWrap;
  12. class ChannelData;
  13. namespace Main {
  14. class Session;
  15. } // namespace Main
  16. namespace Ui {
  17. class Show;
  18. } // namespace Ui
  19. namespace Api {
  20. class ChatParticipant final {
  21. public:
  22. enum class Type {
  23. Creator,
  24. Admin,
  25. Member,
  26. Restricted,
  27. Left,
  28. Banned,
  29. };
  30. explicit ChatParticipant(
  31. const MTPChannelParticipant &p,
  32. not_null<PeerData*> peer);
  33. ChatParticipant(
  34. Type type,
  35. PeerId peerId,
  36. UserId by,
  37. ChatRestrictionsInfo restrictions,
  38. ChatAdminRightsInfo rights,
  39. bool canBeEdited = false,
  40. QString rank = QString());
  41. bool isUser() const;
  42. bool isCreator() const;
  43. bool isCreatorOrAdmin() const;
  44. bool isKicked() const;
  45. bool canBeEdited() const;
  46. UserId by() const;
  47. PeerId id() const;
  48. UserId userId() const;
  49. ChatRestrictionsInfo restrictions() const;
  50. ChatAdminRightsInfo rights() const;
  51. TimeId subscriptionDate() const;
  52. TimeId promotedSince() const;
  53. TimeId restrictedSince() const;
  54. TimeId memberSince() const;
  55. Type type() const;
  56. QString rank() const;
  57. void tryApplyCreatorTo(not_null<ChannelData*> channel) const;
  58. private:
  59. Type _type = Type::Member;
  60. PeerId _peer;
  61. UserId _by; // Banned/Restricted/Promoted.
  62. bool _canBeEdited = false;
  63. QString _rank;
  64. TimeId _subscriptionDate = 0;
  65. TimeId _date = 0;
  66. ChatRestrictionsInfo _restrictions;
  67. ChatAdminRightsInfo _rights;
  68. };
  69. class ChatParticipants final {
  70. public:
  71. struct Parsed {
  72. const int availableCount;
  73. const std::vector<ChatParticipant> list;
  74. };
  75. using TLMembers = MTPDchannels_channelParticipants;
  76. using Members = const std::vector<ChatParticipant> &;
  77. explicit ChatParticipants(not_null<ApiWrap*> api);
  78. void requestLast(not_null<ChannelData*> channel);
  79. void requestBots(not_null<ChannelData*> channel);
  80. void requestAdmins(not_null<ChannelData*> channel);
  81. void requestCountDelayed(not_null<ChannelData*> channel);
  82. static Parsed Parse(
  83. not_null<ChannelData*> channel,
  84. const TLMembers &data);
  85. static Parsed ParseRecent(
  86. not_null<ChannelData*> channel,
  87. const TLMembers &data);
  88. static void Restrict(
  89. not_null<ChannelData*> channel,
  90. not_null<PeerData*> participant,
  91. ChatRestrictionsInfo oldRights,
  92. ChatRestrictionsInfo newRights,
  93. Fn<void()> onDone,
  94. Fn<void()> onFail);
  95. void add(
  96. std::shared_ptr<Ui::Show> show,
  97. not_null<PeerData*> peer,
  98. const std::vector<not_null<UserData*>> &users,
  99. bool passGroupHistory = true,
  100. Fn<void(bool)> done = nullptr);
  101. void requestSelf(not_null<ChannelData*> channel);
  102. void requestForAdd(
  103. not_null<ChannelData*> channel,
  104. Fn<void(const TLMembers&)> callback);
  105. void kick(
  106. not_null<ChatData*> chat,
  107. not_null<PeerData*> participant);
  108. void kick(
  109. not_null<ChannelData*> channel,
  110. not_null<PeerData*> participant,
  111. ChatRestrictionsInfo currentRights);
  112. void unblock(
  113. not_null<ChannelData*> channel,
  114. not_null<PeerData*> participant);
  115. void loadSimilarPeers(not_null<PeerData*> peer);
  116. struct Peers {
  117. std::vector<not_null<PeerData*>> list;
  118. int more = 0;
  119. friend inline bool operator==(
  120. const Peers &,
  121. const Peers &) = default;
  122. };
  123. [[nodiscard]] const Peers &similar(not_null<PeerData*> peer);
  124. [[nodiscard]] auto similarLoaded() const
  125. -> rpl::producer<not_null<PeerData*>>;
  126. void loadRecommendations();
  127. [[nodiscard]] const Peers &recommendations() const;
  128. [[nodiscard]] rpl::producer<> recommendationsLoaded() const;
  129. private:
  130. struct SimilarPeers {
  131. Peers peers;
  132. mtpRequestId requestId = 0;
  133. };
  134. const not_null<Main::Session*> _session;
  135. MTP::Sender _api;
  136. using PeerRequests = base::flat_map<PeerData*, mtpRequestId>;
  137. PeerRequests _participantsRequests;
  138. PeerRequests _botsRequests;
  139. PeerRequests _adminsRequests;
  140. base::DelayedCallTimer _participantsCountRequestTimer;
  141. struct {
  142. ChannelData *channel = nullptr;
  143. mtpRequestId requestId = 0;
  144. Fn<void(const TLMembers&)> callback;
  145. } _forAdd;
  146. base::flat_set<not_null<ChannelData*>> _selfParticipantRequests;
  147. using KickRequest = std::pair<
  148. not_null<ChannelData*>,
  149. not_null<PeerData*>>;
  150. base::flat_map<KickRequest, mtpRequestId> _kickRequests;
  151. base::flat_map<not_null<PeerData*>, SimilarPeers> _similar;
  152. rpl::event_stream<not_null<PeerData*>> _similarLoaded;
  153. SimilarPeers _recommendations;
  154. rpl::variable<bool> _recommendationsLoaded = false;
  155. };
  156. } // namespace Api