From 12b4ffc4f8c62d59bf44fe0b34431b2e409012c8 Mon Sep 17 00:00:00 2001 From: nekobit Date: Thu, 12 May 2022 04:38:03 +0000 Subject: [PATCH] Rewrite nearly everything for json only FossilOrigin-Name: 39a6258b4cf1c188667c54cecacd3d990c04bed0d387431fe25520a07223a6a8 --- include/mastodont_account.h | 2 +- include/mastodont_list.h | 9 +++---- include/mastodont_notification.h | 6 ++--- include/mastodont_scrobbles.h | 12 ++++----- include/mastodont_search.h | 4 +++ include/mastodont_status.h | 4 +-- src/account.c | 12 +++++++++ src/list.c | 39 +++++------------------------ src/notification.c | 42 +++++--------------------------- src/request.c | 11 +++++++-- src/scrobbles.c | 41 ++++--------------------------- src/search.c | 17 ++++--------- src/status.c | 26 +++++++++++++------- src/timeline.c | 10 ++++---- 14 files changed, 82 insertions(+), 153 deletions(-) diff --git a/include/mastodont_account.h b/include/mastodont_account.h index 5a44981..0ca3888 100644 --- a/include/mastodont_account.h +++ b/include/mastodont_account.h @@ -92,7 +92,7 @@ int mastodont_get_account(mastodont_t* data, struct mstdnt_account* acct, struct mstdnt_storage* storage); -int mstdnt_account_json(struct mstdnt_account* status, cJSON* js); +int mstdnt_account_json(struct mstdnt_account* acct, cJSON* js); int mstdnt_account_json_callback(cJSON* json, void* _args); int mstdnt_accounts_json_callback(cJSON* json, void* _args); void _mstdnt_val_account_call(cJSON* v, void* _type); diff --git a/include/mastodont_list.h b/include/mastodont_list.h index 5e9e2bf..2dfd969 100644 --- a/include/mastodont_list.h +++ b/include/mastodont_list.h @@ -26,12 +26,11 @@ struct mstdnt_list char* replies_policy; }; -int mstdnt_list_from_json(struct mstdnt_list* list, cJSON* js); +int mstdnt_list_json(struct mstdnt_list* list, cJSON* js); -int mstdnt_lists_from_result(struct mstdnt_storage* storage, - struct mstdnt_fetch_results* results, - struct mstdnt_list* lists[], - size_t* size); +int mstdnt_lists_json(struct mstdnt_list* lists[], + size_t* size, + cJSON* json); int mastodont_get_lists(mastodont_t* api, struct mstdnt_list* lists[], diff --git a/include/mastodont_notification.h b/include/mastodont_notification.h index e6cce92..6ec2f08 100644 --- a/include/mastodont_notification.h +++ b/include/mastodont_notification.h @@ -55,9 +55,7 @@ struct _mstdnt_notifications_result_cb_args size_t* size; }; -int _mstdnt_notifications_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args); +int mstdnt_notifications_json_callback(cJSON* json, void* _args); int mastodont_get_notifications(mastodont_t* data, struct mstdnt_get_notifications_args* args, @@ -65,7 +63,7 @@ int mastodont_get_notifications(mastodont_t* data, struct mstdnt_notification** notifs, size_t* size); -int mstdnt_notification_from_json(struct mstdnt_notification* notif, cJSON* js); +int mstdnt_notification_json(struct mstdnt_notification* notif, cJSON* js); void mstdnt_cleanup_notifications(struct mstdnt_notification* notif, size_t notif_len); void mstdnt_cleanup_notification(struct mstdnt_notification* notif); diff --git a/include/mastodont_scrobbles.h b/include/mastodont_scrobbles.h index 8817e0e..0230960 100644 --- a/include/mastodont_scrobbles.h +++ b/include/mastodont_scrobbles.h @@ -53,14 +53,12 @@ int mastodont_get_scrobbles(mastodont_t* data, struct mstdnt_scrobble* scrobbles[], size_t* size); -int mstdnt_scrobbles_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_scrobble* scrobbles[], - size_t* size); +int mstdnt_scrobbles_json(struct mstdnt_scrobble* scrobbles[], + size_t* size, + cJSON* json); -int _mstdnt_scrobbles_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* arg); +int mstdnt_scrobbles_json_callback(cJSON* json, + void* arg); int mstdnt_scrobble_json(struct mstdnt_scrobble* scrobble, cJSON* js); diff --git a/include/mastodont_search.h b/include/mastodont_search.h index 4592a75..6ae3b4e 100644 --- a/include/mastodont_search.h +++ b/include/mastodont_search.h @@ -50,6 +50,10 @@ struct mstdnt_search_results size_t tags_len; }; +int mstdnt_search_json_callback(cJSON* json, void* _args); + +int mstdnt_search_json(struct mstdnt_search_results* search_results, cJSON* json); + int mastodont_search(mastodont_t* data, char* query, struct mstdnt_storage* storage, diff --git a/include/mastodont_status.h b/include/mastodont_status.h index 0e10d7b..67c569b 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -106,9 +106,7 @@ int mstdnt_statuses_json(struct mstdnt_status* statuses[], void _mstdnt_val_status_call(cJSON* v, void* _type); void _mstdnt_val_malloc_status_call(cJSON* v, void* _type); -int mstdnt_status_context_json(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_status* statuses_before[], +int mstdnt_status_context_json(struct mstdnt_status* statuses_before[], struct mstdnt_status* statuses_after[], size_t* size_before, size_t* size_after, diff --git a/src/account.c b/src/account.c index 8914232..c038267 100644 --- a/src/account.c +++ b/src/account.c @@ -41,6 +41,18 @@ void _mstdnt_val_malloc_account_call(cJSON* v, void* _type) // GENERATE mstdnt_statuses_json GENERATE_JSON_ARRAY_FUNC(mstdnt_accounts_json, struct mstdnt_account, mstdnt_account_json) +int mstdnt_account_json_callback(cJSON* json, void* _args) +{ + // Not sure why it expects it to be in the child + return mstdnt_account_json(_args, json->child); +} + +int mstdnt_accounts_json_callback(cJSON* json, void* _args) +{ + struct mstdnt_account_args* args = _args; + return mstdnt_accounts_json(args->acct, args->size, json); +} + int mastodont_get_account(mastodont_t* data, int lookup, /* TODO move into separate function for consistancy */ char* id, diff --git a/src/list.c b/src/list.c index fab8bb7..f8aa771 100644 --- a/src/list.c +++ b/src/list.c @@ -19,13 +19,14 @@ #include #include #include +#include struct mstdnt_get_lists_args { struct mstdnt_list** lists; size_t* size; }; -int mstdnt_list_from_json(struct mstdnt_list* list, cJSON* js) +int mstdnt_list_json(struct mstdnt_list* list, cJSON* js) { cJSON* v; @@ -45,41 +46,13 @@ int mstdnt_list_from_json(struct mstdnt_list* list, cJSON* js) return 0; } -int mstdnt_lists_from_result(struct mstdnt_storage* storage, - struct mstdnt_fetch_results* results, - struct mstdnt_list* lists[], - size_t* size) -{ - size_t i = 0; - cJSON* root, *list_j_list; - if (_mstdnt_json_init(&root, results, storage)) - return 1; - if (!cJSON_IsArray(root)) - return 1; - - if (size) *size = cJSON_GetArraySize(root); - - /* malloc array - cJSON does a loop to count, let's do it once preferably */ - *lists = malloc((size ? *size : cJSON_GetArraySize(root)) - * sizeof(struct mstdnt_list)); - if (*lists == NULL) - return 1; +GENERATE_JSON_ARRAY_FUNC(mstdnt_lists_json, struct mstdnt_list, mstdnt_list_json) - cJSON_ArrayForEach(list_j_list, root) - { - mstdnt_list_from_json((*lists) + i++, list_j_list->child); - } - storage->needs_cleanup = 1; - return 0; -} - -static int mstdnt_lists_from_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args) +static int mstdnt_lists_json_callback(cJSON* json, void* _args) { struct mstdnt_get_lists_args* args = _args; - return mstdnt_lists_from_result(storage, results, args->lists, args->size); + return mstdnt_lists_json(args->lists, args->size, json); } int mastodont_get_lists(mastodont_t* data, @@ -99,7 +72,7 @@ int mastodont_get_lists(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &args, - mstdnt_lists_from_result_callback + mstdnt_lists_json_callback }; return mastodont_request(data, &req_args); diff --git a/src/notification.c b/src/notification.c index e9e2c86..23dd896 100644 --- a/src/notification.c +++ b/src/notification.c @@ -20,6 +20,7 @@ #include #include #include +#include static void _mstdnt_val_notif_type_call(cJSON* v, void* _type) { @@ -44,7 +45,7 @@ static void _mstdnt_val_notif_type_call(cJSON* v, void* _type) else if (strcmp(v->valuestring, "pleroma:report") == 0) *type = MSTDNT_NOTIFICATION_REPORT; } -int mstdnt_notification_from_json(struct mstdnt_notification* notif, cJSON* js) +int mstdnt_notification_json(struct mstdnt_notification* notif, cJSON* js) { cJSON* v; @@ -66,43 +67,12 @@ int mstdnt_notification_from_json(struct mstdnt_notification* notif, cJSON* js) return 0; } -int mstdnt_notifications_from_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_notification* notif[], - size_t* size) -{ - size_t i = 0; - cJSON* root, *notif_j_list; - if (_mstdnt_json_init(&root, results, storage)) - return 1; +GENERATE_JSON_ARRAY_FUNC(mstdnt_notifications_json, struct mstdnt_notification, mstdnt_notification_json) - if (size) *size = cJSON_GetArraySize(root); - - /* malloc array - cJSON does a loop to count, let's do it once preferably */ - *notif = calloc(1, (size ? *size : cJSON_GetArraySize(root)) - * sizeof(struct mstdnt_notification)); - if (*notif == NULL) - return 1; - - cJSON_ArrayForEach(notif_j_list, root) - { - /* Null out values incase no match */ - (*notif)[i].account = NULL; - (*notif)[i].status = NULL; - /* notif[i]->pleroma = NULL; */ - - mstdnt_notification_from_json((*notif) + i++, notif_j_list->child); - } - - return 0; -} - -int _mstdnt_notifications_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args) +int mstdnt_notifications_json_callback(cJSON* json, void* _args) { struct _mstdnt_notifications_result_cb_args* args = _args; - return mstdnt_notifications_from_result(results, storage, args->notif, args->size); + return mstdnt_notifications_json(args->notif, args->size, json); } @@ -143,7 +113,7 @@ int mastodont_get_notifications(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &cb_args, - _mstdnt_notifications_result_callback, + mstdnt_notifications_json_callback, }; return mastodont_request(data, &req_args); diff --git a/src/request.c b/src/request.c index 233408d..e14c916 100644 --- a/src/request.c +++ b/src/request.c @@ -118,13 +118,20 @@ int mastodont_request(mastodont_t* data, struct mastodont_request_args* args) } // Create json structure - if (_mstdnt_json_init(&root, &results, storage) && - mstdnt_check_error(storage)) + if (_mstdnt_json_init(&root, &results, storage)) + { + res = 1; + goto cleanup_res; + } + + // Make sure there is no error + if (!mstdnt_check_error(storage)) { /* Optional */ if (args->callback) res = args->callback(storage->root, args->args); } +cleanup_res: mastodont_fetch_results_cleanup(&results); cleanup: if (args->params_post && args->request_type == CURLOPT_POST) free(post); diff --git a/src/scrobbles.c b/src/scrobbles.c index ffc9937..e6f96e6 100644 --- a/src/scrobbles.c +++ b/src/scrobbles.c @@ -19,6 +19,7 @@ #include #include #include +#include int mstdnt_scrobble_json(struct mstdnt_scrobble* scrobble, cJSON* js) { @@ -43,44 +44,12 @@ int mstdnt_scrobble_json(struct mstdnt_scrobble* scrobble, cJSON* js) return 0; } -int mstdnt_scrobbles_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_scrobble* scrobbles[], - size_t* size) -{ - size_t i = 0; - cJSON* root, *scrobble_j_list; - if (_mstdnt_json_init(&root, results, storage) && - !cJSON_IsArray(root)) - return 1; - - if (size) *size = cJSON_GetArraySize(root); - - /* Scrobbles can be an empty array! */ - if (!(size ? *size : cJSON_GetArraySize(root))) - return 0; /* Not an error, but we are done parsing */ - - /* malloc array - cJSON does a loop to count, let's do it once preferably */ - *scrobbles = calloc(1, (size ? *size : cJSON_GetArraySize(root)) - * sizeof(struct mstdnt_scrobble)); - if (*scrobbles == NULL) - return 1; +GENERATE_JSON_ARRAY_FUNC(mstdnt_scrobbles_json, struct mstdnt_scrobble, mstdnt_scrobble_json) - cJSON_ArrayForEach(scrobble_j_list, root) - { - mstdnt_scrobble_json((*scrobbles) + i++, scrobble_j_list->child); - } - - return 0; -} - - -int _mstdnt_scrobbles_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args) +int mstdnt_scrobbles_json_callback(cJSON* json, void* _args) { struct _mstdnt_scrobbles_cb_args* args = _args; - return mstdnt_scrobbles_result(results, storage, args->scrobbles, args->size); + return mstdnt_scrobbles_json(args->scrobbles, args->size, json); } int mastodont_get_scrobbles(mastodont_t* data, @@ -109,7 +78,7 @@ int mastodont_get_scrobbles(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &cb_args, - _mstdnt_scrobbles_result_callback + mstdnt_scrobbles_json_callback }; return mastodont_request(data, &req_args); diff --git a/src/search.c b/src/search.c index 4594818..de07a9f 100644 --- a/src/search.c +++ b/src/search.c @@ -21,6 +21,7 @@ #include #include #include + static const char* type_to_string(enum mstdnt_search_type type) { switch (type) @@ -32,14 +33,8 @@ static const char* type_to_string(enum mstdnt_search_type type) } } -int mstdnt_search_from_result(struct mstdnt_storage* storage, - struct mstdnt_fetch_results* results, - struct mstdnt_search_results* search_results) +int mstdnt_search_json(struct mstdnt_search_results* search_results, cJSON* root) { - cJSON* root; - if (_mstdnt_json_init(&root, results, storage)) - return 1; - // Not many items here, just use cJSON_GetObjectItemCaseSensitive() instead cJSON* statuses = cJSON_GetObjectItemCaseSensitive(root, "statuses"); cJSON* accounts = cJSON_GetObjectItemCaseSensitive(root, "accounts"); @@ -61,11 +56,9 @@ int mstdnt_search_from_result(struct mstdnt_storage* storage, return 0; } -int _mstdnt_search_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args) +int mstdnt_search_json_callback(cJSON* json, void* _args) { - return mstdnt_search_from_result(storage, results, _args); + return mstdnt_search_json(_args, json); } int mastodont_search(mastodont_t* data, @@ -95,7 +88,7 @@ int mastodont_search(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, results, - _mstdnt_search_result_callback + mstdnt_search_json_callback }; return mastodont_request(data, &req_args); diff --git a/src/status.c b/src/status.c index a42e84e..4abaa3e 100644 --- a/src/status.c +++ b/src/status.c @@ -265,13 +265,11 @@ int mastodont_get_status(mastodont_t* data, return mastodont_request(data, &req_args); } -int mstdnt_status_context_json(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_status* statuses_before[], - struct mstdnt_status* statuses_after[], - size_t* size_before, - size_t* size_after, - cJSON* root) +int mstdnt_status_context_json(struct mstdnt_status* statuses_before[], + struct mstdnt_status* statuses_after[], + size_t* size_before, + size_t* size_after, + cJSON* root) { cJSON* v, *status_item; size_t* size_ptr = NULL; @@ -316,6 +314,16 @@ int mstdnt_status_context_json(struct mstdnt_fetch_results* results, return 0; } +int mstdnt_status_context_json_callback(cJSON* json, void* _args) +{ + struct _mstdnt_status_context_result_cb_args* args = _args; + return mstdnt_status_context_json(args->statuses_before, + args->statuses_after, + args->size_before, + args->size_after, + json); +} + int mastodont_get_status_context(mastodont_t* data, char* id, struct mstdnt_storage* storage, @@ -366,7 +374,7 @@ int mastodont_status_favourited_by(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &args, - mstdnt_accounts_callback, + mstdnt_accounts_json_callback, }; return mastodont_request(data, &req_args); @@ -392,7 +400,7 @@ int mastodont_status_reblogged_by(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &args, - mstdnt_accounts_callback, + mstdnt_accounts_json_callback, }; return mastodont_request(data, &req_args); diff --git a/src/timeline.c b/src/timeline.c index 1d778ac..dc654f2 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -45,7 +45,7 @@ int mastodont_timeline_list(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &cb_args, - _mstdnt_statuses_result_callback, + mstdnt_statuses_json_callback, }; return mastodont_request(data, &req_args); @@ -81,7 +81,7 @@ int mastodont_timeline_tag(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &cb_args, - _mstdnt_statuses_result_callback, + mstdnt_statuses_json_callback, }; return mastodont_request(data, &req_args); @@ -111,7 +111,7 @@ int mastodont_timeline_public(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &cb_args, - _mstdnt_statuses_result_callback, + mstdnt_statuses_json_callback, }; return mastodont_request(data, &req_args); @@ -143,7 +143,7 @@ int mastodont_timeline_direct(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &cb_args, - _mstdnt_statuses_result_callback, + mstdnt_statuses_json_callback, }; return mastodont_request(data, &req_args); @@ -173,7 +173,7 @@ int mastodont_timeline_home(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &cb_args, - _mstdnt_statuses_result_callback, + mstdnt_statuses_json_callback, }; return mastodont_request(data, &req_args);