This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/src/easy/easy.hpp

471 lines
12 KiB
C++
Raw Normal View History

2018-03-21 16:45:52 +01:00
/* This file is part of mastodon-cpp.
* Copyright © 2018 tastytea <tastytea@tastytea.de>
*
* 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>
#include <functional>
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"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
2018-03-21 16:45:52 +01:00
using std::string;
2018-03-31 19:43:15 +02:00
using std::uint_fast64_t;
2018-06-14 04:11:28 +02:00
using std::uint_fast16_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.
*
* @since before 0.11.0
2018-03-21 16:45:52 +01:00
*/
class Easy : public API
{
public:
2018-04-01 03:08:22 +02:00
/*!
* @brief Describes the event type
*
* @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.
*
* @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
*
* @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
*
* @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
*
* @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
};
/*!
* @brief Used for stream events.
*
* @since before 0.11.0
*/
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().
*
* @since 0.13.3
2018-05-23 20:21:41 +02:00
*/
typedef std::map<Easy::notification_type, bool> alertmap;
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.
*
* Extracts max_id and since_id from the `Link`-header
*
* @since before 0.11.0
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
*
* @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
*
* @since before 0.11.0
2018-04-03 00:04:47 +02:00
*/
2018-12-04 11:26:28 +01:00
uint_fast64_t next() const;
2018-04-03 00:04:47 +02:00
/*!
* @brief Returns max_id
*
* @since before 0.11.0
*/
2018-12-04 11:26:28 +01:00
uint_fast64_t max_id() const;
2018-04-03 00:04:47 +02:00
/*!
* @brief Returns since_id
*
* @since before 0.11.0
2018-04-03 00:04:47 +02:00
*/
2018-12-04 11:26:28 +01:00
uint_fast64_t prev() const;
2018-04-03 00:04:47 +02:00
/*!
* @brief Returns since_id
*
* @since before 0.11.0
*/
2018-12-04 11:26:28 +01:00
uint_fast64_t since_id() const;
2018-04-03 00:04:47 +02:00
private:
uint_fast64_t _next;
uint_fast64_t _prev;
};
2018-03-21 16:45:52 +01:00
/*!
* @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
2018-03-21 16:45:52 +01:00
*/
explicit Easy(const string &instance, const string &access_token);
/*!
2018-04-01 02:20:10 +02:00
* @brief Turns a JSON array into a vector of strings
*
* @param json JSON string holding the array
*
* @return vector of strings or an empty vector on error
*
* @since before 0.11.0
*/
static const std::vector<string> json_array_to_vector(const string &json);
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
*
* @since before 0.11.0
2018-04-01 03:08:22 +02:00
*/
static const std::vector<stream_event>
parse_stream(const std::string &streamdata);
2018-04-03 00:04:47 +02:00
/*!
* @brief Gets the links from the last answer
*
* @since before 0.11.0
2018-04-03 00:04:47 +02:00
*/
const Link get_link() const;
/*!
* @brief Converts a time_point to a string
*
* The return value can not exceed 1023 chars.
*
* @param timepoint The timepoint
* @param format The format of the string, same as with `strftime`.
*
* Example:
* @code
* auto timepoint = status.created_at();
* cout << Easy::strtime_utc(timepoint, "%F, %T") << '\n';
* @endcode
*
* @return The UTC time as string
*
* @since 0.11.0
*/
static const string strtime_utc(const system_clock::time_point &timepoint,
const string &format);
/*!
* @brief See strtime_utc
*
* @return The local time as string
*
* @since 0.11.0
*/
static const string strtime_local(const system_clock::time_point &timepoint,
const string &format);
2018-06-14 04:11:28 +02:00
// #### simple calls ####
/*!
* @brief Sends a toot.
*
* @param status The status to send
* @param error @ref error "Error code". If the URL has permanently
* changed, 13 is returned and answer is set to the new
* URL.
*
* @return The new Easy::Status
*
* @since 0.18.1
*/
const Status send_post(const Status &status, uint_fast16_t &error);
/*!
* @brief Alias for send_post()
*
* @since 0.17.0
2018-06-14 04:11:28 +02:00
*/
const Status send_toot(const Status &status, uint_fast16_t &error);
2018-06-14 04:11:28 +02:00
2018-04-01 04:27:29 +02:00
/*!
* @brief Base class for all entities.
*
* @since before 0.11.0
2018-04-01 04:27:29 +02:00
*/
2018-03-22 00:33:36 +01:00
class Entity
2018-03-21 16:45:52 +01:00
{
public:
2018-03-25 22:46:38 +02:00
/*!
* @brief Constructs an Entity object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
2018-03-25 22:46:38 +02:00
*/
2018-03-30 19:01:09 +02:00
explicit Entity(const string &json);
/*!
* @brief Constructs an empty Entity object.
*
* @since before 0.11.0
2018-03-30 19:01:09 +02:00
*/
2018-03-30 20:08:47 +02:00
Entity();
2018-03-21 16:45:52 +01:00
/*!
2018-04-01 00:15:35 +02:00
* @brief Replaces the Entity with a new one from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
2018-12-04 11:26:28 +01:00
void from_string(const string &json);
2018-04-01 00:15:35 +02:00
/*!
* @brief Returns the JSON object of the Entity
*
* @return JSON object
*
* @since before 0.11.0
2018-04-01 00:15:35 +02:00
*/
const Json::Value to_object() const;
2018-03-21 16:45:52 +01:00
/*!
2018-03-22 00:33:36 +01:00
* @brief Returns true if the Entity holds valid data
*
* @since before 0.11.0 (virtual since 0.18.2)
2018-03-21 16:45:52 +01:00
*/
2018-12-04 11:26:28 +01:00
virtual bool valid() const = 0;
2018-03-21 16:45:52 +01:00
2018-03-30 19:01:09 +02:00
/*!
2018-04-02 00:49:28 +02:00
* @brief Returns error string sent by the server
*
* @since before 0.11.0
2018-03-30 19:01:09 +02:00
*/
const string error() const;
2018-04-09 21:47:07 +02:00
/*!
* @brief Returns true if the last requested value was set, false if
* it was unset.
*
* Members of Easy::Entity-derived classes return a default
* value depending on its type when the requested value is not
* found in the JSON. "" for strings, false for bools and so
* on. Most of the time this is no problem, but sometimes you
* need to know for sure.
*
* Example:
* @code
* Easy::Account a(jsonstring);
* if (a.note().empty())
* {
* if (a.was_set())
* {
* cout << "Account has an empty description.\n";
* }
* else
* {
* cout << "Account has no description.\n";
* }
* }
* @endcode
*
* @since before 0.11.0
2018-04-09 21:47:07 +02:00
*/
2018-12-04 11:26:28 +01:00
bool was_set() const;
2018-04-09 21:47:07 +02:00
2018-03-31 02:54:00 +02:00
protected:
2018-03-25 22:46:38 +02:00
/*!
2018-03-31 02:54:00 +02:00
* @brief Returns the value of key as Json::Value
*
2018-04-02 00:49:28 +02:00
* Returns an empty object if the value does not exist or is
* null.
2018-03-25 22:46:38 +02:00
*/
const Json::Value get(const string &key) const;
/*!
2018-03-31 02:54:00 +02:00
* @brief Returns the value of key as std::string
2018-03-25 22:46:38 +02:00
*
2018-04-02 00:49:28 +02:00
* returns "" if the value does not exist or is null.
2018-03-25 22:46:38 +02:00
*/
const string get_string(const string &key) const;
/*!
2018-03-31 19:43:15 +02:00
* @brief Returns the value of key as std::uint_fast64_t
2018-03-25 22:46:38 +02:00
*
2018-04-02 00:49:28 +02:00
* Returns 0 if the value does not exist or is null.
2018-03-25 22:46:38 +02:00
*/
2018-12-04 11:26:28 +01:00
uint_fast64_t get_uint64(const string &key) const;
2018-03-25 22:46:38 +02:00
/*!
2018-03-31 02:54:00 +02:00
* @brief Returns the value of key as double
2018-03-25 22:46:38 +02:00
*
2018-04-02 00:49:28 +02:00
* Returns 0.0 if the value does not exist or is null.
2018-03-25 22:46:38 +02:00
*/
2018-12-04 11:26:28 +01:00
double get_double(const string &key) const;
2018-03-25 22:46:38 +02:00
2018-04-01 01:58:03 +02:00
// TODO: Maybe an enum would be better?
2018-03-25 22:46:38 +02:00
/*!
2018-03-31 02:54:00 +02:00
* @brief Returns the value of key as bool
2018-03-25 22:46:38 +02:00
*
2018-04-02 00:49:28 +02:00
* Returns false if the value does not exist or is null.
2018-03-25 22:46:38 +02:00
*/
2018-12-04 11:26:28 +01:00
bool get_bool(const string &key) const;
2018-03-25 22:46:38 +02:00
/*!
2018-03-31 02:54:00 +02:00
* @brief Returns the value of key as time_point
2018-03-25 22:46:38 +02:00
*
2018-04-02 00:49:28 +02:00
* Returns clocks epoch if the value does not exist or is null.
2018-03-25 22:46:38 +02:00
*/
const system_clock::time_point get_time_point(const string &key) const;
2018-03-30 20:08:47 +02:00
/*!
2018-03-31 02:54:00 +02:00
* @brief Returns the value of key as vector
2018-03-30 20:08:47 +02:00
*
2018-04-02 00:49:28 +02:00
* Returns an empty vector if the value does not exist or is
* null.
2018-03-30 20:08:47 +02:00
*/
const std::vector<string> get_vector(const string &key) const;
2018-06-13 23:55:19 +02:00
/*!
* @brief Sets the value of key
2018-06-14 01:52:05 +02:00
*
* @since 0.17.0
2018-06-13 23:55:19 +02:00
*/
2018-12-04 11:26:28 +01:00
void set(const string &key, const Json::Value &value);
2018-06-13 23:55:19 +02:00
2018-12-04 11:26:28 +01:00
std::uint_fast64_t stouint64(const string &str) const;
/*!
* @brief Checks if an Entity is valid
*
* @param attributes The attributes to check
*
* @return true if all attributes are set
*
* @since 0.18.2
*/
2018-12-04 11:26:28 +01:00
bool check_valid(const std::vector<string> &attributes) const;
2018-04-04 04:38:04 +02:00
private:
2018-03-21 16:45:52 +01:00
Json::Value _tree;
mutable bool _was_set;
2018-03-21 16:45:52 +01:00
};
2018-03-21 20:12:45 +01:00
2018-05-05 04:38:08 +02:00
protected:
inline static const string strtime
(const system_clock::time_point &timepoint,
const string &format, const bool &utc);
2018-03-21 16:45:52 +01:00
};
}
#endif // MASTODON_EASY_CPP_HPP