From c65a57e884de51cade584e3f01c7c5627aa6ebd8 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 7 Nov 2022 14:23:49 +0100 Subject: Matrix: fix edit being replaced with old message when re-entering the room (without restart) Add quote colors to 4chan, monospace for codeblocks --- src/plugins/Matrix.cpp | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'src/plugins/Matrix.cpp') diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 14a5bbe..a9284f5 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -599,7 +599,7 @@ namespace QuickMedia { } static std::string message_to_room_description_text(Message *message) { - std::string body = strip(formatted_text_to_qm_text(message->body.c_str(), message->body.size())); + std::string body = strip(formatted_text_to_qm_text(message->body.c_str(), message->body.size(), true)); if(message->type == MessageType::REACTION) return "Reacted with: " + body; else if(message->related_event_type == RelatedEventType::REPLY) @@ -2102,7 +2102,7 @@ namespace QuickMedia { } // TODO: Do not show notification if mention is a reply to somebody else that replies to me? also dont show notification everytime a mention is edited - bool message_contains_user_mention(const std::string &msg, const std::string &username) { + static bool message_contains_user_mention(const std::string &msg, const std::string &username) { if(msg.empty() || username.empty()) return false; @@ -2129,6 +2129,16 @@ namespace QuickMedia { return false; } + bool message_contains_user_mention(const Message *message, const std::string &username, const std::string &user_id) { + const std::string formatted_text = formatted_text_to_qm_text(message->body.c_str(), message->body.size(), false); + return message_contains_user_mention(formatted_text, username) || message_contains_user_mention(formatted_text, user_id); + } + + bool message_contains_user_mention(const BodyItem *body_item, const std::string &username, const std::string &user_id) { + const std::string formatted_text = Text::to_printable_string(body_item->get_description()); + return message_contains_user_mention(formatted_text, username) || message_contains_user_mention(formatted_text, user_id); + } + bool message_is_timeline(Message *message) { return message->type >= MessageType::TEXT && message->type <= MessageType::FILE; } @@ -2186,8 +2196,10 @@ namespace QuickMedia { for(auto &message : new_messages) { // TODO: Is @room ok? shouldn't we also check if the user has permission to do @room? (only when notifications are limited to @mentions) // TODO: Is comparing against read marker timestamp ok enough? - if(me && message->timestamp > read_marker_message_timestamp) - message->notification_mentions_me = message_contains_user_mention(message->body, my_display_name) || message_contains_user_mention(message->body, me->user_id) || message_contains_user_mention(message->body, "@room"); + if(me && message->timestamp > read_marker_message_timestamp) { + std::string message_str = formatted_text_to_qm_text(message->body.c_str(), message->body.size(), false); + message->notification_mentions_me = message_contains_user_mention(message_str, my_display_name) || message_contains_user_mention(message_str, me->user_id) || message_contains_user_mention(message_str, "@room"); + } } } @@ -2277,6 +2289,7 @@ namespace QuickMedia { bool inside_font_tag = false; bool font_tag_has_custom_color = false; bool inside_code_tag = false; + bool allow_formatted_text = false; mgl::Color font_color = mgl::Color(255, 255, 255, 255); }; @@ -2327,7 +2340,7 @@ namespace QuickMedia { if(parse_userdata.inside_code_tag) formatted_text_flags |= FORMATTED_TEXT_FLAG_MONOSPACE; - if(formatted_text_flags != FORMATTED_TEXT_FLAG_NONE) + if(formatted_text_flags != FORMATTED_TEXT_FLAG_NONE && parse_userdata.allow_formatted_text) parse_userdata.result += Text::formatted_text(text_to_add, parse_userdata.font_color, formatted_text_flags); else parse_userdata.result += std::move(text_to_add); @@ -2338,8 +2351,9 @@ namespace QuickMedia { return 0; } - std::string formatted_text_to_qm_text(const char *str, size_t size) { + std::string formatted_text_to_qm_text(const char *str, size_t size, bool allow_formatted_text) { FormattedTextParseUserdata parse_userdata; + parse_userdata.allow_formatted_text = allow_formatted_text; html_parser_parse(str, size, formattext_text_parser_callback, &parse_userdata); return std::move(parse_userdata.result); } @@ -3429,6 +3443,10 @@ namespace QuickMedia { return formatted_body; } + void Matrix::on_exit_room(RoomData *room) { + my_events_transaction_ids.clear(); + } + PluginResult Matrix::post_message(RoomData *room, const std::string &body, std::string &event_id_response, const std::optional &file_info, const std::optional &thumbnail_info, const std::string &msgtype) { std::string transaction_id = create_transaction_id(); if(transaction_id.empty()) @@ -3520,17 +3538,12 @@ namespace QuickMedia { if(msg_begin != std::string::npos) return str.substr(msg_begin + 2); } + } else { + return formatted_text_to_qm_text(str.c_str(), str.size(), false); } return str; } - std::string message_get_body_remove_formatting(Message *message) { - if(message->related_event_type == RelatedEventType::REPLY || message->related_event_type == RelatedEventType::EDIT) - return remove_reply_formatting(message->body); - else - return message->body; - } - static std::string block_quote(const std::string &str) { std::string result; for(char c : str) { -- cgit v1.2.3