calls_group_toasts.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "calls/group/calls_group_toasts.h"
  8. #include "calls/group/calls_group_call.h"
  9. #include "calls/group/calls_group_common.h"
  10. #include "calls/group/calls_group_panel.h"
  11. #include "data/data_peer.h"
  12. #include "data/data_group_call.h"
  13. #include "ui/text/text_utilities.h"
  14. #include "ui/toast/toast.h"
  15. #include "lang/lang_keys.h"
  16. namespace Calls::Group {
  17. namespace {
  18. constexpr auto kErrorDuration = 2 * crl::time(1000);
  19. using State = GroupCall::State;
  20. } // namespace
  21. Toasts::Toasts(not_null<Panel*> panel)
  22. : _panel(panel)
  23. , _call(panel->call()) {
  24. setup();
  25. }
  26. void Toasts::setup() {
  27. setupJoinAsChanged();
  28. setupTitleChanged();
  29. setupRequestedToSpeak();
  30. setupAllowedToSpeak();
  31. setupPinnedVideo();
  32. setupError();
  33. }
  34. void Toasts::setupJoinAsChanged() {
  35. _call->rejoinEvents(
  36. ) | rpl::filter([](RejoinEvent event) {
  37. return (event.wasJoinAs != event.nowJoinAs);
  38. }) | rpl::map([=] {
  39. return _call->stateValue() | rpl::filter([](State state) {
  40. return (state == State::Joined);
  41. }) | rpl::take(1);
  42. }) | rpl::flatten_latest() | rpl::start_with_next([=] {
  43. _panel->showToast((_call->peer()->isBroadcast()
  44. ? tr::lng_group_call_join_as_changed_channel
  45. : tr::lng_group_call_join_as_changed)(
  46. tr::now,
  47. lt_name,
  48. Ui::Text::Bold(_call->joinAs()->name()),
  49. Ui::Text::WithEntities));
  50. }, _lifetime);
  51. }
  52. void Toasts::setupTitleChanged() {
  53. _call->titleChanged(
  54. ) | rpl::filter([=] {
  55. return (_call->lookupReal() != nullptr);
  56. }) | rpl::map([=] {
  57. const auto peer = _call->peer();
  58. return peer->groupCall()->title().isEmpty()
  59. ? peer->name()
  60. : peer->groupCall()->title();
  61. }) | rpl::start_with_next([=](const QString &title) {
  62. _panel->showToast((_call->peer()->isBroadcast()
  63. ? tr::lng_group_call_title_changed_channel
  64. : tr::lng_group_call_title_changed)(
  65. tr::now,
  66. lt_title,
  67. Ui::Text::Bold(title),
  68. Ui::Text::WithEntities));
  69. }, _lifetime);
  70. }
  71. void Toasts::setupAllowedToSpeak() {
  72. _call->allowedToSpeakNotifications(
  73. ) | rpl::start_with_next([=] {
  74. if (_panel->isActive()) {
  75. _panel->showToast(tr::lng_group_call_can_speak_here(tr::now));
  76. } else {
  77. const auto real = _call->lookupReal();
  78. const auto name = (real && !real->title().isEmpty())
  79. ? real->title()
  80. : _call->peer()->name();
  81. Ui::Toast::Show({
  82. .text = tr::lng_group_call_can_speak(
  83. tr::now,
  84. lt_chat,
  85. Ui::Text::Bold(name),
  86. Ui::Text::WithEntities),
  87. });
  88. }
  89. }, _lifetime);
  90. }
  91. void Toasts::setupPinnedVideo() {
  92. _call->videoEndpointPinnedValue(
  93. ) | rpl::map([=](bool pinned) {
  94. return pinned
  95. ? _call->videoEndpointLargeValue()
  96. : rpl::single(_call->videoEndpointLarge());
  97. }) | rpl::flatten_latest(
  98. ) | rpl::filter([=] {
  99. return (_call->shownVideoTracks().size() > 1);
  100. }) | rpl::start_with_next([=](const VideoEndpoint &endpoint) {
  101. const auto pinned = _call->videoEndpointPinned();
  102. const auto peer = endpoint.peer;
  103. if (!peer) {
  104. return;
  105. }
  106. const auto text = [&] {
  107. const auto me = (peer == _call->joinAs());
  108. const auto camera = (endpoint.type == VideoEndpointType::Camera);
  109. if (me) {
  110. const auto key = camera
  111. ? (pinned
  112. ? tr::lng_group_call_pinned_camera_me
  113. : tr::lng_group_call_unpinned_camera_me)
  114. : (pinned
  115. ? tr::lng_group_call_pinned_screen_me
  116. : tr::lng_group_call_unpinned_screen_me);
  117. return key(tr::now);
  118. }
  119. const auto key = camera
  120. ? (pinned
  121. ? tr::lng_group_call_pinned_camera
  122. : tr::lng_group_call_unpinned_camera)
  123. : (pinned
  124. ? tr::lng_group_call_pinned_screen
  125. : tr::lng_group_call_unpinned_screen);
  126. return key(tr::now, lt_user, peer->shortName());
  127. }();
  128. _panel->showToast(text);
  129. }, _lifetime);
  130. }
  131. void Toasts::setupRequestedToSpeak() {
  132. _call->mutedValue(
  133. ) | rpl::combine_previous(
  134. ) | rpl::start_with_next([=](MuteState was, MuteState now) {
  135. if (was == MuteState::ForceMuted && now == MuteState::RaisedHand) {
  136. _panel->showToast(
  137. tr::lng_group_call_tooltip_raised_hand(tr::now));
  138. }
  139. }, _lifetime);
  140. }
  141. void Toasts::setupError() {
  142. _call->errors(
  143. ) | rpl::start_with_next([=](Error error) {
  144. const auto key = [&] {
  145. switch (error) {
  146. case Error::NoCamera: return tr::lng_call_error_no_camera;
  147. case Error::CameraFailed:
  148. return tr::lng_group_call_failed_camera;
  149. case Error::ScreenFailed:
  150. return tr::lng_group_call_failed_screen;
  151. case Error::MutedNoCamera:
  152. return tr::lng_group_call_muted_no_camera;
  153. case Error::MutedNoScreen:
  154. return tr::lng_group_call_muted_no_screen;
  155. case Error::DisabledNoCamera:
  156. return tr::lng_group_call_chat_no_camera;
  157. case Error::DisabledNoScreen:
  158. return tr::lng_group_call_chat_no_screen;
  159. }
  160. Unexpected("Error in Calls::Group::Toasts::setupErrorToasts.");
  161. }();
  162. _panel->showToast({ key(tr::now) }, kErrorDuration);
  163. }, _lifetime);
  164. }
  165. } // namespace Calls::Group