new feature: catches up on missed mentions

This commit is contained in:
tastytea 2018-05-20 14:48:12 +02:00
parent b361753f51
commit 30a8903197
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
3 changed files with 72 additions and 16 deletions

View File

@ -65,6 +65,7 @@ class Listener
{ {
public: public:
Listener(); Listener();
~Listener();
/*! /*!
* @brief Starts listening on Mastodon * @brief Starts listening on Mastodon
@ -75,11 +76,11 @@ public:
*/ */
const void stop(); const void stop();
std::vector<Easy::Notification> get_new_messages(); const std::vector<Easy::Notification> get_new_messages();
const std::vector<Easy::Notification> catchup(); const std::vector<Easy::Notification> catchup();
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 Easy::Status &to_status, const string &message); const bool send_reply(const Easy::Status &to_status, const string &message);
const std::uint_fast64_t get_parent_id(Easy::Notification &notif); const std::uint_fast64_t get_parent_id(const Easy::Notification &notif);
const bool stillrunning() const; const bool stillrunning() const;
@ -92,8 +93,10 @@ private:
std::thread _thread; std::thread _thread;
bool _running; bool _running;
Json::Value _config; Json::Value _config;
const string _configfilepath;
const bool read_config(); const bool read_config();
const bool write_config();
}; };
#endif // EXPANDURL_MASTODON_HPP #endif // EXPANDURL_MASTODON_HPP

View File

@ -55,6 +55,7 @@ int main(int argc, char *argv[])
Listener listener; Listener listener;
listener.start(); listener.start();
std::vector<Easy::Notification> new_messages = listener.catchup();
while (running) while (running)
{ {
@ -66,9 +67,15 @@ int main(int argc, char *argv[])
listener.start(); listener.start();
} }
for (Easy::Notification &notif : 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 &notif : new_messages)
{
cout << "DEBUG: new message\n";
const std::uint_fast64_t id = listener.get_parent_id(notif); const std::uint_fast64_t id = listener.get_parent_id(notif);
cout << "DEBUG: in_reply_to_id: " << id << '\n'; cout << "DEBUG: in_reply_to_id: " << id << '\n';
Easy::Status status; Easy::Status status;
@ -109,6 +116,7 @@ int main(int argc, char *argv[])
"It seems that you didn't reply to a message?"); "It seems that you didn't reply to a message?");
} }
} }
new_messages.clear();
} }
listener.stop(); listener.stop();

View File

@ -32,6 +32,8 @@ Listener::Listener()
, _stream("") , _stream("")
, _ptr(nullptr) , _ptr(nullptr)
, _running(false) , _running(false)
, _configfilepath(static_cast<const string>(getenv("HOME")) +
"/.config/expandurl-mastodon.json")
{ {
if (read_config()) if (read_config())
@ -42,15 +44,22 @@ Listener::Listener()
} }
else else
{ {
cerr << "ERROR: Could not open " << _configfilepath << ".\n";
exit(1); exit(1);
} }
} }
Listener::~Listener()
{
if (!write_config())
{
cerr << "ERROR: Could not open " << _configfilepath << ".\n";
}
}
const bool Listener::read_config() const bool Listener::read_config()
{ {
const string filepath = static_cast<const string>(getenv("HOME")) + std::ifstream file(_configfilepath);
"/.config/expandurl-mastodon.json";
std::ifstream file(filepath);
if (file.is_open()) if (file.is_open())
{ {
@ -62,14 +71,25 @@ const bool Listener::read_config()
_instance = _config["account"].asString(); _instance = _config["account"].asString();
_instance = _instance.substr(_instance.find('@') + 1); _instance = _instance.substr(_instance.find('@') + 1);
_access_token = _config["access_token"].asString(); _access_token = _config["access_token"].asString();
} return true;
else
{
cerr << "ERROR: Could not open " << filepath << ".\n";
return false;
} }
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() const void Listener::start()
@ -105,7 +125,7 @@ const void Listener::stop()
} }
} }
std::vector<Easy::Notification> Listener::get_new_messages() const std::vector<Easy::Notification> Listener::get_new_messages()
{ {
std::vector<Easy::Notification> v; std::vector<Easy::Notification> v;
static std::uint_fast8_t count_empty = 0; static std::uint_fast8_t count_empty = 0;
@ -146,7 +166,31 @@ std::vector<Easy::Notification> Listener::get_new_messages()
const std::vector<Easy::Notification> Listener::catchup() const std::vector<Easy::Notification> Listener::catchup()
{ {
// std::vector<Easy::Notification> 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) 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 &notif) const std::uint_fast64_t Listener::get_parent_id(const Easy::Notification &notif)
{ {
string answer; string answer;
std::uint_fast16_t ret; std::uint_fast16_t ret;
@ -246,6 +290,7 @@ const std::uint_fast64_t Listener::get_parent_id(Easy::Notification &notif)
} }
else else
{ {
_config["last_id"] = std::to_string(notif.id());
Easy::Status s(answer); Easy::Status s(answer);
return s.in_reply_to_id(); return s.in_reply_to_id();
} }