diff options
author | nirav <nirav@teisuu.com> | 2019-03-17 10:17:15 +0530 |
---|---|---|
committer | Dandelion <nirav@teisuu.com> | 2019-03-17 10:17:15 +0530 |
commit | c608bcc3dfab2abe7b66c10f8556086b2d45b3a3 (patch) | |
tree | fe37f7b17ffdafdea96d13782b02c3855154a978 /src/timeline.c | |
parent | 72cedecbbfa6b17b444edf5943dce2ecc307a340 (diff) | |
download | ap_client-c608bcc3dfab2abe7b66c10f8556086b2d45b3a3.tar.gz ap_client-c608bcc3dfab2abe7b66c10f8556086b2d45b3a3.zip |
Fix load_timeline
Diffstat (limited to 'src/timeline.c')
-rw-r--r-- | src/timeline.c | 96 |
1 files changed, 22 insertions, 74 deletions
diff --git a/src/timeline.c b/src/timeline.c index fbd123e..753f9f1 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -13,19 +13,18 @@ static char *timeline_url = "/api/v1/timelines/home"; struct timeline *timeline_from_json(const char *json_data) { + struct timeline *t; 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"); + log_msg(LOG_WARNING, "timeline_from_json", "json root it null"); return NULL; } if (!json_is_array(root)) { - log_msg(LOG_ERROR, "timeline_free", "timeline not initialized"); + log_msg(LOG_WARNING, "timeline_from_json", "json root is not array"); json_decref(root); return NULL; } @@ -41,12 +40,13 @@ struct timeline *timeline_from_json(const char *json_data) t->statuses = calloc(t->size, sizeof(struct status *)); if (!(t->statuses)) { err(1, NULL); + timeline_free(t); json_decref(root); return NULL; } + json_t *data; for (size_t i = 0; i < t->size; i++) { - json_t *data; data = json_array_get(root, i); if (!data) goto error; @@ -65,94 +65,42 @@ error: 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->statuses); 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)) +struct timeline *get_timeline(const char *max_id, const char *since_id, const char *min_id, + int limit) { + char *url, *resp; size_t size; - char *url; - struct load_timeline_call_arg *arg; - pthread_t t; + struct timeline *t; size = strlen(get_instance_url()) + strlen(timeline_url) + 1; url = malloc(size); if (!url) { - err(1, NULL); - return -1; + err(1, "get_timeline"); + return NULL; } 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; + resp = get_request(url); + free(url); + if (!resp) { + return NULL; } - arg->url = url; - arg->callback = callback; + t = timeline_from_json(resp); + free(resp); + if (!t) { + return NULL; + } - return pthread_create(&t, NULL, &load_timeline_call, arg); + return t; } |