Array queries

FossilOrigin-Name: 5186e5190405c1c90f0590d20b94512a346dee595e1d324e1451de5ccc3d909c
This commit is contained in:
me@ow.nekobit.net 2022-04-05 03:10:21 +00:00
parent c14a1d5ddd
commit 15f1142186
5 changed files with 57 additions and 7 deletions

View file

@ -21,6 +21,7 @@
#include <mastodont_list.h>
#include <mastodont_notification.h>
#include <mastodont_status.h>
#include <mastodont_relationship.h>
/* Functions required form curl */
void mastodont_global_curl_init();

View file

@ -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;
};

View file

@ -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,

View file

@ -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;
}

View file

@ -16,6 +16,7 @@
#include <stdlib.h>
#include <mastodont_relationship.h>
#include <mastodont_json_helper.h>
#include <mastodont_query.h>
#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