aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-05-19 23:45:27 +0200
committerdec05eba <dec05eba@protonmail.com>2021-05-20 01:16:46 +0200
commit8e79b2e10827f42d76ec61621f2c484f9cdc2551 (patch)
tree2f55f23bdebace14d4c56d15f72a40cbafd0ca3e /plugins
parent6beaa000d590db342bc0198590686cbbd6304eb4 (diff)
Start on notifications
run matrix delegate on main (ui) thread instead of mutex hell
Diffstat (limited to 'plugins')
-rw-r--r--plugins/Matrix.hpp85
-rw-r--r--plugins/Page.hpp5
2 files changed, 27 insertions, 63 deletions
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<RoomData*, std::shared_ptr<BodyItem>> room_body_item_by_room;
- std::mutex room_body_items_mutex;
- std::map<RoomData*, RoomMessagesData> pending_room_messages;
- std::mutex pending_room_messages_mutex;
-
- std::unordered_map<RoomData*, std::vector<Notification>> unread_notifications;
std::map<RoomData*, std::shared_ptr<Message>> 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<Tab> &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<BodyItem> 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<std::shared_ptr<BodyItem>> room_body_items;
- std::vector<std::string> 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<Tab> &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<BodyItem> body_item, const std::string &tag);
void remove_room_body_item_from_tag(std::shared_ptr<BodyItem> 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<BodyItem> tag_item;
std::vector<std::shared_ptr<BodyItem>> room_body_items;
};
- std::recursive_mutex mutex;
Body *body;
std::map<std::string, TagData> tag_body_items_by_name;
- std::map<std::string, std::vector<std::shared_ptr<BodyItem>>> add_room_body_items_by_tags;
- std::map<std::string, std::vector<std::shared_ptr<BodyItem>>> 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<Tab> &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<BodyItem> 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<std::shared_ptr<BodyItem>> body_items;
- std::vector<std::string> 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<Tab> &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<Tab>&) 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<MatrixEvent> pop_event();
@@ -622,6 +586,7 @@ namespace QuickMedia {
std::shared_ptr<UserInfo> 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<std::function<void()>> ui_thread_tasks;
std::deque<std::unique_ptr<MatrixEvent>> event_queue;
std::mutex event_queue_mutex;
RoomData *current_event_queue_room = nullptr;
diff --git a/plugins/Page.hpp b/plugins/Page.hpp
index 78eb3c4..be6eb76 100644
--- a/plugins/Page.hpp
+++ b/plugins/Page.hpp
@@ -35,6 +35,8 @@ namespace QuickMedia {
(void)result_tabs;
return PluginResult::ERR;
}
+ // Override and return false to make submit run in the main (ui) thread
+ virtual bool submit_is_async() { return true; }
virtual bool clear_search_after_submit() { return false; }
// Note: If pagination is done by fetching the next page until we get to |page|, then the "current page" should be reset everytime |search| is called.
// Note: the first page is 0
@@ -53,9 +55,6 @@ namespace QuickMedia {
// This is called both when first navigating to page and when going back to page
virtual void on_navigate_to_page(Body *body) { (void)body; }
- // Called periodically (every frame right now) if this page is the currently active one
- virtual void update() {}
-
std::unique_ptr<Body> create_body();
std::unique_ptr<SearchBar> create_search_bar(const std::string &placeholder_text, int search_delay);