api_send_progress.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "api/api_send_progress.h"
  8. #include "main/main_session.h"
  9. #include "history/history.h"
  10. #include "data/data_peer.h"
  11. #include "data/data_user.h"
  12. #include "base/unixtime.h"
  13. #include "data/data_peer_values.h"
  14. #include "apiwrap.h"
  15. namespace Api {
  16. namespace {
  17. constexpr auto kCancelTypingActionTimeout = crl::time(5000);
  18. constexpr auto kSendMySpeakingInterval = 3 * crl::time(1000);
  19. constexpr auto kSendMyTypingInterval = 5 * crl::time(1000);
  20. constexpr auto kSendTypingsToOfflineFor = TimeId(30);
  21. } // namespace
  22. SendProgressManager::SendProgressManager(not_null<Main::Session*> session)
  23. : _session(session)
  24. , _stopTypingTimer([=] { cancelTyping(base::take(_stopTypingHistory)); }) {
  25. }
  26. void SendProgressManager::cancel(
  27. not_null<History*> history,
  28. SendProgressType type) {
  29. cancel(history, 0, type);
  30. }
  31. void SendProgressManager::cancel(
  32. not_null<History*> history,
  33. MsgId topMsgId,
  34. SendProgressType type) {
  35. const auto i = _requests.find(Key{ history, topMsgId, type });
  36. if (i != _requests.end()) {
  37. _session->api().request(i->second).cancel();
  38. _requests.erase(i);
  39. }
  40. }
  41. void SendProgressManager::cancelTyping(not_null<History*> history) {
  42. _stopTypingTimer.cancel();
  43. cancel(history, SendProgressType::Typing);
  44. }
  45. void SendProgressManager::update(
  46. not_null<History*> history,
  47. SendProgressType type,
  48. int progress) {
  49. update(history, 0, type, progress);
  50. }
  51. void SendProgressManager::update(
  52. not_null<History*> history,
  53. MsgId topMsgId,
  54. SendProgressType type,
  55. int progress) {
  56. const auto peer = history->peer;
  57. if (peer->isSelf()
  58. || (peer->isChannel()
  59. && !peer->isMegagroup()
  60. && type != SendProgressType::Speaking)) {
  61. return;
  62. }
  63. const auto doing = (progress >= 0);
  64. const auto key = Key{ history, topMsgId, type };
  65. if (updated(key, doing)) {
  66. cancel(history, topMsgId, type);
  67. if (doing) {
  68. send(key, progress);
  69. }
  70. }
  71. }
  72. bool SendProgressManager::updated(const Key &key, bool doing) {
  73. const auto now = crl::now();
  74. const auto i = _updated.find(key);
  75. if (doing) {
  76. const auto sendEach = (key.type == SendProgressType::Speaking)
  77. ? kSendMySpeakingInterval
  78. : kSendMyTypingInterval;
  79. if (i == end(_updated)) {
  80. _updated.emplace(key, now + 2 * sendEach);
  81. } else if (i->second > now + sendEach) {
  82. return false;
  83. } else {
  84. i->second = now + 2 * sendEach;
  85. }
  86. } else {
  87. if (i == end(_updated)) {
  88. return false;
  89. } else if (i->second <= now) {
  90. return false;
  91. } else {
  92. _updated.erase(i);
  93. }
  94. }
  95. return true;
  96. }
  97. void SendProgressManager::send(const Key &key, int progress) {
  98. if (skipRequest(key)) {
  99. return;
  100. }
  101. using Type = SendProgressType;
  102. const auto action = [&]() -> MTPsendMessageAction {
  103. const auto p = MTP_int(progress);
  104. switch (key.type) {
  105. case Type::Typing: return MTP_sendMessageTypingAction();
  106. case Type::RecordVideo: return MTP_sendMessageRecordVideoAction();
  107. case Type::UploadVideo: return MTP_sendMessageUploadVideoAction(p);
  108. case Type::RecordVoice: return MTP_sendMessageRecordAudioAction();
  109. case Type::UploadVoice: return MTP_sendMessageUploadAudioAction(p);
  110. case Type::RecordRound: return MTP_sendMessageRecordRoundAction();
  111. case Type::UploadRound: return MTP_sendMessageUploadRoundAction(p);
  112. case Type::UploadPhoto: return MTP_sendMessageUploadPhotoAction(p);
  113. case Type::UploadFile: return MTP_sendMessageUploadDocumentAction(p);
  114. case Type::ChooseLocation: return MTP_sendMessageGeoLocationAction();
  115. case Type::ChooseContact: return MTP_sendMessageChooseContactAction();
  116. case Type::PlayGame: return MTP_sendMessageGamePlayAction();
  117. case Type::Speaking: return MTP_speakingInGroupCallAction();
  118. case Type::ChooseSticker: return MTP_sendMessageChooseStickerAction();
  119. default: return MTP_sendMessageTypingAction();
  120. }
  121. }();
  122. const auto requestId = _session->api().request(MTPmessages_SetTyping(
  123. MTP_flags(key.topMsgId
  124. ? MTPmessages_SetTyping::Flag::f_top_msg_id
  125. : MTPmessages_SetTyping::Flag(0)),
  126. key.history->peer->input,
  127. MTP_int(key.topMsgId),
  128. action
  129. )).done([=](const MTPBool &result, mtpRequestId requestId) {
  130. done(requestId);
  131. }).send();
  132. _requests.emplace(key, requestId);
  133. if (key.type == Type::Typing) {
  134. _stopTypingHistory = key.history;
  135. _stopTypingTimer.callOnce(kCancelTypingActionTimeout);
  136. }
  137. }
  138. bool SendProgressManager::skipRequest(const Key &key) const {
  139. const auto user = key.history->peer->asUser();
  140. if (!user) {
  141. return false;
  142. } else if (user->isSelf()) {
  143. return true;
  144. } else if (user->isBot() && !user->isSupport()) {
  145. return true;
  146. }
  147. const auto recently = base::unixtime::now() - kSendTypingsToOfflineFor;
  148. const auto lastseen = user->lastseen();
  149. if (lastseen.isRecently()) {
  150. return false;
  151. } else if (const auto value = lastseen.onlineTill()) {
  152. return (value < recently);
  153. }
  154. return true;
  155. }
  156. void SendProgressManager::done(mtpRequestId requestId) {
  157. for (auto i = _requests.begin(), e = _requests.end(); i != e; ++i) {
  158. if (i->second == requestId) {
  159. _requests.erase(i);
  160. break;
  161. }
  162. }
  163. }
  164. } // namespace Api