From 7548de2b2492da518a86c440051eb324ee26dffd Mon Sep 17 00:00:00 2001 From: nekobit Date: Sun, 3 Jul 2022 23:13:26 +0000 Subject: [PATCH] Pleroma Get chats FossilOrigin-Name: e9de6a96241f37a6f2ed75e10c1a8866b1831b92600339642ce67bad711085f7 --- include/mastodont_chats.h | 7 ++++++- include/mastodont_query.h | 2 +- src/chats.c | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/include/mastodont_chats.h b/include/mastodont_chats.h index 2e5d4da..63a2286 100644 --- a/include/mastodont_chats.h +++ b/include/mastodont_chats.h @@ -22,7 +22,7 @@ struct mstdnt_chat { struct mstdnt_account account; const char* id; - int unread; + unsigned int unread; }; struct mstdnt_chats_args @@ -35,6 +35,11 @@ struct mstdnt_chats_args int limit; }; +int mstdnt_chat_json(struct mstdnt_chat* chat, cJSON* js); +int mstdnt_chats_json(struct mstdnt_chat* statuses[], + size_t* size, + cJSON* js); + int mastodont_get_chats_v2(mastodont_t* data, struct mstdnt_args* m_args, struct mstdnt_chats_args* args, diff --git a/include/mastodont_query.h b/include/mastodont_query.h index 9d9dd7e..b482b8d 100644 --- a/include/mastodont_query.h +++ b/include/mastodont_query.h @@ -38,7 +38,7 @@ struct _mstdnt_query_param enum _mstdnt_query_type type; char* key; union param_value { - char* s; + const char* s; int i; mstdnt_bool b; struct _mstdnt_query_array a; diff --git a/src/chats.c b/src/chats.c index d9b0f12..c0db84b 100644 --- a/src/chats.c +++ b/src/chats.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include struct _mstdnt_chats_cb_args { @@ -26,6 +28,36 @@ struct _mstdnt_chats_cb_args size_t* chats_len; }; +int mstdnt_chat_json(struct mstdnt_chat* chat, cJSON* js) +{ + memset(chat, 0, sizeof(struct mstdnt_chat)); + + struct _mstdnt_val_ref vals[] = { + { "account", &(chat->account), _mstdnt_val_account_call }, + { "id", &(chat->id), _mstdnt_val_string_call }, + { "unread", &(chat->unread), _mstdnt_val_uint_call }, + }; + + for (cJSON* v = js; v; v = v->next) + _mstdnt_key_val_ref(v, vals, _mstdnt_arr_len(vals)); + + return 0; +} + +int mstdnt_chat_json_callback(cJSON* json, void* chat) +{ + return mstdnt_chat_json(chat, json->child); +} + +// GENERATE mstdnt_chats_json +GENERATE_JSON_ARRAY_FUNC(mstdnt_chats_json, struct mstdnt_chat, mstdnt_chat_json) + +int mstdnt_chats_json_callback(cJSON* json, void* _args) +{ + struct _mstdnt_chats_cb_args* args = _args; + return mstdnt_chats_json(args->chats, args->chats_len, json); +} + int mastodont_get_chats_v2(mastodont_t* data, struct mstdnt_args* m_args, struct mstdnt_chats_args* args, @@ -47,12 +79,12 @@ int mastodont_get_chats_v2(mastodont_t* data, .storage = storage, .url = "api/v2/pleroma/chats", .params_query = params, - .params_query_len = _mstdnt_arg_len(params), + .params_query_len = _mstdnt_arr_len(params), .params_post = NULL, .params_post_len = 0, .request_type = CURLOPT_HTTPGET, .request_type_custom = NULL, - .args = cb_args, + .args = &cb_args, .callback = mstdnt_chats_json_callback, };