diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-09-26 22:21:55 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-09-26 22:21:55 +0200 |
commit | 98d9b6856061e6c1951b03ca6b197d581e357189 (patch) | |
tree | 5ae068e902f48dd6a4a3d0a82e8e61186b298cdb /src | |
parent | 4eb9d876afd031582908db1ed0ffc10a47f76d57 (diff) |
matrix: send start/stop typing
Diffstat (limited to 'src')
-rw-r--r-- | src/QuickMedia.cpp | 60 | ||||
-rw-r--r-- | src/plugins/Matrix.cpp | 45 |
2 files changed, 102 insertions, 3 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 819e298..d7a3ed7 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3377,6 +3377,19 @@ namespace QuickMedia { sf::Vertex gradient_points[4]; int gradient_inc = 0; + sf::Clock start_typing_timer; + const double typing_timeout_seconds = 3.0; + bool typing = false; + + auto typing_async_func = [matrix](bool new_state, std::string room_id) { + if(new_state) { + matrix->on_start_typing(room_id); + } else { + matrix->on_stop_typing(room_id); + } + }; + std::vector<std::future<void>> typing_futures; + while (current_page == Page::CHAT) { while (window.pollEvent(event)) { base_event_handler(event, Page::EXIT, false, false, false); @@ -3409,13 +3422,23 @@ namespace QuickMedia { tabs[selected_tab].body->clamp_selection(); tabs[selected_tab].body->clear_thumbnails(); selected_tab = std::max(0, selected_tab - 1); - chat_input.clear(); + //chat_input.clear(); + if(typing) { + fprintf(stderr, "Stopped typing\n"); + typing = false; + typing_futures.push_back(std::async(typing_async_func, false, current_room_id)); + } } else if(event.key.code == sf::Keyboard::Right) { tabs[selected_tab].body->filter_search_fuzzy(""); tabs[selected_tab].body->clamp_selection(); tabs[selected_tab].body->clear_thumbnails(); selected_tab = std::min((int)tabs.size() - 1, selected_tab + 1); - chat_input.clear(); + //chat_input.clear(); + if(typing) { + fprintf(stderr, "Stopped typing\n"); + typing = false; + typing_futures.push_back(std::async(typing_async_func, false, current_room_id)); + } } if(tabs[selected_tab].type == ChatTabType::MESSAGES && event.key.control && event.key.code == sf::Keyboard::I) { @@ -3435,11 +3458,42 @@ namespace QuickMedia { } } - if(event.type == sf::Event::TextEntered) + if(event.type == sf::Event::TextEntered) { chat_input.onTextEntered(event.text.unicode); + // TODO: Also show typing event when ctrl+v pasting? + if(tabs[selected_tab].type == ChatTabType::MESSAGES && event.text.unicode != 13) { // Return key + start_typing_timer.restart(); + if(!typing) { + fprintf(stderr, "Started typing\n"); + typing_futures.push_back(std::async(typing_async_func, true, current_room_id)); + } + typing = true; + } + } chat_input.on_event(event); } + if(typing && start_typing_timer.getElapsedTime().asSeconds() >= typing_timeout_seconds) { + fprintf(stderr, "Stopped typing\n"); + typing = false; + typing_futures.push_back(std::async(typing_async_func, false, current_room_id)); + } + + for(auto it = typing_futures.begin(); it != typing_futures.end(); ) { + if(!it->valid()) { + it = typing_futures.erase(it); + continue; + } + + if(it->wait_for(std::chrono::seconds(0)) == std::future_status::ready) { + it->get(); + it = typing_futures.erase(it); + continue; + } + + ++it; + } + if(redraw) { redraw = false; chat_input.onWindowResize(window_size); diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index a38c063..71e451d 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -993,4 +993,49 @@ namespace QuickMedia { this->homeserver = "https://" + homeserver; return PluginResult::OK; } + + PluginResult Matrix::on_start_typing(const std::string &room_id) { + Json::Value request_data(Json::objectValue); + request_data["typing"] = true; + request_data["timeout"] = 30000; // 30 sec timeout + + Json::StreamWriterBuilder builder; + builder["commentStyle"] = "None"; + builder["indentation"] = ""; + + std::vector<CommandArg> additional_args = { + { "-X", "PUT" }, + { "-H", "content-type: application/json" }, + { "--data-binary", Json::writeString(builder, std::move(request_data)) }, + { "-H", "Authorization: Bearer " + access_token } + }; + + std::string server_response; + if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room_id + "/typing/" + url_param_encode(user_id) , server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) + return PluginResult::NET_ERR; + + return PluginResult::OK; + } + + PluginResult Matrix::on_stop_typing(const std::string &room_id) { + Json::Value request_data(Json::objectValue); + request_data["typing"] = false; + + Json::StreamWriterBuilder builder; + builder["commentStyle"] = "None"; + builder["indentation"] = ""; + + std::vector<CommandArg> additional_args = { + { "-X", "PUT" }, + { "-H", "content-type: application/json" }, + { "--data-binary", Json::writeString(builder, std::move(request_data)) }, + { "-H", "Authorization: Bearer " + access_token } + }; + + std::string server_response; + if(download_to_string(homeserver + "/_matrix/client/r0/rooms/" + room_id + "/typing/" + url_param_encode(user_id), server_response, std::move(additional_args), use_tor, true) != DownloadResult::OK) + return PluginResult::NET_ERR; + + return PluginResult::OK; + } }
\ No newline at end of file |