From d2398008e894366e25128dcd5cae9f908e944bdb Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 21 Mar 2018 20:29:33 +0100 Subject: [PATCH] added Attachment class --- CMakeLists.txt | 2 +- src/easy/account.cpp | 24 +++++++- src/easy/attachment.cpp | 128 ++++++++++++++++++++++++++++++++++++++++ src/easy/easy.cpp | 2 +- src/easy/easy.hpp | 41 ++++++++++--- 5 files changed, 187 insertions(+), 10 deletions(-) create mode 100644 src/easy/attachment.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4152d6c..73fdaa3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.7.1 + VERSION 0.7.2 LANGUAGES CXX ) diff --git a/src/easy/account.cpp b/src/easy/account.cpp index bdfc479..38d75e2 100644 --- a/src/easy/account.cpp +++ b/src/easy/account.cpp @@ -1,6 +1,6 @@ /* 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. @@ -177,6 +177,28 @@ const bool Account::locked() const return false; } +const bool Account::has_moved() const +{ + if (_tree["moved"].isObject()) + { + return true; + } + + return false; +} + +const Account Account::moved() const +{ + if (has_moved()) + { + // TODO: Find an account with this node and test + return Account(_tree["moved"].toStyledString()); + } + + ttdebug << "Could not get account data: moved\n"; + return Account(""); +} + const string Account::note() const { if (_tree["note"].isString()) diff --git a/src/easy/attachment.cpp b/src/easy/attachment.cpp new file mode 100644 index 0000000..296c9d2 --- /dev/null +++ b/src/easy/attachment.cpp @@ -0,0 +1,128 @@ +/* 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 +#include +#include +#include "easy.hpp" +#include "macros.hpp" + +using namespace Mastodon; +using Attachment = Easy::Attachment; +using std::string; + +Attachment::Attachment(const string &json) +{ + std::stringstream ss(json); + ss >> _tree; + + if (_tree.isNull()) + { + std::cerr << "ERROR: Could not build Attachment from JSON string\n"; + ttdebug << "String was: " << json << '\n'; + } + else + { + _valid = true; + } +} + +const bool Attachment::valid() const +{ + return _valid; +} + +const string Attachment::description() const +{ + if (_tree["description"].isString()) + { + return _tree["description"].asString(); + } + + ttdebug << "Could not get attachment data: description\n"; + return ""; +} + +const std::uint64_t Attachment::id() const +{ + if (_tree["id"].isUInt64()) + { + return _tree["id"].asUInt64(); + } + + ttdebug << "Could not get attachment data: id\n"; + return 0; +} + +const string Attachment::preview_url() const +{ + if (_tree["preview_url"].isString()) + { + return _tree["preview_url"].asString(); + } + + ttdebug << "Could not get attachment data: preview_url\n"; + return ""; +} + +const string Attachment::remote_url() const +{ + if (_tree["remote_url"].isString()) + { + return _tree["remote_url"].asString(); + } + + ttdebug << "Could not get attachment data: remote_url\n"; + return ""; +} + +const string Attachment::text_url() const +{ + if (_tree["text_url"].isString()) + { + return _tree["text_url"].asString(); + } + + ttdebug << "Could not get attachment data: text_url\n"; + return ""; +} + +const Easy::attachment_type Attachment::type() const +{ + const string strtype = _tree["type"].asString(); + if (strtype.compare("image")) + return attachment_type::image; + else if (strtype.compare("video")) + return attachment_type::video; + else if (strtype.compare("gifv")) + return attachment_type::gifv; + else if (strtype.compare("unknown")) + return attachment_type::unknown; + + ttdebug << "Could not get account data: type\n"; + return attachment_type::unknown; +} + +const string Attachment::url() const +{ + if (_tree["url"].isString()) + { + return _tree["url"].asString(); + } + + ttdebug << "Could not get attachment data: url\n"; + return ""; +} diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index 1ee446c..91672cb 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -1,6 +1,6 @@ /* 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. diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 2b3ea39..e545b49 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -180,7 +180,7 @@ public: const visibility privacy() const; /*! - * @brief Returns if media is amrked as sensitive by default + * @brief Returns if media is marked as sensitive by default */ const bool sensitive() const; @@ -222,14 +222,41 @@ public: */ const bool valid() const; - const uint64_t id() const; - const attachment_type type() const; - const string url() const; - const string remote_url() const; - const string preview_url() const; - const string text_url() const; + /*! + * @brief Returns the image description + */ const string description() const; + /*! + * @brief Returns the ID of the attachment + */ + const uint64_t id() const; + + /*! + * @brief Returns the URL of the preview image + */ + const string preview_url() const; + + /*! + * @brief Returns the remote URL of the original image + */ + const string remote_url() const; + + /*! + * @brief Returns shorter URL for the image + */ + const string text_url() const; + + /*! + * @brief Returns attachment type + */ + const attachment_type type() const; + + /*! + * @brief Returns URL of the locally hosted version of the image + */ + const string url() const; + private: Json::Value _tree; bool _valid;