api_updates.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_pts_waiter.h"
  9. #include "base/timer.h"
  10. class ApiWrap;
  11. class History;
  12. namespace MTP {
  13. class Error;
  14. } // namespace MTP
  15. namespace Main {
  16. class Session;
  17. } // namespace Main
  18. namespace ChatHelpers {
  19. struct EmojiInteractionsBunch;
  20. } // namespace ChatHelpers
  21. namespace Api {
  22. class Updates final {
  23. public:
  24. explicit Updates(not_null<Main::Session*> session);
  25. [[nodiscard]] Main::Session &session() const;
  26. [[nodiscard]] ApiWrap &api() const;
  27. void applyUpdates(
  28. const MTPUpdates &updates,
  29. uint64 sentMessageRandomId = 0);
  30. void applyUpdatesNoPtsCheck(const MTPUpdates &updates);
  31. void applyUpdateNoPtsCheck(const MTPUpdate &update);
  32. void checkForSentToScheduled(const MTPUpdates &updates);
  33. [[nodiscard]] int32 pts() const;
  34. void updateOnline(crl::time lastNonIdleTime = 0);
  35. [[nodiscard]] bool isIdle() const;
  36. [[nodiscard]] rpl::producer<bool> isIdleValue() const;
  37. void checkIdleFinish(crl::time lastNonIdleTime = 0);
  38. bool lastWasOnline() const;
  39. crl::time lastSetOnline() const;
  40. bool isQuitPrevent();
  41. bool updateAndApply(int32 pts, int32 ptsCount, const MTPUpdates &updates);
  42. bool updateAndApply(int32 pts, int32 ptsCount, const MTPUpdate &update);
  43. bool updateAndApply(int32 pts, int32 ptsCount);
  44. void checkLastUpdate(bool afterSleep);
  45. // ms <= 0 - stop timer
  46. void ptsWaiterStartTimerFor(ChannelData *channel, crl::time ms);
  47. void getDifference();
  48. void requestChannelRangeDifference(not_null<History*> history);
  49. void addActiveChat(rpl::producer<PeerData*> chat);
  50. [[nodiscard]] bool inActiveChats(not_null<PeerData*> peer) const;
  51. private:
  52. enum class ChannelDifferenceRequest {
  53. Unknown,
  54. PtsGapOrShortPoll,
  55. AfterFail,
  56. };
  57. enum class SkipUpdatePolicy {
  58. SkipNone,
  59. SkipMessageIds,
  60. SkipExceptGroupCallParticipants,
  61. };
  62. struct ActiveChatTracker {
  63. PeerData *peer = nullptr;
  64. rpl::lifetime lifetime;
  65. };
  66. void channelRangeDifferenceSend(
  67. not_null<ChannelData*> channel,
  68. MsgRange range,
  69. int32 pts);
  70. void channelRangeDifferenceDone(
  71. not_null<ChannelData*> channel,
  72. MsgRange range,
  73. const MTPupdates_ChannelDifference &result);
  74. void updateOnline(crl::time lastNonIdleTime, bool gotOtherOffline);
  75. void sendPing();
  76. void getDifferenceByPts();
  77. void getDifferenceAfterFail();
  78. [[nodiscard]] bool requestingDifference() const {
  79. return _ptsWaiter.requesting();
  80. }
  81. void getChannelDifference(
  82. not_null<ChannelData*> channel,
  83. ChannelDifferenceRequest from = ChannelDifferenceRequest::Unknown);
  84. void differenceDone(const MTPupdates_Difference &result);
  85. void differenceFail(const MTP::Error &error);
  86. void feedDifference(
  87. const MTPVector<MTPUser> &users,
  88. const MTPVector<MTPChat> &chats,
  89. const MTPVector<MTPMessage> &msgs,
  90. const MTPVector<MTPUpdate> &other);
  91. void stateDone(const MTPupdates_State &state);
  92. void setState(int32 pts, int32 date, int32 qts, int32 seq);
  93. void channelDifferenceDone(
  94. not_null<ChannelData*> channel,
  95. const MTPupdates_ChannelDifference &diff);
  96. void channelDifferenceFail(
  97. not_null<ChannelData*> channel,
  98. const MTP::Error &error);
  99. void failDifferenceStartTimerFor(ChannelData *channel);
  100. void feedChannelDifference(const MTPDupdates_channelDifference &data);
  101. void mtpUpdateReceived(const MTPUpdates &updates);
  102. void mtpNewSessionCreated();
  103. void feedUpdateVector(
  104. const MTPVector<MTPUpdate> &updates,
  105. SkipUpdatePolicy policy = SkipUpdatePolicy::SkipNone);
  106. // Doesn't call sendHistoryChangeNotifications itself.
  107. void feedMessageIds(const MTPVector<MTPUpdate> &updates);
  108. // Doesn't call sendHistoryChangeNotifications itself.
  109. void feedUpdate(const MTPUpdate &update);
  110. void applyConvertToScheduledOnSend(
  111. const MTPVector<MTPUpdate> &other,
  112. bool skipScheduledCheck = false);
  113. void applyGroupCallParticipantUpdates(const MTPUpdates &updates);
  114. bool whenGetDiffChanged(
  115. ChannelData *channel,
  116. int32 ms,
  117. base::flat_map<not_null<ChannelData*>, crl::time> &whenMap,
  118. crl::time &curTime);
  119. void handleSendActionUpdate(
  120. PeerId peerId,
  121. MsgId rootId,
  122. PeerId fromId,
  123. const MTPSendMessageAction &action);
  124. void handleEmojiInteraction(
  125. not_null<PeerData*> peer,
  126. const MTPDsendMessageEmojiInteraction &data);
  127. void handleSpeakingInCall(
  128. not_null<PeerData*> peer,
  129. PeerId participantPeerId,
  130. PeerData *participantPeerLoaded);
  131. void handleEmojiInteraction(
  132. not_null<PeerData*> peer,
  133. MsgId messageId,
  134. const QString &emoticon,
  135. ChatHelpers::EmojiInteractionsBunch bunch);
  136. void handleEmojiInteraction(
  137. not_null<PeerData*> peer,
  138. const QString &emoticon);
  139. const not_null<Main::Session*> _session;
  140. int32 _updatesDate = 0;
  141. int32 _updatesQts = -1;
  142. int32 _updatesSeq = 0;
  143. base::Timer _noUpdatesTimer;
  144. base::Timer _onlineTimer;
  145. PtsWaiter _ptsWaiter;
  146. base::flat_map<not_null<ChannelData*>, crl::time> _whenGetDiffByPts;
  147. base::flat_map<not_null<ChannelData*>, crl::time> _whenGetDiffAfterFail;
  148. crl::time _getDifferenceTimeByPts = 0;
  149. crl::time _getDifferenceTimeAfterFail = 0;
  150. base::Timer _byPtsTimer;
  151. base::flat_map<int32, MTPUpdates> _bySeqUpdates;
  152. base::Timer _bySeqTimer;
  153. base::Timer _byMinChannelTimer;
  154. // growing timeout for getDifference calls, if it fails
  155. crl::time _failDifferenceTimeout = 1;
  156. // growing timeout for getChannelDifference calls, if it fails
  157. base::flat_map<
  158. not_null<ChannelData*>,
  159. crl::time> _channelFailDifferenceTimeout;
  160. base::Timer _failDifferenceTimer;
  161. base::flat_map<
  162. not_null<ChannelData*>,
  163. mtpRequestId> _rangeDifferenceRequests;
  164. crl::time _lastUpdateTime = 0;
  165. bool _handlingChannelDifference = false;
  166. base::flat_map<int, ActiveChatTracker> _activeChats;
  167. base::flat_map<
  168. not_null<PeerData*>,
  169. base::flat_map<PeerId, crl::time>> _pendingSpeakingCallParticipants;
  170. mtpRequestId _onlineRequest = 0;
  171. base::Timer _idleFinishTimer;
  172. crl::time _lastSetOnline = 0;
  173. bool _lastWasOnline = false;
  174. rpl::variable<bool> _isIdle = false;
  175. rpl::lifetime _lifetime;
  176. };
  177. [[nodiscard]] bool IsWithdrawalNotification(
  178. const MTPDupdateServiceNotification &);
  179. } // namespace Api