Skip to content

Commit

Permalink
feat(history): add delete message pubnub_api_types
Browse files Browse the repository at this point in the history
Add `delete message` API support to advanced history module.
  • Loading branch information
parfeon committed Aug 8, 2024
1 parent f714dc5 commit 08912dc
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 14 deletions.
129 changes: 129 additions & 0 deletions core/pbcc_advanced_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "pubnub_url_encode.h"

#include "pubnub_assert.h"
#include "pubnub_helper.h"
#include "pubnub_log.h"
#else
#error this module can only be used if PUBNUB_USE_ADVANCED_HISTORY is defined and set to 1
Expand Down Expand Up @@ -543,3 +544,131 @@ enum pubnub_res pbcc_message_counts_prep(
PUBNUB_LOG_DEBUG("pbcc_message_counts_prep. REQUEST =%s\n", p->http_buf);
return (rslt != PNR_OK) ? rslt : PNR_STARTED;
}

enum pubnub_res pbcc_delete_messages_prep(struct pbcc_context* pb,
char const* channel,
char const* start,
char const* end)
{
char const* const uname = pubnub_uname();
char const* user_id = pbcc_user_id_get(pb);
#if PUBNUB_CRYPTO_API
enum pubnub_res rslt = PNR_OK;
#endif

PUBNUB_ASSERT_OPT(NULL != user_id);

pb->msg_ofs = pb->msg_end = 0;
pb->http_content_len = 0;

pb->http_buf_len =
snprintf(pb->http_buf,
sizeof pb->http_buf,
"/v3/history/sub-key/%s/channel/",
pb->subscribe_key);
APPEND_URL_ENCODED_M(pb, channel);

URL_PARAMS_INIT(qparam, PUBNUB_MAX_URL_PARAMS);
if (uname) { ADD_URL_PARAM(qparam, pnsdk, uname); }
ADD_URL_PARAM(qparam, uuid, user_id);
#if PUBNUB_CRYPTO_API
if (pb->secret_key == NULL) { ADD_URL_AUTH_PARAM(pb, qparam, auth); }
ADD_TS_TO_URL_PARAM();
#else
ADD_URL_AUTH_PARAM(pb, qparam, auth);
#endif
if (start) { ADD_URL_PARAM(qparam, start, start); }
if (end) { ADD_URL_PARAM(qparam, end, end); }

#if PUBNUB_CRYPTO_API
SORT_URL_PARAMETERS(qparam);
#endif
ENCODE_URL_PARAMETERS(pb, qparam);
#if PUBNUB_CRYPTO_API
if (pb->secret_key != NULL) {
rslt = pbcc_sign_url(pb, "", pubnubSendViaGET, true);
}
#endif

PUBNUB_LOG_DEBUG("pbcc_delete_messages_prep. REQUEST =%s\n", pb->http_buf);
#if PUBNUB_CRYPTO_API
return (rslt != PNR_OK) ? rslt : PNR_STARTED;
#else
return PNR_STARTED;
#endif
}

pubnub_chamebl_t pbcc_get_delete_messages_response(struct pbcc_context* pb)
{
pubnub_chamebl_t resp;
char const* reply = pb->http_reply;
int reply_len = pb->http_buf_len;

if (PNR_OK != pb->last_result) {
PUBNUB_LOG_ERROR("pbcc_get_delete_messages_response(pb=%p) can be "
"called only if previous transactin "
"PBTT_DELETE_MESSAGES(%d) is finished successfully. "
"Transaction result was: %d('%s')\n",
pb,
PBTT_DELETE_MESSAGES,
pb->last_result,
pubnub_res_2_string(pb->last_result));
resp.ptr = NULL;
resp.size = 0;
return resp;
}

resp.ptr = (char*)reply;
resp.size = reply_len;
return resp;
}

