Query instance for max_toot_chars.

This commit is contained in:
tastytea 2020-01-05 11:06:50 +01:00
parent 80cec5b45d
commit 61a26dbb67
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 48 additions and 1 deletions

View File

@ -19,12 +19,14 @@
#include "curl_wrapper.hpp"
#include <cstdint>
#include <string>
#include <string_view>
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

View File

@ -15,6 +15,8 @@
*/
#include "instance.hpp"
#include "log.hpp"
#include "return_types.hpp"
#include <utility>
@ -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