diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-04-27 22:33:40 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-04-27 22:33:40 +0200 |
commit | f045e579e1faa186ca0ebf6e6d1e562fbcd75727 (patch) | |
tree | cbb8617ca4174954412dc6e371f7f9c11e1a4c9e /src/plugins | |
parent | 0f1319d48f2b29536dfea5851a5a476a17d2d7fb (diff) |
Fix multiline block not visible if only one line, fix naughty users having too long names or newlines in username
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Matrix.cpp | 64 |
1 files changed, 33 insertions, 31 deletions
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 += "</code></pre>"; 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)); |