request extra room info

This commit is contained in:
Sorunome 2019-10-24 14:24:02 +02:00
parent 6394b300de
commit 2be8f3cc2b
No known key found for this signature in database
GPG key ID: 63E31F7B5993A9C4
2 changed files with 56 additions and 1 deletions

View file

@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include <3ds.h> #include <3ds.h>
#include <jansson.h> #include <jansson.h>
#include <map>
namespace Matrix { namespace Matrix {
@ -27,6 +28,11 @@ struct MemberInfo {
std::string avatarUrl; std::string avatarUrl;
}; };
struct ExtraRoomInfo {
std::string canonicalAlias;
std::map<std::string, MemberInfo> members;
};
enum struct RequestError : u8 { enum struct RequestError : u8 {
none, none,
timeout, timeout,
@ -69,10 +75,12 @@ public:
std::string resolveRoom(std::string alias); std::string resolveRoom(std::string alias);
std::vector<std::string> getJoinedRooms(); std::vector<std::string> getJoinedRooms();
RoomInfo getRoomInfo(std::string roomId); RoomInfo getRoomInfo(std::string roomId);
ExtraRoomInfo getExtraRoomInfo(std::string roomId);
MemberInfo getMemberInfo(std::string userId, std::string roomId = ""); MemberInfo getMemberInfo(std::string userId, std::string roomId = "");
std::string getRoomName(std::string roomId); std::string getRoomName(std::string roomId);
std::string getRoomTopic(std::string roomId); std::string getRoomTopic(std::string roomId);
std::string getRoomAvatar(std::string roomId); std::string getRoomAvatar(std::string roomId);
std::string getCanonicalAlias(std::string roomId);
std::string sendEmote(std::string roomId, std::string text); std::string sendEmote(std::string roomId, std::string text);
std::string sendNotice(std::string roomId, std::string text); std::string sendNotice(std::string roomId, std::string text);
std::string sendText(std::string roomId, std::string text); std::string sendText(std::string roomId, std::string text);

View file

@ -154,6 +154,41 @@ RoomInfo Client::getRoomInfo(std::string roomId) {
return info; return info;
} }
ExtraRoomInfo Client::getExtraRoomInfo(std::string roomId) {
roomId = resolveRoom(roomId);
ExtraRoomInfo info;
info.canonicalAlias = getCanonicalAlias(roomId);
// next fetch the members
std::string path = "/_matrix/client/r0/rooms/" + urlencode(roomId) + "/joined_members";
json_t* ret = doRequest("GET", path);
if (!ret) {
return info;
}
json_t* joined = json_object_get(ret, "joined");
if (!joined || json_typeof(joined) != JSON_OBJECT) {
json_decref(ret);
return info;
}
const char* mxid;
json_t* member;
json_object_foreach(joined, mxid, member) {
const char* displayname = json_object_get_string_value(member, "display_name");
const char* avatarUrl = json_object_get_string_value(member, "avatar_url");
MemberInfo memInfo;
if (displayname) {
memInfo.displayname = displayname;
}
if (avatarUrl) {
memInfo.avatarUrl = avatarUrl;
}
info.members[mxid] = memInfo;
}
json_decref(ret);
return info;
}
MemberInfo Client::getMemberInfo(std::string userId, std::string roomId) { MemberInfo Client::getMemberInfo(std::string userId, std::string roomId) {
std::string displayname = ""; std::string displayname = "";
std::string avatarUrl = ""; std::string avatarUrl = "";
@ -233,6 +268,18 @@ std::string Client::getRoomAvatar(std::string roomId) {
return urlStr; return urlStr;
} }
std::string Client::getCanonicalAlias(std::string roomId) {
json_t* ret = getStateEvent(roomId, "m.room.canonical_alias", "");
const char* aliasCStr = json_object_get_string_value(ret, "alias");
if (!aliasCStr) {
if (ret) json_decref(ret);
return "";
}
std::string aliasStr = aliasCStr;
json_decref(ret);
return aliasStr;
}
std::string Client::sendEmote(std::string roomId, std::string text) { std::string Client::sendEmote(std::string roomId, std::string text) {
json_t* request = json_object(); json_t* request = json_object();
json_object_set_new(request, "msgtype", json_string("m.emote")); json_object_set_new(request, "msgtype", json_string("m.emote"));
@ -592,10 +639,10 @@ void Client::registerFilter() {
void Client::syncLoop() { void Client::syncLoop() {
u32 timeout = 60; u32 timeout = 60;
while (true) { while (true) {
std::string token = store->getSyncToken();
if (stopSyncing) { if (stopSyncing) {
return; return;
} }
std::string token = store->getSyncToken();
std::string filterId = store->getFilterId(); std::string filterId = store->getFilterId();
if (filterId == "") { if (filterId == "") {
registerFilter(); registerFilter();