Read param values; timeline, stautus fixes

FossilOrigin-Name: 09a4b2e86f595f4813cdbf6d0d1e90738e0fd62c38c002c48c5224c7c6f80078
This commit is contained in:
me@ow.nekobit.net 2022-02-15 17:18:15 +00:00
parent 2ffe977836
commit a6f5184cfe
9 changed files with 123 additions and 31 deletions

View file

@ -19,8 +19,6 @@
#include <cjson/cJSON.h>
#include <mastodont_fetch.h>
/* Status: Complete */
struct mstdnt_application
{
char* name;
@ -36,8 +34,24 @@ struct mstdnt_app_register_args
char* website;
};
struct mstdnt_app
{
char* id;
char* name;
char* website;
char* redirect_uri;
char* client_id;
char* client_secret;
char* vapid_key;
};
int mstdnt_load_app_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_app* app);
int mastodont_register_app(mastodont_t* data,
struct mstdnt_app_register_args* args,
struct mstdnt_storage* storage);
struct mstdnt_storage* storage,
struct mstdnt_app* app);
#endif /* MASTODONT_ACCOUNT */

View file

@ -16,8 +16,7 @@
#ifndef MASTODONT_JSON_HELPER_H
#define MASTODONT_JSON_HELPER_H
#include "mastodont_types.h"
#define _mstdnt_arr_len(arr) (sizeof(arr)/sizeof(arr[0]))
#include "mastodont_fetch.h"
struct _mstdnt_str_val
{
@ -31,6 +30,10 @@ struct _mstdnt_bool_val
mstdnt_bool* bool_ptr;
};
int _mstdnt_json_init(cJSON** root,
struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage);
int _mstdnt_key_val_iter(cJSON* v,
struct _mstdnt_str_val* str,
size_t str_len,

View file

@ -28,7 +28,7 @@ struct _mstdnt_query_param
{
enum _mstdnt_query_type type;
char* key;
union {
union param_value {
char* s;
int i;
} value;

View file

@ -18,10 +18,12 @@
#include <curl/curl.h>
#include <cjson/cJSON.h>
#define _mstdnt_arr_len(arr) (sizeof(arr)/sizeof(arr[0]))
#define MSTDNT_URLSIZE 2048
#define MSTDNT_URISIZE 512
typedef unsigned char mstdnt_bool;
typedef struct mastodont
{
char* url;

View file

@ -13,11 +13,44 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <mastodont_json_helper.h>
#include <mastodont_application.h>
#include <mastodont_query.h>
int mstdnt_load_app_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_app* app)
{
cJSON* root, *v;
if (_mstdnt_json_init(&root, results, storage))
return 1;
struct _mstdnt_str_val strings[] = {
{ "id", &(app->id) },
{ "name", &(app->name) },
{ "website", &(app->website) },
{ "redirect_uri", &(app->redirect_uri) },
{ "client_id", &(app->client_id) },
{ "client_secret", &(app->client_secret) },
{ "vapid_key", &(app->vapid_key) },
};
for (v = root->child; v; v = v->next)
{
if (_mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings),
NULL, 0) == 1)
{
return 1;
}
}
}
int mastodont_register_app(mastodont_t* data,
struct mstdnt_app_register_args* args,
struct mstdnt_storage* storage)
struct mstdnt_storage* storage,
struct mstdnt_app* app)
{
int res;
struct mstdnt_fetch_results results = { 0 };
@ -34,13 +67,30 @@ int mastodont_register_app(mastodont_t* data,
}
storage->needs_cleanup = 0;
if (mastodont_fetch_curl(data, "api/v1/apps", &results) != CURLE_OK)
return 1;
union param_value u_client_name, u_redirect_uris,
u_scopes, u_website;
/*res = mstdnt_load_statuses_from_result(statuses, storage, &results, size);*/
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_STRING, "client_name", u_client_name },
{ _MSTDNT_QUERY_STRING, "redirect_uris", u_redirect_uris },
{ _MSTDNT_QUERY_STRING, "scopes", u_scopes },
{ _MSTDNT_QUERY_STRING, "website", u_website },
};
char* url = _mstdnt_query_string("api/v1/apps", params, _mstdnt_arr_len(params));
if (mastodont_fetch_curl(data, url, &results) != CURLE_OK)
{
res = 1;
goto cleanup;
}
res = mstdnt_load_app_result(storage, &results, app);
mastodont_fetch_results_cleanup(&results);
cleanup:
free(url);
return res;
}

