new feature: catches up on missed mentions

This commit is contained in:
tastytea 2018-05-20 14:48:12 +02:00
vanhempi b361753f51
commit 30a8903197
Allekirjoittanut: tastytea
GPG avaimen ID: 59346E0EA35C67E5
3 muutettua tiedostoa jossa 72 lisäystä ja 16 poistoa

Näytä tiedosto

@ -65,6 +65,7 @@ class Listener
{
public:
Listener();
~Listener();
/*!
* @brief Starts listening on Mastodon
@ -75,11 +76,11 @@ public:
*/
const void stop();
std::vector<Easy::Notification> get_new_messages();
const std::vector<Easy::Notification> get_new_messages();
const std::vector<Easy::Notification> 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 &notif);
const std::uint_fast64_t get_parent_id(const Easy::Notification &notif);
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

Näytä tiedosto

@ -55,6 +55,7 @@ int main(int argc, char *argv[])
Listener listener;
listener.start();
std::vector<Easy::Notification> new_messages = listener.catchup();
while (running)
{
@ -66,9 +67,15 @@ int main(int argc, char *argv[])
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);
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();

Näytä tiedosto

@ -32,6 +32,8 @@ Listener::Listener()
, _stream("")
, _ptr(nullptr)
, _running(false)
, _configfilepath(static_cast<const string>(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<const string>(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<Easy::Notification> Listener::get_new_messages()
const std::vector<Easy::Notification> Listener::get_new_messages()
{
std::vector<Easy::Notification> v;
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()
{
//
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)
@ -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;
std::uint_fast16_t ret;
@ -246,6 +290,7 @@ const std::uint_fast64_t Listener::get_parent_id(Easy::Notification &notif)
}
else
{
_config["last_id"] = std::to_string(notif.id());
Easy::Status s(answer);
return s.in_reply_to_id();
}