From 280aa9b9bd5339f55bbb7848cc1881f0db46eb4c Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Thu, 3 Mar 2022 04:06:01 +0000 Subject: [PATCH] Switch arguments to mono struct, Copy paste boilerplate file FossilOrigin-Name: 36574ae42d2f6109be984361b60f71d7541109aad1407517b04f5ea905adbb20 --- include/mastodont_application.h | 27 ++---------- include/mastodont_args.h | 70 ++++++++++++++++++++++++++++++++ include/mastodont_notification.h | 33 ++++++++++++--- include/mastodont_status.h | 37 ++--------------- include/mastodont_timeline.h | 21 +--------- src/notification.c | 58 ++++++++++++++++++++++++++ 6 files changed, 165 insertions(+), 81 deletions(-) create mode 100644 include/mastodont_args.h diff --git a/include/mastodont_application.h b/include/mastodont_application.h index 31c0540..9405e6c 100644 --- a/include/mastodont_application.h +++ b/include/mastodont_application.h @@ -16,8 +16,9 @@ #ifndef MASTODONT_APPLICATION #define MASTODONT_APPLICATION #include "mastodont_types.h" +#include "mastodont_args.h" +#include "mastodont_fetch.h" #include -#include struct mstdnt_application { @@ -26,14 +27,6 @@ struct mstdnt_application char* vapid_key; }; -struct mstdnt_app_register_args -{ - char* client_name; - char* redirect_uris; - char* scopes; - char* website; -}; - struct mstdnt_app { char* id; @@ -55,25 +48,13 @@ struct mstdnt_oauth_token time_t time; }; -struct mstdnt_oauth_token_args -{ - char* grant_type; - char* client_id; - char* client_secret; - char* redirect_uri; - char* scope; - char* code; - char* username; - char* password; -}; - int mastodont_register_app(mastodont_t* data, - struct mstdnt_app_register_args* args, + struct mstdnt_args* args, struct mstdnt_storage* storage, struct mstdnt_app* app); int mastodont_obtain_oauth_token(mastodont_t* data, - struct mstdnt_oauth_token_args* args, + struct mstdnt_args* args, struct mstdnt_storage* storage, struct mstdnt_oauth_token* app); diff --git a/include/mastodont_args.h b/include/mastodont_args.h new file mode 100644 index 0000000..c9fb2eb --- /dev/null +++ b/include/mastodont_args.h @@ -0,0 +1,70 @@ +/* + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODONT_ARGUMENTS_H +#define MASTODONT_ARGUMENTS_H + +/* + * Originally, when the arguments were being designed for each function, + * I found that many REST operations tended to result similar variable names + * under the same types. To reduce the amount of duplicate code, and to even + * allow argument reusing between multiple functions, all the args are put + * into one struct, this makes it quite a large struct, but any machine will + * handle it fine. + */ + +struct mstdnt_args +{ + char* client_name; + char* redirect_uris; + char* scopes; + char* website; + char* grant_type; + char* client_id; + char* client_secret; + char* redirect_uri; + char* scope; + char* code; + char* username; + char* password; + int pinned; + char* tagged; + int with_muted; + int exclude_reblogs; + int exclude_replies; + int offset; + int only_media; + char* max_id; + char* since_id; + char* min_id; + int limit; + char* content_type; + int expires_in; + char* in_reply_to_conversation_id; + char* in_reply_to_id; + char* language; + char** media_ids; + void* poll; /* TODO */ + int preview; + char* scheduled_at; + int sensitive; + char* spoiler_text; + char* status; + char* visibility; + int remote; + int local; +}; + +#endif /* MASTODONT_ARGUMENTS_H */ diff --git a/include/mastodont_notification.h b/include/mastodont_notification.h index 2d05ca0..24fbd7d 100644 --- a/include/mastodont_notification.h +++ b/include/mastodont_notification.h @@ -16,6 +16,8 @@ #ifndef MASTODONT_NOTIFICATION #define MASTODONT_NOTIFICATION #include "mastodont_types.h" +#include "mastodont_account.h" +#include "mastodont_status.h" #include enum mstdnt_notification_type @@ -35,15 +37,36 @@ enum mstdnt_notification_type struct mstdnt_notification { char* id; + char* created_at; + struct mstdnt_account* account; + struct mstdnt_status* status; enum mstdnt_notification_type type; }; -int mastodont_notifications(mastodont_t* data, - struct mstdnt_notification** notifs, - struct mstdnt_storage* storage, - size_t* size); +struct mstdnt_get_notifications_args +{ + char** exclude_types; + size_t exclude_types_len; + char* account_id; + char** exclude_visibilities; + size_t exclude_visibilities_len; + enum mstdnt_notification_type* include_types; + mstdnt_bool with_muted; + char* max_id; + char* min_id; + char* since_id; + int offset; + int limit; +}; + +int mastodont_get_notifications(mastodont_t* data, + struct mstdnt_get_notifications_args* args, + struct mstdnt_storage* storage, + struct mstdnt_notification** notifs, + size_t* size); int mstdnt_load_account_from_json(struct mstdnt_notification* notif, cJSON* js); -void _mstdnt_val_notifications_call(cJSON* v, void* _type); +void mstdnt_cleanup_notifications(struct mstdnt_notification* notif, size_t notif_len); +void mstdnt_cleanup_notification(struct mstdnt_notification* notif); #endif /* MASTODONT_NOTIFICATION */ diff --git a/include/mastodont_status.h b/include/mastodont_status.h index ff48e18..6223bdd 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -18,6 +18,7 @@ #include #include "mastodont_pleroma.h" #include "mastodont_types.h" +#include "mastodont_args.h" #include "mastodont_fetch.h" #include "mastodont_attachment.h" #include "mastodont_application.h" @@ -82,38 +83,6 @@ struct mstdnt_status mstdnt_bool pinned; }; - -struct mstdnt_account_statuses_args { - int pinned; - char* tagged; - int with_muted; - int exclude_reblogs; - int exclude_replies; - int offset; - int only_media; - char* max_id; - char* since_id; - char* min_id; - int limit; -}; - -struct mstdnt_create_status_args -{ - char* content_type; - int expires_in; - char* in_reply_to_conversation_id; - char* in_reply_to_id; - char* language; - char** media_ids; - void* poll; /* TODO */ - int preview; - char* scheduled_at; - int sensitive; - char* spoiler_text; - char* status; - char* visibility; -}; - void cleanup_statuses(struct mstdnt_status* statuses, size_t s); void cleanup_status(struct mstdnt_status* status); @@ -129,7 +98,7 @@ int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js); int mastodont_account_statuses(mastodont_t* data, char* id, - struct mstdnt_account_statuses_args* args, + struct mstdnt_args* args, struct mstdnt_storage* storage, struct mstdnt_status* statuses[], size_t* size); @@ -148,7 +117,7 @@ int mastodont_status_context(mastodont_t* data, size_t* size_after); int mastodont_create_status(mastodont_t* data, - struct mstdnt_create_status_args* args, + struct mstdnt_args* args, struct mstdnt_storage* storage); int mastodont_favourite_status(mastodont_t* data, diff --git a/include/mastodont_timeline.h b/include/mastodont_timeline.h index 34c77d6..30cede0 100644 --- a/include/mastodont_timeline.h +++ b/include/mastodont_timeline.h @@ -18,32 +18,15 @@ #include #include -struct mstdnt_timeline_public_args { - int local; - int remote; - int only_media; - char* max_id; - char* since_id; - char* min_id; - int limit; -}; - -struct mstdnt_timeline_list_args { - char* max_id; - char* since_id; - char* min_id; - int limit; -}; - int mastodont_timeline_list(mastodont_t* data, char* list_id, - struct mstdnt_timeline_list_args* args, + struct mstdnt_args* args, struct mstdnt_storage* storage, struct mstdnt_status* statuses[], size_t* size); int mastodont_timeline_public(mastodont_t* data, - struct mstdnt_timeline_public_args* args, + struct mstdnt_args* args, struct mstdnt_storage* storage, struct mstdnt_status* statuses[], size_t* statuses_size); diff --git a/src/notification.c b/src/notification.c index c99eb2e..b42e7e4 100644 --- a/src/notification.c +++ b/src/notification.c @@ -15,3 +15,61 @@ #include +int mastodont_get_notifications(mastodont_t* data, + struct mstdnt_get_notifications_args* args, + struct mstdnt_storage* storage, + struct mstdnt_notification** notifs, + size_t* size) +{ + int res = 0; + struct mstdnt_fetch_results results = { 0 }; + + /* Default args */ + storage->needs_cleanup = 0; + + union param_value + u_account_id = args->account_id, + u_with_muted = args->with_muted, + u_max_id = args->max_id, + u_min_id = args->min_id, + u_since_id = args->since_id, + u_offset = args->offset, + u_limit = args->limit; + + 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* post = _mstdnt_query_string(NULL, params, _mstdnt_arr_len(params)); + + curl_easy_setopt(data->curl, CURLOPT_POSTFIELDS, post); + + if (mastodont_fetch_curl(data, "api/v1/apps", &results, CURLOPT_POST) != CURLE_OK) + { + res = 1; + goto cleanup; + } +/* + if (mstdnt_check_error(&results, storage)) + { + res = 1; + goto cleanup_fetch; + } +*/ + + res = mstdnt_read_app_result(storage, &results, app); + +cleanup_fetch: + mastodont_fetch_results_cleanup(&results); +cleanup: + free(post); + return res; +} + +int mstdnt_load_account_from_json(struct mstdnt_notification* notif, cJSON* js) +{ + +}