Application

FossilOrigin-Name: 03a48870e355ee1d2adcf188808928cf1342cabe19404377183e15c0fe622b53
This commit is contained in:
nekobit 2022-11-08 04:22:47 +00:00
parent fc688c303d
commit 0228cdd97c
3 changed files with 115 additions and 96 deletions

View file

@ -56,21 +56,19 @@ struct mstdnt_application_args
char* website;
};
int mstdnt_register_app(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_application_args* args,
struct mstdnt_storage* storage,
struct mstdnt_app* app);
int
mstdnt_register_app(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_application_args args);
int mstdnt_obtain_oauth_token(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_application_args* args,
struct mstdnt_storage* storage,
struct mstdnt_oauth_token* app);
int
mstdnt_obtain_oauth_token(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_application_args args);
void _mstdnt_val_malloc_application_call(cJSON* v, void* _type);

View file

@ -40,6 +40,12 @@ struct mstdnt_notification
mstdnt_notification_t type;
};
struct mstdnt_notifications
{
struct mstdnt_notification* notifs;
size_t len;
};
struct mstdnt_notifications_args
{
/* Bitmask */
@ -65,43 +71,51 @@ struct _mstdnt_notifications_result_cb_args
};
// Callback helpers
int mstdnt_notification_json_callback(cJSON* json, void* notif);
int mstdnt_notifications_json_callback(cJSON* json, void* _args);
int
mstdnt_notification_json_callback(cJSON* json,
void* args,
mstdnt_request_cb_data* data);
int mstdnt_notification_json(struct mstdnt_notification* notif, cJSON* js);
int mstdnt_notification_pleroma_json(struct mstdnt_notification_pleroma* notif, cJSON* js);
int
mstdnt_notifications_json_callback(cJSON* json,
void* args,
mstdnt_request_cb_data* data);
int mstdnt_get_notifications(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_notifications_args* args,
struct mstdnt_storage* storage,
struct mstdnt_notification** notifs,
size_t* size);
int
mstdnt_notification_json(struct mstdnt_notification* notif, cJSON* js);
int mstdnt_notifications_clear(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_storage* storage);
int
mstdnt_notification_pleroma_json(struct mstdnt_notification_pleroma* notif, cJSON* js);
int mstdnt_notifications_read(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_notifications_args* args,
struct mstdnt_storage* storage,
struct mstdnt_notification* notification);
int
mstdnt_get_notifications(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_notifications_args args);
int mstdnt_notification_dismiss(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_storage* storage,
char* id);
int
mstdnt_notifications_clear(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args);
void mstdnt_cleanup_notifications(struct mstdnt_notification* notif, size_t notif_len);
int
mstdnt_notifications_read(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_notifications_args args);
int
mstdnt_notification_dismiss(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_storage* storage,
char* id);
void mstdnt_cleanup_notifications(struct mstdnt_notifications* notif);
void mstdnt_cleanup_notification(struct mstdnt_notification* notif);
#endif /* MASTODONT_NOTIFICATION */

View file

