diff options
-rw-r--r-- | plugins/Matrix.hpp | 3 | ||||
-rw-r--r-- | src/QuickMedia.cpp | 60 | ||||
-rw-r--r-- | src/plugins/Matrix.cpp | 45 |
3 files changed, 105 insertions, 3 deletions
diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp index 3382270..7b8c13f 100644 --- a/plugins/Matrix.hpp +++ b/plugins/Matrix.hpp @@ -76,6 +76,9 @@ namespace QuickMedia { PluginResult login(const std::string &username, const std::string &password, const std::string &homeserver, std::string &err_msg); PluginResult load_and_verify_cached_session(); + + PluginResult on_start_typing(const std::string &room_id); + PluginResult on_stop_typing(const std::string &room_id); private: PluginResult sync_response_to_body_items(const Json::Value &root); PluginResult get_previous_room_messages(const std::string &room_id, RoomData *room_data); 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 |