diff --git a/README.adoc b/README.adoc index ebb6cb7..f16834b 100644 --- a/README.adoc +++ b/README.adoc @@ -390,7 +390,7 @@ strings and you can use unsupported fields in an `Entity` by converting it to * [x] Attachment * [x] Card * [x] Context -* [ ] Conversation +* [x] Conversation * [x] Emoji * [x] Filter * [x] Instance diff --git a/src/easy/entities/conversation.cpp b/src/easy/entities/conversation.cpp new file mode 100644 index 0000000..4a81c51 --- /dev/null +++ b/src/easy/entities/conversation.cpp @@ -0,0 +1,68 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2018, 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include "conversation.hpp" +#include "debug.hpp" + +using namespace Mastodon; +using Conversation = Easy::Conversation; + +bool Conversation::valid() const +{ + return Entity::check_valid( + { + "id", + "accounts", + "unread" + }); +} + +const string Conversation::id() const +{ + return get_string("id"); +} + +const std::vector Conversation::accounts() const +{ + const Json::Value node = get("accounts"); + if (node.isArray()) + { + std::vector vec; + std::transform(node.begin(), node.end(), std::back_inserter(vec), + [](const Json::Value &value) + { return Easy::Account(value); }); + return vec; + } + + return {}; +} + +const Easy::Status Conversation::last_status() const +{ + const Json::Value &node = get("last_status"); + if (!node.isNull()) + { + return Easy::Status(node); + } + + return {}; +} + +bool Conversation::unread() const +{ + return get_bool("unread"); +} diff --git a/src/easy/entities/conversation.hpp b/src/easy/entities/conversation.hpp new file mode 100644 index 0000000..19906d5 --- /dev/null +++ b/src/easy/entities/conversation.hpp @@ -0,0 +1,77 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2018, 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODON_CPP_EASY_CONVERSATION_HPP +#define MASTODON_CPP_EASY_CONVERSATION_HPP + +#include +#include + +#include "../../mastodon-cpp.hpp" +#include "../entity.hpp" +#include "account.hpp" +#include "status.hpp" + +using std::string; + +namespace Mastodon +{ +namespace Easy +{ + /*! + * @brief Class to hold conversations. + * + * @since 0.107.0 + */ + class Conversation : public Entity + { + public: + using Entity::Entity; + + virtual bool valid() const override; + + /*! + * @brief Returns the id of the conversation. + * + * @since 0.107.0 + */ + const string id() const; + + /*! + * @brief Returns the participating accounts. + * + * @since 0.107.0 + */ + const std::vector accounts() const; + + /*! + * @brief Returns the last status. + * + * @since 0.107.0 + */ + const Status last_status() const; + + /*! + * @brief Returns true if unread. + * + * @since 0.107.0 + */ + bool unread() const; + }; +} +} + +#endif // MASTODON_CPP_EASY_CONVERSATION_HPP diff --git a/src/easy/return_types_easy.cpp b/src/easy/return_types_easy.cpp index e9f8af9..c2288af 100644 --- a/src/easy/return_types_easy.cpp +++ b/src/easy/return_types_easy.cpp @@ -34,6 +34,7 @@ #include "easy/entities/pushsubscription.hpp" #include "easy/entities/filter.hpp" #include "easy/entities/poll.hpp" +#include "easy/entities/conversation.hpp" using namespace Mastodon; @@ -94,6 +95,7 @@ template struct Easy::return_entity; template struct Easy::return_entity; template struct Easy::return_entity; template struct Easy::return_entity; +template struct Easy::return_entity; template diff --git a/tests/entities/conversation.cpp b/tests/entities/conversation.cpp new file mode 100644 index 0000000..0ea547b --- /dev/null +++ b/tests/entities/conversation.cpp @@ -0,0 +1,89 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include "easy/entities/conversation.hpp" +#include "easy/easy.hpp" + +using std::string; +using std::chrono::system_clock; + +using namespace Mastodon; + +SCENARIO ("Easy::Conversation works as intended", "[entity]") +{ + GIVEN ("An Easy::Conversation object") + { + Easy::Conversation conversation; + bool exception = false; + + WHEN ("It is initialized with valid conversation data") + { + const string data = + "\"id\":\"1234567\"," + "\"accounts\":[" + "{\"id\":\"98765\"}," + "{\"id\":\"56789\"}]," + "\"last_status\":null," + "\"unread\":false}"; + + try + { + conversation.from_string(data); + } + catch (const std::exception &e) + { + exception = true; + } + + THEN ("No exception is thrown") + AND_THEN ("Conversation is valid") + AND_THEN ("The attributes are set to the right values") + { + REQUIRE_FALSE(exception); + REQUIRE(conversation.valid()); + REQUIRE(conversation.id() == "1234567"); + REQUIRE(conversation.accounts().size() == 2); + REQUIRE(conversation.unread() == false); + } + } + + WHEN ("It is initialized with an empty string") + { + try + { + conversation.from_string(""); + } + catch (const std::exception &e) + { + exception = true; + } + + THEN ("No exception is thrown") + AND_THEN ("It is not valid") + AND_THEN ("id is empty") + { + REQUIRE_FALSE(exception); + REQUIRE_FALSE(conversation.valid()); + REQUIRE(conversation.id() == ""); + REQUIRE(conversation.accounts().size() == 0); + } + } + } +}