mastodon-cpp  0.30.1
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Protected Member Functions | List of all members
Mastodon::Easy Class Reference

Child of Mastodon::API with abstract methods. More...

#include <easy.hpp>

Inheritance diagram for Mastodon::Easy:
Mastodon::API

Classes

class  Account
 Class to hold accounts. More...
 
class  Application
 Class to hold applications. More...
 
class  Attachment
 Class to hold attachments. More...
 
class  Card
 Class to hold cards. More...
 
class  Context
 Class to hold contexts. More...
 
class  Emoji
 Class to hold emojis. More...
 
class  Entity
 Base class for all entities. More...
 
class  Instance
 Class to hold instances. More...
 
class  Link
 Class to hold the Link-header. More...
 
class  List
 Class to hold lists. More...
 
class  Mention
 Class to hold mentions. More...
 
class  Notification
 Class to hold notifications. More...
 
class  PushSubscription
 Class to hold push subscriptions. More...
 
class  Relationship
 Class to hold relationships. More...
 
class  Report
 Class to hold reports. More...
 
class  Results
 Class to hold results. More...
 
class  Status
 Class to hold statuses. More...
 
class  Tag
 Class to hold tags. More...
 

Public Types

enum  event_type { Update, Notification, Delete, Undefined }
 Describes the event type. More...
 
enum  visibility_type {
  Direct, Private, Unlisted, Public,
  Undefined
}
 Describes visibility of toots. More...
 
enum  attachment_type {
  Image, Video, Gifv, Unknown,
  Undefined
}
 Describes the attachment type. More...
 
enum  card_type {
  Link, Photo, Video, Rich,
  Undefined
}
 Describes the card type. More...
 
enum  notification_type {
  Mention, Reblog, Favourite, Follow,
  Undefined
}
 Describes the notification type. More...
 
typedef std::pair< event_type, string > stream_event
 Used for stream events. More...
 
typedef std::map< Easy::notification_type, bool > alertmap
 Map of 'notification type' and 'push is requested or not'. More...
 
- Public Types inherited from Mastodon::API
enum  v1 {
  accounts_id, accounts_verify_credentials, accounts_id_followers, accounts_id_following,
  accounts_id_statuses, accounts_relationships, accounts_search, blocks,
  domain_blocks, endorsements, favourites, follow_requests,
  instance, custom_emojis, lists, accounts_id_lists,
  lists_id_accounts, lists_id, mutes, notifications,
  notifications_id, reports, search, statuses_id,
  statuses_id_context, statuses_id_card, statuses_id_reblogged_by, statuses_id_favourited_by,
  timelines_home, timelines_public, timelines_tag_hashtag, timelines_list_list_id,
  accounts_update_credentials, accounts_id_follow, accounts_id_unfollow, accounts_id_block,
  accounts_id_unblock, accounts_id_mute, accounts_id_unmute, accounts_id_pin,
  accounts_id_unpin, apps, follow_requests_id_authorize, follow_requests_id_reject,
  follows, media, notifications_clear, notifications_dismiss,
  statuses, statuses_id_reblog, statuses_id_unreblog, statuses_id_favourite,
  statuses_id_unfavourite, statuses_id_pin, statuses_id_unpin, statuses_id_mute,
  statuses_id_unmute, media_id, streaming_user, streaming_public,
  streaming_public_local, streaming_hashtag, streaming_list, push_subscription,
  bookmarks, statuses_id_bookmark, statuses_id_unbookmark
}
 A list of all v1 API calls. More...
 
enum  v2 { search }
 A list of all v2 API calls. More...
 
typedef std::map< string, std::vector< string > > parametermap
 Used for passing parameters. More...
 

Public Member Functions

 Easy (const string &instance, const string &access_token)
 Constructs a new Easy object. More...
 
const Link get_link () const
 Gets the links from the last answer. More...
 
const Status send_post (const Status &status, uint_fast16_t &error)
 Sends a toot. More...
 
const Status send_toot (const Status &status, uint_fast16_t &error)
 Alias for send_post() More...
 
const vector< Notificationget_notifications (uint_fast16_t &error, const uint_fast16_t limit=20, const string since_id=0, const string max_id=0)
 Gets notifications. More...
 
