From df8cbfada237cb4c0467215b55ccb697cc64d568 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 3 Oct 2020 19:51:20 +0200 Subject: Matrix: attempt to fix threading issues --- plugins/Matrix.hpp | 60 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 17 deletions(-) (limited to 'plugins/Matrix.hpp') diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp index be4a59c..de7d9b8 100644 --- a/plugins/Matrix.hpp +++ b/plugins/Matrix.hpp @@ -6,11 +6,16 @@ #include namespace QuickMedia { + struct RoomData; + struct UserInfo { + friend struct RoomData; + std::string user_id; std::string display_name; std::string avatar_url; sf::Color display_name_color; + private: std::string read_marker_event_id; }; @@ -23,8 +28,7 @@ namespace QuickMedia { }; struct Message { - // Index into |RoomData.user_info| - size_t user_id; + std::shared_ptr user; std::string event_id; std::string body; std::string url; @@ -36,19 +40,38 @@ namespace QuickMedia { }; struct RoomData { + std::shared_ptr get_user_by_id(const std::string &user_id); + void add_user(std::shared_ptr user); + + void set_user_read_marker(std::shared_ptr &user, const std::string &event_id); + std::string get_user_read_marker(std::shared_ptr &user); + + void prepend_messages_reverse(std::vector> new_messages); + void append_messages(std::vector> new_messages); + + std::shared_ptr get_message_by_id(const std::string &id); + + std::vector> get_users_excluding_me(const std::string &my_user_id); + + void acquire_room_lock(); + void release_room_lock(); + + const std::vector>& get_messages_thread_unsafe() const; + std::string id; - // Each room has its own list of user data, even if multiple rooms has the same user - // because users can have different display names and avatars in different rooms. - // The value is an index to |user_info|. - std::unordered_map user_info_by_user_id; - std::vector user_info; - std::vector> messages; - std::unordered_map> message_by_event_id; std::string name; std::string avatar_url; std::string prev_batch; bool initial_fetch_finished = false; size_t last_read_index = 0; + private: + std::mutex user_mutex; + std::mutex room_mutex; + // Each room has its own list of user data, even if multiple rooms has the same user + // because users can have different display names and avatars in different rooms. + std::unordered_map> user_info_by_user_id; + std::vector> messages; + std::unordered_map> message_by_event_id; }; enum class MessageDirection { @@ -64,7 +87,7 @@ namespace QuickMedia { std::string content_uri; }; - using RoomSyncMessages = std::unordered_map>>; + using RoomSyncMessages = std::unordered_map, std::vector>>; class Matrix : public Plugin { public: @@ -106,27 +129,30 @@ namespace QuickMedia { PluginResult set_read_marker(const std::string &room_id, const Message *message); // |message| is from |BodyItem.userdata| and is of type |Message*| - bool was_message_posted_by_me(const std::string &room_id, void *message) const; + bool was_message_posted_by_me(void *message); - std::string message_get_author_displayname(RoomData *room_data, Message *message) const; + std::string message_get_author_displayname(Message *message) const; // Cached PluginResult get_config(int *upload_size); - // Note: the returned UserInfo is o - const UserInfo* get_me(const std::string &room_id) const; + std::shared_ptr get_me(const std::string &room_id); private: PluginResult sync_response_to_body_items(const Json::Value &root, RoomSyncMessages &room_messages); - PluginResult get_previous_room_messages(const std::string &room_id, RoomData *room_data); + PluginResult get_previous_room_messages(std::shared_ptr &room_data); void events_add_user_info(const Json::Value &events_json, RoomData *room_data); void events_add_user_read_markers(const Json::Value &events_json, RoomData *room_data); - void events_add_messages(const Json::Value &events_json, RoomData *room_data, MessageDirection message_dir, RoomSyncMessages *room_messages, bool has_unread_notifications); + void events_add_messages(const Json::Value &events_json, std::shared_ptr &room_data, MessageDirection message_dir, RoomSyncMessages *room_messages, bool has_unread_notifications); void events_set_room_name(const Json::Value &events_json, RoomData *room_data); PluginResult upload_file(const std::string &room_id, const std::string &filepath, UploadInfo &file_info, UploadInfo &thumbnail_info, std::string &err_msg); std::shared_ptr get_edited_message_original_message(RoomData *room_data, std::shared_ptr message); + + std::shared_ptr get_room_by_id(const std::string &id); + void add_room(std::shared_ptr room); private: - std::unordered_map> room_data_by_id; + std::unordered_map> room_data_by_id; + std::mutex room_data_mutex; std::string user_id; std::string username; std::string access_token; -- cgit v1.2.3