Skip to content

Commit c0910f6

Browse files
authored
strings in objects API (#185)
feat: possibility to use string in actions API Added possibility to use strings in actions API refactor: `pubnub_action_type` deprecated `pubnub_action_type` enum has been deprecated
1 parent 436ac8a commit c0910f6

8 files changed

+153
-37
lines changed

.pubnub.yml

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
name: c-core
22
schema: 1
3-
version: "4.9.0"
3+
version: "4.10.0"
44
scm: github.com/pubnub/c-core
55
changelog:
6+
- date: 2024-06-14
7+
version: v4.10.0
8+
changes:
9+
- type: feature
10+
text: "Added possibility to use strings in actions API."
11+
- type: improvement
12+
text: "`pubnub_action_type` enum has been deprecated."
613
- date: 2024-03-26
714
version: v4.9.1
815
changes:
@@ -787,7 +794,7 @@ sdks:
787794
distribution-type: source code
788795
distribution-repository: GitHub release
789796
package-name: C-Core
790-
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
797+
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
791798
requires:
792799
-
793800
name: "miniz"
@@ -853,7 +860,7 @@ sdks:
853860
distribution-type: source code
854861
distribution-repository: GitHub release
855862
package-name: C-Core
856-
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
863+
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
857864
requires:
858865
-
859866
name: "miniz"
@@ -919,7 +926,7 @@ sdks:
919926
distribution-type: source code
920927
distribution-repository: GitHub release
921928
package-name: C-Core
922-
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
929+
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
923930
requires:
924931
-
925932
name: "miniz"
@@ -981,7 +988,7 @@ sdks:
981988
distribution-type: source code
982989
distribution-repository: GitHub release
983990
package-name: C-Core
984-
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
991+
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
985992
requires:
986993
-
987994
name: "miniz"
@@ -1042,7 +1049,7 @@ sdks:
10421049
distribution-type: source code
10431050
distribution-repository: GitHub release
10441051
package-name: C-Core
1045-
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
1052+
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
10461053
requires:
10471054
-
10481055
name: "miniz"
@@ -1098,7 +1105,7 @@ sdks:
10981105
distribution-type: source code
10991106
distribution-repository: GitHub release
11001107
package-name: C-Core
1101-
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
1108+
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
11021109
requires:
11031110
-
11041111
name: "miniz"
@@ -1151,7 +1158,7 @@ sdks:
11511158
distribution-type: source code
11521159
distribution-repository: GitHub release
11531160
package-name: C-Core
1154-
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
1161+
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
11551162
requires:
11561163
-
11571164
name: "miniz"

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## v4.10.0
2+
June 14 2024
3+
4+
#### Added
5+
- Added possibility to use strings in actions API.
6+
7+
#### Modified
8+
- `pubnub_action_type` enum has been deprecated.
9+
10+
11+
112
## v4.9.1
213
March 26 2024
314

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CMakeLists.txt for the C-core library of Pubnub.
22
cmake_minimum_required(VERSION 3.12)
33

4-
project(c-core VERSION 4.8.0)
4+
project(c-core VERSION 4.10.0)
55

66
message(STATUS Preparing\ ${PROJECT_NAME}\ version\ ${PROJECT_VERSION})
77

core/pbcc_actions_api.c

+40-20
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,45 @@
1717

1818
/* Maximum number of actions to return in response */
1919
#define MAX_ACTIONS_LIMIT 100
20+
#define MAX_ACTION_TYPE_LENGTH 15
2021

2122

2223
enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
2324
char* obj_buffer,
2425
size_t buffer_size,
2526
enum pubnub_action_type actype,
26-
char const** val)
27-
{
28-
char const* user_id = pbcc_user_id_get(pb);
27+
char const** val) {
2928
char const* type_literal;
3029

30+
switch(actype) {
31+
case pbactypReaction:
32+
type_literal = "\"reaction\"";
33+
break;
34+
case pbactypReceipt:
35+
type_literal = "\"receipt\"";
36+
break;
37+
case pbactypCustom:
38+
type_literal = "\"custom\"";
39+
break;
40+
default:
41+
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
42+
"unknown action type = %d\n",
43+
pb,
44+
actype);
45+
return PNR_INVALID_PARAMETERS;
46+
}
47+
48+
return pbcc_form_the_action_object_str(pb, obj_buffer, buffer_size, type_literal, val);
49+
}
50+
51+
52+
enum pubnub_res pbcc_form_the_action_object_str(struct pbcc_context* pb,
53+
char* obj_buffer,
54+
size_t buffer_size,
55+
char const* action_type,
56+
char const** val) {
57+
char const* user_id = pbcc_user_id_get(pb);
58+
3159
PUBNUB_ASSERT_OPT(user_id != NULL);
3260

3361
if (NULL == user_id) {
@@ -42,25 +70,17 @@ enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
4270
*val);
4371
return PNR_INVALID_PARAMETERS;
4472
}
45-
switch(actype) {
46-
case pbactypReaction:
47-
type_literal = "reaction";
48-
break;
49-
case pbactypReceipt:
50-
type_literal = "receipt";
51-
break;
52-
case pbactypCustom:
53-
type_literal = "custom";
54-
break;
55-
default:
73+
if (('\"' != *action_type) || ('\"' != *(action_type + pb_strnlen_s(action_type, MAX_ACTION_TYPE_LENGTH) - 1))) {
5674
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
57-
"unknown action type = %d\n",
75+
"quotation marks on action type ends are missing: "
76+
"action_type = %s\n",
5877
pb,
59-
actype);
78+
action_type);
6079
return PNR_INVALID_PARAMETERS;
6180
}
81+
6282
if (buffer_size < sizeof("{\"type\":\"\",\"value\":,\"user_id\":\"\"}") +
63-
pb_strnlen_s(type_literal, sizeof "reaction") +
83+
pb_strnlen_s(action_type, MAX_ACTION_TYPE_LENGTH) +
6484
pb_strnlen_s(*val, PUBNUB_MAX_OBJECT_LENGTH) +
6585
pb->user_id_len) {
6686
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
@@ -70,15 +90,15 @@ enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
7090
pb,
7191
(unsigned long)buffer_size,
7292
(unsigned long)(sizeof("{\"type\":\"\",\"value\":,\"user_id\":\"\"}") +
73-
pb_strnlen_s(type_literal, sizeof "reaction") +
93+
pb_strnlen_s(action_type, MAX_ACTION_TYPE_LENGTH) +
7494
pb_strnlen_s(*val, PUBNUB_MAX_OBJECT_LENGTH) +
7595
pb->user_id_len));
7696
return PNR_TX_BUFF_TOO_SMALL;
7797
}
7898
snprintf(obj_buffer,
7999
buffer_size,
80-
"{\"type\":\"%s\",\"value\":%s,\"user_id\":\"%s\"}",
81-
type_literal,
100+
"{\"type\":%s,\"value\":%s,\"user_id\":\"%s\"}",
101+
action_type,
82102
*val,
83103
user_id);
84104
*val = obj_buffer;

