From 8e79b2e10827f42d76ec61621f2c484f9cdc2551 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 19 May 2021 23:45:27 +0200 Subject: Start on notifications run matrix delegate on main (ui) thread instead of mutex hell --- plugins/Matrix.hpp | 85 ++++++++++++++++-------------------------------------- 1 file changed, 25 insertions(+), 60 deletions(-) (limited to 'plugins/Matrix.hpp') diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp index 0c90587..9d6f0b6 100644 --- a/plugins/Matrix.hpp +++ b/plugins/Matrix.hpp @@ -252,6 +252,7 @@ namespace QuickMedia { BANNED }; + // All of methods in this class are called in the main (ui) thread class MatrixDelegate { public: virtual ~MatrixDelegate() = default; @@ -270,8 +271,6 @@ namespace QuickMedia { virtual void add_unread_notification(RoomData *room, std::string event_id, std::string sender, std::string body) = 0; - virtual void update(MatrixPageType page_type, Body *chat_body, bool messages_tab_visible) { (void)page_type; } - virtual void clear_data() = 0; }; @@ -296,53 +295,31 @@ namespace QuickMedia { void add_unread_notification(RoomData *room, std::string event_id, std::string sender, std::string body) override; - void update(MatrixPageType page_type, Body *chat_body, bool messages_tab_visible) override; - void clear_data() override; Program *program; Matrix *matrix; + MatrixChatPage *chat_page; MatrixRoomsPage *rooms_page; MatrixRoomTagsPage *room_tags_page; MatrixInvitesPage *invites_page; private: - void update_room_description(RoomData *room, Messages &new_messages, bool is_initial_sync, bool sync_is_cache, Body *chat_body, bool messages_tab_visible); - void update_pending_room_messages(MatrixPageType page_type, Body *chat_body, bool messages_tab_visible); + void update_room_description(RoomData *room, const Messages &new_messages, bool is_initial_sync, bool sync_is_cache); private: - struct RoomMessagesData { - Messages messages; - bool is_initial_sync; - bool sync_is_cache; - MessageDirection message_dir; - }; - - struct Notification { - std::string event_id; - std::string sender; - std::string body; - }; - std::map> room_body_item_by_room; - std::mutex room_body_items_mutex; - std::map pending_room_messages; - std::mutex pending_room_messages_mutex; - - std::unordered_map> unread_notifications; std::map> last_message_by_room; }; class MatrixRoomsPage : public Page { public: - MatrixRoomsPage(Program *program, Body *body, std::string title, MatrixRoomTagsPage *room_tags_page, SearchBar *search_bar); + MatrixRoomsPage(Program *program, Body *body, std::string title, MatrixRoomTagsPage *room_tags_page); ~MatrixRoomsPage() override; const char* get_title() const override { return title.c_str(); } PluginResult submit(const std::string &title, const std::string &url, std::vector &result_tabs) override; + bool submit_is_async() override { return false; } bool clear_search_after_submit() override { return true; } - void on_navigate_to_page(Body *body) override; - - void update() override; void add_body_item(std::shared_ptr body_item); void move_room_to_top(RoomData *room); @@ -351,31 +328,23 @@ namespace QuickMedia { void set_current_chat_page(MatrixChatPage *chat_page); void clear_data(); - void sort_rooms(); MatrixQuickMedia *matrix_delegate = nullptr; - bool filter_on_update = false; private: - std::mutex mutex; - std::vector> room_body_items; - std::vector pending_remove_body_items; Body *body = nullptr; std::string title; MatrixRoomTagsPage *room_tags_page = nullptr; - SearchBar *search_bar = nullptr; MatrixChatPage *current_chat_page = nullptr; - bool clear_data_on_update = false; - bool sort_on_update = false; }; class MatrixRoomTagsPage : public Page { public: - MatrixRoomTagsPage(Program *program, Body *body, SearchBar *search_bar) : Page(program), body(body), search_bar(search_bar) {} + MatrixRoomTagsPage(Program *program, Body *body) : Page(program), body(body) {} const char* get_title() const override { return "Tags"; } PluginResult submit(const std::string &title, const std::string &url, std::vector &result_tabs) override; + bool submit_is_async() override { return false; } bool clear_search_after_submit() override { return true; } - void update() override; void add_room_body_item_to_tag(std::shared_ptr body_item, const std::string &tag); void remove_room_body_item_from_tag(std::shared_ptr body_item, const std::string &tag); @@ -385,50 +354,37 @@ namespace QuickMedia { void set_current_rooms_page(MatrixRoomsPage *rooms_page); void clear_data(); - void sort_rooms(); MatrixQuickMedia *matrix_delegate = nullptr; - bool filter_on_update = false; private: struct TagData { std::shared_ptr tag_item; std::vector> room_body_items; }; - std::recursive_mutex mutex; Body *body; std::map tag_body_items_by_name; - std::map>> add_room_body_items_by_tags; - std::map>> remove_room_body_items_by_tags; MatrixRoomsPage *current_rooms_page = nullptr; - bool clear_data_on_update = false; - SearchBar *search_bar = nullptr; }; class MatrixInvitesPage : public Page { public: - MatrixInvitesPage(Program *program, Matrix *matrix, Body *body, SearchBar *search_bar); + MatrixInvitesPage(Program *program, Matrix *matrix, Body *body); const char* get_title() const override { return title.c_str(); } PluginResult submit(const std::string &title, const std::string &url, std::vector &result_tabs) override; + bool submit_is_async() override { return false; } bool clear_search_after_submit() override { return true; } - void update() override; void add_body_item(std::shared_ptr body_item); void remove_body_item_by_room_id(const std::string &room_id); void clear_data(); - bool filter_on_update = false; private: Matrix *matrix; - std::mutex mutex; - std::vector> body_items; - std::vector pending_remove_body_items; Body *body; std::string title = "Invites (0)"; size_t prev_invite_count = 0; - bool clear_data_on_update = false; - SearchBar *search_bar = nullptr; }; class MatrixInviteDetailsPage : public Page { @@ -465,14 +421,7 @@ namespace QuickMedia { ~MatrixChatPage(); const char* get_title() const override { return ""; } - PluginResult submit(const std::string &title, const std::string &url, std::vector &result_tabs) override { - (void)title; - (void)url; - (void)result_tabs; - return PluginResult::ERR; - } PageTypez get_type() const override { return PageTypez::CHAT; } - void update() override; const std::string room_id; MatrixRoomsPage *rooms_page = nullptr; @@ -510,6 +459,17 @@ namespace QuickMedia { int current_page; }; + class MatrixNotificationsPage : public Page { + public: + MatrixNotificationsPage(Program *program, Body *notifications_body) : Page(program), notifications_body(notifications_body) {} + const char* get_title() const override { return "Notifications (0)"; } + PluginResult submit(const std::string&, const std::string&, std::vector&) override { + return PluginResult::OK; + } + private: + Body *notifications_body; + }; + class Matrix { public: // TODO: Make this return the Matrix object instead, to force users to call start_sync @@ -578,6 +538,10 @@ namespace QuickMedia { // Also clears the event queue void disable_event_queue(); + // Calls the |MatrixDelegate| pending events. + // Should be called from the main (ui) thread + void update(); + // Has to be called in the main thread. // Returns nullptr if there are no new events. std::unique_ptr pop_event(); @@ -622,6 +586,7 @@ namespace QuickMedia { 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: + MessageQueue> ui_thread_tasks; std::deque> event_queue; std::mutex event_queue_mutex; RoomData *current_event_queue_room = nullptr; -- cgit v1.2.3