From adfdc6109c18776c69d8b519606d6721354ec004 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 3 Oct 2020 11:21:01 +0200 Subject: Matrix: use correct avatar for direct message rooms --- src/plugins/Matrix.cpp | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 8796729..8189806 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -573,23 +573,26 @@ namespace QuickMedia { return user_id.substr(1, index - 1); } - static std::string combine_user_display_names_for_room_name(const std::string &my_user_id, const std::vector &user_info, const std::string &fallback_user_id) { - std::vector users_excluding_me; - for(const UserInfo &user : user_info) { + static std::vector get_users_excluding_me(const std::string &my_user_id, std::vector &user_info) { + std::vector users_excluding_me; + for(UserInfo &user : user_info) { if(user.user_id != my_user_id) { users_excluding_me.push_back(&user); } } + return users_excluding_me; + } + static std::string combine_user_display_names_for_room_name(const std::vector &user_info, const std::string &fallback_user_id) { std::string result; - if(users_excluding_me.size() == 0) + if(user_info.size() == 0) result = extract_user_name_from_user_id(fallback_user_id); - else if(users_excluding_me.size() == 1) - result = users_excluding_me[0]->display_name; - else if(users_excluding_me.size() == 2) - result = users_excluding_me[0]->display_name + " and " + users_excluding_me[1]->display_name; - else if(users_excluding_me.size() > 2) - result = users_excluding_me[0]->display_name + ", " + users_excluding_me[1]->display_name + " and " + std::to_string(users_excluding_me.size() - 2) + " other(s)"; + else if(user_info.size() == 1) + result = user_info[0]->display_name; + else if(user_info.size() == 2) + result = user_info[0]->display_name + " and " + user_info[1]->display_name; + else if(user_info.size() > 2) + result = user_info[0]->display_name + ", " + user_info[1]->display_name + " and " + std::to_string(user_info.size() - 2) + " other(s)"; return result; } @@ -616,6 +619,10 @@ namespace QuickMedia { room_data->name = name_json.asString(); } + std::vector users_excluding_me; + if(room_data->name.empty() || room_data->avatar_url.empty()) + users_excluding_me = get_users_excluding_me(user_id, room_data->user_info); + for(const Json::Value &event_item_json : events_json) { if(!event_item_json.isObject()) continue; @@ -633,12 +640,17 @@ namespace QuickMedia { continue; if(room_data->name.empty()) - room_data->name = combine_user_display_names_for_room_name(user_id, room_data->user_info, creator_json.asString()); + room_data->name = combine_user_display_names_for_room_name(users_excluding_me, creator_json.asString()); if(room_data->avatar_url.empty()) { - auto user_it = room_data->user_info_by_user_id.find(creator_json.asString()); - if(user_it != room_data->user_info_by_user_id.end()) - room_data->avatar_url = room_data->user_info[user_it->second].avatar_url; + if(users_excluding_me.empty()) { + auto user_it = room_data->user_info_by_user_id.find(creator_json.asString()); + if(user_it != room_data->user_info_by_user_id.end()) + room_data->avatar_url = room_data->user_info[user_it->second].avatar_url; + } else { + // TODO: If there are multiple users, then we want to use some other type of avatar, not the first users avatar + room_data->avatar_url = users_excluding_me.front()->avatar_url; + } } } -- cgit v1.2.3