From 30a8903197e5e25cced419cadde3c929dbcee959 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 20 May 2018 14:48:12 +0200 Subject: [PATCH] new feature: catches up on missed mentions --- src/expandurl-mastodon.hpp | 7 ++-- src/main.cpp | 12 +++++-- src/masto.cpp | 69 +++++++++++++++++++++++++++++++------- 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/expandurl-mastodon.hpp b/src/expandurl-mastodon.hpp index 64e0de3..2b391b4 100644 --- a/src/expandurl-mastodon.hpp +++ b/src/expandurl-mastodon.hpp @@ -65,6 +65,7 @@ class Listener { public: Listener(); + ~Listener(); /*! * @brief Starts listening on Mastodon @@ -75,11 +76,11 @@ public: */ const void stop(); - std::vector get_new_messages(); + const std::vector get_new_messages(); const std::vector catchup(); Easy::Status get_status(const std::uint_fast64_t &id); const bool send_reply(const Easy::Status &to_status, const string &message); - const std::uint_fast64_t get_parent_id(Easy::Notification ¬if); + const std::uint_fast64_t get_parent_id(const Easy::Notification ¬if); const bool stillrunning() const; @@ -92,8 +93,10 @@ private: std::thread _thread; bool _running; Json::Value _config; + const string _configfilepath; const bool read_config(); + const bool write_config(); }; #endif // EXPANDURL_MASTODON_HPP diff --git a/src/main.cpp b/src/main.cpp index efd2791..29df598 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,6 +55,7 @@ int main(int argc, char *argv[]) Listener listener; listener.start(); + std::vector new_messages = listener.catchup(); while (running) { @@ -66,9 +67,15 @@ int main(int argc, char *argv[]) listener.start(); } - for (Easy::Notification ¬if : listener.get_new_messages()) + // Only get new messages if we don't have to catchup + if (new_messages.size() == 0) { - cout << "DEBUG: new messages\n"; + new_messages = listener.get_new_messages(); + } + + for (Easy::Notification ¬if : new_messages) + { + cout << "DEBUG: new message\n"; const std::uint_fast64_t id = listener.get_parent_id(notif); cout << "DEBUG: in_reply_to_id: " << id << '\n'; Easy::Status status; @@ -109,6 +116,7 @@ int main(int argc, char *argv[]) "It seems that you didn't reply to a message?"); } } + new_messages.clear(); } listener.stop(); diff --git a/src/masto.cpp b/src/masto.cpp index 2d708a3..ad1e97f 100644 --- a/src/masto.cpp +++ b/src/masto.cpp @@ -32,6 +32,8 @@ Listener::Listener() , _stream("") , _ptr(nullptr) , _running(false) +, _configfilepath(static_cast(getenv("HOME")) + + "/.config/expandurl-mastodon.json") { if (read_config()) @@ -42,15 +44,22 @@ Listener::Listener() } else { + cerr << "ERROR: Could not open " << _configfilepath << ".\n"; exit(1); } } +Listener::~Listener() +{ + if (!write_config()) + { + cerr << "ERROR: Could not open " << _configfilepath << ".\n"; + } +} + const bool Listener::read_config() { - const string filepath = static_cast(getenv("HOME")) + - "/.config/expandurl-mastodon.json"; - std::ifstream file(filepath); + std::ifstream file(_configfilepath); if (file.is_open()) { @@ -62,14 +71,25 @@ const bool Listener::read_config() _instance = _config["account"].asString(); _instance = _instance.substr(_instance.find('@') + 1); _access_token = _config["access_token"].asString(); - } - else - { - cerr << "ERROR: Could not open " << filepath << ".\n"; - return false; + return true; } - return true; + return false; +} + +const bool Listener::write_config() +{ + std::ofstream outfile(_configfilepath); + if (outfile.is_open()) + { + outfile.write(_config.toStyledString().c_str(), + _config.toStyledString().length()); + outfile.close(); + + return true; + } + + return false; } const void Listener::start() @@ -105,7 +125,7 @@ const void Listener::stop() } } -std::vector Listener::get_new_messages() +const std::vector Listener::get_new_messages() { std::vector v; static std::uint_fast8_t count_empty = 0; @@ -146,7 +166,31 @@ std::vector Listener::get_new_messages() const std::vector Listener::catchup() { - // + std::vector v; + const string last_id = _config["last_id"].asString(); + if (last_id != "") + { + cout << "DEBUG: catching up...\n"; + API::parametermap parameter = + { + { "since_id", { last_id } }, + { "exclude_types", { "follow", "favourite", "reblog" } } + }; + string answer; + std::uint_fast16_t ret; + + ret = _masto->get(API::v1::notifications, parameter, answer); + + if (ret == 0) + { + for (const string str : Easy::json_array_to_vector(answer)) + { + v.push_back(Easy::Notification(str)); + } + } + } + + return v; } Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id) @@ -219,7 +263,7 @@ const bool Listener::send_reply(const Easy::Status &to_status, } } -const std::uint_fast64_t Listener::get_parent_id(Easy::Notification ¬if) +const std::uint_fast64_t Listener::get_parent_id(const Easy::Notification ¬if) { string answer; std::uint_fast16_t ret; @@ -246,6 +290,7 @@ const std::uint_fast64_t Listener::get_parent_id(Easy::Notification ¬if) } else { + _config["last_id"] = std::to_string(notif.id()); Easy::Status s(answer); return s.in_reply_to_id(); }