From a14f78903cc0c3d4f404afa7ad6afefaf64a5bf6 Mon Sep 17 00:00:00 2001 From: nekobit Date: Tue, 8 Nov 2022 20:42:03 +0000 Subject: [PATCH] Fix notifications FossilOrigin-Name: 29e8b3867e2064c2cf6147f98918ae448dc574053c35fbfc328aeabd962e53cc --- include/mastodont_notification.h | 1 - include/mastodont_search.h | 32 +++--- include/mastodont_types.h | 4 +- src/account.c | 4 +- src/notification.c | 179 ++++++++++++++----------------- src/search.c | 92 ++++++++-------- src/status.c | 4 +- 7 files changed, 154 insertions(+), 162 deletions(-) diff --git a/include/mastodont_notification.h b/include/mastodont_notification.h index 68b4fae..b595148 100644 --- a/include/mastodont_notification.h +++ b/include/mastodont_notification.h @@ -112,7 +112,6 @@ 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); diff --git a/include/mastodont_search.h b/include/mastodont_search.h index ae1b9b9..deaa59e 100644 --- a/include/mastodont_search.h +++ b/include/mastodont_search.h @@ -42,26 +42,26 @@ struct mstdnt_search_args struct mstdnt_search_results { - struct mstdnt_account* accts; - size_t accts_len; - struct mstdnt_status* statuses; - size_t statuses_len; - struct mstdnt_tag* tags; - size_t tags_len; + struct mstdnt_accounts* accts; + struct mstdnt_statuses* statuses; + struct mstdnt_tags* tags; }; -int mstdnt_search_json_callback(cJSON* json, void* _args); +int +mstdnt_search_json_callback(cJSON* json, + void* _args, + mstdnt_request_cb_data* data); -int mstdnt_search_json(struct mstdnt_search_results* search_results, cJSON* json); +int +mstdnt_search_json(struct mstdnt_search_results* search_results, cJSON* json); -int mstdnt_search(mastodont_t* data, - struct mstdnt_args* m_args, -mstdnt_request_cb_t cb_request, -void* cb_args, - char* query, - struct mstdnt_storage* storage, - struct mstdnt_search_args* args, - struct mstdnt_search_results* results); +int +mstdnt_search(mastodont_t* data, + struct mstdnt_args* m_args, + mstdnt_request_cb_t cb_request, + void* cb_args, + char* query, + struct mstdnt_search_args args); void mstdnt_cleanup_search_results(struct mstdnt_search_results* res); diff --git a/include/mastodont_types.h b/include/mastodont_types.h index ec77eef..da9c4bc 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -31,12 +31,12 @@ typedef int8_t mstdnt_bool; struct mstdnt_storage; // Only god knows -typedef void (*data_free_cb_t)(void*, size_t); +typedef void (*mstdnt_data_free_cb_t)(void*, size_t); typedef void (*mstdnt_request_cb_t)(void* data, void* args); typedef struct mstdnt_request_cb_data { struct mstdnt_storage* storage; void* data; - data_free_cb_t data_free_cb; + mstdnt_data_free_cb_t data_free_cb; } mstdnt_request_cb_data; #define MSTDNT_CB_DATA(_data) (_data->data) diff --git a/src/account.c b/src/account.c index 0b8943e..deb81e0 100644 --- a/src/account.c +++ b/src/account.c @@ -52,7 +52,7 @@ mstdnt_account_json_callback(cJSON* json, (void)args; struct mstdnt_account* acct = malloc(sizeof(struct mstdnt_account)); data->data = acct; - data->data_free_cb = (data_free_cb_t)mstdnt_cleanup_account; + data->data_free_cb = (mstdnt_data_free_cb_t)mstdnt_cleanup_account; // Not sure why it expects it to be in the child return mstdnt_account_json(acct, json->child); } @@ -65,7 +65,7 @@ mstdnt_accounts_json_callback(cJSON* json, (void)args; struct mstdnt_accounts* accts = malloc(sizeof(struct mstdnt_accounts)); data->data = accts; - data->data_free_cb = (data_free_cb_t)mstdnt_cleanup_accounts; + data->data_free_cb = (mstdnt_data_free_cb_t)mstdnt_cleanup_accounts; return mstdnt_accounts_json(&(accts->accts), &(accts->len), json); } diff --git a/src/notification.c b/src/notification.c index 31fc8ad..8d9ed53 100644 --- a/src/notification.c +++ b/src/notification.c @@ -23,7 +23,8 @@ #include #include -static void _mstdnt_val_notif_type_call(cJSON* v, void* _type) +static void +_mstdnt_val_notif_type_call(cJSON* v, void* _type) { mstdnt_notification_t* type = _type; @@ -46,7 +47,8 @@ static void _mstdnt_val_notif_type_call(cJSON* v, void* _type) else if (strcmp(v->valuestring, "pleroma:report") == 0) *type = MSTDNT_NOTIFICATION_REPORT; } -static void _mstdnt_val_malloc_notification_pleroma_call(cJSON* v, void* _type) +static void +_mstdnt_val_malloc_notification_pleroma_call(cJSON* v, void* _type) { struct mstdnt_notification_pleroma** type = _type; @@ -56,7 +58,8 @@ static void _mstdnt_val_malloc_notification_pleroma_call(cJSON* v, void* _type) mstdnt_notification_pleroma_json(*type, v->child); } -int mstdnt_notification_json(struct mstdnt_notification* notif, cJSON* js) +int +mstdnt_notification_json(struct mstdnt_notification* notif, cJSON* js) { cJSON* v; @@ -81,7 +84,8 @@ int mstdnt_notification_json(struct mstdnt_notification* notif, cJSON* js) return 0; } -int mstdnt_notification_pleroma_json(struct mstdnt_notification_pleroma* notif, cJSON* js) +int +mstdnt_notification_pleroma_json(struct mstdnt_notification_pleroma* notif, cJSON* js) { cJSON* v; @@ -101,149 +105,129 @@ int mstdnt_notification_pleroma_json(struct mstdnt_notification_pleroma* notif, return 0; } -int mstdnt_notification_json_callback(cJSON* json, void* notif) +int +mstdnt_notification_json_callback(cJSON* json, + void* args, + mstdnt_request_cb_data* data) { + (void)args; + struct mstdnt_notification* notif = malloc(sizeof(struct mstdnt_notification)); + data->data = notif; + data->data_free_cb = (mstdnt_data_free_cb_t)mstdnt_cleanup_notification; return mstdnt_notification_json(notif, json->child); } GENERATE_JSON_ARRAY_FUNC(mstdnt_notifications_json, struct mstdnt_notification, mstdnt_notification_json) -int mstdnt_notifications_json_callback(cJSON* json, void* _args) +int +mstdnt_notifications_json_callback(cJSON* json, + void* args, + mstdnt_request_cb_data* data) { - struct _mstdnt_notifications_result_cb_args* args = _args; - return mstdnt_notifications_json(args->notif, args->size, json); + (void)args; + struct mstdnt_notifications* notifs = malloc(sizeof(struct mstdnt_notifications)); + data->data = notifs; + data->data_free_cb = (mstdnt_data_free_cb_t)mstdnt_cleanup_notifications; + return mstdnt_notifications_json(&(notifs->notifs), &(notifs->len), json); } -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_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_notifications_result_cb_args req_cb_args = { notifs, size }; - - union param_value u_exclude_types, u_account_id, u_exclude_visibilities, - u_include_types, u_with_muted, u_max_id, u_min_id, - u_since_id, u_offset, u_limit; - /* TODO Arrays of excludes, includes */ - u_account_id.s = args->account_id; - u_with_muted.i = args->with_muted; - u_max_id.s = args->max_id; - u_since_id.s = args->since_id; - u_min_id.s = args->min_id; - u_offset.i = args->offset; - u_limit.i = args->limit; - struct _mstdnt_query_param params[] = { - { _MSTDNT_QUERY_STRING, "account_id", u_account_id }, - { _MSTDNT_QUERY_INT, "with_muted", u_with_muted }, - { _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, "offset", u_offset }, - { _MSTDNT_QUERY_INT, "limit", u_limit }, + { _MSTDNT_QUERY_STRING, "account_id", { .s = args.account_id } }, + { _MSTDNT_QUERY_INT, "with_muted", { .i = args.with_muted } }, + { _MSTDNT_QUERY_STRING, "max_id", { .s = args.max_id } }, + { _MSTDNT_QUERY_STRING, "since_id", { .s = args.since_id } }, + { _MSTDNT_QUERY_STRING, "min_id", { .s = args.min_id } }, + { _MSTDNT_QUERY_INT, "offset", { .i = args.offset } }, + { _MSTDNT_QUERY_INT, "limit", { .i = args.limit } }, }; struct mstdnt_request_args req_args = { - storage, - "api/v1/notifications", - params, _mstdnt_arr_len(params), - NULL, 0, - CURLOPT_HTTPGET, - NULL, - &req_cb_args, - mstdnt_notifications_json_callback, + .url = "api/v1/notifications", + .params_query = params, + .params_query_len = _mstdnt_arr_len(params), + .request_type = CURLOPT_HTTPGET, + .callback = mstdnt_notifications_json_callback, }; return mstdnt_request(data, m_args, cb_request, cb_args, &req_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_notification_dismiss(mastodont_t* data, + struct mstdnt_args* m_args, + mstdnt_request_cb_t cb_request, + void* cb_args, + char* id) { char url[MSTDNT_URLSIZE]; snprintf(url, MSTDNT_URLSIZE, "api/v1/notifications/%s/dismiss", id); struct mstdnt_request_args req_args = { - storage, - url, - NULL, 0, - NULL, 0, - CURLOPT_POST, - NULL, - NULL, - NULL, + .url = url, + .request_type = CURLOPT_POST, + // TODO? }; return mstdnt_request(data, m_args, cb_request, cb_args, &req_args); } -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_notifications_clear(mastodont_t* data, + struct mstdnt_args* m_args, + mstdnt_request_cb_t cb_request, + void* cb_args) { struct mstdnt_request_args req_args = { - storage, - "api/v1/notifications/clear", - NULL, 0, - NULL, 0, - CURLOPT_POST, - NULL, - NULL, - NULL, + .url = "api/v1/notifications/clear", + .request_type = CURLOPT_POST, }; return mstdnt_request(data, m_args, cb_request, cb_args, &req_args); } -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_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_query_param params[] = { - { _MSTDNT_QUERY_STRING, "id", { .s = args ? args->id : NULL } }, - { _MSTDNT_QUERY_STRING, "max_id", { .s = args ? args->max_id : NULL } }, + { _MSTDNT_QUERY_STRING, "id", { .s = args.id } }, + { _MSTDNT_QUERY_STRING, "max_id", { .s = args.max_id } }, }; struct mstdnt_request_args req_args = { - storage, - "api/v1/pleroma/notifications/read", - NULL, 0, - params, _mstdnt_arr_len(params), - CURLOPT_POST, - NULL, - notification, - mstdnt_notification_json_callback, + .url = "api/v1/pleroma/notifications/read", + .params_post = params, + .params_post_len = _mstdnt_arr_len(params), + .request_type = CURLOPT_POST, + .callback = mstdnt_notification_json_callback, }; return mstdnt_request(data, m_args, cb_request, cb_args, &req_args); } - -void mstdnt_cleanup_notifications(struct mstdnt_notification* notifs, size_t notifs_len) +void +mstdnt_cleanup_notifications(struct mstdnt_notifications* notifs) { size_t i; if (!notifs) return; - for (i = 0; i < notifs_len; ++i) - mstdnt_cleanup_notification(notifs + i); + for (i = 0; i < notifs->len; ++i) + mstdnt_cleanup_notification(notifs->notifs + i); - mstdnt_free(notifs); + mstdnt_free(notifs->notifs); } -void mstdnt_cleanup_notification(struct mstdnt_notification* notif) +void +mstdnt_cleanup_notification(struct mstdnt_notification* notif) { if (notif->account) { @@ -252,13 +236,14 @@ void mstdnt_cleanup_notification(struct mstdnt_notification* notif) } if (notif->status) { - mstdnt_cleanup_status(notif->status, 0); + mstdnt_cleanup_status(notif->status); mstdnt_free(notif->status); } mstdnt_free(notif->pleroma); } -const char* mstdnt_notification_t_to_str(mstdnt_notification_t type) +const char* +mstdnt_notification_t_to_str(mstdnt_notification_t type) { switch (type) { diff --git a/src/search.c b/src/search.c index 8893bd5..49354b0 100644 --- a/src/search.c +++ b/src/search.c @@ -22,7 +22,8 @@ #include #include -static const char* type_to_string(enum mstdnt_search_type type) +static const char* +type_to_string(enum mstdnt_search_type type) { switch (type) { @@ -33,7 +34,9 @@ static const char* type_to_string(enum mstdnt_search_type type) } } -int mstdnt_search_json(struct mstdnt_search_results* search_results, cJSON* root) +int +mstdnt_search_json(struct mstdnt_search_results* search_results, + cJSON* root) { // Not many items here, just use cJSON_GetObjectItemCaseSensitive() instead cJSON* statuses = cJSON_GetObjectItemCaseSensitive(root, "statuses"); @@ -41,67 +44,72 @@ int mstdnt_search_json(struct mstdnt_search_results* search_results, cJSON* root cJSON* hashtags = cJSON_GetObjectItemCaseSensitive(root, "hashtags"); // Statuses - mstdnt_statuses_json(&(search_results->statuses), - &(search_results->statuses_len), + mstdnt_statuses_json(&(search_results->statuses->statuses), + &(search_results->statuses->len), statuses); - mstdnt_accounts_json(&(search_results->accts), - &(search_results->accts_len), + mstdnt_accounts_json(&(search_results->accts->accts), + &(search_results->accts->len), accounts); - mstdnt_tags_json(&(search_results->tags), - &(search_results->tags_len), - hashtags); + // TODO finish tags + /* mstdnt_tags_json(&(search_results->tags->tags), */ + /* &(search_results->tags->len), */ + /* hashtags); */ return 0; } -int mstdnt_search_json_callback(cJSON* json, void* _args) +int +mstdnt_search_json_callback(cJSON* json, + void* args, + mstdnt_request_cb_data* data) { - return mstdnt_search_json(_args, json); + struct mstdnt_search_results* results = malloc(sizeof(struct mstdnt_search_results)); + data->data = results; + data->data_free_cb = (mstdnt_data_free_cb_t)mstdnt_cleanup_search_results; + return mstdnt_search_json(results, json); } -int mstdnt_search(mastodont_t* data, - struct mstdnt_args* m_args, -mstdnt_request_cb_t cb_request, -void* cb_args, - char* query, - struct mstdnt_storage* storage, - struct mstdnt_search_args* args, - struct mstdnt_search_results* results) +int +mstdnt_search(mastodont_t* data, + struct mstdnt_args* m_args, + mstdnt_request_cb_t cb_request, + void* cb_args, + char* query, + struct mstdnt_search_args args) { struct _mstdnt_query_param params[] = { - { _MSTDNT_QUERY_STRING, "max_id", { .s = args->max_id } }, - { _MSTDNT_QUERY_STRING, "min_id", { .s = args->min_id } }, - { _MSTDNT_QUERY_STRING, "since_id", { .s = args->since_id } }, + { _MSTDNT_QUERY_STRING, "max_id", { .s = args.max_id } }, + { _MSTDNT_QUERY_STRING, "min_id", { .s = args.min_id } }, + { _MSTDNT_QUERY_STRING, "since_id", { .s = args.since_id } }, { _MSTDNT_QUERY_STRING, "q", { .s = query } }, - { _MSTDNT_QUERY_STRING, "max_id", { .s = args->max_id } }, - { _MSTDNT_QUERY_STRING, "type", { .s = (char*)type_to_string(args->type) } }, - { _MSTDNT_QUERY_INT, "resolve", { .i = args->resolve } }, - { _MSTDNT_QUERY_INT, "following", { .i = args->following } }, - { _MSTDNT_QUERY_INT, "with_relationships", { .i = args->with_relationships } }, - { _MSTDNT_QUERY_INT, "limit", { .i = args->limit } }, - { _MSTDNT_QUERY_INT, "offset", { .i = args->offset } }, + { _MSTDNT_QUERY_STRING, "max_id", { .s = args.max_id } }, + { _MSTDNT_QUERY_STRING, "type", { .s = (char*)type_to_string(args.type) } }, + { _MSTDNT_QUERY_INT, "resolve", { .i = args.resolve } }, + { _MSTDNT_QUERY_INT, "following", { .i = args.following } }, + { _MSTDNT_QUERY_INT, "with_relationships", { .i = args.with_relationships } }, + { _MSTDNT_QUERY_INT, "limit", { .i = args.limit } }, + { _MSTDNT_QUERY_INT, "offset", { .i = args.offset } }, }; struct mstdnt_request_args req_args = { - storage, - "api/v2/search", - params, _mstdnt_arr_len(params), - NULL, 0, - CURLOPT_HTTPGET, - NULL, - results, - mstdnt_search_json_callback + .url = "api/v2/search", + .params_query = params, + .params_query_len = _mstdnt_arr_len(params), + .request_type = CURLOPT_HTTPGET, + .callback = mstdnt_search_json_callback }; - + return mstdnt_request(data, m_args, cb_request, cb_args, &req_args); } -void mstdnt_cleanup_search_results(struct mstdnt_search_results* res) +void +mstdnt_cleanup_search_results(struct mstdnt_search_results* res) { if (!res) return; - mstdnt_cleanup_accounts(res->accts, res->accts_len); - mstdnt_cleanup_statuses(res->statuses, res->statuses_len); - mstdnt_cleanup_tags(res->tags, res->tags_len); + mstdnt_cleanup_accounts(res->accts); + mstdnt_cleanup_statuses(res->statuses); + // TODO + //mstdnt_cleanup_tags(res->tags, res->tags_len); } diff --git a/src/status.c b/src/status.c index 5ce3216..e86e298 100644 --- a/src/status.c +++ b/src/status.c @@ -146,7 +146,7 @@ mstdnt_status_json_callback(cJSON* json, struct mstdnt_status* status = malloc(sizeof(struct mstdnt_status)); data->data = status; - data->data_free_cb = (data_free_cb_t)mstdnt_cleanup_status; + data->data_free_cb = (mstdnt_data_free_cb_t)mstdnt_cleanup_status; return mstdnt_status_json(status, json->child); } @@ -160,7 +160,7 @@ mstdnt_statuses_json_callback(cJSON* json, { struct mstdnt_statuses* statuses = malloc(sizeof(struct mstdnt_statuses)); data->data = statuses; - data->data_free_cb = (data_free_cb_t)mstdnt_cleanup_statuses; + data->data_free_cb = (mstdnt_data_free_cb_t)mstdnt_cleanup_statuses; return mstdnt_statuses_json(&(statuses->statuses), &(statuses->len), json); }