Cleanup status code

FossilOrigin-Name: c7ba954d9554381ab768ff87db702b9e3bb463864bb552278a81bed7da96fdef
This commit is contained in:
me@ow.nekobit.net 2022-02-11 04:10:20 +00:00
parent 947f86ef23
commit 2e88537957
5 changed files with 107 additions and 37 deletions

View file

@ -19,7 +19,7 @@
#include <cjson/cJSON.h>
#define MSTDNT_LOOKUP_ACCT 0
#define MSTDNT_LOOPUP_ID 1
#define MSTDNT_LOOKUP_ID 1
struct mstdnt_account
{

View file

@ -17,6 +17,7 @@
#define MASTODONT_STATUS
#include <cjson/cJSON.h>
#include "mastodont_types.h"
#include "mastodont_fetch.h"
#include "mastodont_attachment.h"
#include "mastodont_application.h"
#include "mastodont_mention.h"
@ -78,6 +79,34 @@ struct mstdnt_status
mstdnt_bool pinned;
};
struct mstdnt_account_statuses_args {
int pinned;
char* tagged;
int with_muted;
int exclude_reblogs;
int exclude_replies;
int offset;
int only_media;
char* max_id;
char* since_id;
char* min_id;
int limit;
};
int mstdnt_load_statuses_from_result(struct mstdnt_status* status[],
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
size_t* size);
int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js);
int mstdnt_account_statuses(mastodont_t* data,
char* id,
struct mstdnt_account_statuses_args* args,
struct mstdnt_storage* storage,
struct mstdnt_status* statuses[],
size_t* size);
#endif /* MASTODONT_STATUS */

View file

@ -19,7 +19,7 @@
#include "mastodont_fetch.h"
int mastodont_account(mastodont_t* data,
int lookup,
int lookup, /* TODO move into separate function for consistancy */
char* id,
struct mstdnt_account* acct,
struct mstdnt_storage* storage,
@ -30,7 +30,7 @@ int mastodont_account(mastodont_t* data,
char url[MSTDNT_URLSIZE];
struct mstdnt_fetch_results results = { 0 };
snprintf(url, MSTDNT_URLSIZE,
lookup ? "/api/v1/accounts/%s" : "/api/v1/accounts/lookup?acct=%s",
lookup ? "api/v1/accounts/%s" : "api/v1/accounts/lookup?acct=%s",
id);
storage->needs_cleanup = 0;
@ -57,6 +57,7 @@ cleanup:
return res;
}
int mstdnt_load_account_from_json(struct mstdnt_account* acct, cJSON* js)
{
cJSON* v;

View file

@ -14,6 +14,7 @@
*/
#include <string.h>
#include <stdlib.h>
#include <mastodont_json_helper.h>
#include <mastodont_status.h>
#include <mastodont_account.h>
@ -56,3 +57,74 @@ int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js)
}
}
}
int mstdnt_load_statuses_from_result(struct mstdnt_status* statuses[],
struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
size_t* size)
{
size_t i = 0;
cJSON* root, *status_j_list;
root = cJSON_ParseWithLength(results->response, results->size);
if (root == NULL)
return 1;
storage->root = root;
storage->needs_cleanup = 1;
if (!cJSON_IsArray(root))
return 1;
if (size) *size = cJSON_GetArraySize(root);
/* malloc array - cJSON does a loop to count, let's do it once preferably */
*statuses = malloc((size ? *size : cJSON_GetArraySize(root))
* sizeof(struct mstdnt_status));
if (*statuses == NULL)
return 1;
cJSON_ArrayForEach(status_j_list, root)
{
mstdnt_load_status_from_json((*statuses) + i++, status_j_list->child);
}
}
int mstdnt_account_statuses(mastodont_t* data,
char* id,
struct mstdnt_account_statuses_args* args,
struct mstdnt_storage* storage,
struct mstdnt_status* statuses[],
size_t* size)
{
int res;
char url[MSTDNT_URLSIZE];
struct mstdnt_fetch_results results = { 0 };
snprintf(url, MSTDNT_URLSIZE, "api/v1/accounts/%s/statuses", id);
/* Default args */
struct mstdnt_account_statuses_args _args;
if (args == NULL)
{
_args.pinned = 0;
_args.tagged = NULL;
_args.with_muted = 1;
_args.offset = 0;
_args.exclude_reblogs = 0;
_args.exclude_replies = 0;
_args.only_media = 0;
_args.max_id = NULL;
_args.since_id = NULL;
_args.min_id = NULL;
_args.limit = 20;
args = &_args;
}
storage->needs_cleanup = 0;
if (mastodont_fetch_curl(data, "api/v1/timelines/public", &results) != CURLE_OK)
return 1;
res = mstdnt_load_statuses_from_result(statuses, storage, &results, size);
mastodont_fetch_results_cleanup(&results);
return res;
}

View file

@ -24,9 +24,7 @@ int mastodont_timeline_public(mastodont_t* data,
struct mstdnt_status* statuses[],
size_t* size)
{
int res = 0;
cJSON* root, *status_j_list;
size_t i = 0;
int res;
struct mstdnt_fetch_results results = { 0 };
/* Default args */
@ -47,38 +45,8 @@ int mastodont_timeline_public(mastodont_t* data,
if (mastodont_fetch_curl(data, "api/v1/timelines/public", &results) != CURLE_OK)
return 1;
root = cJSON_ParseWithLength(results.response, results.size);
if (root == NULL)
{
res = 1;
goto cleanup;
}
storage->root = root;
storage->needs_cleanup = 1;
res = mstdnt_load_statuses_from_result(statuses, storage, &results, size);
if (!cJSON_IsArray(root))
{
/* Likely an error */
res = 1;
goto cleanup;
}
if (size) *size = cJSON_GetArraySize(root);
/* malloc array - cJSON does a loop to count, let's do it once preferably */
*statuses = malloc((size ? *size : cJSON_GetArraySize(root))
* sizeof(struct mstdnt_status));
if (*statuses == NULL)
{
res = 1;
goto cleanup;
}
cJSON_ArrayForEach(status_j_list, root)
{
mstdnt_load_status_from_json((*statuses) + i++, status_j_list->child);
}
cleanup:
mastodont_fetch_results_cleanup(&results);
return res;