diff options
Diffstat (limited to 'src/timeline.c')
-rw-r--r-- | src/timeline.c | 99 |
1 files changed, 91 insertions, 8 deletions
diff --git a/src/timeline.c b/src/timeline.c index 7b0a567..fbd123e 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -1,10 +1,17 @@ #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" -struct timeline *timeline_from_json(char *json_data) +static char *timeline_url = "/api/v1/timelines/home"; + +struct timeline *timeline_from_json(const char *json_data) { json_t *root; json_error_t error; @@ -13,19 +20,19 @@ struct timeline *timeline_from_json(char *json_data) root = json_loads(json_data, 0, &error); if (!root) { - fprintf(stderr, "timeline_free(): json root it null\n"); + log_msg(LOG_ERROR, "timeline_free", "json root it null"); return NULL; } if (!json_is_array(root)) { - fprintf(stderr, "timeline_free(): timeline not initialized\n"); + log_msg(LOG_ERROR, "timeline_free", "timeline not initialized"); json_decref(root); return NULL; } t = calloc(1, sizeof(struct timeline)); if (!t) { - err(1, "timeline_from_json(): failed to allocate memory"); + err(1, NULL); json_decref(root); return NULL; } @@ -33,7 +40,7 @@ struct timeline *timeline_from_json(char *json_data) t->size = json_array_size(root); t->statuses = calloc(t->size, sizeof(struct status *)); if (!(t->statuses)) { - err(1, "timeline_from_json(): failed to allocate memory"); + err(1, NULL); json_decref(root); return NULL; } @@ -59,7 +66,7 @@ error: void timeline_free(struct timeline *t) { if (!t) { - fprintf(stderr, "timeline_free(): timeline not initialized\n"); + log_msg(LOG_ERROR, "timeline_free", "timeline not initialized"); return; } for (size_t i = 0; i < t->size; i++) { @@ -69,7 +76,83 @@ void timeline_free(struct timeline *t) free(t); } -int lod_timeline(const char *max_id, const char *since_id, const char *min_id, int limit) +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)) { - return 0; + 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); } |