Cleaned up, fixed formatting and so on.
All checks were successful
the build was successful

This commit is contained in:
tastytea 2019-03-28 21:23:24 +01:00
parent 76dc6a623c
commit dff3b11c00
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
8 changed files with 419 additions and 371 deletions

View File

@ -1,6 +1,6 @@
/* 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.

View File

@ -23,7 +23,6 @@
#include "debug.hpp"
using namespace Mastodon;
using namespace Mastodon::Easy;
using std::string;
Easy::API::API(const string &instance, const string &access_token)
@ -51,8 +50,8 @@ const std::vector<string> Easy::json_array_to_vector(const string &json)
return {};
}
const std::vector<Easy::stream_event>
Easy::parse_stream(const std::string &streamdata)
const vector<Easy::stream_event> Easy::parse_stream(
const std::string &streamdata)
{
string stream = streamdata;
std::regex reevent("event: (update|notification|delete)\ndata: (.*)\n");
@ -85,19 +84,19 @@ const Easy::Link Easy::API::get_link() const
}
const string Easy::API::strtime_utc(const system_clock::time_point &timepoint,
const string &format)
const string &format)
{
return strtime(timepoint, format, true);
}
const string Easy::API::strtime_local(const system_clock::time_point &timepoint,
const string &format)
const string &format)
{
return strtime(timepoint, format, false);
}
const string Easy::API::strtime(const system_clock::time_point &timepoint,
const string &format, const bool &utc)
const string &format, const bool &utc)
{
constexpr std::uint16_t bufsize = 1024;
std::time_t time = system_clock::to_time_t(timepoint);

View File

@ -44,14 +44,14 @@ namespace Mastodon
{
/*!
* @brief Child of Mastodon::API with abstract methods.
*
*
* @since before 0.11.0
*/
namespace Easy
{
/*!
* @brief Describes the event type
*
*
* @since before 0.11.0
*/
enum class event_type
@ -64,7 +64,7 @@ namespace Easy
/*!
* @brief Describes visibility of toots.
*
*
* @since before 0.11.0
*/
enum class visibility_type
@ -78,7 +78,7 @@ namespace Easy
/*!
* @brief Describes the attachment type
*
*
* @since before 0.11.0
*/
enum class attachment_type
@ -92,7 +92,7 @@ namespace Easy
/*!
* @brief Describes the card type
*
*
* @since before 0.11.0
*/
enum class card_type
@ -106,7 +106,7 @@ namespace Easy
/*!
* @brief Describes the notification type
*
*
* @since before 0.11.0
*/
enum class notification_type
@ -120,7 +120,7 @@ namespace Easy
/*!
* @brief Used for stream events.
*
*
* @since before 0.11.0
*/
typedef std::pair<event_type, string> stream_event;
@ -129,11 +129,12 @@ namespace Easy
* @brief Map of 'notification type' and 'push is requested or not'
*
* Used in PushSubscription::alerts().
*
*
* @since 0.13.3
*/
typedef std::map<Easy::notification_type, bool> alertmap;
// TODO: Get rid of forward declarations.
class Account;
class Application;
class Attachment;
@ -153,9 +154,9 @@ namespace Easy
/*!
* @brief Class to hold the `Link`-header.
*
*
* Extracts max_id and since_id from the `Link`-header
*
*
* @since before 0.11.0
*/
// TODO: Convert to struct?
@ -164,35 +165,35 @@ namespace Easy
public:
/*!
* @param link_header The content of the `Link` header
*
*
* @since before 0.11.0
*/
explicit Link(const string &link_header);
/*!
* @brief Returns max_id
*
*
* @since before 0.11.0
*/
const string next() const;
/*!
* @brief Returns max_id
*
*
* @since before 0.11.0
*/
const string max_id() const;
/*!
* @brief Returns since_id
*
*
* @since before 0.11.0
*/
const string prev() const;
/*!
* @brief Returns since_id
*
*
* @since before 0.11.0
*/
const string since_id() const;
@ -208,7 +209,7 @@ namespace Easy
* @param json JSON string holding the array
*
* @return vector of strings or an empty vector on error
*
*
* @since before 0.11.0
*/
const std::vector<string> json_array_to_vector(const string &json);
@ -219,106 +220,112 @@ namespace Easy
* @param streamdata Data from get_stream()
*
* @return vector of stream events
*
*
* @since before 0.11.0
*/
const std::vector<stream_event>
parse_stream(const std::string &streamdata);
const std::vector<stream_event> parse_stream(const std::string &streamdata);
/*!
* @brief Interface to the Mastodon API, easy version.
*
* Provides convenient functions to deal with the responses you get.
*/
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);
/*!
* @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);
/*!
* @brief Gets the links from the last answer
*
* @since before 0.11.0
*/
const Link get_link() const;
/*!
* @brief Gets the links from the last answer
*
* @since before 0.11.0
*/
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
*/
/*!
* @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
*/
// TODO: Time type, convertible to time_point, str_utc and str_local.
static const string strtime_utc(const system_clock::time_point &timepoint,
const string &format);
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);
/*!
* @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);
// #### simple calls ####
/*!
* @brief Sends a toot.
*
* @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);
// #### simple calls ####
// TODO: Own file.
/*!
* @brief Alias for send_post()
*
* @since 0.17.0
*/
const return_entity<Easy::Status> send_toot(const Status &status);
/*!
* @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);
/*!
* @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 = "");
/*!
* @brief Alias for send_post()
*
* @since 0.17.0
*/
const return_entity<Easy::Status> send_toot(const Status &status);
protected:
inline static const string strtime
/*!
* @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 = "");
protected:
inline static const string strtime
(const system_clock::time_point &timepoint,
const string &format, const bool &utc);
};

View File

@ -28,209 +28,209 @@ namespace Mastodon
{
namespace Easy
{
/*!
* @brief Base class for all entities.
*
* @since before 0.11.0
*/
class Entity
{
public:
/*!
* @brief Constructs an Entity object from a JSON string.
* @brief Base class for all entities.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Entity(const string &json);
class Entity
{
public:
/*!
* @brief Constructs an Entity object from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
explicit Entity(const string &json);
/*!
* @brief Constructs an Entity object from a JSON object.
*
* @param object JSON object
*
* @since 0.100.0
*/
explicit Entity(const Json::Value &object);
/*!
* @brief Constructs an Entity object from a JSON object.
*
* @param object JSON object
*
* @since 0.100.0
*/
explicit Entity(const Json::Value &object);
/*!
* @brief Constructs an empty Entity object.
*
* @since before 0.11.0
*/
Entity();
/*!
* @brief Constructs an empty Entity object.
*
* @since before 0.11.0
*/
Entity();
/*!
* @brief Destroys the object.
*
* @since 0.100.0
*/
virtual ~Entity();
/*!
* @brief Destroys the object.
*
* @since 0.100.0
*/
virtual ~Entity();
/*!
* Returns the JSON object of the Entity
*
* @since 0.100.0
*/
operator const Json::Value() const;
/*!
* Returns the JSON object of the Entity
*
* @since 0.100.0
*/
operator const Json::Value() const;
/*!
* @brief Replaces the Entity with a new one from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
void from_string(const string &json);
/*!
* @brief Replaces the Entity with a new one from a JSON string.
*
* @param json JSON string
*
* @since before 0.11.0
*/
void from_string(const string &json);
/*!
* @brief Returns the JSON object of the Entity
*
* @return JSON object
*
* @since before 0.11.0
*/
const string to_string() const;
/*!
* @brief Returns the JSON object of the Entity
*
* @return JSON object
*
* @since before 0.11.0
*/
const string to_string() const;
/*!
* @brief Replaces the Entity with a new one from a JSON object.
*
* @param object JSON object
*
* @since 0.100.0
*/
void from_object(const Json::Value &object);
/*!
* @brief Replaces the Entity with a new one from a JSON object.
*
* @param object JSON object
*
* @since 0.100.0
*/
void from_object(const Json::Value &object);
/*!
* @brief Returns the JSON object of the Entity
*
* @return JSON object
*
* @since before 0.11.0
*/
const Json::Value to_object() const;
/*!
* @brief Returns the JSON object of the Entity
*
* @return JSON object
*
* @since before 0.11.0
*/
const Json::Value to_object() const;
/*!
* @brief Returns true if the Entity holds valid data
*
* @since before 0.11.0 (virtual since 0.18.2)
*/
virtual bool valid() const = 0;
/*!
* @brief Returns true if the Entity holds valid data
*
* @since before 0.11.0 (virtual since 0.18.2)
*/
virtual bool valid() const = 0;
/*!
* @brief Returns error string sent by the server
*
* @since before 0.11.0
*/
const string error() const;
/*!
* @brief Returns error string sent by the server
*
* @since before 0.11.0
*/
const string error() const;
/*!
* @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
*/
bool was_set() const;
/*!
* @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
*/
bool was_set() const;
protected:
/*!
* @brief Returns the value of key as Json::Value
*
* Returns an empty object if the value does not exist or is
* null.
*/
const Json::Value get(const string &key) const;
protected:
/*!
* @brief Returns the value of key as Json::Value
*
* Returns an empty object if the value does not exist or is
* null.
*/
const Json::Value get(const string &key) const;
/*!
* @brief Returns the value of key as std::string
*
* returns "" if the value does not exist or is null.
*/
const string get_string(const string &key) const;
/*!
* @brief Returns the value of key as std::string
*
* returns "" if the value does not exist or is null.
*/
const string get_string(const string &key) const;
/*!
* @brief Returns the value of key as std::uint64_t
*
* Returns 0 if the value does not exist or is null.
*/
uint64_t get_uint64(const string &key) const;
/*!
* @brief Returns the value of key as std::uint64_t
*
* Returns 0 if the value does not exist or is null.
*/
uint64_t get_uint64(const string &key) const;
/*!
* @brief Returns the value of key as double
*
* Returns 0.0 if the value does not exist or is null.
*/
double get_double(const string &key) const;
/*!
* @brief Returns the value of key as double
*
* Returns 0.0 if the value does not exist or is null.
*/
double get_double(const string &key) const;
// TODO: Maybe an enum would be better?
/*!
* @brief Returns the value of key as bool
*
* Returns false if the value does not exist or is null.
*/
bool get_bool(const string &key) const;
// TODO: Maybe an enum would be better?
/*!
* @brief Returns the value of key as bool
*
* Returns false if the value does not exist or is null.
*/
bool get_bool(const string &key) const;
/*!
* @brief Returns the value of key as time_point
*
* Returns clocks epoch if the value does not exist or is null.
*/
const system_clock::time_point get_time_point(const string &key) const;
/*!
* @brief Returns the value of key as time_point
*
* Returns clocks epoch if the value does not exist or is null.
*/
const system_clock::time_point get_time_point(const string &key) const;
/*!
* @brief Returns the value of key as vector
*
* Returns an empty vector if the value does not exist or is
* null.
*/
const std::vector<string> get_vector(const string &key) const;
/*!
* @brief Returns the value of key as vector
*
* Returns an empty vector if the value does not exist or is
* null.
*/
const std::vector<string> get_vector(const string &key) const;
/*!
* @brief Sets the value of key
*
* @since 0.17.0
*/
void set(const string &key, const Json::Value &value);
/*!
* @brief Sets the value of key
*
* @since 0.17.0
*/
void set(const string &key, const Json::Value &value);
std::uint64_t stouint64(const string &str) const;
std::uint64_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
*/
bool check_valid(const std::vector<string> &attributes) 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
*/
bool check_valid(const std::vector<string> &attributes) const;
private:
Json::Value _tree;
mutable bool _was_set;
private:
Json::Value _tree;
mutable bool _was_set;
};
}
}

