menu_ttl_validator.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "menu/menu_ttl_validator.h"
  8. #include "apiwrap.h"
  9. #include "data/data_channel.h"
  10. #include "data/data_chat.h"
  11. #include "data/data_peer.h"
  12. #include "data/data_user.h"
  13. #include "lang/lang_keys.h"
  14. #include "main/main_session.h"
  15. #include "menu/menu_ttl.h"
  16. #include "ui/layers/generic_box.h"
  17. #include "ui/layers/show.h"
  18. #include "ui/text/text_utilities.h"
  19. #include "ui/toast/toast.h"
  20. #include "ui/text/format_values.h"
  21. #include "styles/style_chat.h"
  22. #include "styles/style_menu_icons.h"
  23. namespace TTLMenu {
  24. namespace {
  25. constexpr auto kToastDuration = crl::time(3500);
  26. void ShowAutoDeleteToast(
  27. std::shared_ptr<Ui::Show> show,
  28. not_null<PeerData*> peer) {
  29. const auto period = peer->messagesTTL();
  30. if (!period) {
  31. show->showToast(tr::lng_ttl_about_tooltip_off(tr::now));
  32. return;
  33. }
  34. const auto duration = (period == 5)
  35. ? u"5 seconds"_q
  36. : Ui::FormatTTL(period);
  37. const auto text = peer->isBroadcast()
  38. ? tr::lng_ttl_about_tooltip_channel(tr::now, lt_duration, duration)
  39. : tr::lng_ttl_about_tooltip(tr::now, lt_duration, duration);
  40. show->showToast(text, kToastDuration);
  41. }
  42. } // namespace
  43. TTLValidator::TTLValidator(
  44. std::shared_ptr<Ui::Show> show,
  45. not_null<PeerData*> peer)
  46. : _peer(peer)
  47. , _show(std::move(show)) {
  48. }
  49. Args TTLValidator::createArgs() const {
  50. const auto peer = _peer;
  51. const auto show = _show;
  52. struct State {
  53. TimeId savingPeriod = 0;
  54. mtpRequestId savingRequestId = 0;
  55. };
  56. const auto state = std::make_shared<State>();
  57. auto callback = [=](
  58. TimeId period,
  59. Fn<void()>) {
  60. auto &api = peer->session().api();
  61. if (state->savingRequestId) {
  62. if (period == state->savingPeriod) {
  63. return;
  64. }
  65. api.request(state->savingRequestId).cancel();
  66. }
  67. state->savingPeriod = period;
  68. state->savingRequestId = api.request(MTPmessages_SetHistoryTTL(
  69. peer->input,
  70. MTP_int(period)
  71. )).done([=](const MTPUpdates &result) {
  72. peer->session().api().applyUpdates(result);
  73. ShowAutoDeleteToast(show, peer);
  74. state->savingRequestId = 0;
  75. }).fail([=] {
  76. state->savingRequestId = 0;
  77. }).send();
  78. show->hideLayer();
  79. };
  80. auto about1 = peer->isUser()
  81. ? tr::lng_ttl_edit_about(lt_user, rpl::single(peer->shortName()))
  82. : peer->isBroadcast()
  83. ? tr::lng_ttl_edit_about_channel()
  84. : tr::lng_ttl_edit_about_group();
  85. auto about2 = tr::lng_ttl_edit_about2(
  86. lt_link,
  87. tr::lng_ttl_edit_about2_link(
  88. ) | rpl::map([=](const QString &s) {
  89. return Ui::Text::Link(s, "tg://settings/auto_delete");
  90. }),
  91. Ui::Text::WithEntities);
  92. auto about = rpl::combine(
  93. std::move(about1),
  94. std::move(about2)
  95. ) | rpl::map([](const QString &s1, TextWithEntities &&s2) {
  96. return TextWithEntities{ s1 }.append(u"\n\n"_q).append(std::move(s2));
  97. });
  98. const auto ttl = peer->messagesTTL();
  99. return { std::move(show), ttl, std::move(about), std::move(callback) };
  100. }
  101. bool TTLValidator::can() const {
  102. return (_peer->isUser()
  103. && !_peer->isSelf()
  104. && !_peer->isNotificationsUser()
  105. && !_peer->asUser()->isInaccessible()
  106. && !_peer->asUser()->starsPerMessage()
  107. && (!_peer->asUser()->requiresPremiumToWrite()
  108. || _peer->session().premium()))
  109. || (_peer->isChat()
  110. && _peer->asChat()->canEditInformation()
  111. && _peer->asChat()->amIn())
  112. || (_peer->isChannel()
  113. && _peer->asChannel()->canEditInformation()
  114. && _peer->asChannel()->amIn());
  115. }
  116. void TTLValidator::showToast() const {
  117. ShowAutoDeleteToast(_show, _peer);
  118. }
  119. const style::icon *TTLValidator::icon() const {
  120. return &st::menuIconTTL;
  121. }
  122. void TTLValidator::showBox() const {
  123. _show->showBox(Box(TTLBox, createArgs()));
  124. }
  125. } // namespace TTLMenu