From 43504f6ef703a179dc7d2e3b3953e1079aab5dcd Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 14 Jun 2018 04:11:28 +0200 Subject: [PATCH] Added Easy::send_toot (Closes #1) --- src/easy/easy.hpp | 49 ++++++++---- src/easy/entities/attachment.cpp | 2 +- src/easy/entities/attachment.hpp | 2 +- src/easy/simple_calls.cpp | 132 +++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 19 deletions(-) create mode 100644 src/easy/simple_calls.cpp diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 2ba2366..317235d 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -34,6 +34,7 @@ using std::string; using std::uint_fast64_t; +using std::uint_fast16_t; using std::chrono::system_clock; namespace Mastodon @@ -115,6 +116,23 @@ public: */ typedef std::map alertmap; + class Account; + class Application; + class Attachment; + class Card; + class Context; + class Emoji; + class Instance; + class List; + class Mention; + class Notification; + class Relationship; + class Report; + class Results; + class Status; + class Tag; + class PushSubscription; + /*! * @brief Class to hold the `Link`-header. * @@ -219,6 +237,20 @@ public: static const string strtime_local(const system_clock::time_point &timepoint, const string &format); + // #### simple calls #### + + /*! + * @brief Sends a toot. + * + * @param status The status to send + * @param error @ref error "Error code". If the URL has permanently + * changed, 13 is returned and answer is set to the new + * URL. + * + * @return The new Easy::Status + */ + const Status send_toot(const Status &status, uint_fast16_t error = 0); + /*! * @brief Base class for all entities. */ @@ -357,23 +389,6 @@ public: mutable bool _was_set; }; - class Account; - class Application; - class Attachment; - class Card; - class Context; - class Emoji; - class Instance; - class List; - class Mention; - class Notification; - class Relationship; - class Report; - class Results; - class Status; - class Tag; - class PushSubscription; - protected: inline static const string strtime (const system_clock::time_point &timepoint, diff --git a/src/easy/entities/attachment.cpp b/src/easy/entities/attachment.cpp index 582e408..c155099 100644 --- a/src/easy/entities/attachment.cpp +++ b/src/easy/entities/attachment.cpp @@ -62,7 +62,7 @@ const std::chrono::duration Attachment::duration() const return std::chrono::duration(sec); } -const string Attachment::file() +const string Attachment::file() const { return get_string("file"); } diff --git a/src/easy/entities/attachment.hpp b/src/easy/entities/attachment.hpp index 5ec694a..3cc9c7e 100644 --- a/src/easy/entities/attachment.hpp +++ b/src/easy/entities/attachment.hpp @@ -91,7 +91,7 @@ namespace Mastodon * * @since 0.17.0 */ - const string file(); + const string file() const; /*! * @brief Sets file to upload diff --git a/src/easy/simple_calls.cpp b/src/easy/simple_calls.cpp new file mode 100644 index 0000000..0ee0f89 --- /dev/null +++ b/src/easy/simple_calls.cpp @@ -0,0 +1,132 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2018 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "easy.hpp" +#include "macros.hpp" +#include "easy/entities/status.hpp" +#include "easy/entities/attachment.hpp" + +using namespace Mastodon; + +const Easy::Status Easy::send_toot(const Status &status, uint_fast16_t error) +{ + API::parametermap parameters; + string answer; + error = 0; + + if (!status.content().empty()) + { + parameters.insert({ "status", { status.content() }}); + } + else + { + ttdebug << "ERROR: Easy::Status::content can not be empty.\n"; + error = 11; + return Status(); + } + + if (status.in_reply_to_id() != 0) + { + parameters.insert({ "in_reply_to_id", + { std::to_string(status.in_reply_to_id()) }}); + } + if (status.sensitive()) + { + parameters.insert({ "sensitive", { "true" }}); + } + if (!status.spoiler_text().empty()) + { + parameters.insert({ "spoiler_text", { status.spoiler_text() }}); + } + if (status.visibility() != visibility_type::Undefined) + { + string visibility; + switch (status.visibility()) + { + case visibility_type::Direct: + visibility = "direct"; + break; + case visibility_type::Private: + visibility = "private"; + break; + case visibility_type::Unlisted: + visibility = "unlisted"; + break; + case visibility_type::Public: + visibility = "public"; + break; + default: + break; + }; + parameters.insert({ "visibility", { visibility }}); + } + if (!status.language().empty()) + { + parameters.insert({ "language", { status.language() }}); + } + if (!status.media_attachments().empty()) + { + std::vector media_ids; + for (const Attachment &att : status.media_attachments()) + { + API::parametermap param_att; + if (!att.file().empty()) + { + param_att.insert({ "file", { att.file() }}); + } + else + { + ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n"; + error = 11; + return Status(); + } + if (!att.description().empty()) + { + param_att.insert({ "description", { att.description() }}); + } + if (!att.focus().empty()) + { + param_att.insert({ "focus", + { std::to_string(att.focus()[0]) + ',' + + std::to_string(att.focus()[1]) }}); + } + + error = post(API::v1::media, param_att, answer); + if (error == 0) + { + Attachment attachment(answer); + media_ids.push_back(std::to_string(attachment.id())); + } + else + { + ttdebug << "ERROR: Could not upload file.\n"; + return Status(); + } + } + + parameters.insert({ "media_ids", media_ids }); + } + + error = post(API::v1::statuses, parameters, answer); + if (error == 0) + { + return Status(answer); + } + else + { + return Status(); + } +}