diff --git a/include/mastodont_status.h b/include/mastodont_status.h index 18709c7..5364306 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -39,7 +39,7 @@ struct mstdnt_status time_t created_at; struct mstdnt_account account; char* content; - char* visibility; + enum mstdnt_visibility_type visibility; mstdnt_bool sensitive; char* spoiler_text; struct mstdnt_attachment* media_attachments; diff --git a/include/mastodont_visibility_types.h b/include/mastodont_visibility_types.h index 6d6aa59..ac2cb80 100644 --- a/include/mastodont_visibility_types.h +++ b/include/mastodont_visibility_types.h @@ -19,12 +19,23 @@ /* These used to uint8_t's, but because lists are custom strings, it was better to make these regular strings */ -#define MSTDNT_VISIBILITY_PUBLIC "public" -#define MSTDNT_VISIBILITY_UNLISTED "unlisted" -#define MSTDNT_VISIBILITY_PRIVATE "private" -#define MSTDNT_VISIBILITY_DIRECT "direct" -#define MSTDNT_VISIBILITY_LOCAL "local" +#define MSTDNT_STR_VISIBILITY_PUBLIC "public" +#define MSTDNT_STR_VISIBILITY_UNLISTED "unlisted" +#define MSTDNT_STR_VISIBILITY_PRIVATE "private" +#define MSTDNT_STR_VISIBILITY_DIRECT "direct" +#define MSTDNT_STR_VISIBILITY_LOCAL "local" typedef char* mstdnt_visibility_t; +enum mstdnt_visibility_type +{ + MSTDNT_VISIBILITY_PUBLIC, + MSTDNT_VISIBILITY_UNLISTED, + MSTDNT_VISIBILITY_PRIVATE, + MSTDNT_VISIBILITY_DIRECT, + MSTDNT_VISIBILITY_LOCAL, + MSTDNT_VISIBILITY_LIST, + MSTDNT_VISIBILITY_UNKNOWN, +}; + #endif /* MASTODONT_VISIBILITY_TYPES_H */ diff --git a/src/json_helper.c b/src/json_helper.c index 46a95f4..e9c7e4f 100644 --- a/src/json_helper.c +++ b/src/json_helper.c @@ -130,11 +130,6 @@ void _mstdnt_val_string_int_call(cJSON* v, void* _type) void _mstdnt_val_string_call(cJSON* v, void* _type) { char** type = _type; - if (!cJSON_IsString(v)) - { - *type = NULL; - return; - } *type = v->valuestring; } diff --git a/src/status.c b/src/status.c index 0820074..a23c56c 100644 --- a/src/status.c +++ b/src/status.c @@ -45,6 +45,35 @@ void _mstdnt_val_malloc_status_call(cJSON* v, void* _type) mstdnt_status_json(*type, v->child); } +// Consider moving to mastodont_visibility_types? +static void _mstdnt_val_visibility_call(cJSON* v, void* _type) +{ + enum mstdnt_visibility_type* type = _type; + + // Check first, as we must read it + if (!cJSON_IsString(v)) + { + *type = MSTDNT_VISIBILITY_UNKNOWN; + return; + } + + char* str = v->valuestring; + if (strcmp(str, "public") == 0) + *type = MSTDNT_VISIBILITY_PUBLIC; + else if (strcmp(str, "unlisted") == 0) + *type = MSTDNT_VISIBILITY_UNLISTED; + else if (strcmp(str, "private") == 0) + *type = MSTDNT_VISIBILITY_PRIVATE; + else if (strcmp(str, "direct") == 0) + *type = MSTDNT_VISIBILITY_DIRECT; + else if (strcmp(str, "local") == 0) + *type = MSTDNT_VISIBILITY_LOCAL; + else if (strcmp(str, "list") == 0) + *type = MSTDNT_VISIBILITY_LIST; + else + *type = MSTDNT_VISIBILITY_UNKNOWN; +} + int mstdnt_status_json(struct mstdnt_status* status, cJSON* js) { cJSON* v; @@ -76,7 +105,7 @@ int mstdnt_status_json(struct mstdnt_status* status, cJSON* js) { "language", &(status->language), _mstdnt_val_string_call }, { "url", &(status->url), _mstdnt_val_string_call }, { "text", &(status->text), _mstdnt_val_string_call }, - { "visibility", &(status->visibility), _mstdnt_val_string_call }, + { "visibility", &(status->visibility), _mstdnt_val_visibility_call }, { "in_reply_to_account_id", &(status->in_reply_to_account_id), _mstdnt_val_string_call }, { "sensitive", &(status->sensitive), _mstdnt_val_bool_call }, { "favourited", &(status->favourited), _mstdnt_val_bool_call },