- Public Member Functions inherited from Mastodon::API
 API (const string &instance, const string &access_token)
 Constructs a new API object. More...
 
void set_useragent (const string &useragent)
 Sets the useragent. Default is mastodon-cpp/version. More...
 
const string get_useragent () const
 Gets the useragent. More...
 
const string get_instance () const
 Returns the instance. More...
 
uint_fast16_t register_app1 (const string &client_name, const string &redirect_uri, const string &scopes, const string &website, string &client_id, string &client_secret, string &url)
 Register application, step 1/2. More...
 
uint_fast16_t register_app1 (const string &instance, const string &client_name, const string &redirect_uri, const string &scopes, const string &website, string &client_id, string &client_secret, string &url)
 
uint_fast16_t register_app2 (const string &client_id, const string &client_secret, const string &redirect_uri, const string &code, string &access_token)
 Register application, step 2/2. More...
 
uint_fast16_t register_app2 (const string &instance, const string &client_id, const string &client_secret, const string &redirect_uri, const string &code, string &access_token)
 
const string get_header (const string &header) const
 Gets the header from the last answer. More...
 
bool exceptions (const bool &value)
 Turn exceptions on or off. Defaults to off. More...
 
bool exceptions () const
 Returns true if exceptions are turned on, false otherwise.
 
void set_proxy (const string &proxy, const string &userpw="")
 Sets the proxy. More...
 
void get_proxy (string &proxy, string &userpw) const
 For internal use. More...
 
uint_fast16_t get (const Mastodon::API::v1 &call, string &answer)
 Make a GET request which doesn't require parameters. More...
 
uint_fast16_t get (const Mastodon::API::v1 &call, const parametermap &parameters, string &answer)
 Make a GET request which requires parameters. More...
 
uint_fast16_t get (const Mastodon::API::v2 &call, const parametermap &parameters, string &answer)
 Make a GET request which requires parameters. More...
 
uint_fast16_t get (const string &call, string &answer)
 Make a custom GET request. More...
 
uint_fast16_t get (const Mastodon::API::v1 &call, const string &argument, string &answer)
 
uint_fast16_t get (const Mastodon::API::v1 &call, const string &argument, const parametermap &parameters, string &answer)
 
uint_fast16_t get_stream (const Mastodon::API::v1 &call, const parametermap &parameters, string &answer, std::unique_ptr< Mastodon::API::http > &ptr)
 Make a streaming GET request. More...
 
uint_fast16_t get_stream (const Mastodon::API::v1 &call, string &answer, std::unique_ptr< Mastodon::API::http > &ptr)
 Make a streaming GET request. More...
 
uint_fast16_t get_stream (const string &call, string &answer, std::unique_ptr< Mastodon::API::http > &ptr)
 Make a streaming GET request. More...
 
uint_fast16_t get_stream (const Mastodon::API::v1 &call, const string &argument, string &answer, std::unique_ptr< Mastodon::API::http > &ptr)
 
uint_fast16_t patch (const Mastodon::API::v1 &call, const parametermap &parameters, string &answer)
 Make a PATCH request. More...
 
uint_fast16_t post (const Mastodon::API::v1 &call, string &answer)
 Make a POST request which doesn't require parameters. More...
 
uint_fast16_t post (const Mastodon::API::v1 &call, const parametermap &parameters, string &answer)
 Make a POST request which requires parameters. More...
 
uint_fast16_t post (const string &call, const parametermap &parameters, string &answer)
 Make a custom POST request. More...
 
uint_fast16_t post (const Mastodon::API::v1 &call, const string &argument, string &answer)
 
uint_fast16_t post (const Mastodon::API::v1 &call, const string &argument, const parametermap &parameters, string &answer)
 
uint_fast16_t put (const Mastodon::API::v1 &call, const parametermap &parameters, string &answer)
 Make a PUT request which requires a parameters. More...
 
uint_fast16_t put (const string &call, const parametermap &parameters, string &answer)
 Make a custom PUT request. More...
 
uint_fast16_t put (const Mastodon::API::v1 &call, const string &argument, const parametermap &parameters, string &answer)
 
uint_fast16_t del (const Mastodon::API::v1 &call, const parametermap &parameters)
 Make a DELETE request which requires parameters. More...
 
uint_fast16_t del (const string &call, const parametermap &parameters, string &answer)
 Make a custom DELETE request. More...
 
uint_fast16_t del (const Mastodon::API::v1 &call, const string &argument)
 
uint_fast16_t del (const Mastodon::API::v1 &call, const string &argument, const parametermap &parameters)
 

Static Public Member Functions

static const std::vector< string > json_array_to_vector (const string &json)
 Turns a JSON array into a vector of strings. More...
 
static const std::vector< stream_eventparse_stream (const std::string &streamdata)
 Split stream into a vector of events. More...
 
static const string strtime_utc (const system_clock::time_point &timepoint, const string &format)
 Converts a time_point to a string. More...
 
static const string strtime_local (const system_clock::time_point &timepoint, const string &format)
 See strtime_utc. More...
 
- Static Public Member Functions inherited from Mastodon::API
static const string urlencode (const string &str)
 Percent-encodes a string. This is done automatically, unless you make a custom request. More...
 
static const string urldecode (const string &str)
 Decodes a percent-encoded string. More...
 
static const string unescape_html (const string &html)
 Replaces HTML entities with UTF-8 characters. More...
 

Static Protected Member Functions

static const string strtime (const system_clock::time_point &timepoint, const string &format, const bool &utc)
 

Detailed Description

Child of Mastodon::API with abstract methods.

Since
before 0.11.0
Examples:
example12_easy_laststatus.cpp, example13_easy_stream.cpp, example14_easy_treeview.cpp, and example16_account_fields.cpp.

Member Typedef Documentation

◆ alertmap

Map of 'notification type' and 'push is requested or not'.

Used in PushSubscription::alerts().

Since
0.13.3

◆ stream_event

typedef std::pair<event_type, string> Mastodon::Easy::stream_event

Used for stream events.

Since
before 0.11.0

Member Enumeration Documentation

◆ attachment_type

Describes the attachment type.

Since
before 0.11.0
84  {
85  Image,
86  Video,
87  Gifv,
88  Unknown,
89  Undefined
90  };

◆ card_type

Describes the card type.

Since
before 0.11.0
98  {
99  Link,
100  Photo,
101  Video,
102  Rich,
103  Undefined
104  };

◆ event_type

Describes the event type.

Since
before 0.11.0
57  {
58  Update,
60  Delete,
61  Undefined
62  };
Class to hold notifications.
Definition: notification.hpp:48

◆ notification_type

Describes the notification type.

Since
before 0.11.0
112  {
113  Mention,
114  Reblog,
115  Favourite,
116  Follow,
117  Undefined
118  };
Class to hold mentions.
Definition: mention.hpp:43

◆ visibility_type

Describes visibility of toots.

Since
before 0.11.0
70  {
71  Direct,
72  Private,
73  Unlisted,
74  Public,
75  Undefined
76  };

Constructor & Destructor Documentation

◆ Easy()

Easy::Easy ( const string &  instance,
const string &  access_token 
)
explicit

Constructs a new Easy object.

To register your application, leave access_token blank and call register_app1() and register_app2().

Parameters
instanceThe hostname of your instance
access_tokenThe access token
Since
before 0.11.0
28 : API(instance, access_token)
29 {}
API(const string &instance, const string &access_token)
Constructs a new API object.
Definition: mastodon-cpp.cpp:30

Member Function Documentation

◆ get_link()

const Easy::Link Easy::get_link ( ) const

Gets the links from the last answer.

Since
before 0.11.0
81 {
82  return Link(get_header("Link"));
83 }
const string get_header(const string &header) const
Gets the header from the last answer.
Definition: mastodon-cpp.cpp:262

◆ get_notifications()

const vector< Easy::Notification > Easy::get_notifications ( uint_fast16_t &  error,
const uint_fast16_t  limit = 20,
const string  since_id = 0,
const string  max_id = 0 
)

Gets notifications.

