diff --git a/CMakeLists.txt b/CMakeLists.txt index 09bc530..2bb2471 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.6) project (mastodon-cpp - VERSION 0.103.0 + VERSION 0.104.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index 20ace4b..12760ba 100644 --- a/README.md +++ b/README.md @@ -270,10 +270,11 @@ Run `make package` from the build directory to generate a tar.gz archive. * [x] GET /api/v1/favourites * [x] POST /api/v1/statuses/:id/favourite * [x] POST /api/v1/statuses/:id/unfavourite - * [ ] GET /api/v1/filters - * [ ] POST /api/v1/filters - * [ ] GET /api/v1/filters/:id - * [ ] DELETE /api/v1/filters/:id + * [x] GET /api/v1/filters + * [x] POST /api/v1/filters + * [x] GET /api/v1/filters/:id + * [x] PUT /api/v1/filters/:id + * [x] DELETE /api/v1/filters/:id * [x] GET /api/v1/follow_requests * [x] POST /api/v1/follow_requests/:id/authorize * [x] POST /api/v1/follow_requests/:id/reject diff --git a/src/api/delete.cpp b/src/api/delete.cpp index a9e90b6..aa90fce 100644 --- a/src/api/delete.cpp +++ b/src/api/delete.cpp @@ -35,25 +35,41 @@ return_call API::del(const Mastodon::API::v1 &call, switch (call) { - case v1::domain_blocks: - strcall = "/api/v1/domain_blocks"; - break; - case v1::lists_id: - strcall = "/api/v1/lists/" + strid; - break; - case v1::lists_id_accounts: - strcall = "/api/v1/lists/" + strid + "/accounts"; - break; - case v1::statuses_id: - strcall = "/api/v1/statuses/" + strid; - break; - case v1::push_subscription: - strcall = "/api/v1/push/subscription"; - break; - default: - ttdebug << "ERROR: Invalid argument.\n"; - return { 22, "Invalid argument", 0, "" }; - break; + case v1::domain_blocks: + { + strcall = "/api/v1/domain_blocks"; + break; + } + case v1::lists_id: + { + strcall = "/api/v1/lists/" + strid; + break; + } + case v1::lists_id_accounts: + { + strcall = "/api/v1/lists/" + strid + "/accounts"; + break; + } + case v1::statuses_id: + { + strcall = "/api/v1/statuses/" + strid; + break; + } + case v1::push_subscription: + { + strcall = "/api/v1/push/subscription"; + break; + } + case v1::filters_id: + { + strcall = "/api/v1/filters/" + strid; + break; + } + default: + { + ttdebug << "ERROR: Invalid argument.\n"; + return { 22, "Invalid argument", 0, "" }; + } } return del(strcall, params); diff --git a/src/api/get.cpp b/src/api/get.cpp index d31e627..95ba505 100644 --- a/src/api/get.cpp +++ b/src/api/get.cpp @@ -221,6 +221,16 @@ const return_call API::get(const Mastodon::API::v1 &call, strcall = "/api/v1/apps/verify_credentials"; break; } + case v1::filters: + { + strcall = "/api/v1/filters"; + break; + } + case v1::filters_id: + { + strcall = "/api/v1/filters/" + strid; + break; + } default: { ttdebug << "ERROR: Invalid argument.\n"; diff --git a/src/api/post.cpp b/src/api/post.cpp index 90a1acc..9c2ad78 100644 --- a/src/api/post.cpp +++ b/src/api/post.cpp @@ -190,6 +190,11 @@ return_call API::post(const Mastodon::API::v1 &call, strcall = "/api/v1/statuses/" + strid + "/unbookmark"; break; } + case v1::filters: + { + strcall = "/api/v1/filters"; + break; + } default: { ttdebug << "ERROR: Invalid argument.\n"; diff --git a/src/api/put.cpp b/src/api/put.cpp index c454686..dc6150f 100644 --- a/src/api/put.cpp +++ b/src/api/put.cpp @@ -35,19 +35,31 @@ return_call API::put(const Mastodon::API::v1 &call, switch (call) { - case v1::lists_id: - strcall = "/api/v1/lists/" + strid; - break; - case v1::media_id: - strcall = "/api/v1/media/" + strid; - break; - case v1::push_subscription: - strcall = "/api/v1/push/subscription"; - break; - default: - ttdebug << "ERROR: Invalid argument.\n"; - return { 22, "Invalid argument", 0, "" }; - break; + case v1::lists_id: + { + strcall = "/api/v1/lists/" + strid; + break; + } + case v1::media_id: + { + strcall = "/api/v1/media/" + strid; + break; + } + case v1::push_subscription: + { + strcall = "/api/v1/push/subscription"; + break; + } + case v1::filters_id: + { + strcall = "/api/v1/filters/" + strid; + break; + } + default: + { + ttdebug << "ERROR: Invalid argument.\n"; + return { 22, "Invalid argument", 0, "" }; + } } return put(strcall, params); diff --git a/src/easy/all.hpp b/src/easy/all.hpp index b4b4894..3a81d0d 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -37,6 +37,7 @@ #include "easy/entities/tag.hpp" #include "easy/entities/token.hpp" #include "easy/entities/pushsubscription.hpp" + #include "easy/entities/filter.hpp" #else #include #include @@ -56,6 +57,7 @@ #include #include #include + #include #endif #endif // MASTODON_CPP_EASY_ALL_HPP diff --git a/src/easy/entities/filter.cpp b/src/easy/entities/filter.cpp new file mode 100644 index 0000000..882255e --- /dev/null +++ b/src/easy/entities/filter.cpp @@ -0,0 +1,84 @@ +/* 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 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 "filter.hpp" + +using std::vector; +using namespace Mastodon; +using Filter = Easy::Filter; + +bool Filter::valid() const +{ + return Entity::check_valid({ "id", + "phrase", + "context", + "irreversible", + "whole_word" }); +} + +const string Filter::id() const +{ + return get_string("id"); +} + +const string Filter::phrase() const +{ + return get_string("phrase"); +} + +const vector Filter::context() const +{ + const Json::Value node = get("context"); + if (node.isArray()) + { + vector vec; + std::transform(node.begin(), node.end(), std::back_inserter(vec), + [](const Json::Value &value) + { + const string strtype = value.asString(); + if (strtype == "home") + return Easy::context_type::Home; + else if (strtype == "notifications") + return Easy::context_type::Notifications; + else if (strtype == "public") + return Easy::context_type::Public; + else if (strtype == "thread") + return Easy::context_type::Thread; + else + return context_type::Undefined; + }); + return vec; + } + + return {}; +} + +const Easy::time Filter::expires_at() const +{ + return get_time("expires_at"); +} + +bool Filter::irreversible() const +{ + return get_bool("irreversible"); +} + +bool Filter::whole_word() const +{ + return get_bool("whole_word"); +} diff --git a/src/easy/entities/filter.hpp b/src/easy/entities/filter.hpp new file mode 100644 index 0000000..da7d64c --- /dev/null +++ b/src/easy/entities/filter.hpp @@ -0,0 +1,98 @@ +/* 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 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 . + */ + +#ifndef MASTODON_CPP_EASY_FILTER_HPP +#define MASTODON_CPP_EASY_FILTER_HPP + +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy/entity.hpp" +#else + #include + #include +#endif + +using std::string; + +namespace Mastodon +{ +namespace Easy +{ + /*! + * @brief Class to hold filters. + * + * @since 0.104.0 + */ + class Filter : public Entity + { + public: + using Entity::Entity; + + virtual bool valid() const override; + + /*! + * @brief Returns the id of the filter + * + * @since 0.104.0 + */ + const string id() const; + + /*! + * @brief Returns the phrase to filter. + * + * @since 0.104.0 + */ + const string phrase() const; + + /*! + * @brief Returns the contexts in which to filter. + * + * @since 0.104.0 + */ + const vector context() const; + + /*! + * @brief Returns the expiration time of the filter. + * + * @since 0.104.0 + */ + const Easy::time expires_at() const; + + /*! + * @brief Returns if the phrase should disappear irreversibly. + * + * If set to true, the filtered posts will be hidden even if filter is + * later removed. + * + * @since 0.104.0 + */ + bool irreversible() const; + + /*! + * @brief Returns if the filter should only be applied if it matches + * the whole word. + * + * @since 0.104.0 + */ + bool whole_word() const; +}; +} +} + +#endif // MASTODON_CPP_EASY_FILTER_HPP diff --git a/src/easy/return_types_easy.cpp b/src/easy/return_types_easy.cpp index 4b2f5b3..55ea6d2 100644 --- a/src/easy/return_types_easy.cpp +++ b/src/easy/return_types_easy.cpp @@ -32,6 +32,7 @@ #include "easy/entities/tag.hpp" #include "easy/entities/token.hpp" #include "easy/entities/pushsubscription.hpp" +#include "easy/entities/filter.hpp" using namespace Mastodon; @@ -88,6 +89,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 @@ -132,3 +134,4 @@ template struct Easy::return_entity_vector; template struct Easy::return_entity_vector; template struct Easy::return_entity_vector; template struct Easy::return_entity_vector; +template struct Easy::return_entity_vector; diff --git a/src/easy/types_easy.hpp b/src/easy/types_easy.hpp index bfd46ab..7473985 100644 --- a/src/easy/types_easy.hpp +++ b/src/easy/types_easy.hpp @@ -102,6 +102,20 @@ namespace Easy Undefined }; + /*! + * @brief Describes the context. + * + * @since 0.104.0 + */ + enum class context_type + { + Home, + Notifications, + Public, + Thread, + Undefined + }; + /*! * @brief Used for stream events. * diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index cff938d..842dbee 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -115,15 +115,17 @@ const curlpp::Forms API::maptoformdata(const parameters &map) } else { - string key = it.key; - if (key == "account_ids" || - key == "exclude_types" || - key == "media_ids") - { - key += "[]"; - } - formdata.push_back( - new curlpp::FormParts::Content(key, it.values.front())); + // Append [] to array keys. + string key = it.key; + if (key == "account_ids" + || key == "exclude_types" + || key == "media_ids" + || key == "context") + { + key += "[]"; + } + formdata.push_back( + new curlpp::FormParts::Content(key, it.values.front())); } } else diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index f37a417..ee5e12d 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -209,8 +209,8 @@ namespace Mastodon statuses_id_favourite, statuses_id_unfavourite, - // filters, - // filters_id, + filters, + filters_id, follow_requests, follow_requests_id_authorize,