From d123c41cd3ad4f0d55ae134be69e7ffd144dbb74 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 18 May 2021 22:05:19 +0200 Subject: Add mention autocomplete --- plugins/Matrix.hpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 9 deletions(-) (limited to 'plugins/Matrix.hpp') diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp index 25b2633..0e96fd7 100644 --- a/plugins/Matrix.hpp +++ b/plugins/Matrix.hpp @@ -19,12 +19,7 @@ namespace QuickMedia { std::string remove_reply_formatting(const std::string &str); std::string message_get_body_remove_formatting(Message *message); std::string extract_first_line_remove_newline_elipses(const std::string &str, size_t max_length); - - enum class UserResolveState { - NOT_RESOLVED, - RESOLVING, - RESOLVED - }; + sf::Color user_id_to_color(const std::string &user_id); struct UserInfo { friend struct RoomData; @@ -34,7 +29,6 @@ namespace QuickMedia { RoomData *room; const sf::Color display_name_color; const std::string user_id; - UserResolveState resolve_state; private: std::string display_name; std::string avatar_url; @@ -63,9 +57,42 @@ namespace QuickMedia { REACTION }; + struct MatrixEventUserInfo { + std::string user_id; + std::optional display_name; + std::optional avatar_url; + }; + class MatrixEvent { public: + enum class Type { + ADD_USER, + REMOVE_USER, + USER_INFO + }; + + MatrixEvent(Type type) : type(type) {} virtual ~MatrixEvent() = default; + + const Type type; + }; + + class MatrixAddUserEvent : public MatrixEvent { + public: + MatrixAddUserEvent(MatrixEventUserInfo user_info) : MatrixEvent(Type::ADD_USER), user_info(std::move(user_info)) {} + const MatrixEventUserInfo user_info; + }; + + class MatrixRemoveUserEvent : public MatrixEvent { + public: + MatrixRemoveUserEvent(MatrixEventUserInfo user_info) : MatrixEvent(Type::REMOVE_USER), user_info(std::move(user_info)) {} + const MatrixEventUserInfo user_info; + }; + + class MatrixUserInfoEvent : public MatrixEvent { + public: + MatrixUserInfoEvent(MatrixEventUserInfo user_info) : MatrixEvent(Type::USER_INFO), user_info(std::move(user_info)) {} + const MatrixEventUserInfo user_info; }; struct Message { @@ -108,6 +135,7 @@ namespace QuickMedia { std::shared_ptr get_message_by_id(const std::string &id); + std::vector> get_users(); std::vector> get_users_excluding_me(const std::string &my_user_id); void acquire_room_lock(); @@ -543,9 +571,24 @@ namespace QuickMedia { std::shared_ptr get_message_by_id(RoomData *room, const std::string &event_id); RoomData* get_room_by_id(const std::string &id); - void update_user_with_latest_state(RoomData *room, const std::string &user_id); void update_room_users(RoomData *room); + + // Only one room can have event queue enabled at once + void enable_event_queue(RoomData *room); + // Also clears the event queue + void disable_event_queue(); + + // Has to be called in the main thread. + // Returns nullptr if there are no new events. + std::unique_ptr pop_event(); private: + // No-op if sync is cache or if |room| is not the currently enabled event queue room + void trigger_event(RoomData *room, std::unique_ptr event); + + void replace_mentions(RoomData *room, std::string &text); + std::string body_to_formatted_body(RoomData *room, const std::string &body); + std::string create_formatted_body_for_message_reply(RoomData *room, const Message *message, const std::string &body); + PluginResult set_qm_last_read_message_timestamp(RoomData *room, int64_t timestamp); PluginResult parse_sync_response(const rapidjson::Document &root, bool is_additional_messages_sync, bool initial_sync); @@ -575,9 +618,12 @@ namespace QuickMedia { void set_next_batch(std::string new_next_batch); std::string get_next_batch(); void clear_sync_cache_for_new_sync(); - std::shared_ptr get_user_by_id(RoomData *room, const std::string &user_id); + std::shared_ptr get_user_by_id(RoomData *room, const std::string &user_id, bool *is_new_user = nullptr, bool create_if_not_found = true); std::string get_filter_cached(); private: + std::deque> event_queue; + std::mutex event_queue_mutex; + RoomData *current_event_queue_room = nullptr; std::vector> rooms; std::unordered_map room_data_by_id; // value is an index into |rooms| std::recursive_mutex room_data_mutex; -- cgit v1.2.3