Skip to content

Commit 82fdbea

Browse files
authored
Add delete messages API (#194)
1 parent f714dc5 commit 82fdbea

11 files changed

+386
-23
lines changed

.pubnub.yml

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
name: c-core
22
schema: 1
3-
version: "4.12.3"
3+
version: "4.13.0"
44
scm: github.com/pubnub/c-core
55
changelog:
6+
- date: 2024-08-09
7+
version: v4.13.0
8+
changes:
9+
- type: feature
10+
text: "Add `delete message` API support to the advanced history module."
611
- date: 2024-08-05
712
version: v4.12.3
813
changes:
@@ -843,7 +848,7 @@ sdks:
843848
distribution-type: source code
844849
distribution-repository: GitHub release
845850
package-name: C-Core
846-
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
851+
location: https://github.com/pubnub/c-core/releases/tag/v4.13.0
847852
requires:
848853
-
849854
name: "miniz"
@@ -909,7 +914,7 @@ sdks:
909914
distribution-type: source code
910915
distribution-repository: GitHub release
911916
package-name: C-Core
912-
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
917+
location: https://github.com/pubnub/c-core/releases/tag/v4.13.0
913918
requires:
914919
-
915920
name: "miniz"
@@ -975,7 +980,7 @@ sdks:
975980
distribution-type: source code
976981
distribution-repository: GitHub release
977982
package-name: C-Core
978-
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
983+
location: https://github.com/pubnub/c-core/releases/tag/v4.13.0
979984
requires:
980985
-
981986
name: "miniz"
@@ -1037,7 +1042,7 @@ sdks:
10371042
distribution-type: source code
10381043
distribution-repository: GitHub release
10391044
package-name: C-Core
1040-
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
1045+
location: https://github.com/pubnub/c-core/releases/tag/v4.13.0
10411046
requires:
10421047
-
10431048
name: "miniz"
@@ -1098,7 +1103,7 @@ sdks:
10981103
distribution-type: source code
10991104
distribution-repository: GitHub release
11001105
package-name: C-Core
1101-
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
1106+
location: https://github.com/pubnub/c-core/releases/tag/v4.13.0
11021107
requires:
11031108
-
11041109
name: "miniz"
@@ -1154,7 +1159,7 @@ sdks:
11541159
distribution-type: source code
11551160
distribution-repository: GitHub release
11561161
package-name: C-Core
1157-
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
1162+
location: https://github.com/pubnub/c-core/releases/tag/v4.13.0
11581163
requires:
11591164
-
11601165
name: "miniz"
@@ -1207,7 +1212,7 @@ sdks:
12071212
distribution-type: source code
12081213
distribution-repository: GitHub release
12091214
package-name: C-Core
1210-
location: https://github.com/pubnub/c-core/releases/tag/v4.12.3
1215+
location: https://github.com/pubnub/c-core/releases/tag/v4.13.0
12111216
requires:
12121217
-
12131218
name: "miniz"

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v4.13.0
2+
August 09 2024
3+
4+
#### Added
5+
- Add `delete message` API support to the advanced history module.
6+
17
## v4.12.3
28
August 05 2024
39

core/pbcc_advanced_history.c

+129
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "pubnub_url_encode.h"
1111

1212
#include "pubnub_assert.h"
13+
#include "pubnub_helper.h"
1314
#include "pubnub_log.h"
1415
#else
1516
#error this module can only be used if PUBNUB_USE_ADVANCED_HISTORY is defined and set to 1
@@ -543,3 +544,131 @@ enum pubnub_res pbcc_message_counts_prep(
543544
PUBNUB_LOG_DEBUG("pbcc_message_counts_prep. REQUEST =%s\n", p->http_buf);
544545
return (rslt != PNR_OK) ? rslt : PNR_STARTED;
545546
}
547+
548+
enum pubnub_res pbcc_delete_messages_prep(struct pbcc_context* pb,
549+
char const* channel,
550+
char const* start,
551+
char const* end)
552+
{
553+
char const* const uname = pubnub_uname();
554+
char const* user_id = pbcc_user_id_get(pb);
555+
#if PUBNUB_CRYPTO_API
556+
enum pubnub_res rslt = PNR_OK;
557+
#endif
558+
559+
PUBNUB_ASSERT_OPT(NULL != user_id);
560+
561+
pb->msg_ofs = pb->msg_end = 0;
562+
pb->http_content_len = 0;
563+
564+
pb->http_buf_len =
565+
snprintf(pb->http_buf,
566+
sizeof pb->http_buf,
567+
"/v3/history/sub-key/%s/channel/",
568+
pb->subscribe_key);
569+
APPEND_URL_ENCODED_M(pb, channel);
570+
571+
URL_PARAMS_INIT(qparam, PUBNUB_MAX_URL_PARAMS);
572+
if (uname) { ADD_URL_PARAM(qparam, pnsdk, uname); }
573+
ADD_URL_PARAM(qparam, uuid, user_id);
574+
#if PUBNUB_CRYPTO_API
575+
if (pb->secret_key == NULL) { ADD_URL_AUTH_PARAM(pb, qparam, auth); }
576+
ADD_TS_TO_URL_PARAM();
577+
#else
578+
ADD_URL_AUTH_PARAM(pb, qparam, auth);
579+
#endif
580+
if (start) { ADD_URL_PARAM(qparam, start, start); }
581+
if (end) { ADD_URL_PARAM(qparam, end, end); }
582+
583+
#if PUBNUB_CRYPTO_API
584+
SORT_URL_PARAMETERS(qparam);
585+
#endif
586+
ENCODE_URL_PARAMETERS(pb, qparam);
587+
#if PUBNUB_CRYPTO_API
588+
if (pb->secret_key != NULL) {
589+
rslt = pbcc_sign_url(pb, "", pubnubSendViaGET, true);
590+
}
591+
#endif
592+
593+
PUBNUB_LOG_DEBUG("pbcc_delete_messages_prep. REQUEST =%s\n", pb->http_buf);
594+
#if PUBNUB_CRYPTO_API
595+
return (rslt != PNR_OK) ? rslt : PNR_STARTED;
596+
#else
597+
return PNR_STARTED;
598+
#endif
599+
}
600+
601+
pubnub_chamebl_t pbcc_get_delete_messages_response(struct pbcc_context* pb)
602+
{
603+
pubnub_chamebl_t resp;
604+
char const* reply = pb->http_reply;
605+
int reply_len = pb->http_buf_len;
606+
607+
if (PNR_OK != pb->last_result) {
608+
PUBNUB_LOG_ERROR("pbcc_get_delete_messages_response(pb=%p) can be "
609+
"called only if previous transactin "
610+
"PBTT_DELETE_MESSAGES(%d) is finished successfully. "
611+
"Transaction result was: %d('%s')\n",
612+
pb,
613+
PBTT_DELETE_MESSAGES,
614+
pb->last_result,
615+
pubnub_res_2_string(pb->last_result));
616+
resp.ptr = NULL;
617+
resp.size = 0;
618+
return resp;
619+
}
620+
621+
resp.ptr = (char*)reply;
622+
resp.size = reply_len;
623+
return resp;
624+
}
625+
626+
enum pubnub_res pbcc_parse_delete_messages_response(struct pbcc_context* pb)
627+
{
628+
enum pbjson_object_name_parse_result jpresult;
629+
struct pbjson_elem el;
630+
struct pbjson_elem found;
631+
char* reply = pb->http_reply;
632+
int reply_len = pb->http_buf_len;
633+
634+
if ((reply[0] != '{') || (reply[reply_len - 1] != '}')) {
635+
PUBNUB_LOG_ERROR(
636+
"Error: pbcc_parse_delete_messages_response(pbcc=%p) - "
637+
"Response is not json object: response='%.*s'\n",
638+
pb,
639+
reply_len,
640+
reply);
641+
return PNR_FORMAT_ERROR;
642+
}
643+
el.start = reply;
644+
el.end = reply + reply_len;
645+
if (pbjson_value_for_field_found(&el, "status", "403")) {
646+
PUBNUB_LOG_ERROR(
647+
"Error: pbcc_parse_delete_messages_response(pbcc=%p) - "
648+
"Access Denied: response='%.*s'\n",
649+
pb,
650+
reply_len,
651+
reply);
652+
return PNR_ACCESS_DENIED;
653+
}
654+
jpresult = pbjson_get_object_value(&el, "error", &found);
655+
if (jonmpOK == jpresult) {
656+
if (pbjson_elem_equals_string(&found, "true")) {
657+
return PNR_ERROR_ON_SERVER;
658+
}
659+
}
660+
else {
661+
PUBNUB_LOG_ERROR(
662+
"Error: pbcc_parse_delete_messages_response(pbcc=%p) - "
663+
"'error' atribute not found in the response. error=%d\n"
664+
"response='%.*s'\n",
665+
pb,
666+
jpresult,
667+
reply_len,
668+
reply);
669+
return PNR_FORMAT_ERROR;
670+
}
671+
pb->chan_ofs = pb->chan_end = 0;
672+
673+
return PNR_OK;
674+
}

core/pbcc_advanced_history.h

+37
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,43 @@ enum pubnub_res pbcc_message_counts_prep(
7070
char const* channel,
7171
char const* timetoken,
7272
char const* channel_timetokens);
73+
74+
/**
75+
* @brief Prepare `delete messages` operation (transaction), mostly by
76+
* formatting the URI of the HTTP request.
77+
*
78+
* @param pb PubNub context which provides resources to send request.
79+
* @param channel Channel from which messages should be deleted.
80+
* @param start Timetoken delimiting the start of time slice (exclusive) to
81+
* delete messages from.
82+
* @param end Timetoken delimiting the end of time slice (inclusive) to
83+
* delete messages to.
84+
* @return Results of `delete messages` transaction call.
85+
*/
86+
enum pubnub_res pbcc_delete_messages_prep(struct pbcc_context* pb,
87+
char const* channel,
88+
char const* start,
89+
char const* end);
90+
91+
/**
92+
* @brief Get `delete messages` service response.
93+
*
94+
* @param pb PubNub context which has been used to delete channel messages.
95+
* @return `pubnub_chamebl_t` with pointer to string with response.
96+
*/
97+
pubnub_chamebl_t pbcc_get_delete_messages_response(struct pbcc_context* pb);
98+
99+
100+
101+
/**
102+
* @brief Parses the string received as a response for a history v3
103+
* operation (transaction).
104+
*
105+
* @param pb PubNub context to parse delete channel messages response.
106+
* @return PNR_OK: OK, !PNR_OK: error (invalid response)
107+
*/
108+
enum pubnub_res pbcc_parse_delete_messages_response(struct pbcc_context* pb);
109+
73110
#endif /* INC_PBCC_ADVANCED_HISTORY */
74111
#endif /* PUBNUB_USE_ADVANCED_HISTORY */
75112

0 commit comments

Comments
 (0)