main_app_config.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "main/main_app_config.h"
  8. #include "apiwrap.h"
  9. #include "base/call_delayed.h"
  10. #include "main/main_account.h"
  11. #include "ui/chat/chat_style.h"
  12. namespace Main {
  13. namespace {
  14. constexpr auto kRefreshTimeout = 3600 * crl::time(1000);
  15. } // namespace
  16. AppConfig::AppConfig(not_null<Account*> account) : _account(account) {
  17. account->sessionChanges(
  18. ) | rpl::filter([=](Session *session) {
  19. return (session != nullptr);
  20. }) | rpl::start_with_next([=] {
  21. refresh();
  22. }, _lifetime);
  23. }
  24. AppConfig::~AppConfig() = default;
  25. void AppConfig::start() {
  26. _account->mtpMainSessionValue(
  27. ) | rpl::start_with_next([=](not_null<MTP::Instance*> instance) {
  28. _api.emplace(instance);
  29. refresh();
  30. }, _lifetime);
  31. }
  32. int AppConfig::quoteLengthMax() const {
  33. return get<int>(u"quote_length_max"_q, 1024);
  34. }
  35. int AppConfig::stargiftConvertPeriodMax() const {
  36. return get<int>(
  37. u"stargifts_convert_period_max"_q,
  38. _account->mtp().isTestMode() ? 300 : (90 * 86400));
  39. }
  40. const std::vector<QString> &AppConfig::startRefPrefixes() {
  41. if (_startRefPrefixes.empty()) {
  42. _startRefPrefixes = get<std::vector<QString>>(
  43. u"starref_start_param_prefixes"_q,
  44. std::vector<QString>());
  45. }
  46. return _startRefPrefixes;
  47. }
  48. bool AppConfig::starrefSetupAllowed() const {
  49. return get<bool>(u"starref_program_allowed"_q, false);
  50. }
  51. bool AppConfig::starrefJoinAllowed() const {
  52. return get<bool>(u"starref_connect_allowed"_q, false);
  53. }
  54. int AppConfig::starrefCommissionMin() const {
  55. return get<int>(u"starref_min_commission_permille"_q, 1);
  56. }
  57. int AppConfig::starrefCommissionMax() const {
  58. return get<int>(u"starref_max_commission_permille"_q, 900);
  59. }
  60. float64 AppConfig::starsWithdrawRate() const {
  61. return get<float64>(u"stars_usd_withdraw_rate_x1000"_q, 1300) / 1000.;
  62. }
  63. bool AppConfig::paidMessagesAvailable() const {
  64. return get<bool>(u"stars_paid_messages_available"_q, false);
  65. }
  66. int AppConfig::paidMessageStarsMax() const {
  67. return get<int>(u"stars_paid_message_amount_max"_q, 10'000);
  68. }
  69. int AppConfig::paidMessageCommission() const {
  70. return get<int>(u"stars_paid_message_commission_permille"_q, 850);
  71. }
  72. int AppConfig::pinnedGiftsLimit() const {
  73. return get<int>(u"stargifts_pinned_to_top_limit"_q, 6);
  74. }
  75. void AppConfig::refresh(bool force) {
  76. if (_requestId || !_api) {
  77. if (force) {
  78. _pendingRefresh = true;
  79. }
  80. return;
  81. }
  82. _pendingRefresh = false;
  83. _requestId = _api->request(MTPhelp_GetAppConfig(
  84. MTP_int(_hash)
  85. )).done([=](const MTPhelp_AppConfig &result) {
  86. _requestId = 0;
  87. result.match([&](const MTPDhelp_appConfig &data) {
  88. _hash = data.vhash().v;
  89. const auto &config = data.vconfig();
  90. if (config.type() != mtpc_jsonObject) {
  91. LOG(("API Error: Unexpected config type."));
  92. return;
  93. }
  94. auto was = ignoredRestrictionReasons();
  95. _data.clear();
  96. for (const auto &element : config.c_jsonObject().vvalue().v) {
  97. element.match([&](const MTPDjsonObjectValue &data) {
  98. _data.emplace_or_assign(qs(data.vkey()), data.vvalue());
  99. });
  100. }
  101. updateIgnoredRestrictionReasons(std::move(was));
  102. DEBUG_LOG(("getAppConfig result handled."));
  103. _refreshed.fire({});
  104. }, [](const MTPDhelp_appConfigNotModified &) {});
  105. if (base::take(_pendingRefresh)) {
  106. refresh();
  107. } else {
  108. refreshDelayed();
  109. }
  110. }).fail([=] {
  111. _requestId = 0;
  112. refreshDelayed();
  113. }).send();
  114. }
  115. void AppConfig::refreshDelayed() {
  116. base::call_delayed(kRefreshTimeout, _account, [=] {
  117. refresh();
  118. });
  119. }
  120. void AppConfig::updateIgnoredRestrictionReasons(std::vector<QString> was) {
  121. _ignoreRestrictionReasons = get<std::vector<QString>>(
  122. u"ignore_restriction_reasons"_q,
  123. std::vector<QString>());
  124. ranges::sort(_ignoreRestrictionReasons);
  125. if (_ignoreRestrictionReasons != was) {
  126. for (const auto &reason : _ignoreRestrictionReasons) {
  127. const auto i = ranges::remove(was, reason);
  128. if (i != end(was)) {
  129. was.erase(i, end(was));
  130. } else {
  131. was.push_back(reason);
  132. }
  133. }
  134. _ignoreRestrictionChanges.fire(std::move(was));
  135. }
  136. }
  137. rpl::producer<> AppConfig::refreshed() const {
  138. return _refreshed.events();
  139. }
  140. rpl::producer<> AppConfig::value() const {
  141. return _refreshed.events_starting_with({});
  142. }
  143. template <typename Extractor>
  144. auto AppConfig::getValue(const QString &key, Extractor &&extractor) const {
  145. const auto i = _data.find(key);
  146. return extractor((i != end(_data))
  147. ? i->second
  148. : MTPJSONValue(MTP_jsonNull()));
  149. }
  150. bool AppConfig::getBool(const QString &key, bool fallback) const {
  151. return getValue(key, [&](const MTPJSONValue &value) {
  152. return value.match([&](const MTPDjsonBool &data) {
  153. return mtpIsTrue(data.vvalue());
  154. }, [&](const auto &data) {
  155. return fallback;
  156. });
  157. });
  158. }
  159. double AppConfig::getDouble(const QString &key, double fallback) const {
  160. return getValue(key, [&](const MTPJSONValue &value) {
  161. return value.match([&](const MTPDjsonNumber &data) {
  162. return data.vvalue().v;
  163. }, [&](const auto &data) {
  164. return fallback;
  165. });
  166. });
  167. }
  168. QString AppConfig::getString(
  169. const QString &key,
  170. const QString &fallback) const {
  171. return getValue(key, [&](const MTPJSONValue &value) {
  172. return value.match([&](const MTPDjsonString &data) {
  173. return qs(data.vvalue());
  174. }, [&](const auto &data) {
  175. return fallback;
  176. });
  177. });
  178. }
  179. std::vector<QString> AppConfig::getStringArray(
  180. const QString &key,
  181. std::vector<QString> &&fallback) const {
  182. return getValue(key, [&](const MTPJSONValue &value) {
  183. return value.match([&](const MTPDjsonArray &data) {
  184. auto result = std::vector<QString>();
  185. result.reserve(data.vvalue().v.size());
  186. for (const auto &entry : data.vvalue().v) {
  187. if (entry.type() != mtpc_jsonString) {
  188. return std::move(fallback);
  189. }
  190. result.push_back(qs(entry.c_jsonString().vvalue()));
  191. }
  192. return result;
  193. }, [&](const auto &data) {
  194. return std::move(fallback);
  195. });
  196. });
  197. }
  198. base::flat_map<QString, QString> AppConfig::getStringMap(
  199. const QString &key,
  200. base::flat_map<QString, QString> &&fallback) const {
  201. return getValue(key, [&](const MTPJSONValue &value) {
  202. return value.match([&](const MTPDjsonObject &data) {
  203. auto result = base::flat_map<QString, QString>();
  204. result.reserve(data.vvalue().v.size());
  205. for (const auto &entry : data.vvalue().v) {
  206. const auto &data = entry.data();
  207. const auto &value = data.vvalue();
  208. if (value.type() != mtpc_jsonString) {
  209. return std::move(fallback);
  210. }
  211. result.emplace(
  212. qs(data.vkey()),
  213. qs(value.c_jsonString().vvalue()));
  214. }
  215. return result;
  216. }, [&](const auto &data) {
  217. return std::move(fallback);
  218. });
  219. });
  220. }
  221. std::vector<int> AppConfig::getIntArray(
  222. const QString &key,
  223. std::vector<int> &&fallback) const {
  224. return getValue(key, [&](const MTPJSONValue &value) {
  225. return value.match([&](const MTPDjsonArray &data) {
  226. auto result = std::vector<int>();
  227. result.reserve(data.vvalue().v.size());
  228. for (const auto &entry : data.vvalue().v) {
  229. if (entry.type() != mtpc_jsonNumber) {
  230. return std::move(fallback);
  231. }
  232. result.push_back(
  233. int(base::SafeRound(entry.c_jsonNumber().vvalue().v)));
  234. }
  235. return result;
  236. }, [&](const auto &data) {
  237. return std::move(fallback);
  238. });
  239. });
  240. }
  241. bool AppConfig::suggestionCurrent(const QString &key) const {
  242. return !_dismissedSuggestions.contains(key)
  243. && ranges::contains(
  244. get<std::vector<QString>>(
  245. u"pending_suggestions"_q,
  246. std::vector<QString>()),
  247. key);
  248. }
  249. rpl::producer<> AppConfig::suggestionRequested(const QString &key) const {
  250. return value(
  251. ) | rpl::filter([=] {
  252. return suggestionCurrent(key);
  253. });
  254. }
  255. void AppConfig::dismissSuggestion(const QString &key) {
  256. Expects(_api.has_value());
  257. if (!_dismissedSuggestions.emplace(key).second) {
  258. return;
  259. }
  260. _api->request(MTPhelp_DismissSuggestion(
  261. MTP_inputPeerEmpty(),
  262. MTP_string(key)
  263. )).send();
  264. }
  265. bool AppConfig::newRequirePremiumFree() const {
  266. return get<bool>(
  267. u"new_noncontact_peers_require_premium_without_ownpremium"_q,
  268. false);
  269. }
  270. } // namespace Main