155 lines
4.7 KiB
C++
155 lines
4.7 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/>.
|
|
*/
|
|
|
|
#ifndef FEDIPOTATO_ACCOUNT_MASTODON_HPP
|
|
#define FEDIPOTATO_ACCOUNT_MASTODON_HPP
|
|
|
|
#include "config.hpp"
|
|
|
|
#include <boost/thread/thread.hpp>
|
|
#include <mastodonpp/mastodonpp.hpp>
|
|
|
|
#include <map>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <thread>
|
|
|
|
namespace FediPotato
|
|
{
|
|
|
|
using std::map;
|
|
using std::unique_ptr;
|
|
using std::optional;
|
|
using std::string;
|
|
using std::string_view;
|
|
using std::thread;
|
|
|
|
/*!
|
|
* @brief Interact with an account using the Mastodon API.
|
|
*
|
|
* @throw std::runtime_error No hashtag or list ID given.
|
|
* @throw std::logic_error Unhandled stream type. (Should never happen)
|
|
* @throw std::runtime_error Directory not found.
|
|
* @throw std::filesystem::filesystem_error Could not create directory.
|
|
* @throw std::ifstream::failure Could not read from file.
|
|
* @throw nlohmann::detail::parse_error Could not parse file.
|
|
* @throw nlohmann::detail::type_error Setting not found / Wrong type.
|
|
* @throw mastodonpp::CURLException libcurl error from
|
|
* [mastodonpp](https://doc.schlomp.space/mastodonpp/).
|
|
*
|
|
* @since 0.1.0
|
|
*
|
|
* @headerfile account_mastodon.hpp FediPotato/account_mastodon.hpp
|
|
*/
|
|
class MastodonAPI
|
|
{
|
|
public:
|
|
/*!
|
|
* @brief The type of stream.
|
|
*
|
|
* @since 0.1.0
|
|
*/
|
|
enum class stream_type
|
|
{
|
|
home_tl,
|
|
instance_tl,
|
|
federated_tl,
|
|
conversations,
|
|
hashtag,
|
|
list
|
|
};
|
|
|
|
public:
|
|
/*!
|
|
* @brief Construct a MastodonAPI object.
|
|
*
|
|
* For possible exceptions see Directories and Config.
|
|
*
|
|
* @throw std::runtime_error Directory not found.
|
|
* @throw std::filesystem::filesystem_error Could not create directory.
|
|
* @throw std::ifstream::failure Could not read from file.
|
|
* @throw nlohmann::detail::parse_error Could not parse file.
|
|
* @throw nlohmann::detail::type_error Setting not found / Wrong type.
|
|
* @throw mastodonpp::CURLException libcurl error from
|
|
* [mastodonpp](https://doc.schlomp.space/mastodonpp/).
|
|
*
|
|
* @since 0.1.0
|
|
*/
|
|
MastodonAPI(string_view application_name, string_view account_name,
|
|
const Config &globalconfig);
|
|
|
|
//! Copy constructor
|
|
MastodonAPI(const MastodonAPI &other) = delete;
|
|
|
|
//! Move constructor
|
|
MastodonAPI(MastodonAPI &&other) noexcept = delete;
|
|
|
|
//! Destructor
|
|
virtual ~MastodonAPI() noexcept = default;
|
|
|
|
//! Copy assignment operator
|
|
MastodonAPI& operator=(const MastodonAPI &other) = delete;
|
|
|
|
//! Move assignment operator
|
|
MastodonAPI& operator=(MastodonAPI &&other) noexcept = delete;
|
|
|
|
public:
|
|
/*!
|
|
* @brief Start streaming a timeline, conversations, hashtag or list.
|
|
*
|
|
* @param type What to stream.
|
|
* @param id The hashtag or list id.
|
|
*
|
|
* @throw std::runtime_error hashtag or list ID given.
|
|
* @throw std::logic_error Unhandled stream type. (Should never happen)
|
|
* @throw mastodonpp::CURLException libcurl error from [mastodonpp]
|
|
* (https://doc.schlomp.space/mastodonpp/).
|
|
*
|
|
* @since 0.1.0
|
|
*/
|
|
void start_stream(stream_type type, optional<string_view> id);
|
|
|
|
/*!
|
|
* @brief Stop streaming a timeline, conversations, hashtag or list.
|
|
*
|
|
* @param type What to stream.
|
|
* @param id The hashtag or list id.
|
|
*
|
|
* @throw std::runtime_error No hashtag or list ID given.
|
|
* @throw std::logic_error Unhandled stream type. (Should never happen)
|
|
*
|
|
* @since 0.1.0
|
|
*/
|
|
void stop_stream(stream_type type, optional<string_view> id);
|
|
|
|
private:
|
|
Config _config;
|
|
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
|
|
|
|
#endif // FEDIPOTATO_ACCOUNT_MASTODON_HPP
|