summaryrefslogtreecommitdiff
path: root/src/timeline.c
blob: fbd123e0c722cd66adac80245eea0689c4ba7d38 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#define _POSIX_C_SOURCE 200809L
#include <err.h>
#include <string.h>
#include <pthread.h>
#include <jansson.h>
#include "timeline.h"
#include "auth.h"
#include "http.h"
#include "string-util.h"
#include "log.h"

static char *timeline_url = "/api/v1/timelines/home";

struct timeline *timeline_from_json(const char *json_data)
{
    json_t *root;
    json_error_t error;
    struct timeline *t;

    root = json_loads(json_data, 0, &error);

    if (!root) {
        log_msg(LOG_ERROR, "timeline_free", "json root it null");
        return NULL;
    }

    if (!json_is_array(root)) {
        log_msg(LOG_ERROR, "timeline_free", "timeline not initialized");
        json_decref(root);
        return NULL;
    }

    t = calloc(1, sizeof(struct timeline));
    if (!t) {
        err(1, NULL);
        json_decref(root);
        return NULL;
    }

    t->size = json_array_size(root);
    t->statuses = calloc(t->size, sizeof(struct status *));
    if (!(t->statuses)) {
        err(1, NULL);
        json_decref(root);
        return NULL;
    }

    for (size_t i = 0; i < t->size; i++) {
        json_t *data;
        data = json_array_get(root, i);
        if (!data)
            goto error;
        t->statuses[i] = status_from_json_t(data);
        if (!(t->statuses[i]))
            goto error;
    }
    json_decref(root);
    return t;

error:
    timeline_free(t);
    json_decref(root);
    return NULL;
}

void timeline_free(struct timeline *t)
{
    if (!t) {
        log_msg(LOG_ERROR, "timeline_free", "timeline not initialized");
        return;
    }
    for (size_t i = 0; i < t->size; i++) {
        if (t->statuses[i])
            status_free(t->statuses[i]);
    }
    free(t);
}

struct load_timeline_call_arg {
    char *url;
    void (*callback)(bool, struct timeline *t);
};

static void *load_timeline_call(void *req_arg)
{
    struct load_timeline_call_arg *arg;
    char *resp;
    struct timeline *t;

    arg = (struct load_timeline_call_arg *)req_arg;
    if (!(arg->url)) {
        log_msg(LOG_ERROR, "load_timeline_call", "invalid arguments");
        goto error;
    }

    resp = get_request(arg->url);
    if (!resp) {
        log_msg(LOG_ERROR, "load_timeline_call", "failed to send http request");
        goto error;
    }

    if (!resp) {
        log_msg(LOG_ERROR, "load_timeline_call", "null response");
        goto error;
    }

    t = timeline_from_json(resp);
    if (!t) {
        log_msg(LOG_ERROR, "load_timeline_call", "null response");
        goto error;
    }

    free(arg->url);
    free(arg);
    free(resp);

    (*(arg->callback))(true, t);
    return NULL;

error:
    if (arg->url)
        free(arg->url);
    free(arg);
    if (resp)
        free(resp);
    (*(arg->callback))(false, NULL);
    return NULL;
}

int get_timeline(const char *max_id, const char *since_id, const char *min_id,
        int limit, void (*callback)(bool success, struct timeline *t))
{
    size_t size;
    char *url;
    struct load_timeline_call_arg *arg;
    pthread_t t;

    size = strlen(get_instance_url()) + strlen(timeline_url) + 1;
    url = malloc(size);
    if (!url) {
        err(1, NULL);
        return -1;
    }

    strlcpy(url, get_instance_url(), size);
    strlcat(url, timeline_url, size);

    arg = calloc(1, sizeof(struct load_timeline_call_arg));
    if (!arg) {
        err(1, NULL);
        return -1;
    }

    arg->url = url;
    arg->callback = callback;

    return pthread_create(&t, NULL, &load_timeline_call, arg);
}