aboutsummaryrefslogtreecommitdiff
path: root/restapi
diff options
context:
space:
mode:
authorJulien Dessaux2019-08-06 15:44:18 +0200
committerJulien Dessaux2019-08-06 17:42:06 +0200
commitfea924350b6ba50dc0cb0e5e133cbbd914137e61 (patch)
tree68e119dec63829356db8614204d0a6b4bb7e5221 /restapi
parentCosmetics (diff)
downloadrocket-cli-client-fea924350b6ba50dc0cb0e5e133cbbd914137e61.tar.gz
rocket-cli-client-fea924350b6ba50dc0cb0e5e133cbbd914137e61.tar.bz2
rocket-cli-client-fea924350b6ba50dc0cb0e5e133cbbd914137e61.zip
Added a proper subscriptions listing for the direct conversations closer
Diffstat (limited to 'restapi')
-rw-r--r--restapi/CMakeLists.txt1
-rw-r--r--restapi/subscriptions.c63
-rw-r--r--restapi/subscriptions.h9
3 files changed, 73 insertions, 0 deletions
diff --git a/restapi/CMakeLists.txt b/restapi/CMakeLists.txt
index 45a22c2..e1cad6a 100644
--- a/restapi/CMakeLists.txt
+++ b/restapi/CMakeLists.txt
@@ -3,3 +3,4 @@ file(GLOB_RECURSE SOURCES *.c)
ADD_LIBRARY(restapi STATIC ${SOURCES})
add_dependencies(restapi cjson-build)
target_link_libraries(restapi common)
+target_link_libraries(restapi ${CMAKE_CURRENT_BINARY_DIR}/../cjson/src/cjson-build/libcjson.a)
diff --git a/restapi/subscriptions.c b/restapi/subscriptions.c
new file mode 100644
index 0000000..b52ea9a
--- /dev/null
+++ b/restapi/subscriptions.c
@@ -0,0 +1,63 @@
+#include <cjson/cJSON.h>
+#include <curl/curl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "common/http.h"
+#include "subscriptions.h"
+
+struct subscription* // returns NULL if error or a uthash of subscriptions that needs to be freed by the caller
+restapi_subscriptions_get(void)
+{
+ struct subscription* subscriptions = NULL;
+
+ const char* buffer = http_get("/api/v1/subscriptions.get");
+
+ if (buffer == NULL) {
+ fprintf(stderr, "Error while subscriptions_get, http get didn't return any data.\n");
+ return NULL;
+ }
+
+ cJSON* json = cJSON_Parse(buffer);
+ if (json == NULL) {
+ const char *error_ptr = cJSON_GetErrorPtr();
+ if (error_ptr != NULL)
+ fprintf(stderr, "Json parsing error before: %s\n", error_ptr);
+ fprintf(stderr, "Error while subscriptions_get, couldn't parse json output :\n%s\n", buffer);
+ goto get_json_cleanup;
+ }
+
+ const cJSON* success = cJSON_GetObjectItemCaseSensitive(json, "success");
+ if (cJSON_IsTrue(success)) {
+ cJSON* updates = cJSON_GetObjectItemCaseSensitive(json, "update");
+ if (!cJSON_IsArray(updates)) {
+ fprintf(stderr, "Error while subscriptions_get, couldn't parse json output :\n%s\n", buffer);
+ goto get_json_cleanup;
+ }
+ const cJSON* update = NULL;
+ cJSON_ArrayForEach(update, updates) {
+ const cJSON* id = cJSON_GetObjectItemCaseSensitive(update, "_id");
+ const cJSON* name = cJSON_GetObjectItemCaseSensitive(update, "name");
+ const cJSON* type = cJSON_GetObjectItemCaseSensitive(update, "t");
+ const cJSON* open = cJSON_GetObjectItemCaseSensitive(update, "open");
+ enum subscription_type etype;
+ if (!cJSON_IsString(id) || id->valuestring == NULL || !cJSON_IsString(name) || name->valuestring == NULL || !cJSON_IsString(type) || type->valuestring == NULL || !cJSON_IsTrue(open))
+ continue;
+ if (strcmp(type->valuestring, "c") == 0)
+ etype = SUBSCRIPTION_CHANNEL;
+ else if (strcmp(type->valuestring, "d") == 0)
+ etype = SUBSCRIPTION_DIRECT;
+ else if (strcmp(type->valuestring, "p") == 0)
+ etype = SUBSCRIPTION_PRIVATE;
+ else {
+ fprintf(stderr, "Bug found : Unknown subscription type %s\n%s\n", type->valuestring, buffer);
+ exit(999);
+ }
+ common_subscription_add(&subscriptions, id->valuestring, name->valuestring, etype);
+ }
+ }
+get_json_cleanup:
+ cJSON_Delete(json);
+ return subscriptions;
+}
diff --git a/restapi/subscriptions.h b/restapi/subscriptions.h
new file mode 100644
index 0000000..0347e15
--- /dev/null
+++ b/restapi/subscriptions.h
@@ -0,0 +1,9 @@
+#ifndef RESTAPI_SUBSCRIPTIONS_H_
+#define RESTAPI_SUBSCRIPTIONS_H_
+
+#include "common/subscriptions.h"
+
+struct subscription* // returns NULL if error or a uthash of subscriptions that needs to be freed by the caller
+restapi_subscriptions_get(void);
+
+#endif