api_sensitive_content.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_sensitive_content.h"
  8. #include "apiwrap.h"
  9. #include "main/main_session.h"
  10. #include "main/main_app_config.h"
  11. namespace Api {
  12. namespace {
  13. constexpr auto kRefreshAppConfigTimeout = crl::time(1);
  14. } // namespace
  15. SensitiveContent::SensitiveContent(not_null<ApiWrap*> api)
  16. : _session(&api->session())
  17. , _api(&api->instance())
  18. , _appConfigReloadTimer([=] { _session->appConfig().refresh(); }) {
  19. }
  20. void SensitiveContent::preload() {
  21. if (!_loaded) {
  22. reload();
  23. }
  24. }
  25. void SensitiveContent::reload(bool force) {
  26. if (_loadRequestId) {
  27. if (force) {
  28. _loadPending = true;
  29. }
  30. return;
  31. }
  32. _loaded = true;
  33. _loadRequestId = _api.request(MTPaccount_GetContentSettings(
  34. )).done([=](const MTPaccount_ContentSettings &result) {
  35. _loadRequestId = 0;
  36. const auto &data = result.data();
  37. const auto enabled = data.is_sensitive_enabled();
  38. const auto canChange = data.is_sensitive_can_change();
  39. const auto changed = (_enabled.current() != enabled)
  40. || (_canChange.current() != canChange);
  41. if (changed) {
  42. _enabled = enabled;
  43. _canChange = canChange;
  44. }
  45. if (base::take(_appConfigReloadForce) || changed) {
  46. _appConfigReloadTimer.callOnce(kRefreshAppConfigTimeout);
  47. }
  48. if (base::take(_loadPending)) {
  49. reload();
  50. }
  51. }).fail([=] {
  52. _loadRequestId = 0;
  53. }).send();
  54. }
  55. bool SensitiveContent::enabledCurrent() const {
  56. return _enabled.current();
  57. }
  58. rpl::producer<bool> SensitiveContent::enabled() const {
  59. return _enabled.value();
  60. }
  61. rpl::producer<bool> SensitiveContent::canChange() const {
  62. return _canChange.value();
  63. }
  64. void SensitiveContent::update(bool enabled) {
  65. if (!_canChange.current()) {
  66. return;
  67. }
  68. using Flag = MTPaccount_SetContentSettings::Flag;
  69. _api.request(_saveRequestId).cancel();
  70. if (const auto load = base::take(_loadRequestId)) {
  71. _api.request(load).cancel();
  72. _loadPending = true;
  73. }
  74. const auto finish = [=] {
  75. _saveRequestId = 0;
  76. if (base::take(_loadPending)) {
  77. _appConfigReloadForce = true;
  78. reload(true);
  79. } else {
  80. _appConfigReloadTimer.callOnce(kRefreshAppConfigTimeout);
  81. }
  82. };
  83. _saveRequestId = _api.request(MTPaccount_SetContentSettings(
  84. MTP_flags(enabled ? Flag::f_sensitive_enabled : Flag(0))
  85. )).done(finish).fail(finish).send();
  86. _enabled = enabled;
  87. }
  88. } // namespace Api