From d0149fa7b7bd2a9058088bf3b6de81cb7dfe1074 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Sat, 7 May 2022 02:07:20 +0000 Subject: [PATCH] Hashtag stuff, fix history FossilOrigin-Name: 508e6165907ca50a344fe543c3b466f5d1d7a5ba0424d70831694d410050e78e --- include/mastodont_json_helper.h | 2 ++ include/mastodont_timeline.h | 6 ++++++ src/history.c | 4 ++-- src/json_helper.c | 36 ++++++++++++++++++++++++++++++++- src/tag.c | 2 +- src/timeline.c | 36 +++++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 4 deletions(-) diff --git a/include/mastodont_json_helper.h b/include/mastodont_json_helper.h index d3146a4..8a90904 100644 --- a/include/mastodont_json_helper.h +++ b/include/mastodont_json_helper.h @@ -38,6 +38,8 @@ 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_int_call(cJSON* v, void* _type); +void _mstdnt_val_string_uint_call(cJSON* v, void* _type); 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); diff --git a/include/mastodont_timeline.h b/include/mastodont_timeline.h index d2cd3a6..ababec5 100644 --- a/include/mastodont_timeline.h +++ b/include/mastodont_timeline.h @@ -56,5 +56,11 @@ int mastodont_timeline_direct(mastodont_t* data, struct mstdnt_status* statuses[], size_t* statuses_size); +int mastodont_timeline_tag(mastodont_t* data, + char* hashtag, + struct mstdnt_timeline_args* args, + struct mstdnt_storage* storage, + struct mstdnt_status* statuses[], + size_t* size); #endif /* MASTODONT_TIMELINE_H */ diff --git a/src/history.c b/src/history.c index 1a2273b..5fb1a95 100644 --- a/src/history.c +++ b/src/history.c @@ -24,8 +24,8 @@ int mstdnt_history_json(struct mstdnt_history* hist, cJSON* js) 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 }, + { "uses", &(hist->uses), _mstdnt_val_string_uint_call }, + { "accounts", &(hist->accounts), _mstdnt_val_string_uint_call }, }; for (cJSON* v = js; v; v = v->next) diff --git a/src/json_helper.c b/src/json_helper.c index 7c1e3f3..1a67e99 100644 --- a/src/json_helper.c +++ b/src/json_helper.c @@ -61,7 +61,41 @@ void _mstdnt_val_string_unix_call(cJSON* v, void* _type) // Convert string to long conv = strtol(v->valuestring, &endptr, 10); - *type = v->valuestring != *endptr ? conv : 0; + *type = v->valuestring != endptr ? conv : 0; +} + +// Fuck you Gargron +void _mstdnt_val_string_uint_call(cJSON* v, void* _type) +{ + long conv; + unsigned* 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; +} + +// Again, fuck you Gargron +void _mstdnt_val_string_int_call(cJSON* v, void* _type) +{ + long conv; + int* 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) diff --git a/src/tag.c b/src/tag.c index 4e7128e..4a00da0 100644 --- a/src/tag.c +++ b/src/tag.c @@ -23,7 +23,7 @@ int mstdnt_tag_json(struct mstdnt_tag* tag, cJSON* js) { struct _mstdnt_generic_args history_args = { - .arg = tag->history, + .arg = &(tag->history), .size = &(tag->history_len) }; diff --git a/src/timeline.c b/src/timeline.c index 3ab4d08..1d778ac 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -51,6 +51,42 @@ int mastodont_timeline_list(mastodont_t* data, return mastodont_request(data, &req_args); } +int mastodont_timeline_tag(mastodont_t* data, + char* hashtag, + struct mstdnt_timeline_args* args, + struct mstdnt_storage* storage, + struct mstdnt_status* statuses[], + size_t* size) +{ + struct _mstdnt_statuses_cb_args cb_args = { statuses, size }; + char url[MSTDNT_URLSIZE]; + snprintf(url, MSTDNT_URLSIZE, "api/v1/timelines/tag/%s", hashtag); + + struct _mstdnt_query_param params[] = { + { _MSTDNT_QUERY_STRING, "max_id", { .s = args->max_id } }, + { _MSTDNT_QUERY_STRING, "since_id", { .s = args->since_id } }, + { _MSTDNT_QUERY_STRING, "min_id", { .s = args->min_id } }, + { _MSTDNT_QUERY_INT, "limit", { .i = args->limit } }, + { _MSTDNT_QUERY_INT, "local", { .i = args->local } }, + { _MSTDNT_QUERY_INT, "offset", { .i = args->offset } }, + { _MSTDNT_QUERY_INT, "remote", { .i = args->remote } }, + { _MSTDNT_QUERY_INT, "only_media", { .i = args->remote } }, + { _MSTDNT_QUERY_INT, "with_muted", { .i = args->remote } }, + }; + + struct mastodont_request_args req_args = { + storage, + url, + params, _mstdnt_arr_len(params), + NULL, 0, + CURLOPT_HTTPGET, + &cb_args, + _mstdnt_statuses_result_callback, + }; + + return mastodont_request(data, &req_args); +} + int mastodont_timeline_public(mastodont_t* data, struct mstdnt_timeline_args* args, struct mstdnt_storage* storage,