Refactor json code; account info
FossilOrigin-Name: 51b7efb786dd9f079897888ade3ce7de78177c52bce7f0e3ec86288b40daa804
This commit is contained in:
parent
b2535569fe
commit
0cde2418cc
6 changed files with 177 additions and 33 deletions
|
@ -15,13 +15,43 @@
|
|||
|
||||
#ifndef MASTODONT_ACCOUNT
|
||||
#define MASTODONT_ACCOUNT
|
||||
#include "mastodont_types.h"
|
||||
#include <cjson/cJSON.h>
|
||||
|
||||
struct mstdnt_account
|
||||
{
|
||||
int i;
|
||||
char* id;
|
||||
char* username;
|
||||
char* acct;
|
||||
char* url;
|
||||
|
||||
/* Display attributes */
|
||||
char* display_name;
|
||||
char* note;
|
||||
char* avatar;
|
||||
char* avatar_static;
|
||||
char* header;
|
||||
char* header_static;
|
||||
mstdnt_bool locked;
|
||||
mstdnt_bool discoverable;
|
||||
|
||||
/* Statistic attributes */
|
||||
char* created_at;
|
||||
char* last_status_at;
|
||||
unsigned statuses_count;
|
||||
unsigned followers_count;
|
||||
unsigned following_count;
|
||||
|
||||
/* Optional attributes */
|
||||
struct mstdnt_account* moved;
|
||||
/* struct mstdnt_field* field; */
|
||||
mstdnt_bool bot;
|
||||
/* struct mstdnt_source* source */
|
||||
mstdnt_bool suspended;
|
||||
char* mute_expires_at;
|
||||
};
|
||||
|
||||
int mstdnt_load_account_from_json(struct mstdnt_status* status, cJSON* js);
|
||||
int mstdnt_load_account_from_json(struct mstdnt_account* status, cJSON* js);
|
||||
|
||||
|
||||
#endif /* MASTODONT_ACCOUNT */
|
||||
|
|
40
include/mastodont_json_helper.h
Normal file
40
include/mastodont_json_helper.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* 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_JSON_HELPER_H
|
||||
#define MASTODONT_JSON_HELPER_H
|
||||
#include "mastodont_types.h"
|
||||
|
||||
#define _mstdnt_arr_len(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||
|
||||
struct _mstdnt_str_val
|
||||
{
|
||||
const char* key;
|
||||
char** key_ptr;
|
||||
};
|
||||
|
||||
struct _mstdnt_bool_val
|
||||
{
|
||||
const char* key;
|
||||
mstdnt_bool* bool_ptr;
|
||||
};
|
||||
|
||||
int _mstdnt_key_val_iter(cJSON* v,
|
||||
struct _mstdnt_str_val* str,
|
||||
size_t str_len,
|
||||
struct _mstdnt_bool_val* bools,
|
||||
size_t bool_len);
|
||||
|
||||
#endif /* MASTODONT_JSON_HELPER_H */
|
|
@ -23,6 +23,7 @@
|
|||
#include "mastodont_account.h"
|
||||
#include "mastodont_emoji.h"
|
||||
#include "mastodont_tag.h"
|
||||
#include "mastodont_account.h"
|
||||
|
||||
/* Status: Complete, not implemented */
|
||||
|
||||
|
|
|
@ -14,12 +14,35 @@
|
|||
*/
|
||||
|
||||
#include <mastodont_account.h>
|
||||
#include <mastodont_json_helper.h>
|
||||
|
||||
int mstdnt_load_account_from_json(struct mstdnt_status* status, cJSON* js)
|
||||
int mstdnt_load_account_from_json(struct mstdnt_account* acct, cJSON* js)
|
||||
{
|
||||
cJSON* v;
|
||||
for (v = js; v; v->next)
|
||||
struct _mstdnt_str_val strings[] = {
|
||||
{ "id", &(acct->id) },
|
||||
{ "username", &(acct->username) },
|
||||
{ "acct", &(acct->acct) },
|
||||
{ "display_name", &(acct->display_name) },
|
||||
{ "created_at", &(acct->created_at) },
|
||||
{ "note", &(acct->note) },
|
||||
{ "url", &(acct->url) },
|
||||
{ "avatar", &(acct->avatar) },
|
||||
{ "avatar_static", &(acct->avatar_static) },
|
||||
{ "header", &(acct->header) },
|
||||
{ "header_static", &(acct->header_static) },
|
||||
{ "last_status_at", &(acct->last_status_at) },
|
||||
{ "mute_expires_at", &(acct->mute_expires_at) }
|
||||
};
|
||||
|
||||
struct _mstdnt_bool_val bools[] = {
|
||||
{ "locked", &(acct->locked) },
|
||||
{ "bot", &(acct->bot) }
|
||||
};
|
||||
|
||||
for (v = js; v; v = v->next)
|
||||
{
|
||||
/* TODO */
|
||||
_mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings),
|
||||
bools, _mstdnt_arr_len(bools));
|
||||
}
|
||||
}
|
||||
|
|
48
src/json_helper.c
Normal file
48
src/json_helper.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* 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_json_helper.h>
|
||||
|
||||
int _mstdnt_key_val_iter(cJSON* v,
|
||||
struct _mstdnt_str_val* str,
|
||||
size_t str_len,
|
||||
struct _mstdnt_bool_val* bools,
|
||||
size_t bool_len)
|
||||
{
|
||||
size_t i;
|
||||
if (str && cJSON_IsString(v))
|
||||
{
|
||||
for (i = 0; i < str_len; ++i)
|
||||
{
|
||||
if (strcmp(str[i].key, v->string) == 0)
|
||||
{
|
||||
*(str[i].key_ptr) = v->valuestring;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (bools && cJSON_IsBool(v))
|
||||
{
|
||||
for (i = 0; i < bool_len; ++i)
|
||||
{
|
||||
if (strcmp(bools[i].key, v->string) == 0)
|
||||
{
|
||||
*(bools[i].bool_ptr) = cJSON_IsTrue(v) ? cJSON_True : cJSON_False;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
58
src/status.c
58
src/status.c
|
@ -14,43 +14,45 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <mastodont_json_helper.h>
|
||||
#include <mastodont_status.h>
|
||||
#include <mastodont_account.h>
|
||||
|
||||
int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js)
|
||||
{
|
||||
cJSON* v;
|
||||
struct _mstdnt_str_val strings[] = {
|
||||
{ "id", &(status->id) },
|
||||
{ "uri", &(status->uri) },
|
||||
{ "created_at", &(status->created_at) },
|
||||
{ "content", &(status->content) },
|
||||
{ "spoiler_text", &(status->spoiler_text) },
|
||||
{ "in_reply_to_id", &(status->in_reply_to_id) },
|
||||
{ "language", &(status->language) },
|
||||
{ "url", &(status->url) },
|
||||
{ "text", &(status->text) },
|
||||
{ "in_reply_to_account_id", &(status->in_reply_to_account_id) }
|
||||
};
|
||||
|
||||
struct _mstdnt_bool_val bools[] = {
|
||||
{ "sensitive", &(status->sensitive) },
|
||||
{ "favourited", &(status->favourited) },
|
||||
{ "reblogged", &(status->reblogged) },
|
||||
{ "muted", &(status->muted) },
|
||||
{ "bookmarked", &(status->bookmarked) },
|
||||
{ "pinned", &(status->pinned) }
|
||||
};
|
||||
|
||||
for (v = js; v; v = v->next)
|
||||
{
|
||||
if (cJSON_IsString(v))
|
||||
if (_mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings),
|
||||
bools, _mstdnt_arr_len(bools)) == 1)
|
||||
{
|
||||
if(strcmp("id", v->string)==0) status->id = v->valuestring;
|
||||
if(strcmp("uri", v->string)==0) status->uri = v->valuestring;
|
||||
if(strcmp("created_at", v->string)==0) status->created_at = v->valuestring;
|
||||
if(strcmp("content", v->string)==0) status->content = v->valuestring;
|
||||
if(strcmp("spoiler_text", v->string)==0) status->spoiler_text = v->valuestring;
|
||||
if(strcmp("in_reply_to_id", v->string)==0) status->in_reply_to_id = v->valuestring;
|
||||
if(strcmp("in_reply_to_account_id", v->string)==0) status->in_reply_to_account_id = v->valuestring;
|
||||
if(strcmp("in_reply_to_id", v->string)==0) status->in_reply_to_id = v->valuestring;
|
||||
if(strcmp("language", v->string)==0) status->language = v->valuestring;
|
||||
if(strcmp("text", v->string)==0) status->text = v->valuestring;
|
||||
if(strcmp("in_reply_to_id", v->string)==0) status->in_reply_to_id = v->valuestring;
|
||||
}
|
||||
else if (cJSON_IsBool(v))
|
||||
{
|
||||
/* It's probably an int 1/0, but for portability reasons I'll use the typedefs */
|
||||
const int val = cJSON_IsTrue(v) ? cJSON_True : cJSON_False;
|
||||
if (strcmp("sensitive", v->string) == 0) status->sensitive = val;
|
||||
if (strcmp("favourited", v->string) == 0) status->favourited = val;
|
||||
if (strcmp("reblogged", v->string) == 0) status->reblogged = val;
|
||||
if (strcmp("muted", v->string) == 0) status->sensitive = val;
|
||||
if (strcmp("bookmarked", v->string) == 0) status->bookmarked = val;
|
||||
if (strcmp("pinned", v->string) == 0) status->pinned = val;
|
||||
}
|
||||
else if (cJSON_IsObject(v))
|
||||
{
|
||||
if (strcmp("account", v->string) == 0)
|
||||
mstdnt_load_account_from_json(&(status->account), v->child);
|
||||
if (cJSON_IsObject(v))
|
||||
{
|
||||
if (strcmp("account", v->string) == 0)
|
||||
mstdnt_load_account_from_json(&(status->account), v->child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue