Node info instance info date time

FossilOrigin-Name: 7daf6486a90f5ef1cee84efc7df496461f195b26e7c7cc91f66e40e092d510bb
This commit is contained in:
nekobit 2022-05-22 06:04:44 +00:00
parent f2f9851015
commit 39b9689939
8 changed files with 249 additions and 2 deletions

View File

@ -0,0 +1,29 @@
/*
* 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_INSTANCE_H
#define MASTODONT_INSTANCE_H
#include "mastodont_types.h"
struct mstdnt_instance
{
int a;
};
// TODO
int mastodont_instance(mastodont_t* api,
struct mstdnt_instance* inst);
#endif /* MASTODONT_INSTANCE_H */

View File

@ -45,6 +45,7 @@ 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);
void _mstdnt_val_sint_call(cJSON* v, void* _type);
void _mstdnt_val_datetime_unix_call(cJSON* v, void* _type);
/* DEPRECATED */
struct _mstdnt_str_val

View File

@ -0,0 +1,51 @@
/*
* 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_NODEINFO_H
#define MASTODONT_NODEINFO_H
#include <mastodont_types.h>
struct mstdnt_nodeinfo_metadata
{
char* version;
};
struct mstdnt_nodeinfo_software
{
char* name;
char* repository;
char* version;
};
struct mstdnt_nodeinfo
{
struct mstdnt_nodeinfo_software* software;
struct mstdnt_nodeinfo_metadata* metadata;
mstdnt_bool open_registrations;
};
int mstdnt_nodeinfo_json(struct mstdnt_nodeinfo* nodeinfo, cJSON* js);
int mstdnt_nodeinfo_json_callback(cJSON* json, void* nodeinfo);
int mastodont_get_nodeinfo(mastodont_t* api,
char* version,
struct mstdnt_storage* storage,
struct mstdnt_nodeinfo* nodeinfo);
void mstdnt_cleanup_nodeinfo(struct mstdnt_nodeinfo* nodeinfo);
#endif /* MASTODONT_NODEINFO_H */

View File

