Attachments and Application updates

FossilOrigin-Name: 9f4ac71f53b076d419306b33fa4cef231d0ece49d66e4e407d2fc06a782e5fcf
This commit is contained in:
nekobit 2022-05-15 01:59:33 +00:00
parent 30c5bae8b3
commit ceb0cbfba7
4 changed files with 21 additions and 75 deletions

View file

@ -48,14 +48,6 @@ 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

@ -47,19 +47,16 @@ struct mstdnt_upload_media_args
/* TODO focus */
};
int mstdnt_attachment_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_attachment* att);
int mstdnt_attachment_json(cJSON* att_json, struct mstdnt_attachment* att);
void _mstdnt_val_attachments_call(cJSON* v, void* _type);
void load_attachment_from_json(struct mstdnt_attachment* att, cJSON* att_json);
int mastodont_upload_media(mastodont_t* api,
struct mstdnt_upload_media_args* args,
struct mstdnt_storage* storage,
struct mstdnt_attachment* attachment);
void cleanup_attachments(struct mstdnt_attachment* attachment);
void mstdnt_cleanup_attachments(struct mstdnt_attachment* attachment);
void _mstdnt_val_attachments_call(cJSON* v, void* _type);
#endif /* MASTODONT_ATTACHMENT */

View file

@ -20,19 +20,11 @@
#include <mastodont_query.h>
#include <mastodont_request.h>
int mstdnt_app_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_app* app)
static int mstdnt_app_json(cJSON* json, struct mstdnt_app* app)
{
cJSON* root, *v;
/* Zero out */
memset(storage, 0, sizeof(struct mstdnt_storage));
memset(app, 0, sizeof(struct mstdnt_app));
if (_mstdnt_json_init(&root, results, storage))
return 1;
struct _mstdnt_val_ref refs[] = {
{ "id", &(app->id), _mstdnt_val_string_call },
{ "name", &(app->name), _mstdnt_val_string_call },
@ -43,31 +35,20 @@ int mstdnt_app_result(struct mstdnt_fetch_results* results,
{ "vapid_key", &(app->vapid_key), _mstdnt_val_string_call },
};
for (v = root->child; v; v = v->next)
{
for (cJSON* v = json->child; v; v = v->next)
if (_mstdnt_key_val_ref(v, refs, _mstdnt_arr_len(refs)))
{
return 1;
}
}
return 0;
}
static int mstdnt_app_result_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* args)
static int mstdnt_app_json_callback(cJSON* json, void* args)
{
return mstdnt_app_result(results, storage, args);
return mstdnt_app_json(json, args);
}
int mstdnt_token_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_oauth_token* app)
static int mstdnt_token_json(cJSON* json, struct mstdnt_oauth_token* app)
{
cJSON* root, *v;
if (_mstdnt_json_init(&root, results, storage))
return 1;
struct _mstdnt_val_ref refs[] = {
{ "access_token", &(app->access_token), _mstdnt_val_string_call },
{ "token_type", &(app->token_type), _mstdnt_val_string_call },
@ -76,22 +57,16 @@ int mstdnt_token_result(struct mstdnt_fetch_results* results,
{ "me", &(app->me), _mstdnt_val_string_call },
};
for (v = root; v; v = v->next)
{
for (cJSON* v = json; v; v = v->next)
if (_mstdnt_key_val_ref(v->child, refs, _mstdnt_arr_len(refs)) == 1)
{
return 1;
}
}
return 0;
}
static int mstdnt_token_result_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* args)
static int mstdnt_token_json_callback(cJSON* json, void* args)
{
return mstdnt_token_result(results, storage, args);
return mstdnt_token_json(json, args);
}
int mastodont_register_app(mastodont_t* data,
@ -113,7 +88,7 @@ int mastodont_register_app(mastodont_t* data,
params, _mstdnt_arr_len(params),
CURLOPT_POST,
app,
mstdnt_app_result_callback
mstdnt_app_json_callback
};
return mastodont_request(data, &req_args);
@ -145,7 +120,7 @@ int mastodont_obtain_oauth_token(mastodont_t* data,
_mstdnt_arr_len(params),
CURLOPT_POST,
token,
mstdnt_token_result_callback
mstdnt_token_json_callback
};
return mastodont_request(data, &req_args);

View file

@ -20,10 +20,10 @@
#include <mastodont_request.h>
#include <mastodont_query.h>
void load_attachment_from_json(struct mstdnt_attachment* att, cJSON* att_json)
int mstdnt_attachment_json(struct mstdnt_attachment* att, cJSON* att_json)
{
cJSON* it;
if (!att) return 1;
/* Zero out */
memset(att, 0, sizeof(struct mstdnt_attachment));
@ -38,25 +38,9 @@ void load_attachment_from_json(struct mstdnt_attachment* att, cJSON* att_json)
{ "blurhash", &(att->blurhash), _mstdnt_val_string_call },
};
for (it = att_json; it; it = it->next)
{
for (cJSON* it = att_json; it; it = it->next)
_mstdnt_key_val_ref(it, refs, _mstdnt_arr_len(refs));
}
}
int mstdnt_attachment_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_attachment* att)
{
/* Can be null sometimes */
if (!att) return 0;
cJSON* root;
if (_mstdnt_json_init(&root, results, storage) ||
!cJSON_IsObject(root))
return 1;
load_attachment_from_json(att, root->child);
return 0;
}
@ -84,15 +68,13 @@ void _mstdnt_val_attachments_call(cJSON* v, void* _type)
int i;
for (it = v_array, i = 0; it; (++i, it = it->next))
{
load_attachment_from_json((*attachments) + i, it->child);
mstdnt_attachment_json(it->child, (*attachments) + i);
}
}
static int mstdnt_attachment_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* _args)
static int mstdnt_attachment_json_callback(cJSON* json, void* _args)
{
return mstdnt_attachment_result(results, storage, _args);
return mstdnt_attachment_result(json, _args);
}
int mastodont_upload_media(mastodont_t* api,