core/pbcc_actions_api.h

+18-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "pubnub_api_types.h"
99
#include "pubnub_memory_block.h"
10+
#include "lib/pb_deprecated.h"
1011

1112
struct pbcc_context;
1213

@@ -25,14 +26,30 @@ enum pubnub_action_type {
2526
};
2627

2728
/** Forms the action object to be sent in 'pubnub_add_action' request body.
29+
30+
@deprecated This function is deprecated. Use pbcc_form_the_action_object_str() instead.
31+
The present declaration will be changed to the string version in the future.
32+
@see pbcc_form_the_action_object_str
33+
2834
@return #PNR_OK on success, an error otherwise
2935
*/
30-
enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
36+
PUBNUB_DEPRECATED enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
3137
char* obj_buffer,
3238
size_t buffer_size,
3339
enum pubnub_action_type actype,
3440
char const** json);
3541

42+
43+
/** Forms the action object to be sent in 'pubnub_add_action' request body.
44+
@return #PNR_OK on success, an error otherwise
45+
*/
46+
enum pubnub_res pbcc_form_the_action_object_str(struct pbcc_context* pb,
47+
char* obj_buffer,
48+
size_t buffer_size,
49+
const char* action_type,
50+
char const** json);
51+
52+
3653
/** Prepares the 'add_action' transaction, mostly by
3754
formatting the URI of the HTTP request.
3855
*/

core/pubnub_actions_api.c

+33-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,40 @@
1717
#include <string.h>
1818

1919

20-
enum pubnub_res pubnub_add_message_action(pubnub_t* pb,
20+
enum pubnub_res pubnub_add_message_action(pubnub_t *pb,
21+
const char *channel,
22+
const char *message_timetoken,
23+
enum pubnub_action_type actype,
24+
const char *value) {
25+
char const* type_literal;
26+
27+
switch(actype) {
28+
case pbactypReaction:
29+
type_literal = "\"reaction\"";
30+
break;
31+
case pbactypReceipt:
32+
type_literal = "\"receipt\"";
33+
break;
34+
case pbactypCustom:
35+
type_literal = "\"custom\"";
36+
break;
37+
default:
38+
PUBNUB_LOG_ERROR("pubnub_add_message_action(pbcc=%p) - "
39+
"unknown action type = %d\n",
40+
pb,
41+
actype);
42+
return PNR_INVALID_PARAMETERS;
43+
}
44+
45+
return pubnub_add_message_action_str(pb, channel, message_timetoken, type_literal, value);
46+
}
47+
48+
49+
enum pubnub_res pubnub_add_message_action_str(pubnub_t* pb,
2150
char const* channel,
2251
char const* message_timetoken,
23-
enum pubnub_action_type actype,
24-
char const* value)
25-
{
52+
char const* actype,
53+
char const* value) {
2654
enum pubnub_res rslt;
2755
char obj_buffer[PUBNUB_BUF_MAXLEN];
2856

@@ -34,7 +62,7 @@ enum pubnub_res pubnub_add_message_action(pubnub_t* pb,
3462
pubnub_mutex_unlock(pb->monitor);
3563
return PNR_IN_PROGRESS;
3664
}
37-
rslt = pbcc_form_the_action_object(&pb->core,
65+
rslt = pbcc_form_the_action_object_str(&pb->core,
3866
obj_buffer,
3967
sizeof obj_buffer,
4068
actype,

core/pubnub_actions_api.h

+34-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "lib/pb_extern.h"
99
#include "pbcc_actions_api.h"
10+
#include "lib/pb_deprecated.h"
1011

1112
#include <stdbool.h>
1213

@@ -18,6 +19,12 @@
1819
If the transaction is finished successfully response will have 'data' field with
1920
added action data. If there is no data, nor error description in the response,
2021
response parsing function returns format error.
22+
23+
@deprecated This function is deprecated. Use pubnub_add_message_action_str() instead.
24+
The present declaration will be changed to the string version in the future.
25+
@see pubnub_add_message_action_str()
26+
@see pbcc_action_type_to_str
27+
2128
@param pb The pubnub context. Can't be NULL
2229
@param channel The channel on which action is referring to.
2330
@param message_timetoken The timetoken(unquoted) of a published message action is
@@ -26,13 +33,39 @@
2633
@param value Json string describing the action that is to be added
2734
@return #PNR_STARTED on success, an error otherwise
2835
*/
29-
PUBNUB_EXTERN enum pubnub_res pubnub_add_message_action(pubnub_t* pb,
36+
PUBNUB_EXTERN PUBNUB_DEPRECATED enum pubnub_res pubnub_add_message_action(pubnub_t* pb,
3037
char const* channel,
3138
char const* message_timetoken,
3239
enum pubnub_action_type actype,
3340
char const* value);
3441

3542

43+
/** Adds new type of message called action as a support for user reactions on a published
44+
messages.
45+
Json string @p value is checked for its quotation marks at its ends. If any of the
46+
quotation marks is missing function returns parameter error.
47+
If the transaction is finished successfully response will have 'data' field with
48+
added action data. If there is no data, nor error description in the response,
49+
response parsing function returns format error.
50+
51+
@deprecated This function is deprecated. Use pubnub_add_message_action_str() instead.
52+
@see pubnub_add_message_action_str()
53+
54+
@param pb The pubnub context. Can't be NULL
55+
@param channel The channel on which action is referring to.
56+
@param message_timetoken The timetoken(unquoted) of a published message action is
57+
applying to
58+
@param action_type Jsoned string describing the action type (Max 15 characters without whitespaces)
59+
@param value Json string describing the action that is to be added
60+
@return #PNR_STARTED on success, an error otherwise
61+
*/
62+
PUBNUB_EXTERN enum pubnub_res pubnub_add_message_action_str(pubnub_t* pb,
63+
char const* channel,
64+
char const* message_timetoken,
65+
char const* action_type,
66+
char const* value);
67+
68+
3669
/** Searches the response(if previous transaction on the @p pb context had been
3770
pubnub_add_message_action and was accomplished successfully) and retrieves timetoken of
3871
a message action was added on.

core/pubnub_version_internal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define INC_PUBNUB_VERSION_INTERNAL
44

55

6-
#define PUBNUB_SDK_VERSION "4.9.0"
6+
#define PUBNUB_SDK_VERSION "4.10.0"
77

88

99
#endif /* !defined INC_PUBNUB_VERSION_INTERNAL */

0 commit comments

Comments
 (0)