2019-12-28 07:13:30 +01:00
|
|
|
/* This file is part of mastorss.
|
|
|
|
* 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 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 "exceptions.hpp"
|
|
|
|
#include "mastoapi.hpp"
|
|
|
|
|
|
|
|
#include <boost/log/trivial.hpp>
|
|
|
|
|
|
|
|
#include <string>
|
2019-12-28 23:10:49 +01:00
|
|
|
#include <string_view>
|
2019-12-28 07:13:30 +01:00
|
|
|
|
|
|
|
namespace mastorss
|
|
|
|
{
|
|
|
|
using std::string;
|
2019-12-28 23:10:49 +01:00
|
|
|
using std::string_view;
|
2019-12-28 07:13:30 +01:00
|
|
|
|
|
|
|
MastoAPI::MastoAPI(const ProfileData &data)
|
|
|
|
: _profile{data}
|
|
|
|
, _masto{_profile.instance, _profile.access_token}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MastoAPI::post_item(const Item &item)
|
|
|
|
{
|
|
|
|
string status{[&]
|
|
|
|
{
|
|
|
|
if (_profile.titles_as_cw)
|
|
|
|
{
|
|
|
|
if (_profile.titles_only)
|
|
|
|
{
|
|
|
|
return string{};
|
|
|
|
}
|
|
|
|
return item.description;
|
|
|
|
}
|
|
|
|
|
|
|
|
string s{item.title};
|
|
|
|
if (!_profile.titles_only)
|
|
|
|
{
|
|
|
|
s.append("\n\n" + item.description);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}()};
|
|
|
|
status.append("\n\n" + item.link);
|
|
|
|
|
2019-12-29 01:05:08 +01:00
|
|
|
const size_t len_status{[&]
|
|
|
|
{
|
|
|
|
if (_profile.titles_as_cw)
|
|
|
|
{
|
|
|
|
// Subjects (CWs) count into the post length.
|
|
|
|
return status.size() + item.title.size();
|
|
|
|
}
|
|
|
|
return status.size();
|
|
|
|
}()};
|
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-28 23:10:49 +01:00
|
|
|
return size_t{};
|
2019-12-28 07:13:30 +01:00
|
|
|
}
|
2019-12-28 23:10:49 +01:00
|
|
|
return _profile.append.size() + 2;
|
|
|
|
}()};
|
|
|
|
const size_t len_max{_profile.max_size};
|
|
|
|
constexpr string_view omission = " […]";
|
|
|
|
|
2019-12-29 01:05:08 +01:00
|
|
|
if ((len_status + len_append) > len_max)
|
2019-12-28 23:10:49 +01:00
|
|
|
{
|
|
|
|
status.resize(len_max - len_append - 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-28 07:13:30 +01:00
|
|
|
|
|
|
|
Mastodon::parameters params{{"status", {status}}};
|
|
|
|
if (_profile.titles_as_cw)
|
|
|
|
{
|
|
|
|
params.push_back({"spoiler_text", {item.title}});
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto ret = _masto.post(Mastodon::API::v1::statuses, params);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
if (ret.http_error_code != 200)
|
|
|
|
{
|
|
|
|
throw HTTPException{ret.http_error_code};
|
|
|
|
}
|
2019-12-28 23:39:56 +01:00
|
|
|
throw MastodonException{ret.error_code};
|
2019-12-28 07:13:30 +01:00
|
|
|
}
|
|
|
|
BOOST_LOG_TRIVIAL(debug) << "Posted status with GUID: " << item.guid;
|
|
|
|
}
|
|
|
|
} // namespace mastorss
|