diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/Matrix.cpp | 66 |
1 files changed, 64 insertions, 2 deletions
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<RoomData>(); 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<BodyItem>(std::move(room_id_str)); - //body_item->url = ""; + auto body_item = std::make_unique<BodyItem>(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); |