Rewrite nearly everything for json only

FossilOrigin-Name: 39a6258b4cf1c188667c54cecacd3d990c04bed0d387431fe25520a07223a6a8
This commit is contained in:
nekobit 2022-05-12 04:38:03 +00:00
parent 7d3751771b
commit 12b4ffc4f8
14 changed files with 82 additions and 153 deletions

View File

@ -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);

View File

@ -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[],

View File

@ -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);

View File

@ -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);

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -19,13 +19,14 @@
#include <mastodont_json_helper.h>
#include <mastodont_fetch.h>
#include <mastodont_request.h>
#include <mastodont_generate.h>
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);

View File

@ -20,6 +20,7 @@
#include <mastodont_json_helper.h>
#include <mastodont_query.h>
#include <mastodont_request.h>
#include <mastodont_generate.h>
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);

View File

@ -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);

View File

@ -19,6 +19,7 @@
#include <mastodont_json_helper.h>
#include <mastodont_query.h>
#include <mastodont_request.h>
#include <mastodont_generate.h>
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);

View File

@ -21,6 +21,7 @@
#include <mastodont_status.h>
#include <mastodont_account.h>
#include <mastodont_tag.h>
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);

View File

@ -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);

View File

@ -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);