Parameters
errorError code
limitMaximum number of notifications
since_idReturn notifications newer than ID
max_idReturn notifications older than ID
Returns
vector of Easy::Notification.
Since
0.21.0
144 {
145  API::parametermap parameters;
146  string answer;
147  error = 0;
148 
149  parameters.insert({ "limit", { std::to_string(limit) } });
150  if (!since_id.empty())
151  {
152  parameters.insert({ "since_id", { since_id } });
153  }
154  if (!max_id.empty())
155  {
156  parameters.insert({ "max_id", { max_id } });
157  }
158 
159  error = API::get(Mastodon::API::v1::notifications, parameters, answer);
160 
161  if (error == 0)
162  {
163  const vector<string> &answer_v = json_array_to_vector(answer);
164  vector<Notification> notifications;
165  notifications.resize(answer_v.size());
166 
167  // Transform vector of strings to vector of Notification.
168  std::transform(answer_v.begin(), answer_v.end(), notifications.begin(),
169  [](const string s)
170  { return Notification(s); });
171 
172  return notifications;
173  }
174  else
175  {
176  ttdebug << "ERROR: Could not get notifications.\n";
177  return { Notification() };
178  }
179 }
Class to hold notifications.
Definition: notification.hpp:48
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
uint_fast16_t get(const Mastodon::API::v1 &call, string &answer)
Make a GET request which doesn&#39;t require parameters.
Definition: get.cpp:207
std::map< string, std::vector< string > > parametermap
Used for passing parameters.
Definition: mastodon-cpp.hpp:189

◆ json_array_to_vector()

const std::vector< string > Easy::json_array_to_vector ( const string &  json)
static

Turns a JSON array into a vector of strings.

Parameters
jsonJSON string holding the array
Returns
vector of strings or an empty vector on error
Since
before 0.11.0
32 {
33  Json::Value json_array;
34  std::stringstream ss(json);
35  ss >> json_array;
36 
37  if (json_array.isArray())
38  {
39  std::vector<string> vec;
40  for (const Json::Value &value : json_array)
41  {
42  vec.push_back(value.toStyledString());
43  }
44  return vec;
45  }
46 
47  ttdebug << "ERROR: JSON string holds no array\n";
48  ttdebug << "String was: " << json << '\n';
49  return {};
50 }

◆ parse_stream()

const std::vector< Easy::stream_event > Easy::parse_stream ( const std::string &  streamdata)
static

Split stream into a vector of events.

Parameters
streamdataData from get_stream()
Returns
vector of stream events
Since
before 0.11.0
54 {
55  string stream = streamdata;
56  std::regex reevent("event: (update|notification|delete)\ndata: (.*)\n");
57  std::smatch match;
58  std::vector<stream_event> vec = {};
59 
60  while (std::regex_search(stream, match, reevent))
61  {
62  const string &event = match[1].str();
63  const string &data = match[2].str();
64  event_type type = event_type::Undefined;
65 
66  if (event.compare("update") == 0)
67  type = event_type::Update;
68  else if (event.compare("notification") == 0)
69  type = event_type::Notification;
70  else if (event.compare("delete") == 0)
71  type = event_type::Delete;
72 
73  vec.push_back(stream_event(type, data));
74  stream = match.suffix().str();
75  }
76 
77  return vec;
78 }
event_type
Describes the event type.
Definition: easy.hpp:56
std::pair< event_type, string > stream_event
Used for stream events.
Definition: easy.hpp:125

◆ send_post()

const Easy::Status Easy::send_post ( const Status status,
uint_fast16_t &  error 
)

Sends a toot.

