This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/src/easy/simple_calls.cpp

172 lines
5.4 KiB
C++
Raw Normal View History

2018-06-14 04:11:28 +02:00
/* This file is part of mastodon-cpp.
2019-03-20 06:15:43 +01:00
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
2018-06-14 04:11:28 +02:00
* This program is free software: you can redistribute it and/or modify
2019-08-15 22:53:38 +02:00
* it under the terms of the GNU Affero General Public License as published by
2018-06-14 04:11:28 +02:00
* 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
2019-08-15 22:53:38 +02:00
* GNU Affero General Public License for more details.
2018-06-14 04:11:28 +02:00
*
2019-08-15 22:53:38 +02:00
* You should have received a copy of the GNU Affero General Public License
2018-06-14 04:11:28 +02:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2019-01-26 06:54:47 +01:00
#include <algorithm>
2018-06-14 04:11:28 +02:00
#include "easy.hpp"
2019-02-22 11:35:06 +01:00
#include "debug.hpp"
2018-06-14 04:11:28 +02:00
#include "easy/entities/status.hpp"
#include "easy/entities/attachment.hpp"
2019-01-26 06:54:47 +01:00
#include "easy/entities/notification.hpp"
2018-06-14 04:11:28 +02:00
using namespace Mastodon::Easy;
2018-06-14 04:11:28 +02:00
const return_entity<Status> API::send_toot(const Status &status)
{
return send_post(status);
}
const return_entity<Status> API::send_post(const Status &status)
2018-06-14 04:11:28 +02:00
{
2019-04-12 01:10:33 +02:00
parameters params;
2018-06-14 04:11:28 +02:00
if (!status.content().empty())
{
2019-04-12 01:10:33 +02:00
params.push_back({ "status", { status.content() }});
2018-06-14 04:11:28 +02:00
}
else
{
ttdebug << "ERROR: Easy::Status::content can not be empty.\n";
return { error::INVALID_ARGUMENT,
"Easy::Status::content can not be empty", 0, Status() };
2018-06-14 04:11:28 +02:00
}
if (!status.in_reply_to_id().empty())
2018-06-14 04:11:28 +02:00
{
2019-04-12 01:10:33 +02:00
params.push_back({ "in_reply_to_id",
{ status.in_reply_to_id() }});
2018-06-14 04:11:28 +02:00
}
if (status.sensitive())
{
2019-04-12 01:10:33 +02:00
params.push_back({ "sensitive", { "true" }});
2018-06-14 04:11:28 +02:00
}
if (!status.spoiler_text().empty())
{
2019-04-12 01:10:33 +02:00
params.push_back({ "spoiler_text", { status.spoiler_text() }});
2018-06-14 04:11:28 +02:00
}
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;
};
2019-04-12 01:10:33 +02:00
params.push_back({ "visibility", { visibility }});
2018-06-14 04:11:28 +02:00
}
if (!status.language().empty())
{
2019-04-12 01:10:33 +02:00
params.push_back({ "language", { status.language() }});
2018-06-14 04:11:28 +02:00
}
if (!status.media_attachments().empty())
{
std::vector<string> media_ids;
for (const Attachment &att : status.media_attachments())
{
2019-04-12 01:10:33 +02:00
parameters param_att;
2018-06-14 04:11:28 +02:00
if (!att.file().empty())
{
2019-04-12 01:10:33 +02:00
param_att.push_back({ "file", { att.file() }});
2018-06-14 04:11:28 +02:00
}
else
{
ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
return { error::INVALID_ARGUMENT,
"Easy::Attachment::file can not be empty", 0,
Status() };
2018-06-14 04:11:28 +02:00
}
if (!att.description().empty())
{
2019-04-12 01:10:33 +02:00
param_att.push_back({ "description", { att.description() }});
2018-06-14 04:11:28 +02:00
}
if (!att.focus().empty())
{
2019-04-12 01:10:33 +02:00
param_att.push_back({ "focus",
{ std::to_string(att.focus()[0]) + ',' +
std::to_string(att.focus()[1]) }});
2018-06-14 04:11:28 +02:00
}
return_call ret = post(API::v1::media, param_att);
if (ret.error_code == 0)
2018-06-14 04:11:28 +02:00
{
Attachment attachment(ret.answer);
media_ids.push_back(attachment.id());
2018-06-14 04:11:28 +02:00
}
else
{
ttdebug << "ERROR: Could not upload file.\n";
return { ret.error_code, ret.error_message,
2019-04-14 17:56:26 +02:00
ret.http_error_code, Status(ret.answer) };
2018-06-14 04:11:28 +02:00
}
}
2019-04-12 01:10:33 +02:00
params.push_back({ "media_ids", media_ids });
2018-06-14 04:11:28 +02:00
}
2019-04-12 01:10:33 +02:00
return_call ret = post(API::v1::statuses, params);
2019-04-14 17:56:26 +02:00
return { ret.error_code, ret.error_message, ret.http_error_code,
Status(ret.answer) };
2018-06-14 04:11:28 +02:00
}
2019-01-26 06:54:47 +01:00
const return_entity_vector<Notification> API::get_notifications(
const uint16_t limit, const string since_id, const string max_id)
2019-01-26 06:54:47 +01:00
{
2019-04-12 01:10:33 +02:00
parameters params;
2019-01-26 06:54:47 +01:00
2019-04-12 01:10:33 +02:00
params.push_back({ "limit", { std::to_string(limit) } });
if (!since_id.empty())
2019-01-26 06:54:47 +01:00
{
2019-04-12 01:10:33 +02:00
params.push_back({ "since_id", { since_id } });
2019-01-26 06:54:47 +01:00
}
if (!max_id.empty())
2019-01-26 06:54:47 +01:00
{
2019-04-12 01:10:33 +02:00
params.push_back({ "max_id", { max_id } });
2019-01-26 06:54:47 +01:00
}
2019-04-12 01:10:33 +02:00
return_call ret = API::get(API::v1::notifications, params);
2019-01-26 06:54:47 +01:00
if (ret.error_code == 0)
2019-01-26 06:54:47 +01:00
{
const vector<string> &answer_v = json_array_to_vector(ret.answer);
vector<Notification> notifications;
2019-01-26 06:54:47 +01:00
notifications.resize(answer_v.size());
// Transform vector of strings to vector of Notification.
std::transform(answer_v.begin(), answer_v.end(), notifications.begin(),
2019-03-11 21:13:58 +01:00
[](const string &s)
{ return Notification(s); });
2019-01-26 06:54:47 +01:00
2019-04-14 17:56:26 +02:00
return { ret.error_code, ret.error_message, ret.http_error_code,
notifications };
2019-01-26 06:54:47 +01:00
}
else
{
ttdebug << "ERROR: Could not get notifications.\n";
2019-04-14 17:56:26 +02:00
return { ret.error_code, ret.error_message, ret.http_error_code, {} };
2019-01-26 06:54:47 +01:00
}
}