Query max_toot_chars the first time get_max_chars() is called.

This commit is contained in:
tastytea 2020-01-06 09:31:05 +01:00
parent 6663cca653
commit 0638f8fe9a
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 39 additions and 31 deletions

View File

@ -94,10 +94,7 @@ public:
* @since 0.1.0 * @since 0.1.0
*/ */
[[nodiscard]] [[nodiscard]]
inline uint64_t get_max_chars() const uint64_t get_max_chars();
{
return _max_chars;
}
private: private:
const string _hostname; const string _hostname;

View File

@ -27,8 +27,15 @@ Instance::Instance(const string_view &hostname, const string_view &access_token)
: _hostname{hostname} : _hostname{hostname}
, _baseuri{"https://" + _hostname} , _baseuri{"https://" + _hostname}
, _access_token{access_token} , _access_token{access_token}
, _max_chars{500} , _max_chars{0}
{}
uint64_t Instance::get_max_chars()
{ {
constexpr uint64_t default_max_chars{500};
if (_max_chars == 0)
{
try try
{ {
debuglog << "Querying " << _hostname << " for max_toot_chars…\n"; debuglog << "Querying " << _hostname << " for max_toot_chars…\n";
@ -36,7 +43,8 @@ Instance::Instance(const string_view &hostname, const string_view &access_token)
_baseuri + "/api/v1/instance")}; _baseuri + "/api/v1/instance")};
if (!answer) if (!answer)
{ {
return; debuglog << "Could not get instance info.\n";
return default_max_chars;
} }
_max_chars = [&answer] _max_chars = [&answer]
@ -46,7 +54,7 @@ Instance::Instance(const string_view &hostname, const string_view &access_token)
if (pos_start == string::npos) if (pos_start == string::npos)
{ {
debuglog << "max_toot_chars not found.\n"; debuglog << "max_toot_chars not found.\n";
return static_cast<uint64_t>(500); return default_max_chars;
} }
pos_start = body.find(':', pos_start) + 1; pos_start = body.find(':', pos_start) + 1;
const size_t pos_end{body.find(',', pos_start)}; const size_t pos_end{body.find(',', pos_start)};
@ -61,6 +69,9 @@ Instance::Instance(const string_view &hostname, const string_view &access_token)
{ {
debuglog << "Unexpected exception: " << e.what() << '\n'; debuglog << "Unexpected exception: " << e.what() << '\n';
} }
}
return _max_chars;
} }
} // namespace mastodonpp } // namespace mastodonpp