This repository has been archived on 2020-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
FediPotato/lib/src/account_mastodon.cpp

158 lines
4.4 KiB
C++

/* This file is part of FediPotato.
* 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/>.
*/
#include "account_mastodon.hpp"
#include "version.hpp"
#include <boost/chrono.hpp>
#include <mastodonpp/connection.hpp>
#include <nlohmann/detail/exceptions.hpp>
#include <stdexcept>
#include <utility>
#include <iostream> // TODO: remove iostream.
namespace FediPotato
{
using boost::chrono::seconds;
using std::make_unique;
using std::logic_error;
using std::runtime_error;
MastodonAPI::MastodonAPI(const string_view application_name,
const string_view account_name,
const Config &globalconfig)
: _config{application_name, string{account_name} += ".json"}
, _instance{_config.get("instance"), _config.get("access_token")}
{
_instance.set_useragent(((string(application_name)
+= " (FediPotato/") += version) += ')');
try
{
_instance.set_proxy(globalconfig.get("proxy"));
}
catch (nlohmann::detail::type_error &) // Setting not found.
{}
}
void MastodonAPI::start_stream(const stream_type type,
const optional<string_view> id)
{
const string strid{get_stream_id(type, id)};
auto t{make_unique<boost::thread>(MastodonAPI::make_stream_thread,
_instance, type, id)};
_streams.insert({strid, std::move(t)});
}
void MastodonAPI::stop_stream(const stream_type type,
const optional<string_view> id)
{
const string strid{get_stream_id(type, id)};
if (_streams.find(strid) != _streams.end())
{
boost::thread &thread{*_streams[strid]};
if (thread.joinable())
{
thread.interrupt();
thread.join();
}
}
}
string MastodonAPI::get_stream_id(const stream_type type,
const optional<string_view> id)
{
switch (type)
{
case stream_type::home_tl:
{
return "home";
break;
}
case stream_type::instance_tl:
{
return "instance";
break;
}
case stream_type::federated_tl:
{
return "federated";
break;
}
case stream_type::conversations:
{
return "conversations";
break;
}
case stream_type::hashtag:
{
if (!id)
{
throw runtime_error{"No hashtag given."};
}
return string("hashtag_") += id.value();
break;
}
case stream_type::list:
{
if (!id)
{
throw runtime_error{"No list ID given."};
}
return string("list_") += id.value();
break;
}
}
throw logic_error{"Unhandled stream type."};
}
void MastodonAPI::make_stream_thread(mastodonpp::Instance &instance,
stream_type type, optional<string_view> id)
{
mastodonpp::Connection connection{instance};
try
{
// TODO: Limit to 40, initial sleep to 5s.
// TODO: Adjust sleeping time based on number of statuses.
// TODO: use type and id.
mastodonpp::endpoint_variant endpoint
{mastodonpp::API::v1::timelines_public};
mastodonpp::parametermap params{{"limit", "1"}};
while (true)
{
auto answer{connection.get(endpoint, params)};
if (!answer.next().empty())
{
params = answer.next();
}
else
{
std::cout << " OHNO-OHNO-OHNO!\n";
}
std::cout << answer.body.substr(0, 100) << "\n\n";
boost::this_thread::sleep_for(seconds(10)); // Is interruptable.
}
}
catch(const boost::thread_interrupted &)
{
std::cout << "Thread interrupted.\n";
}
}
} // namespace FediPotato