mastodon-cpp  0.20.0
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...
 
- 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
83  {
84  Image,
85  Video,
86  Gifv,
87  Unknown,
88  Undefined
89  };

◆ card_type

Describes the card type.

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

◆ event_type

Describes the event type.

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

◆ notification_type

Describes the notification type.

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

◆ visibility_type

Describes visibility of toots.

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

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

◆ 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:55
std::pair< event_type, string > stream_event
Used for stream events.
Definition: easy.hpp:124

◆ 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. If the URL has permanently changed, 13 is returned and answer is set to the new URL.
Returns
The new Easy::Status
Since
0.18.1
30 {
31  API::parametermap parameters;
32  string answer;
33  error = 0;
34 
35  if (!status.content().empty())
36  {
37  parameters.insert({ "status", { status.content() }});
38  }
39  else
40  {
41  ttdebug << "ERROR: Easy::Status::content can not be empty.\n";
42  error = 11;
43  return Status();
44  }
45 
46  if (status.in_reply_to_id() != 0)
47  {
48  parameters.insert({ "in_reply_to_id",
49  { std::to_string(status.in_reply_to_id()) }});
50  }
51  if (status.sensitive())
52  {
53  parameters.insert({ "sensitive", { "true" }});
54  }
55  if (!status.spoiler_text().empty())
56  {
57  parameters.insert({ "spoiler_text", { status.spoiler_text() }});
58  }
59  if (status.visibility() != visibility_type::Undefined)
60  {
61  string visibility;
62  switch (status.visibility())
63  {
64  case visibility_type::Direct:
65  visibility = "direct";
66  break;
67  case visibility_type::Private:
68  visibility = "private";
69  break;
70  case visibility_type::Unlisted:
71  visibility = "unlisted";
72  break;
73  case visibility_type::Public:
74  visibility = "public";
75  break;
76  default:
77  break;
78  };
79  parameters.insert({ "visibility", { visibility }});
80  }
81  if (!status.language().empty())
82  {
83  parameters.insert({ "language", { status.language() }});
84  }
85  if (!status.media_attachments().empty())
86  {
87  std::vector<string> media_ids;
88  for (const Attachment &att : status.media_attachments())
89  {
90  API::parametermap param_att;
91  if (!att.file().empty())
92  {
93  param_att.insert({ "file", { att.file() }});
94  }
95  else
96  {
97  ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
98  error = 11;
99  return Status();
100  }
101  if (!att.description().empty())
102  {
103  param_att.insert({ "description", { att.description() }});
104  }
105  if (!att.focus().empty())
106  {
107  param_att.insert({ "focus",
108  { std::to_string(att.focus()[0]) + ',' +
109  std::to_string(att.focus()[1]) }});
110  }
111 
112  error = post(API::v1::media, param_att, answer);
113  if (error == 0)
114  {
115  Attachment attachment(answer);
116  media_ids.push_back(std::to_string(attachment.id()));
117  }
118  else
119  {
120  ttdebug << "ERROR: Could not upload file.\n";
121  return Status();
122  }
123  }
124 
125  parameters.insert({ "media_ids", media_ids });
126  }
127 
128  error = post(API::v1::statuses, parameters, answer);
129  if (error == 0)
130  {
131  return Status(answer);
132  }
133  else
134  {
135  return Status();
136  }
137 }
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
uint_fast64_t 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
25 {
26  return send_post(status, error);
27 }
const Status send_post(const Status &status, uint_fast16_t &error)
Sends a toot.
Definition: simple_calls.cpp:29

◆ 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: