From 7d3751771bd1a1d118940a8545acf377b21e4b71 Mon Sep 17 00:00:00 2001 From: nekobit Date: Thu, 12 May 2022 03:20:47 +0000 Subject: [PATCH] Account and relationship results rewrite FossilOrigin-Name: e14a7e5080cc140a7eaa6b7be682d93ec910a649389bfb1656d8d3260cfa0762 --- include/mastodont_account.h | 19 ++-------- include/mastodont_relationship.h | 20 ++++------- src/account.c | 61 ++++---------------------------- src/relationship.c | 57 +++++------------------------ 4 files changed, 24 insertions(+), 133 deletions(-) diff --git a/include/mastodont_account.h b/include/mastodont_account.h index c89923c..5a44981 100644 --- a/include/mastodont_account.h +++ b/include/mastodont_account.h @@ -86,28 +86,15 @@ MSTDNT_ACCOUNT_ACTION_DECL(unblock); MSTDNT_ACCOUNT_ACTION_DECL(subscribe); MSTDNT_ACCOUNT_ACTION_DECL(unsubscribe); -int mstdnt_account_from_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_account* acct); - -int mstdnt_accounts_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_account* accts[], - size_t* size); - int mastodont_get_account(mastodont_t* data, int lookup_type, char* id, struct mstdnt_account* acct, struct mstdnt_storage* storage); -int mstdnt_account_from_json(struct mstdnt_account* status, cJSON* js); -int mstdnt_account_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args); -int mstdnt_accounts_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args); +int mstdnt_account_json(struct mstdnt_account* status, 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); void _mstdnt_val_malloc_account_call(cJSON* v, void* _type); diff --git a/include/mastodont_relationship.h b/include/mastodont_relationship.h index 290d7fa..f83d756 100644 --- a/include/mastodont_relationship.h +++ b/include/mastodont_relationship.h @@ -16,6 +16,7 @@ #ifndef MASTODONT_RELATIONSHIP_H #define MASTODONT_RELATIONSHIP_H #include +#include #include #include @@ -41,24 +42,15 @@ struct mstdnt_relationship char* note; }; -int _mstdnt_relationship_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args); - -int mstdnt_relationship_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_relationship* relationship); +int mstdnt_relationship_json_callback(cJSON* json, void* _args); int mstdnt_relationship_json(struct mstdnt_relationship* relationship, cJSON* js); -int mstdnt_relationships_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_relationship* relationships[], - size_t* size); +int mstdnt_relationships_json(struct mstdnt_relationship* relationships[], + size_t* size, + cJSON* json); -int _mstdnt_relationships_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args); +int mstdnt_relationships_json_callback(cJSON* json, void* _args); int mastodont_get_relationships(mastodont_t* data, char** ids, diff --git a/src/account.c b/src/account.c index 7faa605..8914232 100644 --- a/src/account.c +++ b/src/account.c @@ -25,7 +25,7 @@ void _mstdnt_val_account_call(cJSON* v, void* _type) { struct mstdnt_account* type = _type; - mstdnt_account_from_json(type, v->child); + mstdnt_account_json(type, v->child); } void _mstdnt_val_malloc_account_call(cJSON* v, void* _type) @@ -35,60 +35,11 @@ void _mstdnt_val_malloc_account_call(cJSON* v, void* _type) *type = calloc(1, sizeof(struct mstdnt_account)); if (*type) - mstdnt_account_from_json(*type, v->child); -} - -int mstdnt_account_from_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_account* acct) -{ - cJSON* root; - root = cJSON_ParseWithLength(results->response, results->size); - - memset(acct, 0, sizeof(struct mstdnt_account)); - - if (root == NULL) - { - return 1; - } - storage->root = root; - storage->needs_cleanup = 1; - - mstdnt_account_from_json(acct, root->child); - return 0; + mstdnt_account_json(*type, v->child); } // GENERATE mstdnt_statuses_json -GENERATE_JSON_ARRAY_FUNC(mstdnt_accounts_json, struct mstdnt_account, mstdnt_account_from_json) - -int mstdnt_accounts_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_account* accts[], - size_t* size) -{ - size_t i = 0; - cJSON* root, *acct_j_list; - if (_mstdnt_json_init(&root, results, storage) && - !cJSON_IsArray(root)) - return 1; - - return 0; -} - -int mstdnt_account_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* args) -{ - return mstdnt_account_from_result(results, storage, args); -} - -int mstdnt_accounts_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args) -{ - struct mstdnt_account_args* args = _args; - return mstdnt_accounts_result(results, storage, args->acct, args->size); -} +GENERATE_JSON_ARRAY_FUNC(mstdnt_accounts_json, struct mstdnt_account, mstdnt_account_json) int mastodont_get_account(mastodont_t* data, int lookup, /* TODO move into separate function for consistancy */ @@ -109,14 +60,14 @@ int mastodont_get_account(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, acct, /* args */ - mstdnt_account_callback, /* callback */ + mstdnt_account_json_callback, /* callback */ }; return mastodont_request(data, &req_args); } -int mstdnt_account_from_json(struct mstdnt_account* acct, cJSON* js) +int mstdnt_account_json(struct mstdnt_account* acct, cJSON* js) { cJSON* v; @@ -169,7 +120,7 @@ int mstdnt_account_action(mastodont_t* data, NULL, 0, CURLOPT_POST, rel, - _mstdnt_relationship_result_callback + mstdnt_relationship_json_callback }; return mastodont_request(data, &req_args); diff --git a/src/relationship.c b/src/relationship.c index 6c3dc3b..7a20e1f 100644 --- a/src/relationship.c +++ b/src/relationship.c @@ -17,6 +17,7 @@ #include #include #include +#include #define FLAG_ARG(flag) { &(relationship->flags), flag } @@ -81,58 +82,18 @@ int mstdnt_relationship_json(struct mstdnt_relationship* relationship, cJSON* js return 0; } -int mstdnt_relationships_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_relationship* relationships[], - size_t* size) +// GENERATE mstdnt_statuses_json +GENERATE_JSON_ARRAY_FUNC(mstdnt_relationships_json, struct mstdnt_relationship, mstdnt_relationship_json) + +int mstdnt_relationship_json_callback(cJSON* json, void* _args) { - size_t i = 0; - cJSON* root, *rel_j_list; - if (_mstdnt_json_init(&root, results, storage) && - !cJSON_IsArray(root)) - return 1; - - if (size) *size = cJSON_GetArraySize(root); - - *relationships = calloc(1, (size ? *size : cJSON_GetArraySize(root)) - * sizeof(struct mstdnt_relationship)); - if (*relationships == NULL) - return 1; - - cJSON_ArrayForEach(rel_j_list, root) - { - mstdnt_relationship_json((*relationships) + i++, rel_j_list->child); - } - - return 0; + return mstdnt_relationship_json((struct mstdnt_relationship*)_args, json); } -int mstdnt_relationship_result(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - struct mstdnt_relationship* relationship) -{ - if (!relationship) return 0; - - cJSON* root; - if (_mstdnt_json_init(&root, results, storage)) - return 1; - - return mstdnt_relationship_json(relationship, root->child); -} - -int _mstdnt_relationship_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args) -{ - return mstdnt_relationship_result(results, storage, (struct mstdnt_relationship*)_args); -} - -int _mstdnt_relationships_result_callback(struct mstdnt_fetch_results* results, - struct mstdnt_storage* storage, - void* _args) +int mstdnt_relationships_json_callback(cJSON* json, void* _args) { struct _mstdnt_relationships_cb_args* args = _args; - return mstdnt_relationships_result(results, storage, args->relationships, args->size); + return mstdnt_relationships_json(args->relationships, args->size, json); } int mastodont_get_relationships(mastodont_t* data, @@ -159,7 +120,7 @@ int mastodont_get_relationships(mastodont_t* data, NULL, 0, CURLOPT_HTTPGET, &cb_args, - _mstdnt_relationships_result_callback + mstdnt_relationships_json_callback }; return mastodont_request(data, &req_args);