mastodon-cpp  0.17.0
easy.hpp
1 /* This file is part of mastodon-cpp.
2  * Copyright © 2018 tastytea <tastytea@tastytea.de>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MASTODON_EASY_CPP_HPP
18 #define MASTODON_EASY_CPP_HPP
19 
20 #include <string>
21 #include <cstdint>
22 #include <chrono>
23 #include <vector>
24 #include <utility>
25 #include <functional>
26 #include <jsoncpp/json/json.h>
27 
28 // If we are compiling mastodon-cpp, use another include path
29 #ifdef MASTODON_CPP
30  #include "mastodon-cpp.hpp"
31 #else
32  #include <mastodon-cpp/mastodon-cpp.hpp>
33 #endif
34 
35 using std::string;
36 using std::uint_fast64_t;
37 using std::uint_fast16_t;
38 using std::chrono::system_clock;
39 
40 namespace Mastodon
41 {
45 class Easy : public API
46 {
47 public:
51  enum class event_type
52  {
53  Update,
55  Delete,
56  Undefined
57  };
58 
62  enum class visibility_type
63  {
64  Direct,
65  Private,
66  Unlisted,
67  Public,
68  Undefined
69  };
70 
74  enum class attachment_type
75  {
76  Image,
77  Video,
78  Gifv,
79  Unknown,
80  Undefined
81  };
82 
86  enum class card_type
87  {
88  Link,
89  Photo,
90  Video,
91  Rich,
92  Undefined
93  };
94 
98  enum class notification_type
99  {
100  Mention,
101  Reblog,
102  Favourite,
103  Follow,
104  Undefined
105  };
106 
110  typedef std::pair<event_type, string> stream_event;
111 
117  typedef std::map<Easy::notification_type, bool> alertmap;
118 
119  class Account;
120  class Application;
121  class Attachment;
122  class Card;
123  class Context;
124  class Emoji;
125  class Instance;
126  class List;
127  class Mention;
128  class Notification;
129  class Relationship;
130  class Report;
131  class Results;
132  class Status;
133  class Tag;
134  class PushSubscription;
135 
141  class Link
142  {
143  public:
147  explicit Link(const string &link_header);
148 
152  const uint_fast64_t next() const;
153 
157  const uint_fast64_t max_id() const;
158 
162  const uint_fast64_t prev() const;
163 
167  const uint_fast64_t since_id() const;
168 
169  private:
170  uint_fast64_t _next;
171  uint_fast64_t _prev;
172  };
173 
183  explicit Easy(const string &instance, const string &access_token);
184 
192  static const std::vector<string> json_array_to_vector(const string &json);
193 
201  static const std::vector<stream_event>
202  parse_stream(const std::string &streamdata);
203 
207  const Link get_link() const;
208 
227  static const string strtime_utc(const system_clock::time_point &timepoint,
228  const string &format);
229 
237  static const string strtime_local(const system_clock::time_point &timepoint,
238  const string &format);
239 
240  // #### simple calls ####
241 
252  const Status send_toot(const Status &status, uint_fast16_t error = 0);
253 
257  class Entity
258  {
259  public:
265  explicit Entity(const string &json);
266 
270  Entity();
271 
277  const void from_string(const string &json);
278 
284  const Json::Value to_object() const;
285 
289  const bool valid() const;
290 
294  const string error() const;
295 
322  const bool was_set() const;
323 
324  protected:
331  const Json::Value get(const string &key) const;
332 
338  const string get_string(const string &key) const;
339 
345  const uint_fast64_t get_uint64(const string &key) const;
346 
352  const double get_double(const string &key) const;
353 
354  // TODO: Maybe an enum would be better?
360  const bool get_bool(const string &key) const;
361 
367  const system_clock::time_point get_time_point(const string &key) const;
368 
375  const std::vector<string> get_vector(const string &key) const;
376 
382  const void set(const string &key, const Json::Value &value);
383 
384  const std::uint_fast64_t stouint64(const string &str) const;
385 
386  private:
387  Json::Value _tree;
388  bool _valid;
389  mutable bool _was_set;
390  };
391 
392 protected:
393  inline static const string strtime
394  (const system_clock::time_point &timepoint,
395  const string &format, const bool &utc);
396 };
397 }
398 
399 #endif // MASTODON_EASY_CPP_HPP
Class for the Mastodon API.
Definition: mastodon-cpp.hpp:74
static const string strtime_utc(const system_clock::time_point &timepoint, const string &format)
Converts a time_point to a string.
Definition: easy.cpp:85
Class to hold tags.
Definition: tag.hpp:42
const void from_string(const string &json)
Replaces the Entity with a new one from a JSON string.
Definition: entity.cpp:37
Entity()
Constructs an empty Entity object.
Definition: entity.cpp:70
Class to hold notifications.
Definition: notification.hpp:46
Class to hold attachments.
Definition: attachment.hpp:42
Class to hold cards.
Definition: card.hpp:40
const Status send_toot(const Status &status, uint_fast16_t error=0)
Sends a toot.
Definition: simple_calls.cpp:24
card_type
Describes the card type.
Definition: easy.hpp:86
Child of Mastodon::API with abstract methods.
Definition: easy.hpp:45
Class to hold instances.
Definition: instance.hpp:41
Class to hold push subscriptions.
Definition: pushsubscription.hpp:43
static const std::vector< string > json_array_to_vector(const string &json)
Turns a JSON array into a vector of strings.
Definition: easy.cpp:31
const double get_double(const string &key) const
Returns the value of key as double.
Definition: entity.cpp:159
Easy(const string &instance, const string &access_token)
Constructs a new Easy object.
Definition: easy.cpp:27
visibility_type
Describes visibility of toots.
Definition: easy.hpp:62
attachment_type
Describes the attachment type.
Definition: easy.hpp:74
notification_type
Describes the notification type.
Definition: easy.hpp:98
const Json::Value to_object() const
Returns the JSON object of the Entity.
Definition: entity.cpp:65
Class to hold mentions.
Definition: mention.hpp:41
std::map< Easy::notification_type, bool > alertmap
Map of &#39;notification type&#39; and &#39;push is requested or not&#39;.
Definition: easy.hpp:117
const bool was_set() const
Returns true if the last requested value was set, false if it was unset.
Definition: entity.cpp:85
Class to hold reports.
Definition: report.hpp:40
static const string strtime_local(const system_clock::time_point &timepoint, const string &format)
See strtime_utc.
Definition: easy.cpp:91
event_type
Describes the event type.
Definition: easy.hpp:51
Class to hold statuses.
Definition: status.hpp:55
Class to hold accounts.
Definition: account.hpp:44
Class to hold contexts.
Definition: context.hpp:41
Definition: mastodon-cpp.hpp:52
Base class for all entities.
Definition: easy.hpp:257
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:80
Class to hold results.
Definition: results.hpp:45
Class to hold relationships.
Definition: relationship.hpp:40
Class to hold lists.
Definition: list.hpp:41
Class to hold emojis.
Definition: emoji.hpp:38
static const std::vector< stream_event > parse_stream(const std::string &streamdata)
Split stream into a vector of events.
Definition: easy.cpp:53
const bool valid() const
Returns true if the Entity holds valid data.
Definition: entity.cpp:75
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:131
const system_clock::time_point get_time_point(const string &key) const
Returns the value of key as time_point.
Definition: entity.cpp:188
const bool get_bool(const string &key) const
Returns the value of key as bool.
Definition: entity.cpp:173
const std::vector< string > get_vector(const string &key) const
Returns the value of key as vector.
Definition: entity.cpp:207
const Link get_link() const
Gets the links from the last answer.
Definition: easy.cpp:80
Class to hold applications.
Definition: application.hpp:38
std::pair< event_type, string > stream_event
Used for stream events.
Definition: easy.hpp:110
const uint_fast64_t get_uint64(const string &key) const
Returns the value of key as std::uint_fast64_t.
Definition: entity.cpp:145