Rewrite/rename get statuses function

FossilOrigin-Name: 840799db493b11252be79af0404de1da8b5455b75290e6aea96f360a4042bbf4
This commit is contained in:
me@ow.nekobit.net 2022-03-15 02:55:28 +00:00
parent 6b89f84490
commit 328a9f2bd2
4 changed files with 61 additions and 61 deletions

View file

@ -18,16 +18,7 @@
#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
* under the same types. To reduce the amount of duplicate code, and to even
* 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.
* TODO undo this shit, it's actually just gross i'll duplicate the code i do not care
*/
struct mstdnt_args

View file

@ -86,10 +86,10 @@ struct mstdnt_status
void cleanup_statuses(struct mstdnt_status* statuses, size_t s);
void cleanup_status(struct mstdnt_status* status);
int mstdnt_load_statuses_from_result(struct mstdnt_status* status[],
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
size_t* size);
int mstdnt_statuses_from_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_status* status[],
size_t* size);
int mstdnt_status_from_result(struct mstdnt_status* status,
struct mstdnt_storage* storage,

View file

@ -66,19 +66,17 @@ int mastodont_get_account(mastodont_t* data,
lookup ? "api/v1/accounts/%s" : "api/v1/accounts/lookup?acct=%s",
id);
struct mastodont_request_args args = {
struct mastodont_request_args req_args = {
storage,
url,
NULL,
0,
NULL,
0,
NULL, 0,
NULL, 0,
CURLOPT_HTTPGET,
&acct_args, /* args */
mstdnt_account_callback, /* callback */
};
return mastodont_request(data, &args);
return mastodont_request(data, &req_args);
}

View file

@ -21,6 +21,12 @@
#include <mastodont_query.h>
#include <mastodont_pleroma.h>
struct mstdnt_statuses_args
{
struct mstdnt_status** statuses;
size_t* size;
};
int mstdnt_status_from_json(struct mstdnt_status* status, cJSON* js)
{
cJSON* v;
@ -79,10 +85,10 @@ int mstdnt_status_from_result(struct mstdnt_status* status,
return 1;
}
int mstdnt_statuses_from_result(struct mstdnt_status* statuses[],
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
size_t* size)
int mstdnt_statuses_from_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_status* statuses[],
size_t* size)
{
size_t i = 0;
cJSON* root, *status_j_list;
@ -109,42 +115,44 @@ int mstdnt_statuses_from_result(struct mstdnt_status* statuses[],
return 0;
}
int mastodont_get_account_statuses(mastodont_t* data,
char* id,
struct mstdnt_args* args,
struct mstdnt_storage* storage,
struct mstdnt_status* statuses[],
size_t* size)
static int mstdnt_statuses_result_callback(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
void* _args)
{
int res;
char url[MSTDNT_URLSIZE];
struct mstdnt_fetch_results results = { 0 };
snprintf(url, MSTDNT_URLSIZE, "api/v1/accounts/%s/statuses", id);
/* Default args */
storage->needs_cleanup = 0;
if (mastodont_fetch_curl(data, url, &results, CURLOPT_HTTPGET) != CURLE_OK)
return 1;
res = mstdnt_statuses_from_result(statuses, storage, &results, size);
mastodont_fetch_results_cleanup(&results);
return res;
struct mstdnt_statuses_args* args = _args;
return mstdnt_statuses_from_result(storage, results, args->statuses, args->size);
}
/* TODO Still need to handle get params */
int mastodont_get_account_statuses(mastodont_t* data,
char* id,
struct mstdnt_args* args,
struct mstdnt_storage* storage,
struct mstdnt_status* statuses[],
size_t* size)
{
char url[MSTDNT_URLSIZE];
struct mstdnt_statuses_args args = { statuses, size };
snprintf(url, MSTDNT_URLSIZE, "api/v1/accounts/%s/statuses", id);
struct mastodont_request_args req_args = {
storage,
url,
NULL, 0,
NULL, 0, /* TODO */
CURLOPT_HTTPGET,
&args,
mstdnt_statuses_result_callback
};
return mastodont_request(data, &req_args);
}
/* TODO Populate the arguments! */
int mastodont_create_status(mastodont_t* data,
struct mstdnt_args* args,
struct mstdnt_storage* storage)
{
int res = 0;
char* post;
struct mstdnt_fetch_results results = { 0 };
/* Default args */
storage->needs_cleanup = 0;
union param_value u_content_type, u_expires_in,
u_in_reply_to_conversation_id, u_in_reply_to_id,
u_language, u_media_ids, u_poll, u_preview, u_scheduled_at,
@ -170,15 +178,18 @@ int mastodont_create_status(mastodont_t* data,
{ _MSTDNT_QUERY_STRING, "visibility", u_visibility },
};
post = _mstdnt_query_string(data, NULL, params, _mstdnt_arr_len(params));
struct mastodont_request_args req_args = {
storage,
"api/v1/statuses",
NULL, 0,
params, _mstdnt_arr_len(params),
CURLOPT_POST,
NULL,
NULL, /* TODO populate the status back?
* (not sure if the api returns it or not) */
};
curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post);
if (mastodont_fetch_curl(data, "api/v1/statuses", &results, CURLOPT_POST) != CURLE_OK)
return 1;
mastodont_fetch_results_cleanup(&results);
return 0;
return mastodont_request(data, &req_args);
}
int mastodont_favourite_status(mastodont_t* data,