calls_choose_join_as.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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_choose_join_as.h"
  8. #include "calls/group/calls_group_common.h"
  9. #include "calls/group/calls_group_menu.h"
  10. #include "data/data_peer.h"
  11. #include "data/data_user.h"
  12. #include "data/data_channel.h"
  13. #include "data/data_session.h"
  14. #include "data/data_group_call.h"
  15. #include "main/main_session.h"
  16. #include "main/main_account.h"
  17. #include "lang/lang_keys.h"
  18. #include "apiwrap.h"
  19. #include "ui/layers/generic_box.h"
  20. #include "ui/boxes/choose_date_time.h"
  21. #include "ui/text/text_utilities.h"
  22. #include "ui/toast/toast.h"
  23. #include "boxes/peer_list_box.h"
  24. #include "base/unixtime.h"
  25. #include "base/timer_rpl.h"
  26. #include "styles/style_boxes.h"
  27. #include "styles/style_layers.h"
  28. #include "styles/style_calls.h"
  29. namespace Calls::Group {
  30. namespace {
  31. constexpr auto kLabelRefreshInterval = 10 * crl::time(1000);
  32. using Context = ChooseJoinAsProcess::Context;
  33. class ListController : public PeerListController {
  34. public:
  35. ListController(
  36. std::vector<not_null<PeerData*>> list,
  37. not_null<PeerData*> selected);
  38. Main::Session &session() const override;
  39. void prepare() override;
  40. void rowClicked(not_null<PeerListRow*> row) override;
  41. [[nodiscard]] not_null<PeerData*> selected() const;
  42. private:
  43. std::unique_ptr<PeerListRow> createRow(not_null<PeerData*> peer);
  44. std::vector<not_null<PeerData*>> _list;
  45. not_null<PeerData*> _selected;
  46. };
  47. ListController::ListController(
  48. std::vector<not_null<PeerData*>> list,
  49. not_null<PeerData*> selected)
  50. : PeerListController()
  51. , _list(std::move(list))
  52. , _selected(selected) {
  53. }
  54. Main::Session &ListController::session() const {
  55. return _selected->session();
  56. }
  57. std::unique_ptr<PeerListRow> ListController::createRow(
  58. not_null<PeerData*> peer) {
  59. auto result = std::make_unique<PeerListRow>(peer);
  60. if (peer->isSelf()) {
  61. result->setCustomStatus(
  62. tr::lng_group_call_join_as_personal(tr::now));
  63. } else if (const auto channel = peer->asChannel()) {
  64. result->setCustomStatus(
  65. (channel->isMegagroup()
  66. ? tr::lng_chat_status_members
  67. : tr::lng_chat_status_subscribers)(
  68. tr::now,
  69. lt_count,
  70. channel->membersCount()));
  71. }
  72. return result;
  73. }
  74. void ListController::prepare() {
  75. delegate()->peerListSetSearchMode(PeerListSearchMode::Disabled);
  76. for (const auto &peer : _list) {
  77. auto row = createRow(peer);
  78. const auto raw = row.get();
  79. delegate()->peerListAppendRow(std::move(row));
  80. if (peer == _selected) {
  81. delegate()->peerListSetRowChecked(raw, true);
  82. raw->finishCheckedAnimation();
  83. }
  84. }
  85. delegate()->peerListRefreshRows();
  86. }
  87. void ListController::rowClicked(not_null<PeerListRow*> row) {
  88. const auto peer = row->peer();
  89. if (peer == _selected) {
  90. return;
  91. }
  92. const auto previous = delegate()->peerListFindRow(_selected->id.value);
  93. Assert(previous != nullptr);
  94. delegate()->peerListSetRowChecked(previous, false);
  95. delegate()->peerListSetRowChecked(row, true);
  96. _selected = peer;
  97. }
  98. not_null<PeerData*> ListController::selected() const {
  99. return _selected;
  100. }
  101. void ScheduleGroupCallBox(
  102. not_null<Ui::GenericBox*> box,
  103. const JoinInfo &info,
  104. Fn<void(JoinInfo)> done) {
  105. const auto send = [=](TimeId date) {
  106. box->closeBox();
  107. auto copy = info;
  108. copy.scheduleDate = date;
  109. done(std::move(copy));
  110. };
  111. const auto livestream = info.peer->isBroadcast();
  112. const auto duration = box->lifetime().make_state<
  113. rpl::variable<QString>>();
  114. auto description = (info.peer->isBroadcast()
  115. ? tr::lng_group_call_schedule_notified_channel
  116. : tr::lng_group_call_schedule_notified_group)(
  117. lt_duration,
  118. duration->value());
  119. const auto now = QDateTime::currentDateTime();
  120. const auto min = [] {
  121. return base::unixtime::serialize(
  122. QDateTime::currentDateTime().addSecs(12));
  123. };
  124. const auto max = [] {
  125. return base::unixtime::serialize(
  126. QDateTime(QDate::currentDate().addDays(8), QTime(0, 0))) - 1;
  127. };
  128. // At least half an hour later, at zero minutes/seconds.
  129. const auto schedule = QDateTime(
  130. now.date(),
  131. QTime(now.time().hour(), 0)
  132. ).addSecs(60 * 60 * (now.time().minute() < 30 ? 1 : 2));
  133. auto descriptor = Ui::ChooseDateTimeBox(box, {
  134. .title = (livestream
  135. ? tr::lng_group_call_schedule_title_channel()
  136. : tr::lng_group_call_schedule_title()),
  137. .submit = tr::lng_schedule_button(),
  138. .done = send,
  139. .min = min,
  140. .time = base::unixtime::serialize(schedule),
  141. .max = max,
  142. .description = std::move(description),
  143. });
  144. using namespace rpl::mappers;
  145. *duration = rpl::combine(
  146. rpl::single(rpl::empty) | rpl::then(
  147. base::timer_each(kLabelRefreshInterval)
  148. ),
  149. std::move(descriptor.values) | rpl::filter(_1 != 0),
  150. _2
  151. ) | rpl::map([](TimeId date) {
  152. const auto now = base::unixtime::now();
  153. const auto duration = (date - now);
  154. if (duration >= 24 * 60 * 60) {
  155. return tr::lng_days(tr::now, lt_count, duration / (24 * 60 * 60));
  156. } else if (duration >= 60 * 60) {
  157. return tr::lng_hours(tr::now, lt_count, duration / (60 * 60));
  158. }
  159. return tr::lng_minutes(tr::now, lt_count, std::max(duration / 60, 1));
  160. });
  161. }
  162. void ChooseJoinAsBox(
  163. not_null<Ui::GenericBox*> box,
  164. Context context,
  165. JoinInfo info,
  166. Fn<void(JoinInfo)> done) {
  167. box->setWidth(st::groupCallJoinAsWidth);
  168. const auto livestream = info.peer->isBroadcast();
  169. box->setTitle([&] {
  170. switch (context) {
  171. case Context::Create: return livestream
  172. ? tr::lng_group_call_start_as_header_channel()
  173. : tr::lng_group_call_start_as_header();
  174. case Context::Join:
  175. case Context::JoinWithConfirm: return livestream
  176. ? tr::lng_group_call_join_as_header_channel()
  177. : tr::lng_group_call_join_as_header();
  178. case Context::Switch: return tr::lng_group_call_display_as_header();
  179. }
  180. Unexpected("Context in ChooseJoinAsBox.");
  181. }());
  182. const auto &labelSt = (context == Context::Switch)
  183. ? st::groupCallJoinAsLabel
  184. : st::confirmPhoneAboutLabel;
  185. box->addRow(object_ptr<Ui::FlatLabel>(
  186. box,
  187. tr::lng_group_call_join_as_about(),
  188. labelSt));
  189. auto &lifetime = box->lifetime();
  190. const auto delegate = lifetime.make_state<
  191. PeerListContentDelegateSimple
  192. >();
  193. const auto controller = lifetime.make_state<ListController>(
  194. info.possibleJoinAs,
  195. info.joinAs);
  196. if (context == Context::Switch) {
  197. controller->setStyleOverrides(
  198. &st::groupCallJoinAsList,
  199. &st::groupCallMultiSelect);
  200. } else {
  201. controller->setStyleOverrides(
  202. &st::peerListJoinAsList,
  203. nullptr);
  204. }
  205. const auto content = box->addRow(
  206. object_ptr<PeerListContent>(box, controller),
  207. style::margins());
  208. delegate->setContent(content);
  209. controller->setDelegate(delegate);
  210. const auto &peer = info.peer;
  211. if ((context == Context::Create)
  212. && (peer->isChannel() && peer->asChannel()->hasAdminRights())) {
  213. const auto makeLink = [](const QString &text) {
  214. return Ui::Text::Link(text);
  215. };
  216. const auto label = box->addRow(object_ptr<Ui::FlatLabel>(
  217. box,
  218. tr::lng_group_call_or_schedule(
  219. lt_link,
  220. (livestream
  221. ? tr::lng_group_call_schedule_channel
  222. : tr::lng_group_call_schedule)(makeLink),
  223. Ui::Text::WithEntities),
  224. labelSt));
  225. label->overrideLinkClickHandler([=] {
  226. auto withJoinAs = info;
  227. withJoinAs.joinAs = controller->selected();
  228. box->getDelegate()->show(
  229. Box(ScheduleGroupCallBox, withJoinAs, done));
  230. });
  231. }
  232. auto next = (context == Context::Switch)
  233. ? tr::lng_settings_save()
  234. : tr::lng_continue();
  235. box->addButton(std::move(next), [=] {
  236. auto copy = info;
  237. copy.joinAs = controller->selected();
  238. done(std::move(copy));
  239. });
  240. box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
  241. }
  242. [[nodiscard]] TextWithEntities CreateOrJoinConfirmation(
  243. not_null<PeerData*> peer,
  244. ChooseJoinAsProcess::Context context,
  245. bool joinAsAlreadyUsed) {
  246. const auto existing = peer->groupCall();
  247. if (!existing) {
  248. return { peer->isBroadcast()
  249. ? tr::lng_group_call_create_sure_channel(tr::now)
  250. : tr::lng_group_call_create_sure(tr::now) };
  251. }
  252. const auto channel = peer->asChannel();
  253. const auto anonymouseAdmin = channel
  254. && ((channel->isMegagroup() && channel->amAnonymous())
  255. || (channel->isBroadcast()
  256. && (channel->amCreator() || channel->hasAdminRights())));
  257. if (anonymouseAdmin && !joinAsAlreadyUsed) {
  258. return { tr::lng_group_call_join_sure_personal(tr::now) };
  259. } else if (context != ChooseJoinAsProcess::Context::JoinWithConfirm) {
  260. return {};
  261. }
  262. const auto name = !existing->title().isEmpty()
  263. ? existing->title()
  264. : peer->name();
  265. return (peer->isBroadcast()
  266. ? tr::lng_group_call_join_confirm_channel
  267. : tr::lng_group_call_join_confirm)(
  268. tr::now,
  269. lt_chat,
  270. Ui::Text::Bold(name),
  271. Ui::Text::WithEntities);
  272. }
  273. } // namespace
  274. ChooseJoinAsProcess::~ChooseJoinAsProcess() {
  275. if (_request) {
  276. _request->peer->session().api().request(_request->id).cancel();
  277. }
  278. }
  279. void ChooseJoinAsProcess::start(
  280. not_null<PeerData*> peer,
  281. Context context,
  282. std::shared_ptr<Ui::Show> show,
  283. Fn<void(JoinInfo)> done,
  284. PeerData *changingJoinAsFrom) {
  285. Expects(done != nullptr);
  286. const auto isScheduled = (context == Context::CreateScheduled);
  287. const auto session = &peer->session();
  288. if (_request) {
  289. if (_request->peer == peer && !isScheduled) {
  290. _request->context = context;
  291. _request->show = std::move(show);
  292. _request->done = std::move(done);
  293. _request->changingJoinAsFrom = changingJoinAsFrom;
  294. return;
  295. }
  296. session->api().request(_request->id).cancel();
  297. _request = nullptr;
  298. }
  299. const auto createRequest = [=, done = std::move(done)] {
  300. _request = std::make_unique<ChannelsListRequest>(ChannelsListRequest{
  301. .peer = peer,
  302. .show = show,
  303. .done = std::move(done),
  304. .context = context,
  305. .changingJoinAsFrom = changingJoinAsFrom });
  306. };
  307. if (isScheduled) {
  308. auto box = Box(
  309. ScheduleGroupCallBox,
  310. JoinInfo{ .peer = peer, .joinAs = peer },
  311. [=, createRequest = std::move(createRequest)](JoinInfo info) {
  312. createRequest();
  313. finish(info);
  314. });
  315. show->showBox(std::move(box));
  316. return;
  317. }
  318. createRequest();
  319. session->account().sessionChanges(
  320. ) | rpl::start_with_next([=] {
  321. _request = nullptr;
  322. }, _request->lifetime);
  323. requestList();
  324. }
  325. void ChooseJoinAsProcess::requestList() {
  326. const auto session = &_request->peer->session();
  327. _request->id = session->api().request(MTPphone_GetGroupCallJoinAs(
  328. _request->peer->input
  329. )).done([=](const MTPphone_JoinAsPeers &result) {
  330. auto list = result.match([&](const MTPDphone_joinAsPeers &data) {
  331. session->data().processUsers(data.vusers());
  332. session->data().processChats(data.vchats());
  333. const auto &peers = data.vpeers().v;
  334. auto list = std::vector<not_null<PeerData*>>();
  335. list.reserve(peers.size());
  336. for (const auto &peer : peers) {
  337. const auto peerId = peerFromMTP(peer);
  338. if (const auto peer = session->data().peerLoaded(peerId)) {
  339. if (!ranges::contains(list, not_null{ peer })) {
  340. list.push_back(peer);
  341. }
  342. }
  343. }
  344. return list;
  345. });
  346. processList(std::move(list));
  347. }).fail([=] {
  348. finish({
  349. .peer = _request->peer,
  350. .joinAs = _request->peer->session().user(),
  351. });
  352. }).send();
  353. }
  354. void ChooseJoinAsProcess::finish(JoinInfo info) {
  355. const auto done = std::move(_request->done);
  356. const auto box = _request->box;
  357. _request = nullptr;
  358. done(std::move(info));
  359. if (const auto strong = box.data()) {
  360. strong->closeBox();
  361. }
  362. }
  363. void ChooseJoinAsProcess::processList(
  364. std::vector<not_null<PeerData*>> &&list) {
  365. const auto session = &_request->peer->session();
  366. const auto peer = _request->peer;
  367. const auto self = peer->session().user();
  368. auto info = JoinInfo{ .peer = peer, .joinAs = self };
  369. const auto selectedId = peer->groupCallDefaultJoinAs();
  370. if (list.empty()) {
  371. _request->show->showToast(Lang::Hard::ServerError());
  372. return;
  373. }
  374. info.joinAs = [&]() -> not_null<PeerData*> {
  375. const auto loaded = selectedId
  376. ? session->data().peerLoaded(selectedId)
  377. : nullptr;
  378. const auto changingJoinAsFrom = _request->changingJoinAsFrom;
  379. return (changingJoinAsFrom
  380. && ranges::contains(list, not_null{ changingJoinAsFrom }))
  381. ? not_null(changingJoinAsFrom)
  382. : (loaded && ranges::contains(list, not_null{ loaded }))
  383. ? not_null(loaded)
  384. : ranges::contains(list, self)
  385. ? self
  386. : list.front();
  387. }();
  388. info.possibleJoinAs = std::move(list);
  389. const auto onlyByMe = (info.possibleJoinAs.size() == 1)
  390. && (info.possibleJoinAs.front() == self);
  391. // We already joined this voice chat, just rejoin with the same.
  392. const auto byAlreadyUsed = selectedId
  393. && (info.joinAs->id == selectedId)
  394. && (peer->groupCall() != nullptr);
  395. if (!_request->changingJoinAsFrom && (onlyByMe || byAlreadyUsed)) {
  396. auto confirmation = CreateOrJoinConfirmation(
  397. peer,
  398. _request->context,
  399. byAlreadyUsed);
  400. if (confirmation.text.isEmpty()) {
  401. finish(info);
  402. return;
  403. }
  404. const auto livestream = peer->isBroadcast();
  405. const auto creating = !peer->groupCall();
  406. if (creating) {
  407. confirmation
  408. .append("\n\n")
  409. .append(tr::lng_group_call_or_schedule(
  410. tr::now,
  411. lt_link,
  412. Ui::Text::Link((livestream
  413. ? tr::lng_group_call_schedule_channel
  414. : tr::lng_group_call_schedule)(tr::now)),
  415. Ui::Text::WithEntities));
  416. }
  417. const auto guard = base::make_weak(&_request->guard);
  418. const auto safeFinish = crl::guard(guard, [=] { finish(info); });
  419. const auto filter = [=](const auto &...) {
  420. if (guard) {
  421. _request->show->showBox(Box(
  422. ScheduleGroupCallBox,
  423. info,
  424. crl::guard(guard, [=](auto info) { finish(info); })));
  425. }
  426. return false;
  427. };
  428. auto box = Ui::MakeConfirmBox({
  429. .text = confirmation,
  430. .confirmed = crl::guard(guard, [=] { finish(info); }),
  431. .confirmText = (creating
  432. ? tr::lng_create_group_create()
  433. : tr::lng_group_call_join()),
  434. .labelFilter = filter,
  435. });
  436. box->boxClosing(
  437. ) | rpl::start_with_next([=] {
  438. _request = nullptr;
  439. }, _request->lifetime);
  440. _request->box = box.data();
  441. _request->show->showBox(std::move(box));
  442. return;
  443. }
  444. auto box = Box(
  445. ChooseJoinAsBox,
  446. _request->context,
  447. std::move(info),
  448. crl::guard(&_request->guard, [=](auto info) { finish(info); }));
  449. box->boxClosing(
  450. ) | rpl::start_with_next([=] {
  451. _request = nullptr;
  452. }, _request->lifetime);
  453. _request->box = box.data();
  454. _request->show->showBox(std::move(box));
  455. }
  456. } // namespace Calls::Group