From 7bf0f7107b72af01fea83a7343ce1c32e54865e9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 21 Sep 2020 04:15:39 +0200 Subject: Add matrix room names --- src/QuickMedia.cpp | 5 ++-- src/plugins/Matrix.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 1d0518f..d756b98 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3068,7 +3068,7 @@ namespace QuickMedia { // The room id should be saved in a file when changing viewed room. std::string current_room_id; if(!tabs[ROOMS_TAB_INDEX].body->items.empty()) - current_room_id = tabs[ROOMS_TAB_INDEX].body->items[0]->get_title(); + current_room_id = tabs[ROOMS_TAB_INDEX].body->items[0]->url; // TODO: Allow empty initial room (if the user hasn't joined any room yet) assert(!current_room_id.empty()); @@ -3090,8 +3090,7 @@ namespace QuickMedia { } else if(tabs[selected_tab].type == ChatTabType::ROOMS) { BodyItem *selected_item = tabs[selected_tab].body->get_selected(); if(selected_item) { - // TODO: Change to selected_item->url once rooms have a display name - current_room_id = selected_item->get_title(); + current_room_id = selected_item->url; selected_tab = MESSAGES_TAB_INDEX; room_message_index = 0; tabs[MESSAGES_TAB_INDEX].body->clear_items(); diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 77e295e..ad0dfea 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -129,16 +129,22 @@ namespace QuickMedia { continue; std::string room_id_str = room_id_json.asString(); + std::string room_name; auto room_it = room_data_by_id.find(room_id_str); if(room_it == room_data_by_id.end()) { auto room_data = std::make_unique(); room_data_by_id.insert(std::make_pair(room_id_str, std::move(room_data))); + room_name = room_id_str; fprintf(stderr, "Missing room %s from /sync, adding in joined_rooms\n", room_id_str.c_str()); + } else { + room_name = room_it->second->name; + if(room_name.empty()) + room_name = room_id_str; } - auto body_item = std::make_unique(std::move(room_id_str)); - //body_item->url = ""; + auto body_item = std::make_unique(std::move(room_name)); + body_item->url = room_id_str; result_items.push_back(std::move(body_item)); } @@ -225,6 +231,7 @@ namespace QuickMedia { const Json::Value &events_json = state_json["events"]; events_add_user_info(events_json, room_it->second.get()); + events_set_room_name(events_json, room_it->second.get()); } for(Json::Value::const_iterator it = join_json.begin(); it != join_json.end(); ++it) { @@ -255,6 +262,7 @@ namespace QuickMedia { const Json::Value &events_json = timeline_json["events"]; events_add_messages(events_json, room_it->second.get(), MessageDirection::AFTER); + events_set_room_name(events_json, room_it->second.get()); } return PluginResult::OK; @@ -360,6 +368,59 @@ namespace QuickMedia { } } + // Returns empty string on error + static std::string extract_user_name_from_user_id(const std::string &user_id) { + size_t index = user_id.find(':'); + if(index == std::string::npos || index == 0 || user_id.empty() || user_id[0] != '@') + return ""; + return user_id.substr(1, index - 1); + } + + void Matrix::events_set_room_name(const Json::Value &events_json, RoomData *room_data) { + if(!events_json.isArray()) + return; + + for(const Json::Value &event_item_json : events_json) { + if(!event_item_json.isObject()) + continue; + + const Json::Value &type_json = event_item_json["type"]; + if(!type_json.isString() || strcmp(type_json.asCString(), "m.room.name") != 0) + continue; + + const Json::Value &content_json = event_item_json["content"]; + if(!content_json.isObject()) + continue; + + const Json::Value &name_json = content_json["name"]; + if(!name_json.isString()) + continue; + + room_data->name = name_json.asString(); + return; + } + + for(const Json::Value &event_item_json : events_json) { + if(!event_item_json.isObject()) + continue; + + const Json::Value &type_json = event_item_json["type"]; + if(!type_json.isString() || strcmp(type_json.asCString(), "m.room.create") != 0) + continue; + + const Json::Value &content_json = event_item_json["content"]; + if(!content_json.isObject()) + continue; + + const Json::Value &creator_json = content_json["creator"]; + if(!creator_json.isString()) + continue; + + room_data->name = extract_user_name_from_user_id(creator_json.asString()); + return; + } + } + PluginResult Matrix::load_initial_room_data(const std::string &room_id, RoomData *room_data) { std::string from = room_data->prev_batch; if(from.empty()) { @@ -409,6 +470,7 @@ namespace QuickMedia { const Json::Value &state_json = json_root["state"]; events_add_user_info(state_json, room_data); + events_set_room_name(state_json, room_data); const Json::Value &chunk_json = json_root["chunk"]; events_add_messages(chunk_json, room_data, MessageDirection::BEFORE); -- cgit v1.2.3