add sync_event_callback

This commit is contained in:
Sorunome 2019-10-17 21:52:34 +02:00
parent 0abfac1ff4
commit 29a44c9371
No known key found for this signature in database
GPG key ID: 63E31F7B5993A9C4
2 changed files with 14 additions and 4 deletions

View file

@ -24,8 +24,10 @@ public:
bool stopSyncing = false;
bool isSyncing = false;
Thread syncThread;
void (* sync_event_callback)(std::string roomId, json_t* event) = 0;
void processSync(json_t* sync);
json_t* doSync(std::string token);
void startSync();
json_t* doRequest(const char* method, std::string path, json_t* body = NULL);
public:
Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL);
@ -40,9 +42,9 @@ public:
std::string sendEvent(std::string roomId, std::string eventType, json_t* content);
std::string sendStateEvent(std::string roomId, std::string type, std::string stateKey, json_t* content);
std::string redactEvent(std::string roomId, std::string eventId, std::string reason = "");
void setSyncEventCallback(void (*cb)(std::string roomId, json_t* event));
void startSyncLoop();
void stopSyncLoop();
void startSync();
};
}; // namespace Matrix

View file

@ -15,7 +15,7 @@
#define SOC_BUFFERSIZE 0x100000
#define SYNC_TIMEOUT 10000
#define DEBUG 1
#define DEBUG 0
#if DEBUG
#define D
@ -215,6 +215,10 @@ void Client::stopSyncLoop() {
isSyncing = false;
}
void Client::setSyncEventCallback(void (*cb)(std::string roomId, json_t* event)) {
sync_event_callback = cb;
}
void Client::processSync(json_t* sync) {
json_t* rooms = json_object_get(sync, "rooms");
if (!rooms) {
@ -226,10 +230,13 @@ void Client::processSync(json_t* sync) {
const char* roomId;
json_t* room;
size_t index;
json_t* event;
if (leftRooms) {
json_object_foreach(leftRooms, roomId, room) {
// rooms that we left
// emit leave event with roomId
}
}
@ -253,11 +260,12 @@ void Client::processSync(json_t* sync) {
D printf("no events\n");
continue;
}
size_t index;
json_t* event;
json_array_foreach(events, index, event) {
json_t* eventType = json_object_get(event, "type");
D printf("%s\n", json_string_value(eventType));
if (sync_event_callback) {
sync_event_callback(roomId, event);
}
}
}
}