View File

@ -0,0 +1,88 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 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/>.
*/
#include "return_types_easy.hpp"
#include "easy/entities/status.hpp"
#include "easy/entities/notification.hpp"
using namespace Mastodon;
template<typename T>
Easy::return_entity<T>::return_entity()
: entity()
{}
template<typename T>
Easy::return_entity<T>::return_entity(const uint8_t ec, const string &em,
const T &ent)
: entity(ent)
{
error_code = ec;
error_message = em;
}
template<typename T>
Easy::return_entity<T>::return_entity::operator const T() const
{
return entity;
}
template<typename T>
Easy::return_entity<T>::return_entity::operator const string() const
{
return entity.to_string();
}
template<typename T>
std::ostream &Easy::operator <<(std::ostream &out,
const Easy::return_entity<T> &ret)
{
out << ret.entity.to_string();
return out;
}
// Explicit instantiations, so it can be used from outside.
// TODO: Complete this.
template struct Easy::return_entity<Easy::Status>;
template struct Easy::return_entity<Easy::Notification>;
template<typename T>
Easy::return_entity_vector<T>::
return_entity_vector::return_entity_vector()
: entities()
{}
template<typename T>
Easy::return_entity_vector<T>::return_entity_vector::return_entity_vector(
const uint8_t ec, const string &em, const vector<T> &vec)
: entities(vec)
{
error_code = ec;
error_message = em;
}
template<typename T>
Easy::return_entity_vector<T>::return_entity_vector::operator const vector<T>()
const
{
return entities;
}
// Explicit instantiations, so it can be used from outside.
// TODO: Complete this.
template struct Easy::return_entity_vector<Easy::Status>;
template struct Easy::return_entity_vector<Easy::Notification>;

