aboutsummaryrefslogtreecommitdiff
path: root/restapi/subscriptions.c
blob: 5a1abe6ae919e37b71f014b7750d05abc48fe1f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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* rid = cJSON_GetObjectItemCaseSensitive(update, "rid");
            const cJSON* name = cJSON_GetObjectItemCaseSensitive(update, "name");
            const cJSON* type = cJSON_GetObjectItemCaseSensitive(update, "t");
            const cJSON* open = cJSON_GetObjectItemCaseSensitive(update, "open");
            const cJSON* unread = cJSON_GetObjectItemCaseSensitive(update, "unread");
            enum subscription_type etype;
            if (!cJSON_IsString(rid) || rid->valuestring == NULL || !cJSON_IsString(name) || name->valuestring == NULL || !cJSON_IsString(type) || type->valuestring == NULL || !cJSON_IsTrue(open) || !cJSON_IsNumber(unread))
                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, rid->valuestring, name->valuestring, etype, unread->valueint);
        }
    }
get_json_cleanup:
    cJSON_Delete(json);
    return subscriptions;
}