From f045e579e1faa186ca0ebf6e6d1e562fbcd75727 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 27 Apr 2021 22:33:40 +0200 Subject: Fix multiline block not visible if only one line, fix naughty users having too long names or newlines in username --- plugins/Matrix.hpp | 3 +++ src/QuickMedia.cpp | 8 +++---- src/plugins/Matrix.cpp | 64 ++++++++++++++++++++++++++------------------------ 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp index f76d70d..1063e0e 100644 --- a/plugins/Matrix.hpp +++ b/plugins/Matrix.hpp @@ -14,8 +14,11 @@ namespace QuickMedia { struct RoomData; struct Message; + static const int AUTHOR_MAX_LENGTH = 48; + std::string remove_reply_formatting(const std::string &str); std::string message_get_body_remove_formatting(Message *message); + std::string extract_first_line_remove_newline_elipses(const std::string &str, size_t max_length); enum class UserResolveState { NOT_RESOLVED, diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 804d3d3..16f19d4 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3495,7 +3495,7 @@ namespace QuickMedia { static std::shared_ptr message_to_body_item(RoomData *room, Message *message, const std::string &my_display_name, const std::string &my_user_id) { auto body_item = BodyItem::create(""); - body_item->set_author(room->get_user_display_name(message->user)); + body_item->set_author(extract_first_line_remove_newline_elipses(room->get_user_display_name(message->user), AUTHOR_MAX_LENGTH)); body_item->set_description(strip(message_get_body_remove_formatting(message))); body_item->set_timestamp(message->timestamp); if(!message->thumbnail_url.empty()) { @@ -3567,7 +3567,7 @@ namespace QuickMedia { }; static void user_update_display_info(BodyItem *body_item, RoomData *room, Message *message) { - body_item->set_author(room->get_user_display_name(message->user)); + body_item->set_author(extract_first_line_remove_newline_elipses(room->get_user_display_name(message->user), AUTHOR_MAX_LENGTH)); if(!is_visual_media_message_type(message->type)) { body_item->thumbnail_url = room->get_user_avatar_url(message->user); @@ -3801,7 +3801,7 @@ namespace QuickMedia { for(auto it = unresolved_reactions.begin(); it != unresolved_reactions.end();) { auto body_item = find_body_item_by_event_id(body_items.data(), body_items.size(), (*it)->related_event_id); if(body_item) { - body_item->add_reaction(current_room->get_user_display_name((*it)->user) + ": " + (*it)->body, (*it).get()); + body_item->add_reaction(extract_first_line_remove_newline_elipses(current_room->get_user_display_name((*it)->user), AUTHOR_MAX_LENGTH) + ": " + (*it)->body, (*it).get()); it = unresolved_reactions.erase(it); } else { ++it; @@ -3812,7 +3812,7 @@ namespace QuickMedia { if(message->type == MessageType::REACTION) { auto body_item = find_body_item_by_event_id(body_items.data(), body_items.size(), message->related_event_id); if(body_item) - body_item->add_reaction(current_room->get_user_display_name(message->user) + ": " + message->body, message.get()); + body_item->add_reaction(extract_first_line_remove_newline_elipses(current_room->get_user_display_name(message->user), AUTHOR_MAX_LENGTH) + ": " + message->body, message.get()); else unresolved_reactions.push_back(message); } else if(message->type == MessageType::REDACTION) { diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 0c06458..3c4a6dd 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -59,22 +59,24 @@ static std::string tag_get_name(const std::string &tag) { } } -static std::string extract_first_line_elipses(const std::string &str, size_t max_length) { - size_t index = str.find('\n'); - if(index == std::string::npos) { - if(str.size() > max_length) - return str.substr(0, max_length) + " (...)"; - return str; - } else if(index == 0) { - return ""; - } else { - return str.substr(0, std::min(index, max_length)) + " (...)"; - } -} - namespace QuickMedia { static const sf::Vector2i thumbnail_max_size(600, 337); + std::string extract_first_line_remove_newline_elipses(const std::string &str, size_t max_length) { + std::string result = str; + string_replace_all(result, '\n', " "); + size_t index = result.find('\n'); + if(index == std::string::npos) { + if(result.size() > max_length) + return result.substr(0, max_length) + " ..."; + return result; + } else if(index == 0) { + return ""; + } else { + return result.substr(0, std::min(index, max_length)) + " ..."; + } + } + static void remove_body_item_by_url(BodyItems &body_items, const std::string &url) { for(auto it = body_items.begin(); it != body_items.end();) { if((*it)->url == url) @@ -378,7 +380,7 @@ namespace QuickMedia { } void MatrixQuickMedia::add_invite(const std::string &room_id, const Invite &invite) { - std::string invited_by_display_name = invite.invited_by->room->get_user_display_name(invite.invited_by); + std::string invited_by_display_name = extract_first_line_remove_newline_elipses(invite.invited_by->room->get_user_display_name(invite.invited_by), AUTHOR_MAX_LENGTH); auto body_item = BodyItem::create(invite.room_name); body_item->set_description("Invited by " + invited_by_display_name + " (" + invite.invited_by->user_id + ")"); body_item->url = room_id; @@ -491,13 +493,13 @@ namespace QuickMedia { static std::string message_to_room_description_text(Message *message) { std::string body = strip(message->body); if(message->type == MessageType::REACTION) - return "Reacted with: " + extract_first_line_elipses(body, 150); + return "Reacted with: " + extract_first_line_remove_newline_elipses(body, 150); else if(message->related_event_type == RelatedEventType::REPLY) - return extract_first_line_elipses(remove_reply_formatting(body), 150); + return extract_first_line_remove_newline_elipses(remove_reply_formatting(body), 150); else if(message->related_event_type == RelatedEventType::EDIT) - return "Edited: " + extract_first_line_elipses(remove_reply_formatting(body), 150); + return "Edited: " + extract_first_line_remove_newline_elipses(remove_reply_formatting(body), 150); else - return extract_first_line_elipses(body, 150); + return extract_first_line_remove_newline_elipses(body, 150); } void MatrixQuickMedia::update_room_description(RoomData *room, Messages &new_messages, bool is_initial_sync, bool sync_is_cache, Body *chat_body, bool messages_tab_visible) { @@ -552,7 +554,7 @@ namespace QuickMedia { std::string room_desc; if(set_room_as_unread) room_desc += "Unread: "; - room_desc += matrix->message_get_author_displayname(last_unread_message) + ": " + message_to_room_description_text(last_unread_message); + room_desc += extract_first_line_remove_newline_elipses(matrix->message_get_author_displayname(last_unread_message), AUTHOR_MAX_LENGTH) + ": " + message_to_room_description_text(last_unread_message); int unread_notification_count = room->unread_notification_count; if(unread_notification_count > 0 && set_room_as_unread) { @@ -569,7 +571,7 @@ namespace QuickMedia { rooms_page->move_room_to_top(room); room_tags_page->move_room_to_top(room); } else if(is_initial_sync) { - room->body_item->set_description(matrix->message_get_author_displayname(last_new_message.get()) + ": " + message_to_room_description_text(last_new_message.get())); + room->body_item->set_description(extract_first_line_remove_newline_elipses(matrix->message_get_author_displayname(last_new_message.get()), AUTHOR_MAX_LENGTH) + ": " + message_to_room_description_text(last_new_message.get())); room->body_item->set_description_color(sf::Color(179, 179, 179)); } } @@ -583,7 +585,7 @@ namespace QuickMedia { auto &messages = it.second.messages; bool is_initial_sync = it.second.is_initial_sync; //auto &room_body_item = room_body_item_by_room[room]; - //std::string room_desc = matrix->message_get_author_displayname(it.second.back().get()) + ": " + extract_first_line_elipses(it.second.back()->body, 150); + //std::string room_desc = extract_first_line_remove_newline_elipses(matrix->message_get_author_displayname(it.second.back().get()), AUTHOR_MAX_LENGTH) + ": " + extract_first_line_remove_newline_elipses(it.second.back()->body, 150); //room_body_item->set_description(std::move(room_desc)); if(!it.second.sync_is_cache && it.second.message_dir == MessageDirection::AFTER && !is_initial_sync) { @@ -591,7 +593,7 @@ namespace QuickMedia { if(message->notification_mentions_me) { // TODO: What if the message or username begins with "-"? also make the notification image be the avatar of the user if((!is_window_focused || room != current_room) && message->related_event_type != RelatedEventType::EDIT && message->related_event_type != RelatedEventType::REDACTION) { - show_notification("QuickMedia matrix - " + matrix->message_get_author_displayname(message.get()) + " (" + room->get_name() + ")", message->body); + show_notification("QuickMedia matrix - " + extract_first_line_remove_newline_elipses(matrix->message_get_author_displayname(message.get()), AUTHOR_MAX_LENGTH) + " (" + room->get_name() + ")", message->body); } } } @@ -2018,8 +2020,8 @@ namespace QuickMedia { // An old reaction that has been removed. New reactions are removed with m.redact return nullptr; } else if(strcmp(type_json.GetString(), "m.room.member") == 0) { - std::string user_display_name = room_data->get_user_display_name(user); - std::string sender_display_name = room_data->get_user_display_name(user_sender); + std::string user_display_name = extract_first_line_remove_newline_elipses(room_data->get_user_display_name(user), AUTHOR_MAX_LENGTH); + std::string sender_display_name = extract_first_line_remove_newline_elipses(room_data->get_user_display_name(user_sender), AUTHOR_MAX_LENGTH); std::string body; std::string reason_str; const rapidjson::Value &reason_json = GetMember(*content_json, "reason"); @@ -2045,7 +2047,7 @@ namespace QuickMedia { prev_displayname_str = std::string(prev_displayname_json.GetString(), prev_displayname_json.GetStringLength()); else prev_displayname_str = sender_json_str; - body = prev_displayname_str + " changed their display name to " + new_displayname_str; + body = extract_first_line_remove_newline_elipses(prev_displayname_str, AUTHOR_MAX_LENGTH) + " changed their display name to " + extract_first_line_remove_newline_elipses(new_displayname_str, AUTHOR_MAX_LENGTH); room_data->set_user_display_name(user, std::move(new_displayname_str)); } else if((!new_displayname_json.IsString() || new_displayname_json.GetStringLength() == 0) && prev_displayname_json.IsString()) { body = user_display_name + " removed their display name"; @@ -2184,7 +2186,7 @@ namespace QuickMedia { prefix = "💾 Download "; } else if(strcmp(content_type.GetString(), "m.emote") == 0) { // this is a /me message, TODO: show /me messages differently message->type = MessageType::TEXT; - prefix = "*" + room_data->get_user_display_name(user) + "* "; + prefix = "*" + extract_first_line_remove_newline_elipses(room_data->get_user_display_name(user), AUTHOR_MAX_LENGTH) + "* "; } else if(strcmp(content_type.GetString(), "m.notice") == 0) { // TODO: show notices differently message->type = MessageType::TEXT; prefix = "* NOTICE * "; @@ -2226,11 +2228,11 @@ namespace QuickMedia { if(user_info.size() == 0) result = extract_user_name_from_user_id(fallback_user_id); else if(user_info.size() == 1) - result = user_info[0]->room->get_user_display_name(user_info[0]); + result = extract_first_line_remove_newline_elipses(user_info[0]->room->get_user_display_name(user_info[0]), AUTHOR_MAX_LENGTH); else if(user_info.size() == 2) - result = user_info[0]->room->get_user_display_name(user_info[0]) + " and " + user_info[1]->room->get_user_display_name(user_info[1]); + result = extract_first_line_remove_newline_elipses(user_info[0]->room->get_user_display_name(user_info[0]) + " and " + user_info[1]->room->get_user_display_name(user_info[1]), 64); else if(user_info.size() > 2) - result = user_info[0]->room->get_user_display_name(user_info[0]) + ", " + user_info[1]->room->get_user_display_name(user_info[1]) + " and " + std::to_string(user_info.size() - 2) + " other(s)"; + result = extract_first_line_remove_newline_elipses(user_info[0]->room->get_user_display_name(user_info[0]) + ", " + user_info[1]->room->get_user_display_name(user_info[1]) + " and " + std::to_string(user_info.size() - 2) + " other(s)", 64); return result; } @@ -2699,7 +2701,7 @@ namespace QuickMedia { std::string line_str(str, size); html_escape_sequences(line_str); - if(size >= 3 && strncmp(str, "```", 3) == 0) { + if(size >= 3 && strncmp(str, "```", 3) == 0 && line_str.find("```", 3) == std::string::npos) { if(is_inside_code_block) { formatted_body += ""; is_inside_code_block = false; @@ -3737,7 +3739,7 @@ namespace QuickMedia { if(num_joined_members_json.IsInt()) { if(!description.empty()) description += '\n'; - description += "👤" + std::to_string(num_joined_members_json.GetInt()); + description += ("👤" + std::to_string(num_joined_members_json.GetInt()) + " users"); } room_body_item->set_description(std::move(description)); -- cgit v1.2.3