diff --git a/include/mastodont_attachment.h b/include/mastodont_attachment.h index 0d1d0a4..1713380 100644 --- a/include/mastodont_attachment.h +++ b/include/mastodont_attachment.h @@ -15,9 +15,15 @@ #ifndef MASTODONT_ATTACHMENT #define MASTODONT_ATTACHMENT - +#include "mastodont_types.h" /* Status: Complete, not implemented */ +struct _mstdnt_attachment_args +{ + struct mstdnt_attachment** attachments; + size_t* size; +}; + enum mstdnt_attachment_type { MSTDNT_ATTACHMENT_UNKNOWN, @@ -39,4 +45,6 @@ struct mstdnt_attachment char* blurhash; }; +void _mstdnt_val_attachments_call(cJSON* v, void* _type); + #endif /* MASTODONT_ATTACHMENT */ diff --git a/include/mastodont_json_helper.h b/include/mastodont_json_helper.h index b2123bb..c90a766 100644 --- a/include/mastodont_json_helper.h +++ b/include/mastodont_json_helper.h @@ -22,7 +22,7 @@ struct _mstdnt_val_ref { const char* key; void* val; - void (*handle)(cJSON*, void*); + void (*handle)(cJSON*, void*;) }; int _mstdnt_json_init(cJSON** root, diff --git a/include/mastodont_status.h b/include/mastodont_status.h index b3bbc63..119e775 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -49,6 +49,7 @@ struct mstdnt_status mstdnt_bool sensitive; char* spoiler_text; struct mstdnt_attachment* media_attachments; + size_t media_attachments_len; struct mstdnt_application application; /* Rendering attributes */ diff --git a/src/attachment.c b/src/attachment.c new file mode 100644 index 0000000..2cebc12 --- /dev/null +++ b/src/attachment.c @@ -0,0 +1,66 @@ +/* + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +static void load_attachment_from_json(struct mstdnt_attachment* att, cJSON* att_json) +{ + cJSON* it; + struct _mstdnt_val_ref refs[] = { + { "id", &(att->id), _mstdnt_val_string_call }, + /* TODO type */ + { "url", &(att->url), _mstdnt_val_string_call }, + { "preview_url", &(att->preview_url), _mstdnt_val_string_call }, + { "remote_url", &(att->remote_url), _mstdnt_val_string_call }, + { "hash", &(att->hash), _mstdnt_val_string_call }, + { "description", &(att->description), _mstdnt_val_string_call }, + { "blurhash", &(att->blurhash), _mstdnt_val_string_call }, + }; + + for (it = att_json; it; it = it->next) + { + _mstdnt_key_val_ref(it, refs, _mstdnt_arr_len(refs)); + } +} + +void _mstdnt_val_attachments_call(cJSON* v, void* _type) +{ + struct _mstdnt_attachment_args* args = _type; + struct mstdnt_attachment** attachments = args->attachments; + cJSON* v_array = v->child; + cJSON* att = NULL; + + size_t size = cJSON_GetArraySize(v_array); + args->size = size; + /* No attachments, ignore */ + if (size == 0) + { + *attachments = NULL; + return; + } + + *attachments = malloc(sizeof(struct mstdnt_attachment) * size); + if (*attachments == NULL) + return; + + cJSON* it; + int i; + for (it = v_array, i = 0; it; (++i, it = it->next)) + { + load_attachment_from_json((*attachments) + i, it->child); + } +} diff --git a/src/status.c b/src/status.c index 2b08dd2..9e75c11 100644 --- a/src/status.c +++ b/src/status.c @@ -23,26 +23,33 @@ int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js) { cJSON* v; + + struct _mstdnt_attachment_args att_args = { + &(status->media_attachments), + &(status->media_attachments_len), + }; + struct _mstdnt_val_ref vals[] = { - { "id", &(status->id), _mstdnt_val_string_call, NULL }, - { "uri", &(status->uri), _mstdnt_val_string_call, NULL }, - { "created_at", &(status->created_at), _mstdnt_val_string_call, NULL }, - { "content", &(status->content), _mstdnt_val_string_call, NULL }, - { "spoiler_text", &(status->spoiler_text), _mstdnt_val_string_call, NULL }, - { "in_reply_to_id", &(status->in_reply_to_id), _mstdnt_val_string_call, NULL }, - { "language", &(status->language), _mstdnt_val_string_call, NULL }, - { "url", &(status->url), _mstdnt_val_string_call, NULL }, - { "text", &(status->text), _mstdnt_val_string_call, NULL }, - { "in_reply_to_account_id", &(status->in_reply_to_account_id), _mstdnt_val_string_call, NULL }, - { "sensitive", &(status->sensitive), _mstdnt_val_bool_call, NULL }, - { "favourited", &(status->favourited), _mstdnt_val_bool_call, NULL }, - { "reblogged", &(status->reblogged), _mstdnt_val_bool_call, NULL }, - { "muted", &(status->muted), _mstdnt_val_bool_call, NULL }, - { "bookmarked", &(status->bookmarked), _mstdnt_val_bool_call, NULL }, - { "pinned", &(status->pinned), _mstdnt_val_bool_call, NULL }, - { "reblogs_count", &(status->reblogs_count), _mstdnt_val_uint_call, NULL }, - { "favourites_count", &(status->favourites_count), _mstdnt_val_uint_call, NULL }, - { "replies_count", &(status->replies_count), _mstdnt_val_uint_call, NULL }, + { "id", &(status->id), _mstdnt_val_string_call }, + { "uri", &(status->uri), _mstdnt_val_string_call }, + { "created_at", &(status->created_at), _mstdnt_val_string_call }, + { "content", &(status->content), _mstdnt_val_string_call }, + { "spoiler_text", &(status->spoiler_text), _mstdnt_val_string_call }, + { "in_reply_to_id", &(status->in_reply_to_id), _mstdnt_val_string_call }, + { "language", &(status->language), _mstdnt_val_string_call }, + { "url", &(status->url), _mstdnt_val_string_call }, + { "text", &(status->text), _mstdnt_val_string_call }, + { "in_reply_to_account_id", &(status->in_reply_to_account_id), _mstdnt_val_string_call }, + { "sensitive", &(status->sensitive), _mstdnt_val_bool_call }, + { "favourited", &(status->favourited), _mstdnt_val_bool_call }, + { "reblogged", &(status->reblogged), _mstdnt_val_bool_call }, + { "muted", &(status->muted), _mstdnt_val_bool_call }, + { "bookmarked", &(status->bookmarked), _mstdnt_val_bool_call }, + { "pinned", &(status->pinned), _mstdnt_val_bool_call }, + { "reblogs_count", &(status->reblogs_count), _mstdnt_val_uint_call }, + { "favourites_count", &(status->favourites_count), _mstdnt_val_uint_call }, + { "replies_count", &(status->replies_count), _mstdnt_val_uint_call }, + { "media_attachments", &att_args, _mstdnt_val_attachments_call }, }; for (v = js; v; v = v->next)