From 4e2ae058e634ab9fd6f413c8a59b66adc6960bc2 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Tue, 1 Mar 2022 04:30:34 +0000 Subject: [PATCH] Emoji reacts FossilOrigin-Name: 8646e9448b6e79884d9c713c06907b9d9baf2d1402630d6ae25ea102783f53b3 --- include/mastodont_attachment.h | 6 ---- include/mastodont_emoji.h | 4 ++- include/mastodont_json_helper.h | 6 ++++ include/mastodont_pleroma.h | 8 +++-- include/mastodont_status.h | 2 +- src/attachment.c | 4 +-- src/emoji.c | 62 +++++++++++++++++++++++++++++++++ src/pleroma.c | 15 +++++--- src/status.c | 4 +-- 9 files changed, 92 insertions(+), 19 deletions(-) create mode 100644 src/emoji.c diff --git a/include/mastodont_attachment.h b/include/mastodont_attachment.h index 1713380..eee3671 100644 --- a/include/mastodont_attachment.h +++ b/include/mastodont_attachment.h @@ -18,12 +18,6 @@ #include "mastodont_types.h" /* Status: Complete, not implemented */ -struct _mstdnt_attachment_args -{ - struct mstdnt_attachment** attachments; - size_t* size; -}; - enum mstdnt_attachment_type { MSTDNT_ATTACHMENT_UNKNOWN, diff --git a/include/mastodont_emoji.h b/include/mastodont_emoji.h index e85de88..c70267f 100644 --- a/include/mastodont_emoji.h +++ b/include/mastodont_emoji.h @@ -29,7 +29,7 @@ struct mstdnt_emoji char* category; }; -struct mstdnt_emoji_react +struct mstdnt_emoji_reaction { char* name; size_t count; @@ -37,4 +37,6 @@ struct mstdnt_emoji_react /* TODO Accounts */ }; +void _mstdnt_val_emoji_reactions_call(cJSON* v, void* _type); + #endif /* MASTODONT_EMOJI */ diff --git a/include/mastodont_json_helper.h b/include/mastodont_json_helper.h index ce353b4..6881fa0 100644 --- a/include/mastodont_json_helper.h +++ b/include/mastodont_json_helper.h @@ -18,6 +18,12 @@ #include "mastodont_types.h" #include "mastodont_fetch.h" +struct _mstdnt_generic_args +{ + void* arg; + size_t* size; +}; + struct _mstdnt_val_ref { const char* key; diff --git a/include/mastodont_pleroma.h b/include/mastodont_pleroma.h index 668effe..dd8b46a 100644 --- a/include/mastodont_pleroma.h +++ b/include/mastodont_pleroma.h @@ -18,13 +18,15 @@ #include #include -struct mstdnt_pleroma +struct mstdnt_status_pleroma { /* content */ int conversation_id; int direct_conversation_id; char* expires_at; char* in_reply_to_account_acct; + struct mstdnt_emoji_reaction* emoji_reactions; + size_t emoji_reactions_len; mstdnt_bool local; mstdnt_bool parent_visible; char* pinned_at; @@ -32,7 +34,7 @@ struct mstdnt_pleroma int thread_muted; }; -int mstdnt_load_pleroma_from_json(struct mstdnt_pleroma* pleroma, cJSON* js); -void _mstdnt_val_pleroma_call(cJSON* v, void* _type); +int mstdnt_load_status_pleroma_from_json(struct mstdnt_status_pleroma* pleroma, cJSON* js); +void _mstdnt_val_status_pleroma_call(cJSON* v, void* _type); #endif /* MASTODONT_PLEROMA */ diff --git a/include/mastodont_status.h b/include/mastodont_status.h index 09b4071..54c70f0 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -52,7 +52,7 @@ struct mstdnt_status struct mstdnt_attachment* media_attachments; size_t media_attachments_len; struct mstdnt_application application; - struct mstdnt_pleroma pleroma; + struct mstdnt_status_pleroma pleroma; /* Rendering attributes */ struct mstdnt_mention* mentions; diff --git a/src/attachment.c b/src/attachment.c index 36c9414..3dc2a8f 100644 --- a/src/attachment.c +++ b/src/attachment.c @@ -39,8 +39,8 @@ static void load_attachment_from_json(struct mstdnt_attachment* att, cJSON* att_ void _mstdnt_val_attachments_call(cJSON* v, void* _type) { - struct _mstdnt_attachment_args* args = _type; - struct mstdnt_attachment** attachments = args->attachments; + struct _mstdnt_generic_args* args = _type; + struct mstdnt_attachment** attachments = args->arg; cJSON* v_array = v->child; cJSON* att = NULL; diff --git a/src/emoji.c b/src/emoji.c new file mode 100644 index 0000000..8d4c847 --- /dev/null +++ b/src/emoji.c @@ -0,0 +1,62 @@ +/* + * 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 . + */ + +#include +#include +#include + +static void load_emoji_reacts_from_json(struct mstdnt_emoji_reaction* emo, cJSON* emo_json) +{ + cJSON* it; + struct _mstdnt_val_ref refs[] = { + { "name", &(emo->name), _mstdnt_val_string_call }, + { "count", &(emo->count), _mstdnt_val_uint_call }, + { "me", &(emo->me), _mstdnt_val_bool_call }, + }; + + for (it = emo_json; it; it = it->next) + { + _mstdnt_key_val_ref(it, refs, _mstdnt_arr_len(refs)); + } +} + +void _mstdnt_val_emoji_reactions_call(cJSON* v, void* _type) +{ + struct _mstdnt_generic_args* args = _type; + struct mstdnt_emoji_reaction** emos = 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) + { + *emos = NULL; + return; + } + + *emos = malloc(sizeof(struct mstdnt_emoji_reaction) * size); + if (*emos == NULL) + return; + + cJSON* it; + int i; + for (it = v_array, i = 0; it; (++i, it = it->next)) + { + load_emoji_reacts_from_json((*emos) + i, it->child); + } +} + diff --git a/src/pleroma.c b/src/pleroma.c index 43ae674..5a07beb 100644 --- a/src/pleroma.c +++ b/src/pleroma.c @@ -16,14 +16,21 @@ #include #include -int mstdnt_load_pleroma_from_json(struct mstdnt_pleroma* pleroma, cJSON* js) +int mstdnt_load_status_pleroma_from_json(struct mstdnt_status_pleroma* pleroma, cJSON* js) { cJSON* v; + + struct _mstdnt_generic_args emo_args = { + &(pleroma->emoji_reactions), + &(pleroma->emoji_reactions_len), + }; + struct _mstdnt_val_ref refs[] = { { "conversation_id", &(pleroma->conversation_id), _mstdnt_val_sint_call }, { "direct_conversation_id", &(pleroma->direct_conversation_id), _mstdnt_val_sint_call }, { "expires_at", &(pleroma->expires_at), _mstdnt_val_string_call }, { "in_reply_to_account_acct", &(pleroma->in_reply_to_account_acct), _mstdnt_val_string_call }, + { "emoji_reactions", &emo_args, _mstdnt_val_emoji_reactions_call }, { "pinned_at", &(pleroma->pinned_at), _mstdnt_val_string_call }, { "thread_muted", &(pleroma->thread_muted), _mstdnt_val_sint_call }, }; @@ -34,9 +41,9 @@ int mstdnt_load_pleroma_from_json(struct mstdnt_pleroma* pleroma, cJSON* js) } } -void _mstdnt_val_pleroma_call(cJSON* v, void* _type) +void _mstdnt_val_status_pleroma_call(cJSON* v, void* _type) { - struct mstdnt_pleroma* type = _type; + struct mstdnt_status_pleroma* type = _type; - mstdnt_load_pleroma_from_json(type, v->child); + mstdnt_load_status_pleroma_from_json(type, v->child); } diff --git a/src/status.c b/src/status.c index 72e9d1f..3f0be36 100644 --- a/src/status.c +++ b/src/status.c @@ -25,7 +25,7 @@ int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js) { cJSON* v; - struct _mstdnt_attachment_args att_args = { + struct _mstdnt_generic_args att_args = { &(status->media_attachments), &(status->media_attachments_len), }; @@ -51,7 +51,7 @@ int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js) { "favourites_count", &(status->favourites_count), _mstdnt_val_uint_call }, { "replies_count", &(status->replies_count), _mstdnt_val_uint_call }, { "media_attachments", &att_args, _mstdnt_val_attachments_call }, - { "pleroma", &(status->pleroma), _mstdnt_val_pleroma_call } + { "pleroma", &(status->pleroma), _mstdnt_val_status_pleroma_call } }; for (v = js; v; v = v->next)