Cleanup, update lists code

FossilOrigin-Name: 1751da6b1b8f80b776083a8ea0bc92c787d09deb049a70faff3b860135be6190
This commit is contained in:
me@ow.nekobit.net 2022-03-15 02:31:19 +00:00
parent 1b321c8567
commit 88dcf586f5
10 changed files with 151 additions and 119 deletions

View file

@ -16,6 +16,7 @@
#ifndef MASTODONT_ACCOUNT
#define MASTODONT_ACCOUNT
#include "mastodont_types.h"
#include "mastodont_fetch.h"
#include <cjson/cJSON.h>
#define MSTDNT_LOOKUP_ACCT 0
@ -54,14 +55,19 @@ struct mstdnt_account
char* mute_expires_at;
};
int mastodont_account(mastodont_t* data,
int lookup_type,
char* id,
struct mstdnt_account* acct,
struct mstdnt_storage* storage,
size_t* size);
int mstdnt_account_from_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_account* acct,
size_t* size);
int mstdnt_load_account_from_json(struct mstdnt_account* status, cJSON* js);
int mastodont_get_account(mastodont_t* data,
int lookup_type,
char* id,
struct mstdnt_account* acct,
struct mstdnt_storage* storage,
size_t* size);
int mstdnt_account_from_json(struct mstdnt_account* status, cJSON* js);
#endif /* MASTODONT_ACCOUNT */

View file

@ -48,6 +48,14 @@ struct mstdnt_oauth_token
time_t time;
};
int mstdnt_app_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_app* app);
int mstdnt_token_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_oauth_token* app);
int mastodont_register_app(mastodont_t* data,
struct mstdnt_args* args,
struct mstdnt_storage* storage,

View file

@ -16,6 +16,7 @@
#ifndef MASTODONT_ARGUMENTS_H
#define MASTODONT_ARGUMENTS_H
#include "mastodont_notif_types.h"
/*
* Originally, when the arguments were being designed for each function,
* I found that many REST operations tended to result similar variable names
@ -23,6 +24,10 @@
* allow argument reusing between multiple functions, all the args are put
* into one struct, this makes it quite a large struct, but any machine will
* handle it fine.
*
* It's ugly, I do not care. The other method caused me to write extreme duplicate
* amounts of code. If it's too memory hungry, reusing this struct in a static/global
* is an option.
*/
struct mstdnt_args

View file

@ -26,12 +26,12 @@ struct mstdnt_list
char* replies_policy;
};
int mstdnt_load_list_from_json(struct mstdnt_list* list, cJSON* js);
int mstdnt_list_from_json(struct mstdnt_list* list, cJSON* js);
int mstdnt_load_lists_from_result(struct mstdnt_list* lists[],
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
size_t* size);
int mstdnt_lists_from_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_list* lists[],
size_t* size);
int mastodont_get_lists(mastodont_t* api,
struct mstdnt_list* lists[],

View file

@ -18,6 +18,7 @@
#include "mastodont_types.h"
#include "mastodont_account.h"
#include "mastodont_status.h"
#include "mastodont_args.h"
#include <cjson/cJSON.h>
struct mstdnt_notification
@ -29,29 +30,13 @@ struct mstdnt_notification
enum mstdnt_notification_type type;
};
struct mstdnt_get_notifications_args
{
char** exclude_types;
size_t exclude_types_len;
char* account_id;
char** exclude_visibilities;
size_t exclude_visibilities_len;
enum mstdnt_notification_type* include_types;
mstdnt_bool with_muted;
char* max_id;
char* min_id;
char* since_id;
int offset;
int limit;
};
int mastodont_get_notifications(mastodont_t* data,
struct mstdnt_get_notifications_args* args,
struct mstdnt_args* args,
struct mstdnt_storage* storage,
struct mstdnt_notification** notifs,
size_t* size);
int mstdnt_load_notification_from_json(struct mstdnt_notification* notif, cJSON* js);
int mstdnt_notification_from_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

