mastodon-cpp  0.15.1
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::chrono::system_clock;
38 
39 namespace Mastodon
40 {
44 class Easy : public API
45 {
46 public:
50  enum class event_type
51  {
52  Update,
54  Delete,
55  Undefined
56  };
57 
61  enum class visibility_type
62  {
63  Direct,
64  Private,
65  Unlisted,
66  Public,
67  Undefined
68  };
69 
73  enum class attachment_type
74  {
75  Image,
76  Video,
77  Gifv,
78  Unknown,
79  Undefined
80  };
81 
85  enum class card_type
86  {
87  Link,
88  Photo,
89  Video,
90  Rich,
91  Undefined
92  };
93 
97  enum class notification_type
98  {
99  Mention,
100  Reblog,
101  Favourite,
102  Follow,
103  Undefined
104  };
105 
109  typedef std::pair<event_type, string> stream_event;
110 
116  typedef std::map<Easy::notification_type, bool> alertmap;
117 
123  class Link
124  {
125  public:
129  explicit Link(const string &link_header);
130 
134  const uint_fast64_t next() const;
135 
139  const uint_fast64_t max_id() const;
140 
144  const uint_fast64_t prev() const;
145 
149  const uint_fast64_t since_id() const;
150 
151  private:
152  uint_fast64_t _next;
153  uint_fast64_t _prev;
154  };
155 
165  explicit Easy(const string &instance, const string &access_token);
166 
174  static const std::vector<string> json_array_to_vector(const string &json);
175 
183  static const std::vector<stream_event>
184  parse_stream(const std::string &streamdata);
185 
189  const Link get_link() const;
190 
209  static const string strtime_utc(const system_clock::time_point &timepoint,
210  const string &format);
211 
219  static const string strtime_local(const system_clock::time_point &timepoint,
220  const string &format);
221 
225  class Entity
226  {
227  public:
233  explicit Entity(const string &json);
234 
238  Entity();
239 
245  const void from_string(const string &json);
246 
252  const Json::Value to_object() const;
253 
257  const bool valid() const;
258 
262  const string error() const;
263 
290  const bool was_set() const;
291 
292  protected:
299  const Json::Value get(const string &key) const;
300 
306  const string get_string(const string &key) const;
307 
313  const uint_fast64_t get_uint64(const string &key) const;
314 
320  const double get_double(const string &key) const;
321 
322  // TODO: Maybe an enum would be better?
328  const bool get_bool(const string &key) const;
329 
335  const system_clock::time_point get_time_point(const string &key) const;
336 
343  const std::vector<string> get_vector(const string &key) const;
344 
345  const std::uint_fast64_t stouint64(const string &str) const;
346 
347  private:
348  Json::Value _tree;
349  bool _valid;
350  mutable bool _was_set;
351  };
352 
353  class Account;
354  class Application;
355  class Attachment;
356  class Card;
357  class Context;
358  class Emoji;
359  class Instance;
360  class List;
361  class Mention;
362  class Notification;
363  class Relationship;
364  class Report;
365  class Results;
366  class Status;
367  class Tag;
368  class PushSubscription;
369 
370 protected:
371  inline static const string strtime
372  (const system_clock::time_point &timepoint,
373  const string &format, const bool &utc);
374 };
375 }
376 
377 #endif // MASTODON_EASY_CPP_HPP
Class for the Mastodon API.
Definition: mastodon-cpp.hpp:75
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:38
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
card_type
Describes the card type.
Definition: easy.hpp:85
Child of Mastodon::API with abstract methods.
Definition: easy.hpp:44
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:61
attachment_type
Describes the attachment type.
Definition: easy.hpp:73
notification_type
Describes the notification type.
Definition: easy.hpp:97
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:116
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:50
Class to hold statuses.
Definition: status.hpp:55
Class to hold accounts.
Definition: account.hpp:42
Class to hold contexts.
Definition: context.hpp:41
Definition: mastodon-cpp.hpp:51
Base class for all entities.
Definition: easy.hpp:225
const string error() const
Returns error string sent by the server.
Definition: entity.cpp:80
Class to hold results.
Definition: results.hpp:43
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:109
const uint_fast64_t get_uint64(const string &key) const
Returns the value of key as std::uint_fast64_t.
Definition: entity.cpp:145