ability to get user info

This commit is contained in:
Sorunome 2019-10-18 13:26:52 +02:00
parent f6c761c90b
commit 678be3e6d4
No known key found for this signature in database
GPG key ID: 63E31F7B5993A9C4
2 changed files with 53 additions and 4 deletions

View file

@ -17,7 +17,12 @@ public:
struct RoomInfo {
std::string name;
std::string topic;
std::string avatar;
std::string avatarUrl;
};
struct UserInfo {
std::string displayname;
std::string avatarUrl;
};
class Client {
@ -46,6 +51,7 @@ public:
std::string resolveRoom(std::string alias);
std::vector<std::string> getJoinedRooms();
RoomInfo getRoomInfo(std::string roomId);
UserInfo getUserInfo(std::string userId, std::string roomId = "");
std::string getRoomName(std::string roomId);
std::string getRoomTopic(std::string roomId);
std::string getRoomAvatar(std::string roomId);

View file

@ -140,7 +140,50 @@ RoomInfo Client::getRoomInfo(std::string roomId) {
RoomInfo info = {
name: getRoomName(roomId),
topic: getRoomTopic(roomId),
avatar: getRoomAvatar(roomId),
avatarUrl: getRoomAvatar(roomId),
};
return info;
}
UserInfo Client::getUserInfo(std::string userId, std::string roomId) {
std::string displayname = "";
std::string avatarUrl = "";
if (roomId != "") {
// first try fetching fro the room
json_t* ret = getStateEvent(roomId, "m.room.member", userId);
if (ret) {
json_t* val;
val = json_object_get(ret, "displayname");
if (val) {
displayname = json_string_value(val);
}
val = json_object_get(ret, "avatar_url");
if (val) {
avatarUrl = json_string_value(val);
}
json_decref(ret);
}
}
if (displayname == "") {
// then attempt the account
std::string path = "/_matrix/client/r0/profile/" + urlencode(userId);
json_t* ret = doRequest("GET", path);
if (ret) {
json_t* val;
val = json_object_get(ret, "displayname");
if (val) {
displayname = json_string_value(val);
}
val = json_object_get(ret, "avatar_url");
if (val) {
avatarUrl = json_string_value(val);
}
json_decref(ret);
}
}
UserInfo info = {
displayname: displayname,
avatarUrl: avatarUrl,
};
return info;
}
@ -467,7 +510,7 @@ json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body)
return NULL;
}
D printf_top("%s\n", readBuffer.c_str());
// D printf_top("%s\n", readBuffer.c_str());
json_error_t error;
json_t* content = json_loads(readBuffer.c_str(), 0, &error);
if (!content) {
@ -579,7 +622,7 @@ json_t* Client::doRequestHttpc(const char* method, std::string url, json_t* body
httpcCloseContext(&context);
D printf_top("%s\n", buf);
// D printf_top("%s\n", buf);
json_error_t error;
json_t* content = json_loads((char*)buf, 0, &error);