/* This file is part of FediPotato. * Copyright © 2020 tastytea * * 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 . */ #ifndef FEDIPOTATO_ACCOUNT_MASTODON_HPP #define FEDIPOTATO_ACCOUNT_MASTODON_HPP #include "config.hpp" #include #include #include #include #include #include #include #include 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 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 id); private: Config _config; mastodonpp::Instance _instance; map> _streams; private: static string get_stream_id(stream_type type, optional id); static void make_stream_thread(mastodonpp::Instance &instance, stream_type type, optional id); }; } // namespace FediPotato #endif // FEDIPOTATO_ACCOUNT_MASTODON_HPP