#pragma once #include "../include/FileAnalyzer.hpp" #include "Plugin.hpp" #include "Page.hpp" #include #include #include #include namespace QuickMedia { // Dummy, only play one video. TODO: Play all videos in room, as related videos? class MatrixVideoPage : public Page { public: MatrixVideoPage(Program *program) : Page(program) {} 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; } bool is_video_page() const override { return true; } }; 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; }; enum class MessageType { TEXT, IMAGE, VIDEO, AUDIO, FILE, REDACTION }; enum class RelatedEventType { NONE, REPLY, EDIT, REDACTION }; struct Message { std::shared_ptr user; std::string event_id; std::string body; std::string url; std::string thumbnail_url; std::string related_event_id; RelatedEventType related_event_type; bool mentions_me = false; time_t timestamp = 0; MessageType type; }; 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); // Ignores duplicates void prepend_messages_reverse(std::vector> new_messages); // Ignores duplicates 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; std::string name; std::string avatar_url; std::string prev_batch; bool initial_fetch_finished = false; // These are messages fetched with |Matrix::get_message_by_id|. Needed to show replies, when replying to old message not part of /sync. // The value is nullptr if the message is fetched and cached but the event if referenced an invalid message. // TODO: Verify if replied to messages are also part of /sync; then this is not needed. std::unordered_map> fetched_messages_by_event_id; 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 { BEFORE, AFTER }; struct UploadInfo { ContentType content_type; size_t file_size; std::optional dimensions; std::optional duration_seconds; std::string content_uri; }; using Messages = std::vector>; using RoomSyncMessages = std::unordered_map, Messages>; using Rooms = std::vector>; bool message_contains_user_mention(const std::string &msg, const std::string &username); class Matrix { public: PluginResult sync(RoomSyncMessages &room_messages); void get_room_join_updates(Rooms &new_rooms); PluginResult get_all_synced_room_messages(std::shared_ptr room, Messages &messages); PluginResult get_previous_room_messages(std::shared_ptr room, Messages &messages); // |url| should only be set when uploading media. // TODO: Make api better. PluginResult post_message(std::shared_ptr room, const std::string &body, const std::optional &file_info, const std::optional &thumbnail_info); // |relates_to| is from |BodyItem.userdata| and is of type |Message*| PluginResult post_reply(std::shared_ptr room, const std::string &body, void *relates_to); // |relates_to| is from |BodyItem.userdata| and is of type |Message*| PluginResult post_edit(std::shared_ptr room, const std::string &body, void *relates_to); PluginResult post_file(std::shared_ptr room, const std::string &filepath, std::string &err_msg); PluginResult login(const std::string &username, const std::string &password, const std::string &homeserver, std::string &err_msg); PluginResult logout(); // |message| is from |BodyItem.userdata| and is of type |Message*| PluginResult delete_message(std::shared_ptr room, void *message, std::string &err_msg); PluginResult load_and_verify_cached_session(); PluginResult on_start_typing(std::shared_ptr room); PluginResult on_stop_typing(std::shared_ptr room); PluginResult set_read_marker(std::shared_ptr room, const Message *message); // |message| is from |BodyItem.userdata| and is of type |Message*| bool was_message_posted_by_me(void *message); std::string message_get_author_displayname(Message *message) const; // Cached PluginResult get_config(int *upload_size); std::shared_ptr get_me(std::shared_ptr room); // Returns nullptr if message cant be found. Note: cached std::shared_ptr get_message_by_id(RoomData *room, const std::string &event_id); bool use_tor = false; private: PluginResult sync_response_to_body_items(const rapidjson::Document &root, RoomSyncMessages &room_messages); PluginResult get_previous_room_messages(std::shared_ptr &room_data); void events_add_user_info(const rapidjson::Value &events_json, RoomData *room_data); void events_add_user_read_markers(const rapidjson::Value &events_json, RoomData *room_data); void events_add_messages(const rapidjson::Value &events_json, std::shared_ptr &room_data, MessageDirection message_dir, RoomSyncMessages *room_messages, bool has_unread_notifications); void events_set_room_name(const rapidjson::Value &events_json, RoomData *room_data); std::shared_ptr parse_message_event(const rapidjson::Value &event_item_json, RoomData *room_data); PluginResult upload_file(std::shared_ptr room, 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); DownloadResult download_json(rapidjson::Document &result, const std::string &url, std::vector additional_args, bool use_browser_useragent = false, std::string *err_msg = nullptr) const; private: std::vector> rooms; std::unordered_map room_data_by_id; // value is an index into |rooms| size_t room_list_read_index = 0; std::mutex room_data_mutex; std::string user_id; std::string username; std::string access_token; std::string homeserver; std::optional upload_limit; std::string next_batch; }; }