mastodon-cpp  0.20.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 {
47 class Easy : public API
48 {
49 public:
55  enum class event_type
56  {
57  Update,
59  Delete,
60  Undefined
61  };
62 
68  enum class visibility_type
69  {
70  Direct,
71  Private,
72  Unlisted,
73  Public,
74  Undefined
75  };
76 
82  enum class attachment_type
83  {
84  Image,
85  Video,
86  Gifv,
87  Unknown,
88  Undefined
89  };
90 
96  enum class card_type
97  {
98  Link,
99  Photo,
100  Video,
101  Rich,
102  Undefined
103  };
104 
110  enum class notification_type
111  {
112  Mention,
113  Reblog,
114  Favourite,
115  Follow,
116  Undefined
117  };
118 
124  typedef std::pair<event_type, string> stream_event;
125 
133  typedef std::map<Easy::notification_type, bool> alertmap;
134 
135  class Account;
136  class Application;
137  class Attachment;
138  class Card;
139  class Context;
140  class Emoji;
141  class Instance;
142  class List;
143  class Mention;
144  class Notification;
145  class Relationship;
146  class Report;
147  class Results;
148  class Status;
149  class Tag;
150  class PushSubscription;
151 
159  class Link
160  {
161  public:
167  explicit Link(const string &link_header);
168 
174  uint_fast64_t next() const;
175 
181  uint_fast64_t max_id() const;
182 
188  uint_fast64_t prev() const;
189 
195  uint_fast64_t since_id() const;
196 
197  private:
198  uint_fast64_t _next;
199  uint_fast64_t _prev;
200  };
201 
213  explicit Easy(const string &instance, const string &access_token);
214 
224  static const std::vector<string> json_array_to_vector(const string &json);
225 
235  static const std::vector<stream_event>
236  parse_stream(const std::string &streamdata);
237 
243  const Link get_link() const;
244 
263  static const string strtime_utc(const system_clock::time_point &timepoint,
264  const string &format);
265 
273  static const string strtime_local(const system_clock::time_point &timepoint,
274  const string &format);
275 
276  // #### simple calls ####
277 
290  const Status send_post(const Status &status, uint_fast16_t &error);
291 
297  const Status send_toot(const Status &status, uint_fast16_t &error);
298 
304  class Entity
305  {
306  public:
314  explicit Entity(const string &json);
315 
321  Entity();
322 
330  void from_string(const string &json);
331 
339  const Json::Value to_object() const;
340 
346  virtual bool valid() const = 0;
347 
353  const string error() const;
354 
383  bool was_set() const;
384 
385  protected:
392  const Json::Value get(const string &key) const;
393 
399  const string get_string(const string &key) const;
400 
406  uint_fast64_t get_uint64(const string &key) const;
407 
413  double get_double(const string &key) const;
414 
415  // TODO: Maybe an enum would be better?
421  bool get_bool(const string &key) const;
422 
428  const system_clock::time_point get_time_point(const string &key) const;
429 
436  const std::vector<string> get_vector(const string &key) const;
437 
443  void set(const string &key, const Json::Value &value);
444 
445  std::uint_fast64_t stouint64(const string &str) const;
446 
456  bool check_valid(const std::vector<string> &attributes) const;
457 
458  private:
459  Json::Value _tree;
460  mutable bool _was_set;
461  };
462 
463 protected:
464  inline static const string strtime
465  (const system_clock::time_point &timepoint,
466  const string &format, const bool &utc);
467 };
468 }
469 
470 #endif // MASTODON_EASY_CPP_HPP
Class for the Mastodon API.
Definition: mastodon-cpp.hpp:77
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:44
Entity()
Constructs an empty Entity object.
Definition: entity.cpp:65
Class to hold notifications.
Definition: notification.hpp:48
Class to hold attachments.
Definition: attachment.hpp:44
Class to hold cards.
Definition: card.hpp:42
card_type
Describes the card type.
Definition: easy.hpp:96
Child of Mastodon::API with abstract methods.
Definition: easy.hpp:47
Class to hold instances.
Definition: instance.hpp:45
double get_double(const string &key) const
Returns the value of key as double.
Definition: entity.cpp:162
virtual bool valid() const =0
Returns true if the Entity holds valid data.
Class to hold push subscriptions.
Definition: pushsubscription.hpp:43
const Status send_post(const Status &status, uint_fast16_t &error)
Sends a toot.
Definition: simple_calls.cpp:29
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
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:68
attachment_type
Describes the attachment type.
Definition: easy.hpp:82
bool was_set() const
Returns true if the last requested value was set, false if it was unset.
Definition: entity.cpp:88
notification_type
Describes the notification type.
Definition: easy.hpp:110
const Json::Value to_object() const
Returns the JSON object of the Entity.
Definition: entity.cpp:60
Class to hold mentions.
Definition: mention.hpp:43
std::map< Easy::notification_type, bool > alertmap
Map of &#39;notification type&#39; and &#39;push is requested or not&#39;.
Definition: easy.hpp:133
Class to hold reports.
Definition: report.hpp:42
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:55
uint_fast64_t get_uint64(const string &key) const
Returns the value of key as std::uint_fast64_t.
Definition: entity.cpp:148
bool check_valid(const std::vector< string > &attributes) const
Checks if an Entity is valid.
Definition: entity.cpp:69
Class to hold statuses.
Definition: status.hpp:59
Class to hold accounts.
Definition: account.hpp:46
Class to hold contexts.
Definition: context.hpp:43
void from_string(const string &json)
Replaces the Entity with a new one from a JSON string.
Definition: entity.cpp:36
bool get_bool(const string &key) const
Returns the value of key as bool.
Definition: entity.cpp:176
const Status send_toot(const Status &status, uint_fast16_t &error)
Alias for send_post()
Definition: simple_calls.cpp:24
Definition: mastodon-cpp.hpp:52
Base class for all entities.
Definition: easy.hpp:304
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:83
Class to hold results.
Definition: results.hpp:47
Class to hold relationships.
Definition: relationship.hpp:42
Class to hold lists.
Definition: list.hpp:43
Class to hold emojis.
Definition: emoji.hpp:40
static const std::vector< stream_event > parse_stream(const std::string &streamdata)
Split stream into a vector of events.
Definition: easy.cpp:53
const string get_string(const string &key) const
Returns the value of key as std::string.
Definition: entity.cpp:134
const system_clock::time_point get_time_point(const string &key) const
Returns the value of key as time_point.
Definition: entity.cpp:191
const std::vector< string > get_vector(const string &key) const
Returns the value of key as vector.
Definition: entity.cpp:210
const Link get_link() const
Gets the links from the last answer.
Definition: easy.cpp:80
Class to hold applications.
Definition: application.hpp:40
std::pair< event_type, string > stream_event
Used for stream events.
Definition: easy.hpp:124