Statuses search

FossilOrigin-Name: b2ec899b448b0bbebca4fd8786b8a6a3c1610a6c5b1628ebaa1206afff268781
This commit is contained in:
me@ow.nekobit.net 2022-05-04 02:34:02 +00:00
parent cf00b3c003
commit f928ae9794

View file

@ -13,10 +13,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <mastodont_search.h>
#include <mastodont_json_helper.h>
#include <mastodont_query.h>
#include <mastodont_request.h>
#include <mastodont_status.h>
#include <mastodont_account.h>
static const char* type_to_string(enum mstdnt_search_type type)
{
@ -29,6 +32,43 @@ static const char* type_to_string(enum mstdnt_search_type type)
}
}
int mstdnt_search_from_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_search_results* search_results)
{
size_t i = 0;
size_t arr_len = 0;
size_t size = 0;
cJSON* root, *item;
if (_mstdnt_json_init(&root, results, storage))
return 1;
// Not many items here, just use cJSON_GetObjectItemCaseSensitive() instead
cJSON* statuses = cJSON_GetObjectItemCaseSensitive(root, "statuses");
cJSON* accounts = cJSON_GetObjectItemCaseSensitive(root, "accounts");
cJSON* hashtags = cJSON_GetObjectItemCaseSensitive(root, "hashtags");
// Statuses
if (statuses && cJSON_IsArray(statuses) && (size = cJSON_GetArraySize(statuses)))
{
search_results->statuses = calloc(1, size * sizeof(struct mstdnt_status));
search_results->statuses_len = size;
cJSON_ArrayForEach(item, statuses)
{
mstdnt_status_from_json(search_results->statuses + arr_len++, item->child);
}
}
return 0;
}
int _mstdnt_search_result_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* _args)
{
return mstdnt_search_from_result(storage, results, _args);
}
int mastodont_search(mastodont_t* data,
char* query,
struct mstdnt_storage* storage,
@ -41,7 +81,7 @@ int mastodont_search(mastodont_t* data,
{ _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 = type_to_string(args->type) } },
{ _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 } },
@ -56,8 +96,7 @@ int mastodont_search(mastodont_t* data,
NULL, 0,
CURLOPT_HTTPGET,
results,
/* _mstdnt_search_result_callback, */
NULL
_mstdnt_search_result_callback
};
return mastodont_request(data, &req_args);