2018-03-21 16:45:52 +01:00
|
|
|
/* This file is part of mastodon-cpp.
|
2019-02-22 12:33:03 +01:00
|
|
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
2018-03-21 16:45:52 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MASTODON_EASY_CPP_HPP
|
|
|
|
#define MASTODON_EASY_CPP_HPP
|
|
|
|
|
|
|
|
#include <string>
|
2018-03-25 22:46:38 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <chrono>
|
2018-03-30 20:08:47 +02:00
|
|
|
#include <vector>
|
2018-04-01 03:08:22 +02:00
|
|
|
#include <utility>
|
2018-04-03 01:20:15 +02:00
|
|
|
#include <functional>
|
2019-03-02 20:21:31 +01:00
|
|
|
#include <ostream>
|
2018-03-21 16:45:52 +01:00
|
|
|
#include <jsoncpp/json/json.h>
|
2018-03-22 00:33:36 +01:00
|
|
|
|
2018-03-21 20:12:45 +01:00
|
|
|
// If we are compiling mastodon-cpp, use another include path
|
|
|
|
#ifdef MASTODON_CPP
|
|
|
|
#include "mastodon-cpp.hpp"
|
2019-03-29 14:44:39 +01:00
|
|
|
#include "easy/return_types_easy.hpp"
|
2018-03-21 20:12:45 +01:00
|
|
|
#else
|
|
|
|
#include <mastodon-cpp/mastodon-cpp.hpp>
|
2019-03-29 14:44:39 +01:00
|
|
|
#include <mastodon-cpp/easy/return_types_easy.hpp>
|
2018-03-21 20:12:45 +01:00
|
|
|
#endif
|
2018-03-21 16:45:52 +01:00
|
|
|
|
|
|
|
using std::string;
|
2019-01-26 06:54:47 +01:00
|
|
|
using std::vector;
|
2019-02-22 08:29:54 +01:00
|
|
|
using std::uint64_t;
|
|
|
|
using std::uint16_t;
|
2018-03-25 22:46:38 +02:00
|
|
|
using std::chrono::system_clock;
|
2018-03-21 16:45:52 +01:00
|
|
|
|
|
|
|
namespace Mastodon
|
|
|
|
{
|
|
|
|
/*!
|
|
|
|
* @brief Child of Mastodon::API with abstract methods.
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-03-21 16:45:52 +01:00
|
|
|
*/
|
2019-03-28 15:30:56 +01:00
|
|
|
namespace Easy
|
2018-03-21 16:45:52 +01:00
|
|
|
{
|
2018-04-01 03:08:22 +02:00
|
|
|
/*!
|
|
|
|
* @brief Describes the event type
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-04-01 03:08:22 +02:00
|
|
|
*/
|
|
|
|
enum class event_type
|
|
|
|
{
|
|
|
|
Update,
|
|
|
|
Notification,
|
|
|
|
Delete,
|
|
|
|
Undefined
|
|
|
|
};
|
|
|
|
|
2018-03-21 16:45:52 +01:00
|
|
|
/*!
|
|
|
|
* @brief Describes visibility of toots.
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-03-21 16:45:52 +01:00
|
|
|
*/
|
2018-03-31 04:29:55 +02:00
|
|
|
enum class visibility_type
|
2018-03-21 16:45:52 +01:00
|
|
|
{
|
|
|
|
Direct,
|
|
|
|
Private,
|
|
|
|
Unlisted,
|
|
|
|
Public,
|
|
|
|
Undefined
|
|
|
|
};
|
|
|
|
|
2018-03-21 20:12:45 +01:00
|
|
|
/*!
|
|
|
|
* @brief Describes the attachment type
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-03-21 20:12:45 +01:00
|
|
|
*/
|
|
|
|
enum class attachment_type
|
|
|
|
{
|
2018-04-01 05:14:02 +02:00
|
|
|
Image,
|
|
|
|
Video,
|
|
|
|
Gifv,
|
|
|
|
Unknown,
|
|
|
|
Undefined
|
2018-03-21 20:12:45 +01:00
|
|
|
};
|
|
|
|
|
2018-03-25 19:43:48 +02:00
|
|
|
/*!
|
|
|
|
* @brief Describes the card type
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-03-25 19:43:48 +02:00
|
|
|
*/
|
|
|
|
enum class card_type
|
|
|
|
{
|
2018-04-01 05:14:02 +02:00
|
|
|
Link,
|
|
|
|
Photo,
|
|
|
|
Video,
|
|
|
|
Rich,
|
|
|
|
Undefined
|
2018-03-25 19:43:48 +02:00
|
|
|
};
|
|
|
|
|
2018-03-30 23:51:09 +02:00
|
|
|
/*!
|
|
|
|
* @brief Describes the notification type
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-03-30 23:51:09 +02:00
|
|
|
*/
|
|
|
|
enum class notification_type
|
|
|
|
{
|
2018-04-01 05:14:02 +02:00
|
|
|
Mention,
|
|
|
|
Reblog,
|
|
|
|
Favourite,
|
|
|
|
Follow,
|
|
|
|
Undefined
|
2018-03-30 23:51:09 +02:00
|
|
|
};
|
|
|
|
|
2018-04-03 01:20:15 +02:00
|
|
|
/*!
|
|
|
|
* @brief Used for stream events.
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-04-03 01:20:15 +02:00
|
|
|
*/
|
2018-04-01 03:08:22 +02:00
|
|
|
typedef std::pair<event_type, string> stream_event;
|
|
|
|
|
2018-05-23 20:21:41 +02:00
|
|
|
/*!
|
|
|
|
* @brief Map of 'notification type' and 'push is requested or not'
|
|
|
|
*
|
|
|
|
* Used in PushSubscription::alerts().
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since 0.13.3
|
2018-05-23 20:21:41 +02:00
|
|
|
*/
|
|
|
|
typedef std::map<Easy::notification_type, bool> alertmap;
|
|
|
|
|
2019-03-29 14:44:39 +01:00
|
|
|
struct time
|
|
|
|
{
|
|
|
|
system_clock::time_point timepoint = system_clock::time_point();
|
|
|
|
|
|
|
|
operator const system_clock::time_point();
|
|
|
|
operator const string();
|
|
|
|
friend std::ostream &operator <<(std::ostream &out,
|
|
|
|
const Easy::time &t);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Converts time to a string.
|
|
|
|
*
|
|
|
|
* The return value can not exceed 1023 chars.
|
|
|
|
*
|
|
|
|
* @param format The format of the string, same as with
|
|
|
|
* `strftime`.
|
|
|
|
* @param local Use local time (default).
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* @code
|
|
|
|
* Mastodon::Easy::time timepoint = status.created_at();
|
|
|
|
* std::cout << timepoint.strtime("%F, %T UTC", false) << '\n';
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* @return The time as string.
|
|
|
|
*
|
|
|
|
* @since 0.100.0
|
|
|
|
*/
|
|
|
|
const string strtime (const string &format,
|
|
|
|
const bool &local = true) const;
|
|
|
|
};
|
|
|
|
|
2019-03-28 21:23:24 +01:00
|
|
|
// TODO: Get rid of forward declarations.
|
2018-06-14 04:11:28 +02:00
|
|
|
class Account;
|
|
|
|
class Application;
|
|
|
|
class Attachment;
|
|
|
|
class Card;
|
|
|
|
class Context;
|
|
|
|
class Emoji;
|
|
|
|
class Instance;
|
|
|
|
class List;
|
|
|
|
class Mention;
|
|
|
|
class Notification;
|
|
|
|
class Relationship;
|
|
|
|
class Report;
|
|
|
|
class Results;
|
|
|
|
class Status;
|
|
|
|
class Tag;
|
|
|
|
class PushSubscription;
|
|
|
|
|
2018-04-03 00:04:47 +02:00
|
|
|
/*!
|
|
|
|
* @brief Class to hold the `Link`-header.
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-04-03 01:20:15 +02:00
|
|
|
* Extracts max_id and since_id from the `Link`-header
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-04-03 00:04:47 +02:00
|
|
|
*/
|
2019-03-28 15:30:56 +01:00
|
|
|
// TODO: Convert to struct?
|
2018-04-03 00:04:47 +02:00
|
|
|
class Link
|
|
|
|
{
|
|
|
|
public:
|
2018-04-04 04:38:04 +02:00
|
|
|
/*!
|
|
|
|
* @param link_header The content of the `Link` header
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-04-04 04:38:04 +02:00
|
|
|
*/
|
2018-04-03 00:04:47 +02:00
|
|
|
explicit Link(const string &link_header);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Returns max_id
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-04-03 00:04:47 +02:00
|
|
|
*/
|
2019-01-27 03:50:28 +01:00
|
|
|
const string next() const;
|
2018-04-03 00:04:47 +02:00
|
|
|
|
2018-04-03 01:20:15 +02:00
|
|
|
/*!
|
|
|
|
* @brief Returns max_id
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-04-03 01:20:15 +02:00
|
|
|
*/
|
2019-01-27 03:50:28 +01:00
|
|
|
const string max_id() const;
|
2018-04-03 01:20:15 +02:00
|
|
|
|
2018-04-03 00:04:47 +02:00
|
|
|
/*!
|
|
|
|
* @brief Returns since_id
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-04-03 00:04:47 +02:00
|
|
|
*/
|
2019-01-27 03:50:28 +01:00
|
|
|
const string prev() const;
|
2018-04-03 00:04:47 +02:00
|
|
|
|
2018-04-03 01:20:15 +02:00
|
|
|
/*!
|
|
|
|
* @brief Returns since_id
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-04-03 01:20:15 +02:00
|
|
|
*/
|
2019-01-27 03:50:28 +01:00
|
|
|
const string since_id() const;
|
2018-04-03 01:20:15 +02:00
|
|
|
|
2018-04-03 00:04:47 +02:00
|
|
|
private:
|
2019-01-27 03:50:28 +01:00
|
|
|
string _next;
|
|
|
|
string _prev;
|
2018-04-03 00:04:47 +02:00
|
|
|
};
|
|
|
|
|
2018-03-31 20:03:31 +02:00
|
|
|
/*!
|
2018-04-01 02:20:10 +02:00
|
|
|
* @brief Turns a JSON array into a vector of strings
|
2018-03-31 20:03:31 +02:00
|
|
|
*
|
|
|
|
* @param json JSON string holding the array
|
|
|
|
*
|
|
|
|
* @return vector of strings or an empty vector on error
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-03-31 20:03:31 +02:00
|
|
|
*/
|
2019-03-28 15:30:56 +01:00
|
|
|
const std::vector<string> json_array_to_vector(const string &json);
|
2018-03-31 20:03:31 +02:00
|
|
|
|
2018-04-01 03:08:22 +02:00
|
|
|
/*!
|
|
|
|
* @brief Split stream into a vector of events
|
|
|
|
*
|
|
|
|
* @param streamdata Data from get_stream()
|
|
|
|
*
|
|
|
|
* @return vector of stream events
|
2019-03-28 21:23:24 +01:00
|
|
|
*
|
2018-06-14 11:17:26 +02:00
|
|
|
* @since before 0.11.0
|
2018-04-01 03:08:22 +02:00
|
|
|
*/
|
2019-03-28 21:23:24 +01:00
|
|
|
const std::vector<stream_event> parse_stream(const std::string &streamdata);
|
2018-04-01 03:08:22 +02:00
|
|
|
|
2019-03-28 15:30:56 +01:00
|
|
|
/*!
|
2019-03-28 21:23:24 +01:00
|
|
|
* @brief Interface to the Mastodon API, easy version.
|
2019-03-28 15:30:56 +01:00
|
|
|
*
|
2019-03-28 21:23:24 +01:00
|
|
|
* Provides convenient functions to deal with the responses you get.
|
2019-03-28 15:30:56 +01:00
|
|
|
*/
|
2019-03-28 21:23:24 +01:00
|
|
|
class API : public Mastodon::API
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
* @brief Constructs a new Easy object.
|
|
|
|
*
|
|
|
|
* To register your application, leave access_token blank and
|
|
|
|
* call register_app1() and register_app2().
|
|
|
|
*
|
|
|
|
* @param instance The hostname of your instance
|
|
|
|
* @param access_token The access token
|
|
|
|
*
|
|
|
|
* @since before 0.11.0
|
|
|
|
*/
|
|
|
|
explicit API(const string &instance, const string &access_token);
|
2019-03-28 15:30:56 +01:00
|
|
|
|
2019-03-28 21:23:24 +01:00
|
|
|
/*!
|
|
|
|
* @brief Gets the links from the last answer
|
|
|
|
*
|
|
|
|
* @since before 0.11.0
|
|
|
|
*/
|
|
|
|
const Link get_link() const;
|
2018-04-03 00:04:47 +02:00
|
|
|
|
2019-03-28 21:23:24 +01:00
|
|
|
/*!
|
|
|
|
* @brief Sends a post.
|
|
|
|
*
|
|
|
|
* @param status The status to send
|
|
|
|
* @param error @ref error "Error code"
|
|
|
|
*
|
|
|
|
* @return The new Easy::Status
|
|
|
|
*
|
|
|
|
* @since 0.18.1
|
|
|
|
*/
|
|
|
|
const return_entity<Easy::Status> send_post(const Status &status);
|
2018-06-14 04:11:28 +02:00
|
|
|
|
2019-03-28 21:23:24 +01:00
|
|
|
/*!
|
|
|
|
* @brief Alias for send_post()
|
|
|
|
*
|
|
|
|
* @since 0.17.0
|
|
|
|
*/
|
|
|
|
const return_entity<Easy::Status> send_toot(const Status &status);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief Gets notifications.
|
|
|
|
*
|
|
|
|
* @param error @ref error "Error code"
|
|
|
|
* @param limit Maximum number of notifications
|
|
|
|
* @param since_id Return notifications newer than ID
|
|
|
|
* @param max_id Return notifications older than ID
|
|
|
|
*
|
|
|
|
* @return vector of Easy::Notification.
|
|
|
|
*
|
|
|
|
* @since 0.21.0
|
|
|
|
*/
|
|
|
|
const return_entity_vector<Easy::Notification> get_notifications(
|
|
|
|
const uint16_t limit = 20, const string since_id = "",
|
|
|
|
const string max_id = "");
|
2019-03-29 14:44:39 +01:00
|
|
|
};
|
2019-03-28 15:30:56 +01:00
|
|
|
}
|
2018-03-21 16:45:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MASTODON_EASY_CPP_HPP
|