@ -21,7 +21,8 @@
#include <mastodont_query.h>
#include <mastodont_request.h>
static int mstdnt_app_json(cJSON* json, struct mstdnt_app* app)
static int
mstdnt_app_json(cJSON* json, struct mstdnt_app* app)
{
if (!json) return 1;
/* Zero out */
@ -43,12 +44,19 @@ static int mstdnt_app_json(cJSON* json, struct mstdnt_app* app)
return 0;
}
static int mstdnt_app_json_callback(cJSON* json, void* args)
static int
mstdnt_app_json_callback(cJSON* json,
void* args,
mstdnt_request_cb_data* data)
{
return mstdnt_app_json(json->child, args);
(void)args;
struct mstdnt_app* app = malloc(sizeof(struct mstdnt_app));
data->data = app;
return mstdnt_app_json(json->child, app);
}
static int mstdnt_token_json(cJSON* json, struct mstdnt_oauth_token* app)
static int
mstdnt_token_json(cJSON* json, struct mstdnt_oauth_token* app)
{
struct _mstdnt_val_ref refs[] = {
{ "access_token", &(app->access_token), _mstdnt_val_string_call },
@ -64,12 +72,21 @@ static int mstdnt_token_json(cJSON* json, struct mstdnt_oauth_token* app)
return 0;
}
static int mstdnt_token_json_callback(cJSON* json, void* args)
static int
mstdnt_token_json_callback(cJSON* json,
void* args,
mstdnt_request_cb_data* data)
{
return mstdnt_token_json(json->child, args);
(void)args;
struct mstdnt_oauth_token* oauth_token = malloc(sizeof(struct mstdnt_oauth_token));
data->data = oauth_token;
data->data_free_cb = NULL;
// TODO this is correct?
return mstdnt_token_json(json->child, oauth_token);
}
void _mstdnt_val_malloc_application_call(cJSON* v, void* _type)
void
_mstdnt_val_malloc_application_call(cJSON* v, void* _type)
{
struct mstdnt_app** type = _type;
// We can skip an array size check by just seeing
@ -87,65 +104,55 @@ void _mstdnt_val_malloc_application_call(cJSON* v, void* _type)
mstdnt_app_json(v->child, *type);
}
int mstdnt_register_app(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_application_args* args,
struct mstdnt_storage* storage,
struct mstdnt_app* app)
int
mstdnt_register_app(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_application_args args)
{
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_STRING, "client_name", { .s = args->client_name } },
{ _MSTDNT_QUERY_STRING, "redirect_uris", { .s = args->redirect_uris } },
{ _MSTDNT_QUERY_STRING, "scopes", { .s = args->scopes } },
{ _MSTDNT_QUERY_STRING, "website", { .s = args->website } },
{ _MSTDNT_QUERY_STRING, "client_name", { .s = args.client_name } },
{ _MSTDNT_QUERY_STRING, "redirect_uris", { .s = args.redirect_uris } },
{ _MSTDNT_QUERY_STRING, "scopes", { .s = args.scopes } },
{ _MSTDNT_QUERY_STRING, "website", { .s = args.website } },
};
struct mstdnt_request_args req_args = {
storage,
"api/v1/apps",
NULL, 0,
params, _mstdnt_arr_len(params),
CURLOPT_POST,
NULL,
app,
mstdnt_app_json_callback
.url = "api/v1/apps",
.params_post = params,
.params_post_len = _mstdnt_arr_len(params),
.request_type = CURLOPT_POST,
.callback = mstdnt_app_json_callback
};
return mstdnt_request(data, m_args, cb_request, cb_args, &req_args);
}
int mstdnt_obtain_oauth_token(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_application_args* args,
struct mstdnt_storage* storage,
struct mstdnt_oauth_token* token)
int
mstdnt_obtain_oauth_token(mastodont_t* data,
struct mstdnt_args* m_args,
mstdnt_request_cb_t cb_request,
void* cb_args,
struct mstdnt_application_args args)
{
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_STRING, "grant_type", { .s = args->grant_type } },
{ _MSTDNT_QUERY_STRING, "client_id", { .s = args->client_id } },
{ _MSTDNT_QUERY_STRING, "client_secret", { .s = args->client_secret } },
{ _MSTDNT_QUERY_STRING, "redirect_uri", { .s = args->redirect_uri } },
{ _MSTDNT_QUERY_STRING, "scope", { .s = args->scope } },
{ _MSTDNT_QUERY_STRING, "code", { .s = args->code } },
{ _MSTDNT_QUERY_STRING, "username", { .s = args->username } },
{ _MSTDNT_QUERY_STRING, "password", { .s = args->password } },
{ _MSTDNT_QUERY_STRING, "grant_type", { .s = args.grant_type } },
{ _MSTDNT_QUERY_STRING, "client_id", { .s = args.client_id } },
{ _MSTDNT_QUERY_STRING, "client_secret", { .s = args.client_secret } },
{ _MSTDNT_QUERY_STRING, "redirect_uri", { .s = args.redirect_uri } },
{ _MSTDNT_QUERY_STRING, "scope", { .s = args.scope } },
{ _MSTDNT_QUERY_STRING, "code", { .s = args.code } },
{ _MSTDNT_QUERY_STRING, "username", { .s = args.username } },
{ _MSTDNT_QUERY_STRING, "password", { .s = args.password } },
};
struct mstdnt_request_args req_args = {
storage,
"oauth/token",
NULL,
0,
params,
_mstdnt_arr_len(params),
CURLOPT_POST,
NULL,
token,
mstdnt_token_json_callback
.url = "oauth/token",
.params_post = params,
.params_post_len = _mstdnt_arr_len(params),
.request_type = CURLOPT_POST,
.callback = mstdnt_token_json_callback
};
return mstdnt_request(data, m_args, cb_request, cb_args, &req_args);