Show messages

FossilOrigin-Name: 8625e1feab9f5191c1eb2a39c8885f1ced89426da92b2e97811afbbb65be73f5
This commit is contained in:
nekobit 2022-07-05 09:44:46 +00:00
parent 1a35b65266
commit d95ac33e25
3 changed files with 111 additions and 3 deletions

View file

@ -15,8 +15,11 @@
#ifndef MASTODONT_CHATS_H
#define MASTODONT_CHATS_H
#include <stddef.h>
#include <time.h>
#include "mastodont_types.h"
#include "mastodont_account.h"
#include "mastodont_emoji.h"
struct mstdnt_chat
{
@ -25,6 +28,18 @@ struct mstdnt_chat
unsigned int unread;
};
struct mstdnt_message
{
char* account_id;
char* chat_id;
char* content;
time_t created_at;
char* id;
struct mstdnt_emoji* emojis;
size_t emojis_len;
mstdnt_bool unread;
};
struct mstdnt_chats_args
{
mstdnt_bool with_muted;
@ -32,13 +47,16 @@ struct mstdnt_chats_args
const char* min_id;
const char* since_id;
int offset;
int limit;
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 mstdnt_message_json(struct mstdnt_message* message, cJSON* js);
int mstdnt_message_json_callback(cJSON* json, void* chat);
int mstdnt_messages_json(struct mstdnt_message* message[], size_t* size, cJSON* js);
int mastodont_get_chats_v2(mastodont_t* data,
struct mstdnt_args* m_args,
@ -47,4 +65,12 @@ int mastodont_get_chats_v2(mastodont_t* data,
struct mstdnt_chat* chats[],
size_t* chats_len);
int mastodont_get_chat_messages(mastodont_t* data,
struct mstdnt_args* m_args,
char* chat_id,
struct mstdnt_chats_args* args,
struct mstdnt_storage* storage,
struct mstdnt_message* chats[],
size_t* size);
#endif // MASTODONT_CHATS_H

View file

@ -141,7 +141,7 @@ int mastodont_get_account(mastodont_t* data,
struct mstdnt_storage* storage)
{
/* Url */
char url[MSTDNT_URLSIZE] = { 0 };
char url[MSTDNT_URLSIZE];
snprintf(url, MSTDNT_URLSIZE,
lookup ? "api/v1/accounts/%s" : "api/v1/accounts/lookup?acct=%s",
id);

View file

@ -28,6 +28,12 @@ struct _mstdnt_chats_cb_args
size_t* chats_len;
};
struct _mstdnt_messages_cb_args
{
struct mstdnt_message** messages;
size_t* messages_len;
};
int mstdnt_chat_json(struct mstdnt_chat* chat, cJSON* js)
{
memset(chat, 0, sizeof(struct mstdnt_chat));
@ -44,6 +50,36 @@ int mstdnt_chat_json(struct mstdnt_chat* chat, cJSON* js)
return 0;
}
int mstdnt_message_json(struct mstdnt_message* message, cJSON* js)
{
memset(message, 0, sizeof(struct mstdnt_message));
struct _mstdnt_generic_args emj_args = {
&(message->emojis),
&(message->emojis_len)
};
struct _mstdnt_val_ref vals[] = {
{ "account_id", &(message->account_id), _mstdnt_val_string_call },
{ "chat_id", &(message->chat_id), _mstdnt_val_string_call },
{ "content", &(message->content), _mstdnt_val_string_call },
{ "created_at", &(message->created_at), _mstdnt_val_datetime_unix_call },
{ "id", &(message->id), _mstdnt_val_string_call },
{ "emojis", &emj_args, _mstdnt_val_emojis_call },
{ "unread", &(message->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_message_json_callback(cJSON* json, void* chat)
{
return mstdnt_message_json(chat, json->child);
}
int mstdnt_chat_json_callback(cJSON* json, void* chat)
{
return mstdnt_chat_json(chat, json->child);
@ -52,12 +88,21 @@ int mstdnt_chat_json_callback(cJSON* json, void* chat)
// 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)
// GENERATE mstdnt_messages_json
GENERATE_JSON_ARRAY_FUNC(mstdnt_messages_json, struct mstdnt_message, mstdnt_message_json)
static 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);
}
static int mstdnt_messages_json_callback(cJSON* json, void* _args)
{
struct _mstdnt_messages_cb_args* args = _args;
return mstdnt_messages_json(args->messages, args->messages_len, json);
}
int mastodont_get_chats_v2(mastodont_t* data,
struct mstdnt_args* m_args,
struct mstdnt_chats_args* args,
@ -90,3 +135,40 @@ int mastodont_get_chats_v2(mastodont_t* data,
return mastodont_request(data, m_args, &req_args);
}
int mastodont_get_chat_messages(mastodont_t* data,
struct mstdnt_args* m_args,
char* chat_id,
struct mstdnt_chats_args* args,
struct mstdnt_storage* storage,
struct mstdnt_message* messages[],
size_t* size)
{
char url[MSTDNT_URLSIZE];
snprintf(url, MSTDNT_URLSIZE, "api/v1/pleroma/chats/%s/messages", chat_id);
struct _mstdnt_messages_cb_args cb_args = { messages, size };
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_BOOL, "with_muted", { .b = args->with_muted } },
{ _MSTDNT_QUERY_STRING, "max_id", { .s = args->max_id } },
{ _MSTDNT_QUERY_STRING, "min_id", { .s = args->min_id } },
{ _MSTDNT_QUERY_STRING, "since_id", { .s = args->since_id } },
{ _MSTDNT_QUERY_INT, "limit", { .i = args->limit } },
{ _MSTDNT_QUERY_INT, "offset", { .i = args->offset } },
};
struct mastodont_request_args req_args = {
.storage = storage,
.url = url,
.params_query = 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,
.callback = mstdnt_messages_json_callback,
};
return mastodont_request(data, m_args, &req_args);
}