diff options
Diffstat (limited to 'libgs/src/timeline.c')
-rw-r--r-- | libgs/src/timeline.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/libgs/src/timeline.c b/libgs/src/timeline.c new file mode 100644 index 0000000..fa0a2e8 --- /dev/null +++ b/libgs/src/timeline.c @@ -0,0 +1,93 @@ +#define _POSIX_C_SOURCE 200809L +#include <err.h> +#include <string.h> +#include <pthread.h> +#include <jansson.h> +#include "timeline.h" +#include "auth.h" +#include "config.h" +#include "http.h" +#include "string-util.h" +#include "log.h" + +struct gs_timeline *gs_timeline_from_json(const char *json_data) +{ + struct gs_timeline *t; + json_t *root; + json_error_t error; + + root = json_loads(json_data, 0, &error); + if (!root) { + gs_log(GS_WARNING, "timeline_from_json", "json root it null"); + return NULL; + } + + if (!json_is_array(root)) { + gs_log(GS_WARNING, "timeline_from_json", "json root is not array"); + json_decref(root); + return NULL; + } + + t = calloc(1, sizeof(struct gs_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); + gs_timeline_free(t); + json_decref(root); + return NULL; + } + + json_t *data; + for (size_t i = 0; i < t->size; i++) { + data = json_array_get(root, i); + if (!data) + goto error; + t->statuses[i] = gs_status_from_json_t(data); + if (!(t->statuses[i])) + goto error; + } + json_decref(root); + return t; + +error: + gs_timeline_free(t); + json_decref(root); + return NULL; +} + +void gs_timeline_free(struct gs_timeline *t) +{ + for (size_t i = 0; i < t->size; i++) { + if (t->statuses[i]) + gs_status_free(t->statuses[i]); + } + free(t->statuses); + free(t); +} + +struct gs_timeline *gs_timeline_get(GSClient *c, const char *max_id, + const char *since_id, const char *min_id, int limit) +{ + char *resp; + struct gs_timeline *t; + + resp = (char *)gs_client_do_api(c, 1, "/api/v1/timelines/home", NULL); + if (!resp) { + return NULL; + } + + t = gs_timeline_from_json(resp); + free(resp); + if (!t) { + return NULL; + } + + return t; +} |