Parameters
statusThe status to send
errorError code
Returns
The new Easy::Status
Since
0.18.1
32 {
33  API::parametermap parameters;
34  string answer;
35  error = 0;
36 
37  if (!status.content().empty())
38  {
39  parameters.insert({ "status", { status.content() }});
40  }
41  else
42  {
43  ttdebug << "ERROR: Easy::Status::content can not be empty.\n";
44  error = 11;
45  return Status();
46  }
47 
48  if (!status.in_reply_to_id().empty())
49  {
50  parameters.insert({ "in_reply_to_id",
51  { status.in_reply_to_id() }});
52  }
53  if (status.sensitive())
54  {
55  parameters.insert({ "sensitive", { "true" }});
56  }
57  if (!status.spoiler_text().empty())
58  {
59  parameters.insert({ "spoiler_text", { status.spoiler_text() }});
60  }
61  if (status.visibility() != visibility_type::Undefined)
62  {
63  string visibility;
64  switch (status.visibility())
65  {
66  case visibility_type::Direct:
67  visibility = "direct";
68  break;
69  case visibility_type::Private:
70  visibility = "private";
71  break;
72  case visibility_type::Unlisted:
73  visibility = "unlisted";
74  break;
75  case visibility_type::Public:
76  visibility = "public";
77  break;
78  default:
79  break;
80  };
81  parameters.insert({ "visibility", { visibility }});
82  }
83  if (!status.language().empty())
84  {
85  parameters.insert({ "language", { status.language() }});
86  }
87  if (!status.media_attachments().empty())
88  {
89  std::vector<string> media_ids;
90  for (const Attachment &att : status.media_attachments())
91  {
92  API::parametermap param_att;
93  if (!att.file().empty())
94  {
95  param_att.insert({ "file", { att.file() }});
96  }
97  else
98  {
99  ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
100  error = 11;
101  return Status();
102  }
103  if (!att.description().empty())
104  {
105  param_att.insert({ "description", { att.description() }});
106  }
107  if (!att.focus().empty())
108  {
109  param_att.insert({ "focus",
110  { std::to_string(att.focus()[0]) + ',' +
111  std::to_string(att.focus()[1]) }});
112  }
113 
114  error = post(API::v1::media, param_att, answer);
115  if (error == 0)
116  {
117  Attachment attachment(answer);
118  media_ids.push_back(attachment.id());
119  }
120  else
121  {
122  ttdebug << "ERROR: Could not upload file.\n";
123  return Status();
124  }
125  }
126 
127  parameters.insert({ "media_ids", media_ids });
128  }
129 
130  error = post(API::v1::statuses, parameters, answer);
131  if (error == 0)
132  {
133  return Status(answer);
134  }
135  else
136  {
137  return Status();
138  }
139 }
bool sensitive() const
Returns true if the attachments should be hidden by default.
Definition: status.cpp:244
Class to hold attachments.
Definition: attachment.hpp:44
uint_fast16_t post(const Mastodon::API::v1 &call, string &answer)
Make a POST request which doesn&#39;t require parameters.
Definition: post.cpp:140
const std::vector< Attachment > media_attachments() const
Returns the attachments.
Definition: status.cpp:162
const string language() const
Returns the language of the status.
Definition: status.cpp:151
Class to hold statuses.
Definition: status.hpp:59
visibility_type visibility() const
Returns the visibility of the status.
Definition: status.cpp:292
const string content() const
Returns content of status.
Definition: status.cpp:93
std::map< string, std::vector< string > > parametermap
Used for passing parameters.
Definition: mastodon-cpp.hpp:189
const string spoiler_text() const
Returns the spoiler text.
Definition: status.cpp:255
const string in_reply_to_id() const
Returns the ID of the status it replies to.
Definition: status.cpp:135

◆ send_toot()

const Easy::Status Easy::send_toot ( const Status status,
uint_fast16_t &  error 
)

Alias for send_post()

Since
0.17.0
27 {
28  return send_post(status, error);
29 }
const Status send_post(const Status &status, uint_fast16_t &error)
Sends a toot.
Definition: simple_calls.cpp:31

◆ strtime_local()

const string Easy::strtime_local ( const system_clock::time_point &  timepoint,
const string &  format 
)
static

See strtime_utc.

Returns
The local time as string
Since
0.11.0
93 {
94  return strtime(timepoint, format, false);
95 }

◆ strtime_utc()

const string Easy::strtime_utc ( const system_clock::time_point &  timepoint,
const string &  format 
)
static

Converts a time_point to a string.

The return value can not exceed 1023 chars.

Parameters
timepointThe timepoint
formatThe format of the string, same as with strftime.

Example:

auto timepoint = status.created_at();
cout << Easy::strtime_utc(timepoint, "%F, %T") << '\n';
Returns
The UTC time as string
Since
0.11.0
87 {
88  return strtime(timepoint, format, true);
89 }

The documentation for this class was generated from the following files: