2020-01-03 12:42:10 +01:00
|
|
|
/* This file is part of mastodonpp.
|
|
|
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-01-06 14:43:10 +01:00
|
|
|
#include "answer.hpp"
|
2020-01-03 12:42:10 +01:00
|
|
|
#include "instance.hpp"
|
2020-01-05 11:06:50 +01:00
|
|
|
#include "log.hpp"
|
2020-01-03 12:42:10 +01:00
|
|
|
|
2020-01-11 19:21:34 +01:00
|
|
|
#include <algorithm>
|
2020-01-11 18:13:15 +01:00
|
|
|
#include <exception>
|
2020-01-11 19:21:34 +01:00
|
|
|
#include <vector>
|
2020-01-11 18:13:15 +01:00
|
|
|
|
2020-01-03 12:42:10 +01:00
|
|
|
namespace mastodonpp
|
|
|
|
{
|
|
|
|
|
2020-01-11 19:21:34 +01:00
|
|
|
using std::sort;
|
2020-01-05 20:10:54 +01:00
|
|
|
using std::stoull;
|
2020-01-11 18:13:15 +01:00
|
|
|
using std::exception;
|
2020-01-11 19:21:34 +01:00
|
|
|
using std::vector;
|
2020-01-03 12:42:10 +01:00
|
|
|
|
2020-01-08 16:46:27 +01:00
|
|
|
Instance::Instance(const string_view hostname, const string_view access_token)
|
2020-01-06 09:11:32 +01:00
|
|
|
: _hostname{hostname}
|
2020-01-05 10:33:54 +01:00
|
|
|
, _baseuri{"https://" + _hostname}
|
2020-01-06 09:11:32 +01:00
|
|
|
, _access_token{access_token}
|
2020-01-06 09:31:05 +01:00
|
|
|
, _max_chars{0}
|
|
|
|
{}
|
|
|
|
|
|
|
|
uint64_t Instance::get_max_chars()
|
2020-01-05 11:06:50 +01:00
|
|
|
{
|
2020-01-06 09:31:05 +01:00
|
|
|
constexpr uint64_t default_max_chars{500};
|
2020-01-05 20:47:34 +01:00
|
|
|
|
2020-01-06 09:31:05 +01:00
|
|
|
if (_max_chars == 0)
|
|
|
|
{
|
|
|
|
try
|
2020-01-05 20:47:34 +01:00
|
|
|
{
|
2020-01-06 09:31:05 +01:00
|
|
|
debuglog << "Querying " << _hostname << " for max_toot_chars…\n";
|
|
|
|
const auto answer{make_request(http_method::GET,
|
2020-01-08 12:56:16 +01:00
|
|
|
_baseuri + "/api/v1/instance", {})};
|
2020-01-06 09:31:05 +01:00
|
|
|
if (!answer)
|
2020-01-05 11:06:50 +01:00
|
|
|
{
|
2020-01-06 09:31:05 +01:00
|
|
|
debuglog << "Could not get instance info.\n";
|
|
|
|
return default_max_chars;
|
2020-01-05 20:47:34 +01:00
|
|
|
}
|
2020-01-05 11:06:50 +01:00
|
|
|
|
2020-01-06 09:31:05 +01:00
|
|
|
_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<uint64_t>(stoull(max_toot_chars));
|
|
|
|
}();
|
|
|
|
debuglog << "Set _max_chars to: " << _max_chars << '\n';
|
|
|
|
}
|
2020-01-11 18:13:15 +01:00
|
|
|
catch (const exception &e)
|
2020-01-06 09:31:05 +01:00
|
|
|
{
|
|
|
|
debuglog << "Unexpected exception: " << e.what() << '\n';
|
|
|
|
}
|
2020-01-05 11:06:50 +01:00
|
|
|
}
|
2020-01-06 09:31:05 +01:00
|
|
|
|
|
|
|
return _max_chars;
|
2020-01-05 11:06:50 +01:00
|
|
|
}
|
2020-01-03 12:42:10 +01:00
|
|
|
|
2020-01-11 19:21:34 +01:00
|
|
|
answer_type Instance::get_nodeinfo()
|
|
|
|
{
|
|
|
|
debuglog << "Finding location of NodeInfo on " << _hostname << "…\n";
|
|
|
|
auto answer{make_request(http_method::GET,
|
|
|
|
_baseuri + "/.well-known/nodeinfo", {})};
|
|
|
|
if (!answer)
|
|
|
|
{
|
|
|
|
debuglog << "NodeInfo not found.\n";
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t pos{0};
|
|
|
|
vector<string> hrefs;
|
|
|
|
constexpr string_view searchstring{R"("href":")"};
|
|
|
|
while ((pos = answer.body.find(searchstring, pos)) != string::npos)
|
|
|
|
{
|
|
|
|
pos += searchstring.size();
|
|
|
|
auto endpos{answer.body.find('"', pos)};
|
|
|
|
hrefs.push_back(answer.body.substr(pos, endpos - pos));
|
|
|
|
debuglog << "Found href: " << hrefs.back() << '\n';
|
|
|
|
}
|
|
|
|
sort(hrefs.begin(), hrefs.end()); // We assume they are sortable strings.
|
|
|
|
debuglog << "Selecting href: " << hrefs.back() << '\n';
|
|
|
|
|
|
|
|
return make_request(http_method::GET, hrefs.back(), {});
|
|
|
|
}
|
|
|
|
|
2020-01-03 12:42:10 +01:00
|
|
|
} // namespace mastodonpp
|