Account and relationship results rewrite

FossilOrigin-Name: e14a7e5080cc140a7eaa6b7be682d93ec910a649389bfb1656d8d3260cfa0762
This commit is contained in:
nekobit 2022-05-12 03:20:47 +00:00
parent 3191da7ea1
commit 7d3751771b
4 changed files with 24 additions and 133 deletions

View file

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

View file

@ -16,6 +16,7 @@
#ifndef MASTODONT_RELATIONSHIP_H
#define MASTODONT_RELATIONSHIP_H
#include <stdint.h>
#include <cjson/cJSON.h>
#include <mastodont_types.h>
#include <mastodont_request.h>
@ -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,

View file

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

View file

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