Hashtags json and search

FossilOrigin-Name: d0cc6075d95805c39199f035f7763348469795df8493a28472595553e89a6d50
This commit is contained in:
me@ow.nekobit.net 2022-05-06 04:58:19 +00:00
parent 14d610683d
commit 7e40d4ac35
8 changed files with 145 additions and 7 deletions

View file

@ -15,6 +15,7 @@
#ifndef MASTODONT_HISTORY
#define MASTODONT_HISTORY
#include <cjson/cJSON.h>
#include <time.h>
struct mstdnt_history
@ -24,5 +25,8 @@ struct mstdnt_history
unsigned accounts;
};
int mstdnt_history_json(struct mstdnt_history* tag, cJSON* js);
int mstdnt_histories_json(struct mstdnt_history* tags[], size_t* array_size, cJSON* js);
void _mstdnt_val_histories_call(cJSON* v, void* _type);
#endif /* MASTODONT_TAG */

View file

@ -38,6 +38,7 @@ int _mstdnt_json_init(cJSON** root,
int _mstdnt_key_val_ref(cJSON* v, struct _mstdnt_val_ref* refs,
size_t refs_len);
void _mstdnt_val_string_unix_call(cJSON* v, void* _type);
void _mstdnt_val_string_call(cJSON* v, void* _type);
void _mstdnt_val_bool_call(cJSON* v, void* _type);
void _mstdnt_val_uint_call(cJSON* v, void* _type);

View file

@ -15,15 +15,20 @@
#ifndef MASTODONT_TAG
#define MASTODONT_TAG
#include <stdint.h>
#include "mastodont_history.h"
struct mstdnt_tag
{
char* name;
char* url;
struct mstdnt_history history[];
struct mstdnt_history* history;
size_t history_len;
};
void load_tag_json(struct mstdnt_tag* tag, cJSON* emo_json);
int mstdnt_tag_json(struct mstdnt_tag* tag, cJSON* emo_json);
int mstdnt_tags_json(struct mstdnt_tag* array[], size_t* array_size, cJSON* js);
void mstdnt_cleanup_tag(struct mstdnt_tag* tag);
void mstdnt_cleanup_tags(struct mstdnt_tag* tags, size_t s);
#endif /* MASTODONT_TAG */

View file

@ -20,6 +20,7 @@
#include <mastodont_request.h>
#include <mastodont_json_helper.h>
#include <mastodont_generate.h>
void _mstdnt_val_account_call(cJSON* v, void* _type)
{
struct mstdnt_account* type = _type;

66
src/history.c Normal file
View file

@ -0,0 +1,66 @@
/*
* 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 <string.h>
#include <mastodont_history.h>
#include <mastodont_json_helper.h>
#include <mastodont_generate.h>
int mstdnt_history_json(struct mstdnt_history* hist, cJSON* js)
{
memset(hist, 0, sizeof(struct mstdnt_history));
struct _mstdnt_val_ref refs[] = {
{ "day", &(hist->day), _mstdnt_val_string_unix_call },
{ "uses", &(hist->uses), _mstdnt_val_uint_call },
{ "accounts", &(hist->accounts), _mstdnt_val_uint_call },
};
for (cJSON* v = js; v; v = v->next)
_mstdnt_key_val_ref(v, refs, _mstdnt_arr_len(refs));
return 0;
}
// Generated function
GENERATE_JSON_ARRAY_FUNC(mstdnt_histories_json, struct mstdnt_history, mstdnt_history_json)
void _mstdnt_val_histories_call(cJSON* v, void* _type)
{
struct _mstdnt_generic_args* args = _type;
struct mstdnt_history** hist = args->arg;
cJSON* v_array = v->child;
cJSON* att = NULL;
size_t size = cJSON_GetArraySize(v);
*(args->size) = size;
/* No attachments, ignore */
if (size == 0)
{
*hist = NULL;
return;
}
*hist = calloc(1, sizeof(struct mstdnt_history) * size);
if (*hist == NULL)
return;
cJSON* it;
int i;
for (it = v_array, i = 0; it; (++i, it = it->next))
{
mstdnt_history_json((*hist) + i, it->child);
}
}

View file

@ -13,9 +13,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <mastodont_json_helper.h>
// UNIX timestamp, above but I don't care
#define TIMESTAMP_LEN 16
int _mstdnt_json_init(cJSON** root,
struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage)
@ -43,6 +48,22 @@ int _mstdnt_key_val_ref(cJSON* v, struct _mstdnt_val_ref* refs,
return 1;
}
void _mstdnt_val_string_unix_call(cJSON* v, void* _type)
{
long conv;
time_t* type = _type;
char* endptr;
if (!cJSON_IsString(v))
{
*type = 0;
return;
}
// Convert string to long
conv = strtol(v->valuestring, &endptr, 10);
*type = v->valuestring != *endptr ? conv : 0;
}
void _mstdnt_val_string_call(cJSON* v, void* _type)
{
char** type = _type;

View file

@ -20,7 +20,7 @@
#include <mastodont_request.h>
#include <mastodont_status.h>
#include <mastodont_account.h>
#include <mastodont_tag.h>
static const char* type_to_string(enum mstdnt_search_type type)
{
switch (type)
@ -53,6 +53,10 @@ int mstdnt_search_from_result(struct mstdnt_storage* storage,
mstdnt_accounts_json(&(search_results->accts),
&(search_results->accts_len),
accounts);
mstdnt_tags_json(&(search_results->accts),
&(search_results->accts_len),
accounts);
return 0;
}

View file

@ -13,13 +13,49 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
void load_tag_json(struct mstdnt_tag* tag, cJSON* js)
{
cJSON* v;
#include <string.h>
#include <stdlib.h>
#include <mastodont_tag.h>
#include <mastodont_history.h>
#include <mastodont_generate.h>
#include <mastodont_json_helper.h>
int mstdnt_tag_json(struct mstdnt_tag* tag, cJSON* js)
{
struct _mstdnt_generic_args history_args = {
.arg = tag->history,
.size = &(tag->history_len)
};
memset(tag, 0, sizeof(struct mstdnt_tag));
struct _mstdnt_val_ref refs[] = {
{ "name", &(tag->name), _mstdnt_val_string_call },
{ "url", &(tag->url), _mstdnt_val_string_call },
{ "history", &history_args, _mstdnt_val_histories_call },
};
for (cJSON* v = js; v; v = v->next)
_mstdnt_key_val_ref(v, refs, _mstdnt_arr_len(refs));
return 0;
}
// Generated function
GENERATE_JSON_ARRAY_FUNC(mstdnt_tags_json, struct mstdnt_tag, mstdnt_tag_json)
void mstdnt_cleanup_tag(struct mstdnt_tag* tag)
{
if (tag->history)
free(tag->history);
}
void mstdnt_cleanup_tags(struct mstdnt_tag* tags, size_t s)
{
if (!tags) return;
for (size_t i = 0; i < s; ++i)
{
mstdnt_cleanup_tag(tags + i);
}
free(tags);
}