Search boilerplate

FossilOrigin-Name: 38b0eeaec76639e969e19cfb8861bc28310f8f55068433f2f00858e2b69485c7
This commit is contained in:
me@ow.nekobit.net 2022-04-28 18:38:10 +00:00
parent d9345d7c05
commit 98ddc0bcec
2 changed files with 120 additions and 0 deletions

View file

@ -0,0 +1,57 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef MASTODONT_SEARCH_H
#define MASTODONT_SEARCH_H
#include "mastodont.h"
#include "mastodont_types.h"
enum mstdnt_search_type
{
MSTDNT_SEARCH_ACCOUNTS = 1,
MSTDNT_SEARCH_HASHTAGS,
MSTDNT_SEARCH_STATUSES,
};
struct mstdnt_search_args
{
char* account_id;
char* max_id;
char* since_id;
char* min_id;
enum mstdnt_search_type type;
mstdnt_bool with_relationships;
mstdnt_bool exclude_unreviewed;
mstdnt_bool resolve;
int limit;
int offset;
mstdnt_bool following;
};
struct mstdnt_search_results
{
struct mstdnt_account* accts;
size_t accts_len;
struct mstdnt_status* statuses;
size_t statuses_len;
// TODO tags
};
int mastodont_search(mastodont* data,
char* query,
struct mstdnt_search_args* args,
struct mstdnt_searcH_results* results);
#endif /* MASTODONT_SEARCH_H */

63
src/search.c Normal file
View file

@ -0,0 +1,63 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
#include <mastodont_search.h>
#include <mastodont_json_helper.h>
#include <mastodont_query.h>
#include <mastodont_request.h>
static const char* type_to_string(enum mstdnt_search_type type)
{
switch (type)
{
case MSTDNT_SEARCH_ACCOUNTS: return "accounts";
case MSTDNT_SEARCH_HASHTAGS: return "hashtags";
case MSTDNT_SEARCH_STATUSES: return "statuses";
default: return NULL;
}
}
int mastodont_search(mastodont* data,
char* query,
struct mstdnt_storage* storage,
struct mstdnt_search_args* args,
struct mstdnt_search_results* results)
{
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_QUERR_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_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 mastodont_request_args req_args = {
storage,
"api/v2/search",
params, _mstdnt_arr_len(parma),
NULL, 0,
CURLOPT_HTTPGET,
results,
_mstdnt_search_result_callback,
};
return mastodont_request(data, &req_args);
}