room info callback

This commit is contained in:
Sorunome 2019-10-20 23:49:08 +02:00
parent ca77f6450c
commit a5c1f204d2
No known key found for this signature in database
GPG key ID: 63E31F7B5993A9C4
2 changed files with 68 additions and 14 deletions

View file

@ -8,8 +8,6 @@
namespace Matrix { namespace Matrix {
typedef void (*eventCallback)(std::string roomId, json_t* event);
class Store { class Store {
public: public:
virtual void setSyncToken(std::string token) = 0; virtual void setSyncToken(std::string token) = 0;
@ -34,6 +32,9 @@ enum struct RequestError : u8 {
timeout, timeout,
}; };
typedef void (*eventCallback)(std::string roomId, json_t* event);
typedef void (*roomInfoCallback)(std::string roomId, RoomInfo info);
class Client { class Client {
private: private:
public: public:
@ -50,6 +51,7 @@ public:
eventCallback event = NULL; eventCallback event = NULL;
eventCallback leaveRoom = NULL; eventCallback leaveRoom = NULL;
eventCallback inviteRoom = NULL; eventCallback inviteRoom = NULL;
roomInfoCallback roomInfo = NULL;
} callbacks; } callbacks;
void processSync(json_t* sync); void processSync(json_t* sync);
void registerFilter(); void registerFilter();
@ -84,6 +86,7 @@ public:
void setEventCallback(eventCallback cb); void setEventCallback(eventCallback cb);
void setLeaveRoomCallback(eventCallback cb); void setLeaveRoomCallback(eventCallback cb);
void setInviteRoomCallback(eventCallback cb); void setInviteRoomCallback(eventCallback cb);
void setRoomInfoCallback(roomInfoCallback cb);
}; };
}; // namespace Matrix }; // namespace Matrix

View file

@ -1,4 +1,4 @@
#include "../include/matrixclient.h" #include <matrixclient.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <3ds.h> #include <3ds.h>
@ -17,7 +17,7 @@
#define SOC_BUFFERSIZE 0x100000 #define SOC_BUFFERSIZE 0x100000
#define SYNC_TIMEOUT 10000 #define SYNC_TIMEOUT 10000
#define DEBUG 1 #define DEBUG 0
#if DEBUG #if DEBUG
#define D #define D
@ -25,9 +25,15 @@
#define D for(;0;) #define D for(;0;)
#endif #endif
#if DEBUG
PrintConsole* topScreenConsole = NULL; PrintConsole* topScreenConsole = NULL;
#endif
#if DEBUG
#define printf_top(f_, ...) do {consoleSelect(topScreenConsole);printf((f_), ##__VA_ARGS__);} while(0) #define printf_top(f_, ...) do {consoleSelect(topScreenConsole);printf((f_), ##__VA_ARGS__);} while(0)
#else
#define printf_top(f_, ...) do {} while(0)
#endif
namespace Matrix { namespace Matrix {
@ -43,10 +49,12 @@ Client::Client(std::string homeserverUrl, std::string matrixToken, Store* client
clientStore = new MemoryStore; clientStore = new MemoryStore;
} }
store = clientStore; store = clientStore;
#if DEBUG
if (!topScreenConsole) { if (!topScreenConsole) {
topScreenConsole = new PrintConsole; topScreenConsole = new PrintConsole;
consoleInit(GFX_TOP, topScreenConsole); consoleInit(GFX_TOP, topScreenConsole);
} }
#endif
} }
std::string Client::getToken() { std::string Client::getToken() {
@ -369,6 +377,10 @@ void Client::setInviteRoomCallback(eventCallback cb) {
callbacks.inviteRoom = cb; callbacks.inviteRoom = cb;
} }
void Client::setRoomInfoCallback(roomInfoCallback cb) {
callbacks.roomInfo = cb;
}
void Client::processSync(json_t* sync) { void Client::processSync(json_t* sync) {
json_t* rooms = json_object_get(sync, "rooms"); json_t* rooms = json_object_get(sync, "rooms");
if (!rooms) { if (!rooms) {
@ -477,23 +489,62 @@ void Client::processSync(json_t* sync) {
} }
} }
if (joinedRooms && callbacks.event) { if (joinedRooms) {
json_object_foreach(joinedRooms, roomId, room) { json_object_foreach(joinedRooms, roomId, room) {
// rooms that we are joined // rooms that we are joined
json_t* state = json_object_get(room, "state");
if (callbacks.roomInfo && state) {
json_t* events = json_object_get(state, "events");
if (events) {
RoomInfo info;
bool addedInfo = false;
json_array_foreach(events, index, event) {
json_t* type = json_object_get(event, "type");
if (!type) {
continue;
}
json_t* content = json_object_get(event, "content");
if (!content) {
continue;
}
const char* typeStr = json_string_value(type);
if (strcmp(typeStr, "m.room.name") == 0) {
json_t* name = json_object_get(content, "name");
if (name) {
info.name = json_string_value(name);
addedInfo = true;
}
} else if (strcmp(typeStr, "m.room.topic") == 0) {
json_t* topic = json_object_get(event, "topic");
if (topic) {
info.topic = json_string_value(topic);
addedInfo = true;
}
} else if (strcmp(typeStr, "m.room.avatar") == 0) {
json_t* url = json_object_get(event, "url");
if (url) {
info.avatarUrl = json_string_value(url);
addedInfo = true;
}
}
}
if (addedInfo) {
callbacks.roomInfo(roomId, info);
}
}
}
json_t* timeline = json_object_get(room, "timeline"); json_t* timeline = json_object_get(room, "timeline");
if (!timeline) { if (callbacks.event && timeline) {
continue;
}
json_t* events = json_object_get(timeline, "events"); json_t* events = json_object_get(timeline, "events");
if (!events) { if (events) {
continue;
}
json_array_foreach(events, index, event) { json_array_foreach(events, index, event) {
callbacks.event(roomId, event); callbacks.event(roomId, event);
} }
} }
} }
} }
}
}
void Client::registerFilter() { void Client::registerFilter() {
static const char *json = static const char *json =