@ -91,30 +91,31 @@ int mstdnt_load_statuses_from_result(struct mstdnt_status* status[],
struct mstdnt_fetch_results* results,
size_t* size);
int mstdnt_load_status_from_result(struct mstdnt_status* status,
int mstdnt_status_from_result(struct mstdnt_status* status,
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results);
int mstdnt_status_from_json(struct mstdnt_status* status, cJSON* js);
int mastodont_get_account_statuses(mastodont_t* data,
char* id,
struct mstdnt_args* args,
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results);
int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js);
struct mstdnt_status* statuses[],
size_t* size);
int mastodont_account_statuses(mastodont_t* data,
char* id,
struct mstdnt_args* args,
struct mstdnt_storage* storage,
struct mstdnt_status* statuses[],
size_t* size);
int mastodont_get_status(mastodont_t* data,
char* id,
struct mstdnt_storage* storage,
struct mstdnt_status* status);
int mastodont_view_status(mastodont_t* data,
char* id,
struct mstdnt_storage* storage,
struct mstdnt_status* status);
int mastodont_status_context(mastodont_t* data,
char* id,
struct mstdnt_storage* storage,
struct mstdnt_status* statuses_before[],
struct mstdnt_status* statuses_after[],
size_t* size_before,
size_t* size_after);
int mastodont_get_status_context(mastodont_t* data,
char* id,
struct mstdnt_storage* storage,
struct mstdnt_status* statuses_before[],
struct mstdnt_status* statuses_after[],
size_t* size_before,
size_t* size_after);
int mastodont_create_status(mastodont_t* data,
struct mstdnt_args* args,

View file

@ -17,24 +17,19 @@
#include <mastodont_account.h>
#include <mastodont_request.h>
#include <mastodont_json_helper.h>
#include "mastodont_fetch.h"
struct mastodont_account_args
struct mstdnt_account_args
{
struct mstdnt_account* acct;
size_t* size;
};
static int mastodont_account_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* _args)
int mstdnt_account_from_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_account* acct,
size_t* size)
{
struct mastodont_account_args* args = _args;
struct mstdnt_account* acct = args->acct;
size_t* size = args->size;
cJSON* root;
/* TODO cleanup this */
root = cJSON_ParseWithLength(results->response, results->size);
if (root == NULL)
@ -44,18 +39,27 @@ static int mastodont_account_callback(struct mstdnt_fetch_results* results,
storage->root = root;
storage->needs_cleanup = 1;
mstdnt_load_account_from_json(acct, root->child);
mstdnt_account_from_json(acct, root->child);
return 0;
}
int mastodont_account(mastodont_t* data,
int lookup, /* TODO move into separate function for consistancy */
char* id,
struct mstdnt_account* acct,
struct mstdnt_storage* storage,
size_t* size)
static int mstdnt_account_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* _args)
{
struct mastodont_account_args acct_args = { acct, size };
struct mstdnt_account_args* args = _args;
return mstdnt_account_from_result(results, storage, args->acct, args->size);
}
int mastodont_get_account(mastodont_t* data,
int lookup, /* TODO move into separate function for consistancy */
char* id,
struct mstdnt_account* acct,
struct mstdnt_storage* storage,
size_t* size)
{
struct mstdnt_account_args acct_args = { acct, size };
/* Url */
char url[MSTDNT_URLSIZE];
snprintf(url, MSTDNT_URLSIZE,
@ -71,14 +75,14 @@ int mastodont_account(mastodont_t* data,
0,
CURLOPT_HTTPGET,
&acct_args, /* args */
mastodont_account_callback, /* callback */
mstdnt_account_callback, /* callback */
};
return mastodont_request(data, &args);
}
int mstdnt_load_account_from_json(struct mstdnt_account* acct, cJSON* js)
int mstdnt_account_from_json(struct mstdnt_account* acct, cJSON* js)
{
cJSON* v;
struct _mstdnt_val_ref refs[] = {

View file

@ -19,11 +19,10 @@
#include <mastodont_query.h>
#include <mastodont_request.h>
static int mstdnt_read_app_result_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* args)
int mstdnt_app_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_app* app)
{
struct mstdnt_app* app = args;
cJSON* root, *v;
if (_mstdnt_json_init(&root, results, storage))
return 1;
@ -48,11 +47,17 @@ static int mstdnt_read_app_result_callback(struct mstdnt_fetch_results* results,
return 0;
}
static int mstdnt_read_token_result_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* args)
static int mstdnt_app_result_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* args)
{
return mstdnt_app_result(results, storage, args);
}
int mstdnt_token_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_oauth_token* app)
{
struct mstdnt_oauth_token* app = args;
cJSON* root, *v;
if (_mstdnt_json_init(&root, results, storage))
return 1;
@ -76,7 +81,12 @@ static int mstdnt_read_token_result_callback(struct mstdnt_fetch_results* result
return 0;
}
static int mstdnt_token_result_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* args)
{
mstdnt_token_result(results, storage, args);
}
int mastodont_register_app(mastodont_t* data,
struct mstdnt_args* args,
@ -100,13 +110,11 @@ int mastodont_register_app(mastodont_t* data,
struct mastodont_request_args req_args = {
storage,
"api/v1/apps",
NULL,
0,
params,
_mstdnt_arr_len(params),
NULL, 0,
params, _mstdnt_arr_len(params),
CURLOPT_POST,
app,
mstdnt_read_app_result_callback
mstdnt_app_result_callback
};
return mastodont_request(data, &req_args);
@ -150,7 +158,7 @@ int mastodont_obtain_oauth_token(mastodont_t* data,
_mstdnt_arr_len(params),
CURLOPT_POST,
token,
mstdnt_read_token_result_callback
mstdnt_token_result_callback
};
return mastodont_request(data, &req_args);

View file

@ -17,8 +17,14 @@
#include <mastodont_list.h>
#include <mastodont_json_helper.h>
#include <mastodont_fetch.h>
#include <mastodont_request.h>
int mstdnt_load_list_from_json(struct mstdnt_list* list, cJSON* js)
struct mstdnt_get_lists_args {
struct mstdnt_list** lists;
size_t* size;
};
int mstdnt_list_from_json(struct mstdnt_list* list, cJSON* js)
{
cJSON* v;
struct _mstdnt_str_val strings[] = {
@ -34,11 +40,10 @@ int mstdnt_load_list_from_json(struct mstdnt_list* list, cJSON* js)
return 0;
}
int mstdnt_load_lists_from_result(struct mstdnt_list* lists[],
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
size_t* size)
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;
@ -58,30 +63,39 @@ int mstdnt_load_lists_from_result(struct mstdnt_list* lists[],
cJSON_ArrayForEach(list_j_list, root)
{
mstdnt_load_list_from_json((*lists) + i++, list_j_list->child);
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)
{
struct mstdnt_get_lists_args* args = _args;
return mstdnt_lists_from_result(storage, results, args->lists, args->size);
}
int mastodont_get_lists(mastodont_t* data,
struct mstdnt_list* lists[],
struct mstdnt_storage* storage,
size_t* size)
{
int res = 0;
cJSON* root;
struct mstdnt_fetch_results results = { 0 };
storage->needs_cleanup = 0;
struct mstdnt_get_lists_args args = {
lists,
size
};
struct mastodont_request_args req_args = {
storage,
"api/v1/lists",
NULL, 0,
NULL, 0,
CURLOPT_HTTPGET,
&args,
mstdnt_lists_from_result_callback
};
if (mastodont_fetch_curl(data, "api/v1/lists",
&results, CURLOPT_HTTPGET) != CURLE_OK)
{
return 1;
}
res = mstdnt_load_lists_from_result(lists, storage, &results, size);
mastodont_fetch_results_cleanup(&results);
return res;
return mastodont_request(data, &req_args);
}

View file

@ -21,7 +21,7 @@
#include <mastodont_query.h>
#include <mastodont_pleroma.h>
int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js)
int mstdnt_status_from_json(struct mstdnt_status* status, cJSON* js)
{
cJSON* v;
@ -58,13 +58,13 @@ int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js)
if (_mstdnt_key_val_ref(v, vals, _mstdnt_arr_len(vals)) == 1)
if (cJSON_IsObject(v))
if (strcmp("account", v->string) == 0)
mstdnt_load_account_from_json(&(status->account), v->child);
mstdnt_account_from_json(&(status->account), v->child);
return 0;
}
int mstdnt_load_status_from_result(struct mstdnt_status* status,
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results)
int mstdnt_status_from_result(struct mstdnt_status* status,
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results)
{
cJSON* root;
if (_mstdnt_json_init(&root, results, storage))
@ -73,13 +73,13 @@ int mstdnt_load_status_from_result(struct mstdnt_status* status,
if (!cJSON_IsObject(root))
return 1;
if (!mstdnt_load_status_from_json(status, root->child))
if (!mstdnt_status_from_json(status, root->child))
storage->needs_cleanup = 1;
return 1;
}
int mstdnt_load_statuses_from_result(struct mstdnt_status* statuses[],
int mstdnt_statuses_from_result(struct mstdnt_status* statuses[],
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
size_t* size)
@ -102,13 +102,14 @@ int mstdnt_load_statuses_from_result(struct mstdnt_status* statuses[],
cJSON_ArrayForEach(status_j_list, root)
{
mstdnt_load_status_from_json((*statuses) + i++, status_j_list->child);
mstdnt_status_from_json((*statuses) + i++, status_j_list->child);
}
storage->needs_cleanup = 1;
return 0;
}
int mastodont_account_statuses(mastodont_t* data,
int mastodont_get_account_statuses(mastodont_t* data,
char* id,
struct mstdnt_args* args,
struct mstdnt_storage* storage,
@ -126,7 +127,7 @@ int mastodont_account_statuses(mastodont_t* data,
if (mastodont_fetch_curl(data, url, &results, CURLOPT_HTTPGET) != CURLE_OK)
return 1;
res = mstdnt_load_statuses_from_result(statuses, storage, &results, size);
res = mstdnt_statuses_from_result(statuses, storage, &results, size);
mastodont_fetch_results_cleanup(&results);
@ -231,7 +232,7 @@ int mastodont_view_status(mastodont_t* data,
if (mastodont_fetch_curl(data, url, &results, CURLOPT_HTTPGET) != CURLE_OK)
return 1;
res = mstdnt_load_status_from_result(status, storage, &results);
res = mstdnt_status_from_result(status, storage, &results);
mastodont_fetch_results_cleanup(&results);
@ -296,7 +297,7 @@ int mastodont_status_context(mastodont_t* data,
return 1;
cJSON_ArrayForEach(status_item, v)
mstdnt_load_status_from_json((*stat_ptr) + (*size_ptr)++, status_item->child);
mstdnt_status_from_json((*stat_ptr) + (*size_ptr)++, status_item->child);
}
}
storage->needs_cleanup = 1;