api_websites.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_websites.h"
  8. #include "api/api_authorizations.h"
  9. #include "api/api_blocked_peers.h"
  10. #include "apiwrap.h"
  11. #include "data/data_session.h"
  12. #include "data/data_user.h"
  13. #include "main/main_session.h"
  14. namespace Api {
  15. namespace {
  16. [[nodiscard]] Websites::Entry ParseEntry(
  17. not_null<Data::Session*> owner,
  18. const MTPDwebAuthorization &data) {
  19. auto result = Websites::Entry{
  20. .hash = data.vhash().v,
  21. .bot = owner->user(data.vbot_id()),
  22. .platform = qs(data.vplatform()),
  23. .domain = qs(data.vdomain()),
  24. .browser = qs(data.vbrowser()),
  25. .ip = qs(data.vip()),
  26. .location = qs(data.vregion()),
  27. };
  28. result.activeTime = data.vdate_active().v
  29. ? data.vdate_active().v
  30. : data.vdate_created().v;
  31. result.active = Authorizations::ActiveDateString(result.activeTime);
  32. return result;
  33. }
  34. } // namespace
  35. Websites::Websites(not_null<ApiWrap*> api)
  36. : _session(&api->session())
  37. , _api(&api->instance()) {
  38. }
  39. void Websites::reload() {
  40. if (_requestId) {
  41. return;
  42. }
  43. _requestId = _api.request(MTPaccount_GetWebAuthorizations(
  44. )).done([=](const MTPaccount_WebAuthorizations &result) {
  45. _requestId = 0;
  46. _lastReceived = crl::now();
  47. const auto owner = &_session->data();
  48. const auto &data = result.data();
  49. owner->processUsers(data.vusers());
  50. _list = ranges::views::all(
  51. data.vauthorizations().v
  52. ) | ranges::views::transform([&](const MTPwebAuthorization &auth) {
  53. return ParseEntry(owner, auth.data());
  54. }) | ranges::to<List>;
  55. _listChanges.fire({});
  56. }).fail([=] {
  57. _requestId = 0;
  58. }).send();
  59. }
  60. void Websites::cancelCurrentRequest() {
  61. _api.request(base::take(_requestId)).cancel();
  62. }
  63. void Websites::requestTerminate(
  64. Fn<void(const MTPBool &result)> &&done,
  65. Fn<void(const MTP::Error &error)> &&fail,
  66. std::optional<uint64> hash,
  67. UserData *botToBlock) {
  68. const auto send = [&](auto request) {
  69. _api.request(
  70. std::move(request)
  71. ).done([=, done = std::move(done)](const MTPBool &result) {
  72. done(result);
  73. if (hash) {
  74. _list.erase(
  75. ranges::remove(_list, *hash, &Entry::hash),
  76. end(_list));
  77. } else {
  78. _list.clear();
  79. }
  80. _listChanges.fire({});
  81. }).fail(
  82. std::move(fail)
  83. ).send();
  84. };
  85. if (hash) {
  86. send(MTPaccount_ResetWebAuthorization(MTP_long(*hash)));
  87. if (botToBlock) {
  88. botToBlock->session().api().blockedPeers().block(botToBlock);
  89. }
  90. } else {
  91. send(MTPaccount_ResetWebAuthorizations());
  92. }
  93. }
  94. Websites::List Websites::list() const {
  95. return _list;
  96. }
  97. auto Websites::listValue() const
  98. -> rpl::producer<Websites::List> {
  99. return rpl::single(
  100. list()
  101. ) | rpl::then(
  102. _listChanges.events() | rpl::map([=] { return list(); })
  103. );
  104. }
  105. rpl::producer<int> Websites::totalValue() const {
  106. return rpl::single(
  107. total()
  108. ) | rpl::then(
  109. _listChanges.events() | rpl::map([=] { return total(); })
  110. );
  111. }
  112. int Websites::total() const {
  113. return _list.size();
  114. }
  115. crl::time Websites::lastReceivedTime() {
  116. return _lastReceived;
  117. }
  118. } // namespace Api