diff --git a/include/mastodont_scrobbles.h b/include/mastodont_scrobbles.h index 0ba2dda..5325829 100644 --- a/include/mastodont_scrobbles.h +++ b/include/mastodont_scrobbles.h @@ -20,7 +20,7 @@ #include #include -struct mstdnt_scrobble +typedef struct mstdnt_scrobble { struct mstdnt_account account; char* album; @@ -29,7 +29,13 @@ struct mstdnt_scrobble char* id; int length; char* title; -}; +} mstdnt_scrobble; + +typedef struct mstdnt_scrobbles +{ + mstdnt_scrobble* scrobbles; + size_t len; +} mstdnt_scrobbles; struct mstdnt_get_scrobbles_args { @@ -40,29 +46,38 @@ struct mstdnt_get_scrobbles_args int limit; }; +// DEPRECATED struct _mstdnt_scrobbles_cb_args { struct mstdnt_scrobble** scrobbles; size_t* size; }; -int mstdnt_get_scrobbles(mastodont_t* data, - struct mstdnt_args* m_args, -mstdnt_request_cb_t cb_request, -void* cb_args, - char* id, - struct mstdnt_get_scrobbles_args* args, - struct mstdnt_storage* storage, - struct mstdnt_scrobble* scrobbles[], - size_t* size); +int +mstdnt_get_scrobbles(mastodont_t* data, + struct mstdnt_args* m_args, + mstdnt_request_cb_t cb_request, + void* cb_args, + char* id, + struct mstdnt_get_scrobbles_args args); -int mstdnt_scrobbles_json(struct mstdnt_scrobble* scrobbles[], - size_t* size, - cJSON* json); +int +mstdnt_scrobbles_json(struct mstdnt_scrobble* scrobbles[], + size_t* size, + cJSON* json); -int mstdnt_scrobbles_json_callback(cJSON* json, - void* arg); +int +mstdnt_scrobbles_json_callback(cJSON* json, + void* arg, + mstdnt_request_cb_data* data); -int mstdnt_scrobble_json(struct mstdnt_scrobble* scrobble, cJSON* js); +int +mstdnt_scrobble_json(struct mstdnt_scrobble* scrobble, cJSON* js); + +void +mstdnt_cleanup_scrobble(mstdnt_scrobble* scrobble); + +void +mstdnt_cleanup_scrobbles(mstdnt_scrobbles* scrobbles); #endif /* MASTODONT_SCROBBLES_H */ diff --git a/src/scrobbles.c b/src/scrobbles.c index b99962a..6c4c129 100644 --- a/src/scrobbles.c +++ b/src/scrobbles.c @@ -21,7 +21,8 @@ #include #include -int mstdnt_scrobble_json(struct mstdnt_scrobble* scrobble, cJSON* js) +int +mstdnt_scrobble_json(struct mstdnt_scrobble* scrobble, cJSON* js) { cJSON* v; @@ -46,45 +47,60 @@ int mstdnt_scrobble_json(struct mstdnt_scrobble* scrobble, cJSON* js) GENERATE_JSON_ARRAY_FUNC(mstdnt_scrobbles_json, struct mstdnt_scrobble, mstdnt_scrobble_json) -int mstdnt_scrobbles_json_callback(cJSON* json, void* _args) +int +mstdnt_scrobbles_json_callback(cJSON* json, + void* _args, + mstdnt_request_cb_data* data) { - struct _mstdnt_scrobbles_cb_args* args = _args; - return mstdnt_scrobbles_json(args->scrobbles, args->size, json); + mstdnt_scrobbles* scrobbles = mstdnt_malloc(sizeof(mstdnt_scrobbles)); + data->data = scrobbles; + data->data_free_cb = (mstdnt_data_free_cb_t)mstdnt_cleanup_scrobbles; + return mstdnt_scrobbles_json(&(scrobbles->scrobbles), &(scrobbles->len), json); } -int mstdnt_get_scrobbles(mastodont_t* data, - struct mstdnt_args* m_args, -mstdnt_request_cb_t cb_request, -void* cb_args, - char* id, - struct mstdnt_get_scrobbles_args* args, - struct mstdnt_storage* storage, - struct mstdnt_scrobble* scrobbles[], - size_t* size) +int +mstdnt_get_scrobbles(mastodont_t* data, + struct mstdnt_args* m_args, + mstdnt_request_cb_t cb_request, + void* cb_args, + char* id, + struct mstdnt_get_scrobbles_args args) { - struct _mstdnt_scrobbles_cb_args req_cb_args = { scrobbles, size }; char url[MSTDNT_URLSIZE]; snprintf(url, MSTDNT_URLSIZE, "api/v1/pleroma/accounts/%s/scrobbles", id); struct _mstdnt_query_param params[] = { - { _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, "offset", { .i = args->offset } }, - { _MSTDNT_QUERY_INT, "limit", { .i = args->limit } }, + { _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, "offset", { .i = args.offset } }, + { _MSTDNT_QUERY_INT, "limit", { .i = args.limit } }, }; struct mstdnt_request_args req_args = { - storage, - url, - params, _mstdnt_arr_len(params), - NULL, 0, - CURLOPT_HTTPGET, - NULL, - &req_cb_args, - mstdnt_scrobbles_json_callback + .url = url, + .params_query = params, + .params_query_len = _mstdnt_arr_len(params), + .request_type = CURLOPT_HTTPGET, + .callback = mstdnt_scrobbles_json_callback }; return mstdnt_request(data, m_args, cb_request, cb_args, &req_args); } +void +mstdnt_cleanup_scrobble(mstdnt_scrobble* scrobble) +{ + mstdnt_cleanup_account(&(scrobble->account)); +} + +void +mstdnt_cleanup_scrobbles(mstdnt_scrobbles* scrobbles) +{ + if (!scrobbles) return; + for (size_t i = 0; i < scrobbles->len; ++i) + { + mstdnt_cleanup_scrobble(scrobbles->scrobbles + i); + } + mstdnt_free(scrobbles->scrobbles); +}