@ -15,6 +15,7 @@
#ifndef MASTODONT_STATUS
#define MASTODONT_STATUS
#include <time.h>
#include <cjson/cJSON.h>
#include "mastodont_pleroma.h"
#include "mastodont_types.h"
@ -35,7 +36,7 @@ struct mstdnt_status
{
char* id;
char* uri;
char* created_at;
time_t created_at;
struct mstdnt_account account;
char* content;
char* visibility;

23
src/instance.c Normal file
View File

@ -0,0 +1,23 @@
/*
* 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_instance.h>
int mastodont_instance(mastodont_t* api,
struct mstdnt_instance* inst)
{
return -1;
}

View File

@ -64,6 +64,25 @@ void _mstdnt_val_string_unix_call(cJSON* v, void* _type)
*type = v->valuestring != endptr ? conv : 0;
}
void _mstdnt_val_datetime_unix_call(cJSON* v, void* _type)
{
struct tm conv_time = { 0 };
time_t* type = _type;
if (sscanf(v->valuestring, "%d-%d-%dT%d:%d:%d.000Z",
&conv_time.tm_year - 1900,
&conv_time.tm_mon,
&conv_time.tm_mday,
&conv_time.tm_hour,
&conv_time.tm_min,
&conv_time.tm_sec) == 6)
{
*type = mktime(&conv_time);
}
else
*type = 0; // 70's, baby!
}
// Fuck you Gargron
void _mstdnt_val_string_uint_call(cJSON* v, void* _type)
{

123
src/nodeinfo.c Normal file
View File

@ -0,0 +1,123 @@
/*
* 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_nodeinfo.h>
#include <stdlib.h>
#include <string.h>
#include <mastodont_request.h>
#include <mastodont_json_helper.h>
static void _mstdnt_val_software_malloc_call(cJSON* v, void* _type)
{
struct mstdnt_nodeinfo_software** type = _type;
*type = calloc(1, sizeof(struct mstdnt_nodeinfo_software));
// Do json stuff in here, it's only done once
if (*type)
{
struct mstdnt_nodeinfo_software* soft = *type;
// Zero
memset(soft, 0, sizeof(struct mstdnt_nodeinfo_software));
struct _mstdnt_val_ref refs[] = {
{ "name", &(soft->name), _mstdnt_val_string_call },
{ "repository", &(soft->repository), _mstdnt_val_string_call },
{ "version", &(soft->version), _mstdnt_val_string_call },
};
for (cJSON* w = v->child; w; w = w->next)
_mstdnt_key_val_ref(w, refs, _mstdnt_arr_len(refs));
}
}
static void _mstdnt_val_metadata_malloc_call(cJSON* v, void* _type)
{
struct mstdnt_nodeinfo_metadata** type = _type;
*type = calloc(1, sizeof(struct mstdnt_nodeinfo_metadata));
// Do json stuff in here, it's only done once
if (*type)
{
struct mstdnt_nodeinfo_metadata* soft = *type;
// Zero
memset(soft, 0, sizeof(struct mstdnt_nodeinfo_metadata));
struct _mstdnt_val_ref refs[] = {
// TODO
{ "version", &(soft->version), _mstdnt_val_string_call },
};
for (cJSON* w = v->child; w; w = w->next)
_mstdnt_key_val_ref(w, refs, _mstdnt_arr_len(refs));
}
}
int mstdnt_nodeinfo_json(struct mstdnt_nodeinfo* nodeinfo, cJSON* js)
{
// Zero out
memset(nodeinfo, 0, sizeof(struct mstdnt_nodeinfo));
struct _mstdnt_val_ref refs[] = {
{ "software", &(nodeinfo->software), _mstdnt_val_software_malloc_call },
{ "metadata", &(nodeinfo->metadata), _mstdnt_val_metadata_malloc_call },
{ "openRegistrations", &(nodeinfo->open_registrations), _mstdnt_val_bool_call },
// TODO
};
for (cJSON* v = js; v; v = v->next)
{
_mstdnt_key_val_ref(v, refs, _mstdnt_arr_len(refs));
}
return 0;
}
int mstdnt_nodeinfo_json_callback(cJSON* json, void* nodeinfo)
{
return mstdnt_nodeinfo_json(nodeinfo, json);
}
int mastodont_get_nodeinfo(mastodont_t* api,
char* version,
struct mstdnt_storage* storage,
struct mstdnt_nodeinfo* nodeinfo)
{
char url[MSTDNT_URLSIZE];
snprintf(url, MSTDNT_URLSIZE, "nodeinfo/%s.json", version ? version : "2.1");
struct mastodont_request_args req_args = {
storage,
url,
NULL, 0,
NULL, 0,
CURLOPT_HTTPGET,
nodeinfo,
mstdnt_nodeinfo_json_callback
};
return mastodont_request(api, &req_args);
}
void mstdnt_cleanup_nodeinfo(struct mstdnt_nodeinfo* nodeinfo)
{
if (!nodeinfo) return;
free(nodeinfo->software);
free(nodeinfo->metadata);
}

View File

@ -69,7 +69,7 @@ int mstdnt_status_json(struct mstdnt_status* status, cJSON* js)
struct _mstdnt_val_ref vals[] = {
{ "id", &(status->id), _mstdnt_val_string_call },
{ "uri", &(status->uri), _mstdnt_val_string_call },
{ "created_at", &(status->created_at), _mstdnt_val_string_call },
{ "created_at", &(status->created_at), _mstdnt_val_datetime_unix_call },
{ "content", &(status->content), _mstdnt_val_string_call },
{ "spoiler_text", &(status->spoiler_text), _mstdnt_val_string_call },
{ "in_reply_to_id", &(status->in_reply_to_id), _mstdnt_val_string_call },