View File

@ -20,101 +20,55 @@
#include <string>
#include <vector>
#include <ostream>
#include <cstdint>
#include "mastodon-cpp.hpp"
using std::string;
using std::vector;
using std::uint8_t;
namespace Mastodon
{
template <typename T>
struct return_entity;
// https://stackoverflow.com/a/4661372/5965450
template <typename T>
std::ostream &operator <<(std::ostream&, const return_entity<T>&);
template <typename T>
struct return_entity : return_base
namespace Easy
{
T entity;
template <typename T>
struct return_entity;
template <typename T> // https://stackoverflow.com/a/4661372/5965450
std::ostream &operator <<(std::ostream&, const return_entity<T>&);
return_entity();
return_entity(const uint8_t ec, const string &em, const T &ent);
/*!
* @brief Return types for calls that return a single `Easy::Entity`.
*/
template <typename T>
struct return_entity : return_base
{
T entity;
operator const T() const;
operator const string() const;
return_entity();
return_entity(const uint8_t ec, const string &em, const T &ent);
friend std::ostream &operator <<<T>(std::ostream &out,
const return_entity<T> &ret);
};
operator const T() const;
operator const string() const;
template <typename T>
struct return_entity_vector : return_base
{
vector<T> entities;
// FIXME: Can't get it to work, don't know why.
friend std::ostream &operator <<<T>(std::ostream &out,
const return_entity<T> &ret);
};
return_entity_vector();
return_entity_vector(const uint8_t ec, const string &em,
const vector<T> &vec);
/*!
* @brief Return types for calls that return multiple `Easy::Entity`s.
*/
template <typename T>
struct return_entity_vector : return_base
{
vector<T> entities;
operator const vector<T>() const;
};
return_entity_vector();
return_entity_vector(const uint8_t ec, const string &em,
const vector<T> &vec);
template<typename T>
return_entity<T>::return_entity()
: entity()
{}
template<typename T>
return_entity<T>::return_entity(const uint8_t ec, const string &em,
const T &ent)
: entity(ent)
{
error_code = ec;
error_message = em;
}
template<typename T>
return_entity<T>::return_entity::operator const T() const
{
return entity;
}
template<typename T>
return_entity<T>::return_entity::operator const string() const
{
return entity.to_string();
}
template<typename T>
std::ostream &operator <<(std::ostream &out, const return_entity<T> &ret)
{
out << ret.entity.to_string();
return out;
}
// Explicit instantiation
// template struct Mastodon::return_entity<Easy::Status>;
template<typename T>
return_entity_vector<T>::return_entity_vector::return_entity_vector()
: entities()
{}
template<typename T>
return_entity_vector<T>::return_entity_vector::return_entity_vector(
const uint8_t ec, const string &em, const vector<T> &vec)
: entities(vec)
{
error_code = ec;
error_message = em;
}
template<typename T>
return_entity_vector<T>::return_entity_vector::operator const vector<T>() const
{
return entities;
operator const vector<T>() const;
};
}
}

View File

@ -21,14 +21,14 @@
#include "easy/entities/attachment.hpp"
#include "easy/entities/notification.hpp"
using namespace Mastodon;
using namespace Mastodon::Easy;
const return_entity<Easy::Status> Easy::API::send_toot(const Status &status)
const return_entity<Status> API::send_toot(const Status &status)
{
return send_post(status);
}
const return_entity<Easy::Status> Easy::API::send_post(const Status &status)
const return_entity<Status> API::send_post(const Status &status)
{
API::parametermap parameters;
@ -129,7 +129,7 @@ const return_entity<Easy::Status> Easy::API::send_post(const Status &status)
return { ret.error_code, ret.error_message, Status(ret.answer) };
}
const return_entity_vector<Easy::Notification> Easy::API::get_notifications(
const return_entity_vector<Notification> API::get_notifications(
const uint16_t limit, const string since_id, const string max_id)
{
API::parametermap parameters;

View File

@ -70,7 +70,7 @@ namespace Mastodon
} return_call;
/*!
* @brief Class for the Mastodon API.
* @brief Interface to the Mastodon API.
*
* All input is expected to be UTF-8. Binary data must be
* base64-encoded or a filename.