data_send_action.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "data/data_send_action.h"
  8. #include "data/data_user.h"
  9. #include "history/history.h"
  10. #include "history/view/history_view_send_action.h"
  11. namespace Data {
  12. SendActionManager::SendActionManager()
  13. : _animation([=](crl::time now) { return callback(now); }) {
  14. }
  15. HistoryView::SendActionPainter *SendActionManager::lookupPainter(
  16. not_null<History*> history,
  17. MsgId rootId) {
  18. if (!rootId) {
  19. return history->sendActionPainter();
  20. }
  21. const auto i = _painters.find(history);
  22. if (i == end(_painters)) {
  23. return nullptr;
  24. }
  25. const auto j = i->second.find(rootId);
  26. if (j == end(i->second)) {
  27. return nullptr;
  28. }
  29. const auto result = j->second.lock();
  30. if (!result) {
  31. i->second.erase(j);
  32. if (i->second.empty()) {
  33. _painters.erase(i);
  34. }
  35. return nullptr;
  36. }
  37. crl::on_main([copy = result] {
  38. });
  39. return result.get();
  40. }
  41. void SendActionManager::registerFor(
  42. not_null<History*> history,
  43. MsgId rootId,
  44. not_null<UserData*> user,
  45. const MTPSendMessageAction &action,
  46. TimeId when) {
  47. if (history->peer->isSelf()) {
  48. return;
  49. }
  50. const auto sendAction = lookupPainter(history, rootId);
  51. if (!sendAction) {
  52. return;
  53. }
  54. if (sendAction->updateNeedsAnimating(user, action)) {
  55. user->madeAction(when);
  56. if (!_sendActions.contains(std::pair{ history, rootId })) {
  57. _sendActions.emplace(std::pair{ history, rootId }, crl::now());
  58. _animation.start();
  59. }
  60. }
  61. }
  62. auto SendActionManager::repliesPainter(
  63. not_null<History*> history,
  64. MsgId rootId)
  65. -> std::shared_ptr<SendActionPainter> {
  66. auto &weak = _painters[history][rootId];
  67. if (auto strong = weak.lock()) {
  68. return strong;
  69. }
  70. auto result = std::make_shared<SendActionPainter>(history, rootId);
  71. weak = result;
  72. return result;
  73. }
  74. void SendActionManager::repliesPainterRemoved(
  75. not_null<History*> history,
  76. MsgId rootId) {
  77. const auto i = _painters.find(history);
  78. if (i == end(_painters)) {
  79. return;
  80. }
  81. const auto j = i->second.find(rootId);
  82. if (j == end(i->second) || j->second.lock()) {
  83. return;
  84. }
  85. i->second.erase(j);
  86. if (i->second.empty()) {
  87. _painters.erase(i);
  88. }
  89. }
  90. void SendActionManager::repliesPaintersClear(
  91. not_null<History*> history,
  92. not_null<UserData*> user) {
  93. auto &map = _painters[history];
  94. for (auto i = map.begin(); i != map.end();) {
  95. if (auto strong = i->second.lock()) {
  96. strong->clear(user);
  97. ++i;
  98. } else {
  99. i = map.erase(i);
  100. }
  101. }
  102. if (map.empty()) {
  103. _painters.erase(history);
  104. }
  105. }
  106. bool SendActionManager::callback(crl::time now) {
  107. for (auto i = begin(_sendActions); i != end(_sendActions);) {
  108. const auto sendAction = lookupPainter(
  109. i->first.first,
  110. i->first.second);
  111. if (sendAction && sendAction->updateNeedsAnimating(now)) {
  112. ++i;
  113. } else {
  114. i = _sendActions.erase(i);
  115. }
  116. }
  117. return !_sendActions.empty();
  118. }
  119. auto SendActionManager::animationUpdated() const
  120. -> rpl::producer<SendActionManager::AnimationUpdate> {
  121. return _animationUpdate.events();
  122. }
  123. void SendActionManager::updateAnimation(AnimationUpdate &&update) {
  124. _animationUpdate.fire(std::move(update));
  125. }
  126. auto SendActionManager::speakingAnimationUpdated() const
  127. -> rpl::producer<not_null<History*>> {
  128. return _speakingAnimationUpdate.events();
  129. }
  130. void SendActionManager::updateSpeakingAnimation(not_null<History*> history) {
  131. _speakingAnimationUpdate.fire_copy(history);
  132. }
  133. void SendActionManager::clear() {
  134. _sendActions.clear();
  135. }
  136. } // namespace Data