enum pubnub_res pbcc_parse_delete_messages_response(struct pbcc_context* pb)
{
enum pbjson_object_name_parse_result jpresult;
struct pbjson_elem el;
struct pbjson_elem found;
char* reply = pb->http_reply;
int reply_len = pb->http_buf_len;

if ((reply[0] != '{') || (reply[reply_len - 1] != '}')) {
PUBNUB_LOG_ERROR(
"Error: pbcc_parse_delete_messages_response(pbcc=%p) - "
"Response is not json object: response='%.*s'\n",
pb,
reply_len,
reply);
return PNR_FORMAT_ERROR;
}
el.start = reply;
el.end = reply + reply_len;
if (pbjson_value_for_field_found(&el, "status", "403")) {
PUBNUB_LOG_ERROR(
"Error: pbcc_parse_delete_messages_response(pbcc=%p) - "
"Access Denied: response='%.*s'\n",
pb,
reply_len,
reply);
return PNR_ACCESS_DENIED;
}
jpresult = pbjson_get_object_value(&el, "error", &found);
if (jonmpOK == jpresult) {
if (pbjson_elem_equals_string(&found, "true")) {
return PNR_ERROR_ON_SERVER;
}
}
else {
PUBNUB_LOG_ERROR(
"Error: pbcc_parse_delete_messages_response(pbcc=%p) - "
"'error' atribute not found in the response. error=%d\n"
"response='%.*s'\n",
pb,
jpresult,
reply_len,
reply);
return PNR_FORMAT_ERROR;
}
pb->chan_ofs = pb->chan_end = 0;

return PNR_OK;
}
37 changes: 37 additions & 0 deletions core/pbcc_advanced_history.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,43 @@ enum pubnub_res pbcc_message_counts_prep(
char const* channel,
char const* timetoken,
char const* channel_timetokens);

/**
* @brief Prepare `delete messages` operation (transaction), mostly by
* formatting the URI of the HTTP request.
*
* @param pb PubNub context which provides resources to send request.
* @param channel Channel from which messages should be deleted.
* @param start Timetoken delimiting the start of time slice (exclusive) to
* delete messages from.
* @param end Timetoken delimiting the end of time slice (inclusive) to
* delete messages to.
* @return Results of `delete messages` transaction call.
*/
enum pubnub_res pbcc_delete_messages_prep(struct pbcc_context* pb,
char const* channel,
char const* start,
char const* end);

/**
* @brief Get `delete messages` service response.
*
* @param pb PubNub context which has been used to delete channel messages.
* @return `pubnub_chamebl_t` with pointer to string with response.
*/
pubnub_chamebl_t pbcc_get_delete_messages_response(struct pbcc_context* pb);



/**
* @brief Parses the string received as a response for a history v3
* operation (transaction).
*
* @param pb PubNub context to parse delete channel messages response.
* @return PNR_OK: OK, !PNR_OK: error (invalid response)
*/
enum pubnub_res pbcc_parse_delete_messages_response(struct pbcc_context* pb);

#endif /* INC_PBCC_ADVANCED_HISTORY */
#endif /* PUBNUB_USE_ADVANCED_HISTORY */

88 changes: 77 additions & 11 deletions core/pubnub_advanced_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
int pubnub_get_error_message(pubnub_t* pb, pubnub_chamebl_t* o_msg)
{
int rslt;

PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));

pubnub_mutex_lock(pb->monitor);
Expand All @@ -30,7 +30,7 @@ int pubnub_get_error_message(pubnub_t* pb, pubnub_chamebl_t* o_msg)


int pubnub_get_chan_msg_counts_size(pubnub_t* pb)
{
{
/* Number of '"channel":msg_count' pairs */
int rslt;

Expand All @@ -42,14 +42,14 @@ int pubnub_get_chan_msg_counts_size(pubnub_t* pb)
return -1;
}
rslt = pbcc_get_chan_msg_counts_size(&(pb->core));

pubnub_mutex_unlock(pb->monitor);
return rslt;
}


