From 15f1142186f1d10dd91453a36a58c2460963540d Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Tue, 5 Apr 2022 03:10:21 +0000 Subject: [PATCH] Array queries FossilOrigin-Name: 5186e5190405c1c90f0590d20b94512a346dee595e1d324e1451de5ccc3d909c --- include/mastodont.h | 1 + include/mastodont_query.h | 10 +++++++- include/mastodont_relationship.h | 1 + src/query.c | 40 ++++++++++++++++++++++++++++---- src/relationship.c | 12 ++++++++-- 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/include/mastodont.h b/include/mastodont.h index 64b9973..65ba5ee 100644 --- a/include/mastodont.h +++ b/include/mastodont.h @@ -21,6 +21,7 @@ #include #include #include +#include /* Functions required form curl */ void mastodont_global_curl_init(); diff --git a/include/mastodont_query.h b/include/mastodont_query.h index 072b0a4..7ad79c6 100644 --- a/include/mastodont_query.h +++ b/include/mastodont_query.h @@ -21,7 +21,14 @@ enum _mstdnt_query_type { _MSTDNT_QUERY_STRING, - _MSTDNT_QUERY_INT + _MSTDNT_QUERY_INT, + _MSTDNT_QUERY_ARRAY +}; + +struct _mstdnt_query_array +{ + char** arr; + size_t arr_len; }; struct _mstdnt_query_param @@ -31,6 +38,7 @@ struct _mstdnt_query_param union param_value { char* s; int i; + struct _mstdnt_query_array a; } value; }; diff --git a/include/mastodont_relationship.h b/include/mastodont_relationship.h index a8936c7..4dfb10d 100644 --- a/include/mastodont_relationship.h +++ b/include/mastodont_relationship.h @@ -50,6 +50,7 @@ int mstdnt_relationships_result(struct mstdnt_fetch_results* results, int _mstdnt_relationships_result_callback(struct mstdnt_fetch_results* results, struct mstdnt_storage* storage, + void* _args); int mastodont_get_relationships(mastodont_t* data, diff --git a/src/query.c b/src/query.c index 480b7c2..985bbf0 100644 --- a/src/query.c +++ b/src/query.c @@ -53,11 +53,27 @@ char* _mstdnt_query_string(mastodont_t* data, /* We'll call them res to represent the query parameters */ int res_count = 0; + size_t arr_ind = 0; + char* key_ptr; + /* If it's an array, we treat it as a "fake" regular key and keep iterating through it */ for (i = 0; i < param_len; ++i) { escape_str = NULL; - if (params[i].key && + + /* Start up array */ + if (params[i].type == _MSTDNT_QUERY_ARRAY && arr_ind == 0) + { + size_t str_s = strlen(params[i].key); + key_ptr = malloc(str_s+3); /* 2 "[]" + 1 \0 */ + strcpy(key_ptr, params[i].key); + strcpy(key_ptr+str_s, "[]"); + } + + if (params[i].type != _MSTDNT_QUERY_ARRAY) + key_ptr = params[i].key; + + if (key_ptr && !(params[i].type == _MSTDNT_QUERY_STRING && params[i].value.s == NULL)) { @@ -72,6 +88,10 @@ char* _mstdnt_query_string(mastodont_t* data, snprintf(conv_val, CONV_SIZE, "%d", params[i].value.i); val_ptr = conv_val; } + else if (params[i].type == _MSTDNT_QUERY_ARRAY) + { + val_ptr = params[i].value.a.arr[arr_ind]; + } else /* Point to it, it's a string */ { /* First, let's encode it */ @@ -81,7 +101,7 @@ char* _mstdnt_query_string(mastodont_t* data, } /* Get lengths */ - key_len = strlen(params[i].key); + key_len = strlen(key_ptr); val_len = strlen(val_ptr); res_prev = res_len; @@ -99,15 +119,27 @@ char* _mstdnt_query_string(mastodont_t* data, else result[res_len] = '\0'; /* Copy over strings (skip & sign ;; +1) */ - strcpy(result + res_prev, params[i].key); + strcpy(result + res_prev, key_ptr); result[res_prev + key_len] = '='; strcpy(result + res_prev + 1 + key_len, val_ptr); /* Only free if flag is set, meaning it needs to be free'd */ if (!MSTDNT_T_FLAG_ISSET(data, MSTDNT_FLAG_NO_URI_SANITIZE)) curl_free(escape_str); } + + ++arr_ind; + /* Finish array stuff */ + if (params[i].type == _MSTDNT_QUERY_ARRAY) + if (arr_ind >= params[i].value.a.arr_len) + { + arr_ind = 0; + free(key_ptr); + } + else { + ++arr_ind; + --i; /* Flip flop i */ + } } - return result; } diff --git a/src/relationship.c b/src/relationship.c index 7c23191..b91344e 100644 --- a/src/relationship.c +++ b/src/relationship.c @@ -16,6 +16,7 @@ #include #include #include +#include #define FLAG_ARG(flag) { &(relationship->flags), flag } @@ -120,13 +121,20 @@ int mastodont_get_relationships(mastodont_t* data, { /* struct _mstdnt_query_param params[] = { 0 }; */ struct _mstdnt_relationships_cb_args cb_args = { relationships, size }; + + union param_value u_ids; + u_ids.a.arr = ids; + u_ids.a.arr_len = ids_len; + + struct _mstdnt_query_param params[] = { + { _MSTDNT_QUERY_ARRAY, "id", u_ids } + }; struct mastodont_request_args req_args = { storage, "api/v1/accounts/relationships", + params, _mstdnt_arr_len(params), NULL, 0, - NULL, 0, - /* params, _mstdnt_arr_len(params), */ CURLOPT_HTTPGET, &cb_args, _mstdnt_relationships_result_callback