ability to get user info
This commit is contained in:
parent
f6c761c90b
commit
678be3e6d4
2 changed files with 53 additions and 4 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue