diff --git a/include/instance.hpp b/include/instance.hpp index 1fa8506..f80e201 100644 --- a/include/instance.hpp +++ b/include/instance.hpp @@ -19,12 +19,14 @@ #include "curl_wrapper.hpp" +#include #include #include namespace mastodonpp { +using std::uint64_t; using std::string; using std::string_view; @@ -41,6 +43,8 @@ public: /*! * @brief Construct a new Instance object. * + * Also queries `/api/v1/instance` for `max_toot_chars'. + * * @param hostname The hostname of the instance. * @param access_token Your access token. * @@ -83,10 +87,22 @@ public: return _access_token; } + /*! + * @brief Returns the maximum number of characters per post. + * + * @since 0.1.0 + */ + [[nodiscard]] + inline uint64_t get_max_chars() const + { + return _max_chars; + } + private: const string _hostname; const string _baseuri; string _access_token; + uint64_t _max_chars; }; } // namespace mastodonpp diff --git a/src/instance.cpp b/src/instance.cpp index c5e9e22..fc56aa8 100644 --- a/src/instance.cpp +++ b/src/instance.cpp @@ -15,6 +15,8 @@ */ #include "instance.hpp" +#include "log.hpp" +#include "return_types.hpp" #include @@ -27,6 +29,35 @@ Instance::Instance(string hostname, string access_token) : _hostname{move(hostname)} , _baseuri{"https://" + _hostname} , _access_token{move(access_token)} -{} + , _max_chars{500} +{ + try + { + const auto answer{make_request(http_method::GET, + _baseuri + "/api/v1/instance")}; + if (answer) + { + debuglog << "Querying instance for max_toot_chars…\n"; + auto &body{answer.body}; + size_t pos_start{body.find("max_toot_chars")}; + if (pos_start == string::npos) + { + debuglog << "max_toot_chars not found."; + return; + } + 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)}; + _max_chars = std::stoull(max_toot_chars); + debuglog << "Set _max_chars to: " << _max_chars << '\n'; + } + } + catch (const std::exception &e) + { + debuglog << "Unexpected exception: " << e.what() << '\n'; + } +} } // namespace mastodonpp