[WIP] Add Mastodon threads.

This commit is contained in:
tastytea 2020-04-18 16:35:41 +02:00
parent 0299f628c8
commit 3ff1e04715
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 90 additions and 14 deletions

View File

@ -19,6 +19,7 @@
:uri-rpm-build: http://www.rpm.org
:uri-clang-tidy: https://clang.llvm.org/extra/clang-tidy/
:uri-qt: https://doc.qt.io/qt-5/gettingstarted.html
:uri-boost: https://www.boost.org/
*{project}* is a client for link:{uri-wp-fediverse}[Fediverse] servers.
// It currently supports link:{uri-wp-mastodon}[Mastodon] and
@ -76,8 +77,9 @@
* C\++ compiler with C++17 support (tested: link:{uri-gcc}[GCC] 7/8/9,
link:{uri-clang}[clang] 6/7)
* link:{uri-cmake}[CMake] (at least: 3.9)
* link:{uri-mastodonpp}[mastodonpp] (at least: 0.5)
* link:{uri-mastodonpp}[mastodonpp] (at least: 0.5.4)
* link:{uri-nlohmann-json}[nlohmann-json] (tested: 3.6)
* link:{uri-boost}[Boost] (at least: 1.62)
* link:{uri-qt}[Qt] (tested: 5.13)
* Optional
** Library documentation: link:{uri-doxygen}[Doxygen] (tested: 1.8)

View File

@ -1,8 +1,8 @@
include(CMakeFindDependencyMacro)
find_dependency(mastodonpp 0.5 REQUIRED CONFIG)
find_dependency(mastodonpp 0.5.4 REQUIRED CONFIG)
find_dependency(Filesystem REQUIRED)
find_dependency(nlohmann_json REQUIRED CONFIG)
find_package(Threads REQUIRED)
find_package(Boost 1.62.0 REQUIRED COMPONENTS chrono thread)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")

View File

@ -19,9 +19,11 @@
#include "config.hpp"
#include <boost/thread/thread.hpp>
#include <mastodonpp/mastodonpp.hpp>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
@ -31,6 +33,7 @@ namespace FediPotato
{
using std::map;
using std::unique_ptr;
using std::optional;
using std::string;
using std::string_view;
@ -136,11 +139,14 @@ public:
private:
Config _config;
map<string,thread> _streams;
mastodonpp::Instance _instance;
map<string,unique_ptr<boost::thread>> _streams;
private:
static string get_stream_id(stream_type type, optional<string_view> id);
static void make_stream_thread(mastodonpp::Instance &instance,
stream_type type, optional<string_view> id);
};
} // namespace FediPotato

View File

@ -21,6 +21,7 @@
#include "config.hpp"
#include "version.hpp"
#include <optional>
#include <string>
#include <string_view>
@ -53,7 +54,11 @@ public:
explicit FediPotato(const string_view application_name)
: _application_name{application_name}
, _config{application_name, "general.json"}
{}
{
MastodonAPI test{account_mastodon("test")};
test.start_stream(MastodonAPI::stream_type::home_tl, std::nullopt);
test.stop_stream(MastodonAPI::stream_type::home_tl, std::nullopt);
}
//! Copy constructor
FediPotato(const FediPotato &other) = default;

View File

@ -5,7 +5,9 @@ includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
Name: ${name}
Description: @PROJECT_DESCRIPTION@
URL: @PROJECT_HOMEPAGE_URL@
Version: @PROJECT_VERSION@
Cflags: -I${includedir}
Libs: -L${libdir} -l${name}
Requires: mastodonpp
Cflags: -I${includedir}
Libs: -L${libdir} -l${name} -lboost_thread
Libs.private: -lboost_chrono

View File

@ -1,9 +1,9 @@
include(GNUInstallDirs)
find_package(mastodonpp 0.5 REQUIRED CONFIG)
find_package(mastodonpp 0.5.4 REQUIRED CONFIG)
find_package(Filesystem REQUIRED)
find_package(nlohmann_json REQUIRED CONFIG)
find_package(Threads REQUIRED)
find_package(Boost 1.62.0 REQUIRED COMPONENTS chrono thread)
add_library(${PROJECT_NAME})
@ -17,8 +17,12 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR})
target_link_libraries(${PROJECT_NAME}
PRIVATE mastodonpp::mastodonpp Threads::Threads
PUBLIC std::filesystem nlohmann_json::nlohmann_json)
PUBLIC
std::filesystem
nlohmann_json::nlohmann_json
mastodonpp::mastodonpp
Boost::chrono
Boost::thread)
install(TARGETS ${PROJECT_NAME}
EXPORT "${PROJECT_NAME}Targets"

View File

@ -17,12 +17,19 @@
#include "account_mastodon.hpp"
#include "version.hpp"
#include <exception>
#include <stdexcept>
#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;
@ -34,19 +41,36 @@ MastodonAPI::MastodonAPI(const string_view application_name,
{
_instance.set_useragent(((string(application_name)
+= " (FediPotato/") += version) += ')');
_instance.set_proxy(globalconfig.get("proxy"));
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,
@ -97,4 +121,37 @@ string MastodonAPI::get_stream_id(const stream_type type,
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