Browse Source

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

tags/0.1.3
tastytea 1 year ago
parent
commit
5ebaf7678c
Signed by: tastytea <tastytea@tastytea.de> GPG Key ID: 59346E0EA35C67E5
5 changed files with 69 additions and 37 deletions
  1. 1
    1
      CMakeLists.txt
  2. 1
    1
      README.md
  3. 8
    5
      src/expandurl-mastodon.hpp
  4. 25
    23
      src/main.cpp
  5. 34
    7
      src/masto.cpp

+ 1
- 1
CMakeLists.txt View File

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


+ 1
- 1
README.md 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
mention the bot account (`@expandurl@botsin.space` for example).

+ 8
- 5
src/expandurl-mastodon.hpp View File

@@ -26,6 +26,8 @@
#include <mastodon-cpp/easy/all.hpp>

using std::string;
using Mastodon::API;
using Mastodon::Easy;

void signal_handler(int signum);
/*!
@@ -71,16 +73,17 @@ public:
*/
const void stop();

std::vector<Mastodon::Easy::Notification> get_new_messages();
Mastodon::Easy::Status get_status(const std::uint_fast64_t &id);
const bool send_reply(const Mastodon::Easy::Status &status,
const string &message);
std::vector<Easy::Notification> get_new_messages();
Easy::Status get_status(const std::uint_fast64_t &id);
const bool send_reply(const Easy::Status &status, const string &message);
const std::uint_fast64_t get_parent_id(Easy::Notification &notif);

private:
string _instance;
string _access_token;
std::unique_ptr<Easy> _masto;
string _stream;
std::unique_ptr<Mastodon::API::http> _ptr;
std::unique_ptr<API::http> _ptr;
std::thread _thread;
};


+ 25
- 23
src/main.cpp View File

@@ -73,48 +73,50 @@ int main(int argc, char *argv[])

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())
{
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;

if (id > 0)
{
status = listener.get_status(id);
}
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()))
if (status.valid())
{
message += url + " \n";
}
if (!message.empty())
{
message = '@' + notif.status().account().acct() +
' ' + message;
if (listener.send_reply(notif.status(), message))
string message = "";
for (const string &url : get_urls(status.content()))
{
message += url + " \n";
}
if (!message.empty())
{
cout << "Sent reply: " << message;
if (!listener.send_reply(notif.status(), message))
{
cerr << "FIXME!\n";
}
}
else
{
cerr << "ERROR: could not send reply to " <<
notif.status().id() << '\n';
listener.send_reply(notif.status(),
"I couldn't find an URL in the message you "
"replied to. 😞");
}
}
else
{
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();

+ 34
- 7
src/masto.cpp View File

@@ -22,14 +22,14 @@

using std::cerr;
using std::string;
using Mastodon::Easy;

Listener::Listener()
: _instance("")
, _access_token("")
, _stream("")
, _ptr(nullptr)
{}
{
}

const bool Listener::start()
{
@@ -43,6 +43,10 @@ const bool Listener::start()
_instance = _instance.substr(_instance.find('@') + 1);
std::getline(file, _access_token);
file.close();

_masto = std::make_unique<Easy>(_instance, _access_token);
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
global::version);
}
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)
{
Easy masto(_instance, _access_token);
std::uint_fast16_t ret;
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)
{
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 string &message)
{
Easy masto(_instance, _access_token);
std::uint_fast16_t ret;
string answer;
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 } },
{ "visibility", { strvisibility } },
{ "status", { message } }
{ "status", { '@' + status.account().acct() + ' ' + message } }
};

if (status.sensitive())
@@ -144,14 +147,38 @@ const bool Listener::send_reply(const Easy::Status &status,
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)
{
cerr << "DEBUG: Sent reply: " << message << '\n';
return true;
}
else
{
cerr << "ERROR: could not send reply to " << id << '\n';
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;
}

Loading…
Cancel
Save