2019-12-28 07:13:30 +01:00
|
|
|
/* This file is part of mastorss.
|
2020-05-10 17:01:20 +02:00
|
|
|
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
|
2019-12-28 07:13:30 +01:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mastoapi.hpp"
|
|
|
|
|
2020-11-05 14:14:16 +01:00
|
|
|
#include "exceptions.hpp"
|
|
|
|
|
2019-12-28 07:13:30 +01:00
|
|
|
#include <boost/log/trivial.hpp>
|
2020-05-10 17:01:20 +02:00
|
|
|
#include <boost/regex.hpp>
|
2019-12-28 07:13:30 +01:00
|
|
|
|
2020-11-21 20:32:15 +01:00
|
|
|
#include <iostream>
|
2019-12-28 07:13:30 +01:00
|
|
|
#include <string>
|
2019-12-28 23:10:49 +01:00
|
|
|
#include <string_view>
|
2019-12-28 07:13:30 +01:00
|
|
|
|
|
|
|
namespace mastorss
|
|
|
|
{
|
2020-05-10 17:01:20 +02:00
|
|
|
using boost::regex;
|
|
|
|
using boost::regex_replace;
|
2019-12-28 07:13:30 +01:00
|
|
|
using std::string;
|
2019-12-28 23:10:49 +01:00
|
|
|
using std::string_view;
|
2019-12-28 07:13:30 +01:00
|
|
|
|
2020-01-01 12:43:42 +01:00
|
|
|
MastoAPI::MastoAPI(ProfileData &data)
|
2019-12-28 07:13:30 +01:00
|
|
|
: _profile{data}
|
2020-01-15 19:05:49 +01:00
|
|
|
, _instance{_profile.instance, _profile.access_token}
|
2020-11-05 14:14:16 +01:00
|
|
|
{}
|
2019-12-28 07:13:30 +01:00
|
|
|
|
2020-11-21 20:32:15 +01:00
|
|
|
void MastoAPI::post_item(const Item &item, bool dry_run)
|
2019-12-28 07:13:30 +01:00
|
|
|
{
|
2020-05-10 17:01:20 +02:00
|
|
|
string title = replacements_apply(item.title);
|
|
|
|
string link = replacements_apply(item.link);
|
|
|
|
|
2020-11-05 14:14:16 +01:00
|
|
|
// clang-format off
|
2019-12-28 07:13:30 +01:00
|
|
|
string status{[&]
|
|
|
|
{
|
|
|
|
if (_profile.titles_as_cw)
|
|
|
|
{
|
|
|
|
if (_profile.titles_only)
|
|
|
|
{
|
|
|
|
return string{};
|
|
|
|
}
|
2020-05-10 17:01:20 +02:00
|
|
|
return replacements_apply(item.description);
|
2019-12-28 07:13:30 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 17:01:20 +02:00
|
|
|
string s{title};
|
2019-12-28 07:13:30 +01:00
|
|
|
if (!_profile.titles_only)
|
|
|
|
{
|
2020-05-10 17:01:20 +02:00
|
|
|
s.append("\n\n" + replacements_apply(item.description));
|
2019-12-28 07:13:30 +01:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}()};
|
2020-11-05 14:14:16 +01:00
|
|
|
// clang-format on
|
2019-12-28 07:13:30 +01:00
|
|
|
|
2020-11-05 14:14:16 +01:00
|
|
|
// clang-format off
|
2019-12-28 23:10:49 +01:00
|
|
|
const size_t len_append{[&]
|
2019-12-28 07:13:30 +01:00
|
|
|
{
|
2019-12-28 23:10:49 +01:00
|
|
|
if (_profile.append.empty())
|
2019-12-28 07:13:30 +01:00
|
|
|
{
|
2019-12-29 02:41:13 +01:00
|
|
|
return size_t{0};
|
2019-12-28 07:13:30 +01:00
|
|
|
}
|
2019-12-28 23:10:49 +01:00
|
|
|
return _profile.append.size() + 2;
|
|
|
|
}()};
|
2019-12-29 01:49:47 +01:00
|
|
|
const size_t len_max{[&]
|
|
|
|
{
|
|
|
|
if (_profile.titles_as_cw)
|
|
|
|
{
|
|
|
|
// Subjects (CWs) count into the post length.
|
2020-05-10 17:01:20 +02:00
|
|
|
return _profile.max_size - title.size();
|
2019-12-29 01:49:47 +01:00
|
|
|
}
|
|
|
|
return _profile.max_size;
|
2020-05-10 17:01:20 +02:00
|
|
|
}() - link.size() - 2 - len_append};
|
2020-11-05 14:14:16 +01:00
|
|
|
// clang-format off
|
2019-12-29 03:47:57 +01:00
|
|
|
BOOST_LOG_TRIVIAL(debug)
|
|
|
|
<< "Maximum text (without link and appendix) length: " << len_max;
|
2019-12-28 23:10:49 +01:00
|
|
|
|
2020-05-10 17:01:20 +02:00
|
|
|
if (status.size() > len_max)
|
2019-12-28 23:10:49 +01:00
|
|
|
{
|
2019-12-29 03:33:24 +01:00
|
|
|
constexpr string_view omission = " […]";
|
2019-12-29 03:47:57 +01:00
|
|
|
status.resize(len_max - omission.size());
|
2019-12-29 00:10:13 +01:00
|
|
|
|
|
|
|
// Don't cut in the middle of a word.
|
|
|
|
const auto pos = status.rfind(' ');
|
|
|
|
if (pos != string::npos)
|
|
|
|
{
|
2019-12-29 01:45:13 +01:00
|
|
|
status.resize(pos);
|
2019-12-29 00:10:13 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 23:10:49 +01:00
|
|
|
status.append(omission);
|
2019-12-29 01:49:47 +01:00
|
|
|
BOOST_LOG_TRIVIAL(debug) << "Status resized to: " << status.size();
|
2019-12-28 23:10:49 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 17:01:20 +02:00
|
|
|
status.append("\n\n" + link);
|
2019-12-29 02:16:17 +01:00
|
|
|
|
2019-12-28 23:10:49 +01:00
|
|
|
if (!_profile.append.empty())
|
|
|
|
{
|
2019-12-28 07:13:30 +01:00
|
|
|
status.append("\n\n" + _profile.append);
|
|
|
|
}
|
2019-12-29 01:05:08 +01:00
|
|
|
BOOST_LOG_TRIVIAL(debug) << "Status length: " << status.size();
|
2019-12-29 03:33:24 +01:00
|
|
|
BOOST_LOG_TRIVIAL(debug) << "Status: \"" << status << '"';
|
2019-12-28 07:13:30 +01:00
|
|
|
|
2020-11-21 20:32:15 +01:00
|
|
|
if (!dry_run)
|
2019-12-28 07:13:30 +01:00
|
|
|
{
|
2020-11-21 20:32:15 +01:00
|
|
|
mastodonpp::parametermap params{{"status", status}};
|
|
|
|
if (_profile.titles_as_cw)
|
|
|
|
{
|
|
|
|
params.insert({"spoiler_text", title});
|
|
|
|
}
|
2019-12-28 07:13:30 +01:00
|
|
|
|
2020-11-21 20:32:15 +01:00
|
|
|
mastodonpp::Connection connection{_instance};
|
|
|
|
const auto ret = connection.post(mastodonpp::API::v1::statuses, params);
|
|
|
|
if (!ret)
|
2019-12-28 07:13:30 +01:00
|
|
|
{
|
2020-11-21 20:32:15 +01:00
|
|
|
if (ret.http_status != 200)
|
|
|
|
{
|
|
|
|
throw HTTPException{ret.http_status};
|
|
|
|
}
|
|
|
|
throw CURLException{ret.curl_error_code};
|
2019-12-28 07:13:30 +01:00
|
|
|
}
|
2020-11-21 20:32:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
using std::cout;
|
|
|
|
cout << " WOULD POST: \n";
|
2020-11-21 20:38:51 +01:00
|
|
|
if (_profile.titles_as_cw)
|
|
|
|
{
|
|
|
|
cout << "Subject: " << title << '\n';
|
|
|
|
}
|
2020-11-21 20:32:15 +01:00
|
|
|
cout << "Status:\n" << status << '\n';
|
2019-12-28 07:13:30 +01:00
|
|
|
}
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "Posted status with GUID: " << item.guid;
|
2020-01-01 12:43:42 +01:00
|
|
|
|
|
|
|
_profile.guids.push_back(item.guid);
|
2020-11-05 14:14:33 +01:00
|
|
|
if (_profile.guids.size() > Config::max_guids)
|
2020-01-01 12:43:42 +01:00
|
|
|
{
|
|
|
|
_profile.guids.pop_front();
|
|
|
|
}
|
2019-12-28 07:13:30 +01:00
|
|
|
}
|
2020-05-10 17:01:20 +02:00
|
|
|
|
|
|
|
string MastoAPI::replacements_apply(const string &text)
|
|
|
|
{
|
|
|
|
string out = text;
|
|
|
|
for (const auto &replacement : _profile.replacements)
|
|
|
|
{
|
|
|
|
out = regex_replace(out, regex{replacement.first}, replacement.second);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2019-12-28 07:13:30 +01:00
|
|
|
} // namespace mastorss
|