bugfix: in_reply_to_id only shows up in the status after it is fetched again

This commit is contained in:
tastytea 2018-05-12 02:30:57 +02:00
parent b5c2e90cf8
commit 5ebaf7678c
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
5 changed files with 69 additions and 37 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7) cmake_minimum_required (VERSION 3.7)
project (expandurl-mastodon project (expandurl-mastodon
VERSION 0.1.1 VERSION 0.1.2
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -1,4 +1,4 @@
**expandurl-mastodon** is a Mastodon bot that expands a shortened URL. **expandurl-mastodon** is a Mastodon bot that expands shortened URLs.
If you want the bot to expand an URL, reply to the post with the URL in it and If you want the bot to expand an URL, reply to the post with the URL in it and
mention the bot account (`@expandurl@botsin.space` for example). mention the bot account (`@expandurl@botsin.space` for example).

View File

@ -26,6 +26,8 @@
#include <mastodon-cpp/easy/all.hpp> #include <mastodon-cpp/easy/all.hpp>
using std::string; using std::string;
using Mastodon::API;
using Mastodon::Easy;
void signal_handler(int signum); void signal_handler(int signum);
/*! /*!
@ -71,16 +73,17 @@ public:
*/ */
const void stop(); const void stop();
std::vector<Mastodon::Easy::Notification> get_new_messages(); std::vector<Easy::Notification> get_new_messages();
Mastodon::Easy::Status get_status(const std::uint_fast64_t &id); Easy::Status get_status(const std::uint_fast64_t &id);
const bool send_reply(const Mastodon::Easy::Status &status, const bool send_reply(const Easy::Status &status, const string &message);
const string &message); const std::uint_fast64_t get_parent_id(Easy::Notification &notif);
private: private:
string _instance; string _instance;
string _access_token; string _access_token;
std::unique_ptr<Easy> _masto;
string _stream; string _stream;
std::unique_ptr<Mastodon::API::http> _ptr; std::unique_ptr<API::http> _ptr;
std::thread _thread; std::thread _thread;
}; };

View File

@ -73,48 +73,50 @@ int main(int argc, char *argv[])
while (running) while (running)
{ {
std::this_thread::sleep_for(std::chrono::seconds(10)); std::this_thread::sleep_for(std::chrono::seconds(2));
for (Easy::Notification &notif : listener.get_new_messages()) for (Easy::Notification &notif : listener.get_new_messages())
{ {
const std::uint_fast64_t id = notif.status().in_reply_to_id(); cerr << "DEBUG: new messages\n";
const std::uint_fast64_t id = listener.get_parent_id(notif);
cerr << "DEBUG: in_reply_to_id: " << id << '\n';
Easy::Status status; Easy::Status status;
if (id > 0) if (id > 0)
{ {
status = listener.get_status(id); status = listener.get_status(id);
} if (status.valid())
else
{
listener.send_reply(notif.status(),
"I couldn't find the message you replied to. 😞");
}
if (status.valid())
{
string message = "";
for (const string &url : get_urls(status.content()))
{ {
message += url + " \n"; string message = "";
} for (const string &url : get_urls(status.content()))
if (!message.empty())
{
message = '@' + notif.status().account().acct() +
' ' + message;
if (listener.send_reply(notif.status(), message))
{ {
cout << "Sent reply: " << message; message += url + " \n";
}
if (!message.empty())
{
if (!listener.send_reply(notif.status(), message))
{
cerr << "FIXME!\n";
}
} }
else else
{ {
cerr << "ERROR: could not send reply to " << listener.send_reply(notif.status(),
notif.status().id() << '\n'; "I couldn't find an URL in the message you "
"replied to. 😞");
} }
} }
else else
{ {
listener.send_reply(notif.status(), listener.send_reply(notif.status(),
"I couldn't find an URL in the message you replied to. 😞"); "I couldn't get the message you replied to. 😞");
} }
} }
else
{
listener.send_reply(notif.status(),
"It seems that you didn't reply to a message?");
}
} }
} }
listener.stop(); listener.stop();

View File

@ -22,14 +22,14 @@
using std::cerr; using std::cerr;
using std::string; using std::string;
using Mastodon::Easy;
Listener::Listener() Listener::Listener()
: _instance("") : _instance("")
, _access_token("") , _access_token("")
, _stream("") , _stream("")
, _ptr(nullptr) , _ptr(nullptr)
{} {
}
const bool Listener::start() const bool Listener::start()
{ {
@ -43,6 +43,10 @@ const bool Listener::start()
_instance = _instance.substr(_instance.find('@') + 1); _instance = _instance.substr(_instance.find('@') + 1);
std::getline(file, _access_token); std::getline(file, _access_token);
file.close(); file.close();
_masto = std::make_unique<Easy>(_instance, _access_token);
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
global::version);
} }
else else
{ {
@ -89,11 +93,11 @@ std::vector<Easy::Notification> Listener::get_new_messages()
Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id) Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id)
{ {
Easy masto(_instance, _access_token);
std::uint_fast16_t ret; std::uint_fast16_t ret;
string answer; string answer;
ret = masto.get(Easy::v1::statuses_id, {{ "id", { std::to_string(id) }}}, answer); ret = _masto->get(Easy::v1::statuses_id, {{ "id", { std::to_string(id) }}},
answer);
if (ret == 0) if (ret == 0)
{ {
return Easy::Status(answer); return Easy::Status(answer);
@ -108,7 +112,6 @@ Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id)
const bool Listener::send_reply(const Easy::Status &status, const bool Listener::send_reply(const Easy::Status &status,
const string &message) const string &message)
{ {
Easy masto(_instance, _access_token);
std::uint_fast16_t ret; std::uint_fast16_t ret;
string answer; string answer;
const string id = std::to_string(status.id()); const string id = std::to_string(status.id());
@ -131,7 +134,7 @@ const bool Listener::send_reply(const Easy::Status &status,
{ {
{ "in_reply_to_id", { id } }, { "in_reply_to_id", { id } },
{ "visibility", { strvisibility } }, { "visibility", { strvisibility } },
{ "status", { message } } { "status", { '@' + status.account().acct() + ' ' + message } }
}; };
if (status.sensitive()) if (status.sensitive())
@ -144,14 +147,38 @@ const bool Listener::send_reply(const Easy::Status &status,
parameters.insert({ "spoiler_text", { status.spoiler_text() } }); parameters.insert({ "spoiler_text", { status.spoiler_text() } });
} }
ret = masto.post(Easy::v1::statuses, parameters, answer); ret = _masto->post(Easy::v1::statuses, parameters, answer);
if (ret == 0) if (ret == 0)
{ {
cerr << "DEBUG: Sent reply: " << message << '\n';
return true; return true;
} }
else else
{ {
cerr << "ERROR: could not send reply to " << id << '\n';
return false; return false;
} }
} }
const std::uint_fast64_t Listener::get_parent_id(Easy::Notification &notif)
{
string answer;
std::uint_fast16_t ret;
ret = _masto->get(API::v1::statuses_id,
{{ "id", { std::to_string(notif.status().id()) }}},
answer);
if (ret > 0)
{
cerr << "ERROR: " << ret << " (in " << __FUNCTION__ << ")\n";
return 0;
}
else
{
Easy::Status s(answer);
return s.in_reply_to_id();
}
return 0;
}