diff --git a/include/instance.hpp b/include/instance.hpp index 17425cd..914a470 100644 --- a/include/instance.hpp +++ b/include/instance.hpp @@ -94,10 +94,7 @@ public: * @since 0.1.0 */ [[nodiscard]] - inline uint64_t get_max_chars() const - { - return _max_chars; - } + uint64_t get_max_chars(); private: const string _hostname; diff --git a/src/instance.cpp b/src/instance.cpp index c765345..b367f9b 100644 --- a/src/instance.cpp +++ b/src/instance.cpp @@ -27,40 +27,51 @@ Instance::Instance(const string_view &hostname, const string_view &access_token) : _hostname{hostname} , _baseuri{"https://" + _hostname} , _access_token{access_token} - , _max_chars{500} + , _max_chars{0} +{} + +uint64_t Instance::get_max_chars() { - try - { - debuglog << "Querying " << _hostname << " for max_toot_chars…\n"; - const auto answer{make_request(http_method::GET, - _baseuri + "/api/v1/instance")}; - if (!answer) - { - return; - } + constexpr uint64_t default_max_chars{500}; - _max_chars = [&answer] + if (_max_chars == 0) + { + try { - auto &body{answer.body}; - size_t pos_start{body.find("max_toot_chars")}; - if (pos_start == string::npos) + debuglog << "Querying " << _hostname << " for max_toot_chars…\n"; + const auto answer{make_request(http_method::GET, + _baseuri + "/api/v1/instance")}; + if (!answer) { - debuglog << "max_toot_chars not found.\n"; - return static_cast(500); + debuglog << "Could not get instance info.\n"; + return default_max_chars; } - pos_start = body.find(':', pos_start) + 1; - const size_t pos_end{body.find(',', pos_start)}; - const auto max_toot_chars{body.substr(pos_start, - pos_end - pos_start)}; - return static_cast(stoull(max_toot_chars)); - }(); - debuglog << "Set _max_chars to: " << _max_chars << '\n'; - } - catch (const std::exception &e) - { - debuglog << "Unexpected exception: " << e.what() << '\n'; + _max_chars = [&answer] + { + auto &body{answer.body}; + size_t pos_start{body.find("max_toot_chars")}; + if (pos_start == string::npos) + { + debuglog << "max_toot_chars not found.\n"; + return default_max_chars; + } + pos_start = body.find(':', pos_start) + 1; + const size_t pos_end{body.find(',', pos_start)}; + + const auto max_toot_chars{body.substr(pos_start, + pos_end - pos_start)}; + return static_cast(stoull(max_toot_chars)); + }(); + debuglog << "Set _max_chars to: " << _max_chars << '\n'; + } + catch (const std::exception &e) + { + debuglog << "Unexpected exception: " << e.what() << '\n'; + } } + + return _max_chars; } } // namespace mastodonpp