enum pubnub_res pubnub_message_counts(pubnub_t* pb,
char const* channel,
char const* channel,
char const* timetoken)
{
enum pubnub_res rslt;
Expand All @@ -65,10 +65,12 @@ enum pubnub_res pubnub_message_counts(pubnub_t* pb,
}

if (strchr(timetoken, ',') == NULL) {
rslt = pbcc_message_counts_prep(PBTT_MESSAGE_COUNTS , &(pb->core), channel, timetoken, NULL);
rslt = pbcc_message_counts_prep(
PBTT_MESSAGE_COUNTS, &(pb->core), channel, timetoken, NULL);
}
else {
rslt = pbcc_message_counts_prep(PBTT_MESSAGE_COUNTS , &(pb->core), channel, NULL, timetoken);
rslt = pbcc_message_counts_prep(
PBTT_MESSAGE_COUNTS, &(pb->core), channel, NULL, timetoken);
}
if (PNR_STARTED == rslt) {
pb->trans = PBTT_MESSAGE_COUNTS;
Expand All @@ -82,8 +84,8 @@ enum pubnub_res pubnub_message_counts(pubnub_t* pb,
}


int pubnub_get_chan_msg_counts(pubnub_t* pb,
size_t* io_count,
int pubnub_get_chan_msg_counts(pubnub_t* pb,
size_t* io_count,
struct pubnub_chan_msg_count* chan_msg_counters)
{
int rslt;
Expand All @@ -98,7 +100,7 @@ int pubnub_get_chan_msg_counts(pubnub_t* pb,
return -1;
}
rslt = pbcc_get_chan_msg_counts(&(pb->core), io_count, chan_msg_counters);

pubnub_mutex_unlock(pb->monitor);
return rslt;
}
Expand All @@ -107,7 +109,7 @@ int pubnub_get_chan_msg_counts(pubnub_t* pb,
int pubnub_get_message_counts(pubnub_t* pb, char const* channel, int* o_count)
{
int rslt;

PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));
PUBNUB_ASSERT_OPT(channel != NULL);
PUBNUB_ASSERT_OPT(o_count != NULL);
Expand All @@ -118,9 +120,73 @@ int pubnub_get_message_counts(pubnub_t* pb, char const* channel, int* o_count)
return -1;
}
rslt = pbcc_get_message_counts(&(pb->core), channel, o_count);


pubnub_mutex_unlock(pb->monitor);
return rslt;
}

struct pubnub_delete_messages_options pubnub_delete_messages_defopts(void)
{
struct pubnub_delete_messages_options options;

options.start = NULL;
options.end = NULL;

return options;
}

enum pubnub_res pubnub_delete_messages(pubnub_t* pb,
char const* channel,
struct pubnub_delete_messages_options options)
{
PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));
PUBNUB_ASSERT_OPT(NULL != channel);

pubnub_mutex_lock(pb->monitor);
if (!pbnc_can_start_transaction(pb)) {
pubnub_mutex_unlock(pb->monitor);
return PNR_IN_PROGRESS;
}

enum pubnub_res rslt =
pbcc_delete_messages_prep(&pb->core, channel, options.start, options.end);

if (PNR_STARTED == rslt) {
pb->trans = PBTT_DELETE_MESSAGES;
pb->method = pubnubUseDELETE;
pb->core.last_result = PNR_STARTED;
pbnc_fsm(pb);
rslt = pb->core.last_result;
}
pubnub_mutex_unlock(pb->monitor);

return rslt;
}

pubnub_chamebl_t pubnub_get_delete_messages_response(pubnub_t* pb)
{
pubnub_chamebl_t resp;
PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));

pubnub_mutex_lock(pb->monitor);
if (PBTT_DELETE_MESSAGES != pb->trans) {
PUBNUB_LOG_ERROR(
"pubnub_get_delete_messages_response(pb=%p) can be "
"called only if previous transaction is "
"PBTT_DELETE_MESSAGES(%d). Previous transaction was: %d\n",
pb,
PBTT_DELETE_MESSAGES,
pb->trans);
pubnub_mutex_unlock(pb->monitor);
resp.ptr = NULL;
resp.size = 0;
return resp;
}

resp = pbcc_get_delete_messages_response(&pb->core);
pubnub_mutex_unlock(pb->monitor);

return resp;
}

#endif /* PUBNUB_USE_ADVANCED_HISTORY */
Loading

0 comments on commit 08912dc

Please sign in to comment.