View file

@ -15,6 +15,18 @@
#include <mastodont_json_helper.h>
int _mstdnt_json_init(cJSON** root,
struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage)
{
*root = cJSON_ParseWithLength(results->response, results->size);
if (*root == NULL)
return 1;
storage->root = *root;
storage->needs_cleanup = 1;
return 0;
}
int _mstdnt_key_val_iter(cJSON* v,
struct _mstdnt_str_val* str,
size_t str_len,

View file

@ -45,7 +45,9 @@ char* _mstdnt_query_string(char* src,
for (i = 0; i < param_len; ++i)
{
if (params[i].key)
if (params[i].key &&
!(params[i].type == _MSTDNT_QUERY_STRING &&
params[i].value.s == NULL))
{
if (res_count++ == 0)
/* Replaces Null terminator */
@ -80,8 +82,8 @@ char* _mstdnt_query_string(char* src,
/* Copy over strings (skip & sign ;; +1) */
strcpy(result + res_prev, params[i].key);
result[res_prev - (res_count - 1 != 0) + key_len] = '=';
strcpy(result + res_prev + 1 + key_len - (res_count - 1 != 0), val_ptr);
result[res_prev + key_len] = '=';
strcpy(result + res_prev + 1 + key_len, val_ptr);
}
}

View file

@ -65,11 +65,8 @@ int mstdnt_load_statuses_from_result(struct mstdnt_status* statuses[],
{
size_t i = 0;
cJSON* root, *status_j_list;
root = cJSON_ParseWithLength(results->response, results->size);
if (root == NULL)
if (_mstdnt_json_init(&root, results, storage))
return 1;
storage->root = root;
storage->needs_cleanup = 1;
if (!cJSON_IsArray(root))
return 1;

View file

@ -43,27 +43,39 @@ int mastodont_timeline_public(mastodont_t* data,
}
storage->needs_cleanup = 0;
union param_value u_local, u_remote, u_only_media,
u_max_id, u_since_id, u_min_id, u_limit;
u_local.i = args->local;
u_remote.i = args->remote;
u_only_media.i = args->only_media;
u_max_id.s = args->max_id;
u_since_id.s = args->since_id;
u_min_id.s = args->min_id;
u_limit.i = args->limit;
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_STRING, NULL, "rat" },
{ _MSTDNT_QUERY_STRING, "lolled", "ratted" },
{ _MSTDNT_QUERY_INT, "local", u_local },
{ _MSTDNT_QUERY_INT, "remote", u_remote },
{ _MSTDNT_QUERY_INT, "only_media", u_only_media },
{ _MSTDNT_QUERY_STRING, "max_id", u_max_id },
{ _MSTDNT_QUERY_STRING, "since_id", u_since_id },
{ _MSTDNT_QUERY_STRING, "min_id", u_min_id },
{ _MSTDNT_QUERY_INT, "limit", u_limit },
};
char* url = _mstdnt_query_string("api/v1/timelines/public", params, _mstdnt_arr_len(params));
char* url = _mstdnt_query_string("api/v1/timelines/public",
params, 2);
puts(url);
free (url);
return 1;
if (mastodont_fetch_curl(data, "api/v1/timelines/public", &results) != CURLE_OK)
return 1;
if (mastodont_fetch_curl(data, url, &results) != CURLE_OK)
{
res = 1;
goto cleanup;
}
res = mstdnt_load_statuses_from_result(statuses, storage, &results, size);
mastodont_fetch_results_cleanup(&results);
cleanup:
free(url);
return res;
}