Added Easy::Conversation.

This commit is contained in:
tastytea 2019-09-20 02:26:07 +02:00
parent 1b514df555
commit c3e00e2e31
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 237 additions and 1 deletions

View File

@ -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

View File

@ -0,0 +1,68 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#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<Easy::Account> Conversation::accounts() const
{
const Json::Value node = get("accounts");
if (node.isArray())
{
std::vector<Easy::Account> 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");
}

View File

@ -0,0 +1,77 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_EASY_CONVERSATION_HPP
#define MASTODON_CPP_EASY_CONVERSATION_HPP
#include <string>
#include <vector>
#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<Account> 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

View File

@ -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<Easy::Token>;
template struct Easy::return_entity<Easy::PushSubscription>;
template struct Easy::return_entity<Easy::Filter>;
template struct Easy::return_entity<Easy::Poll>;
template struct Easy::return_entity<Easy::Conversation>;
template<typename T>

View File

@ -0,0 +1,89 @@
/* This file is part of mastodon-cpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <exception>
#include <string>
#include <chrono>
#include <catch.hpp>
#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);
}
}
}
}