Skip to content

Commit

Permalink
strings in objects API (#185)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Xavrax authored Jun 14, 2024
1 parent 436ac8a commit c0910f6
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 37 deletions.
23 changes: 15 additions & 8 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
name: c-core
schema: 1
version: "4.9.0"
version: "4.10.0"
scm: github.com/pubnub/c-core
changelog:
- date: 2024-06-14
version: v4.10.0
changes:
- type: feature
text: "Added possibility to use strings in actions API."
- type: improvement
text: "`pubnub_action_type` enum has been deprecated."
- date: 2024-03-26
version: v4.9.1
changes:
Expand Down Expand Up @@ -787,7 +794,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -853,7 +860,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -919,7 +926,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -981,7 +988,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1042,7 +1049,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1098,7 +1105,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
requires:
-
name: "miniz"
Expand Down Expand Up @@ -1151,7 +1158,7 @@ sdks:
distribution-type: source code
distribution-repository: GitHub release
package-name: C-Core
location: https://github.com/pubnub/c-core/releases/tag/v4.9.0
location: https://github.com/pubnub/c-core/releases/tag/v4.10.0
requires:
-
name: "miniz"
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## v4.10.0
June 14 2024

#### Added
- Added possibility to use strings in actions API.

#### Modified
- `pubnub_action_type` enum has been deprecated.



## v4.9.1
March 26 2024

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# CMakeLists.txt for the C-core library of Pubnub.
cmake_minimum_required(VERSION 3.12)

project(c-core VERSION 4.8.0)
project(c-core VERSION 4.10.0)

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

Expand Down
60 changes: 40 additions & 20 deletions core/pbcc_actions_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,45 @@

/* Maximum number of actions to return in response */
#define MAX_ACTIONS_LIMIT 100
#define MAX_ACTION_TYPE_LENGTH 15


enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
char* obj_buffer,
size_t buffer_size,
enum pubnub_action_type actype,
char const** val)
{
char const* user_id = pbcc_user_id_get(pb);
char const** val) {
char const* type_literal;

switch(actype) {
case pbactypReaction:
type_literal = "\"reaction\"";
break;
case pbactypReceipt:
type_literal = "\"receipt\"";
break;
case pbactypCustom:
type_literal = "\"custom\"";
break;
default:
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
"unknown action type = %d\n",
pb,
actype);
return PNR_INVALID_PARAMETERS;
}

return pbcc_form_the_action_object_str(pb, obj_buffer, buffer_size, type_literal, val);
}


enum pubnub_res pbcc_form_the_action_object_str(struct pbcc_context* pb,
char* obj_buffer,
size_t buffer_size,
char const* action_type,
char const** val) {
char const* user_id = pbcc_user_id_get(pb);

PUBNUB_ASSERT_OPT(user_id != NULL);

if (NULL == user_id) {
Expand All @@ -42,25 +70,17 @@ enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
*val);
return PNR_INVALID_PARAMETERS;
}
switch(actype) {
case pbactypReaction:
type_literal = "reaction";
break;
case pbactypReceipt:
type_literal = "receipt";
break;
case pbactypCustom:
type_literal = "custom";
break;
default:
if (('\"' != *action_type) || ('\"' != *(action_type + pb_strnlen_s(action_type, MAX_ACTION_TYPE_LENGTH) - 1))) {
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
"unknown action type = %d\n",
"quotation marks on action type ends are missing: "
"action_type = %s\n",
pb,
actype);
action_type);
return PNR_INVALID_PARAMETERS;
}

if (buffer_size < sizeof("{\"type\":\"\",\"value\":,\"user_id\":\"\"}") +
pb_strnlen_s(type_literal, sizeof "reaction") +
pb_strnlen_s(action_type, MAX_ACTION_TYPE_LENGTH) +
pb_strnlen_s(*val, PUBNUB_MAX_OBJECT_LENGTH) +
pb->user_id_len) {
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
Expand All @@ -70,15 +90,15 @@ enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
pb,
(unsigned long)buffer_size,
(unsigned long)(sizeof("{\"type\":\"\",\"value\":,\"user_id\":\"\"}") +
pb_strnlen_s(type_literal, sizeof "reaction") +
pb_strnlen_s(action_type, MAX_ACTION_TYPE_LENGTH) +
pb_strnlen_s(*val, PUBNUB_MAX_OBJECT_LENGTH) +
pb->user_id_len));
return PNR_TX_BUFF_TOO_SMALL;
}
snprintf(obj_buffer,
buffer_size,
"{\"type\":\"%s\",\"value\":%s,\"user_id\":\"%s\"}",
type_literal,
"{\"type\":%s,\"value\":%s,\"user_id\":\"%s\"}",
action_type,
*val,
user_id);
*val = obj_buffer;
Expand Down
19 changes: 18 additions & 1 deletion core/pbcc_actions_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "pubnub_api_types.h"
#include "pubnub_memory_block.h"
#include "lib/pb_deprecated.h"

struct pbcc_context;

Expand All @@ -25,14 +26,30 @@ enum pubnub_action_type {
};

/** Forms the action object to be sent in 'pubnub_add_action' request body.
@deprecated This function is deprecated. Use pbcc_form_the_action_object_str() instead.
The present declaration will be changed to the string version in the future.
@see pbcc_form_the_action_object_str
@return #PNR_OK on success, an error otherwise
*/
enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
PUBNUB_DEPRECATED enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
char* obj_buffer,
size_t buffer_size,
enum pubnub_action_type actype,
char const** json);


/** Forms the action object to be sent in 'pubnub_add_action' request body.
@return #PNR_OK on success, an error otherwise
*/
enum pubnub_res pbcc_form_the_action_object_str(struct pbcc_context* pb,
char* obj_buffer,
size_t buffer_size,
const char* action_type,
char const** json);


/** Prepares the 'add_action' transaction, mostly by
formatting the URI of the HTTP request.
*/
Expand Down
38 changes: 33 additions & 5 deletions core/pubnub_actions_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,40 @@
#include <string.h>


enum pubnub_res pubnub_add_message_action(pubnub_t* pb,
enum pubnub_res pubnub_add_message_action(pubnub_t *pb,
const char *channel,
const char *message_timetoken,
enum pubnub_action_type actype,
const char *value) {
char const* type_literal;

switch(actype) {
case pbactypReaction:
type_literal = "\"reaction\"";
break;
case pbactypReceipt:
type_literal = "\"receipt\"";
break;
case pbactypCustom:
type_literal = "\"custom\"";
break;
default:
PUBNUB_LOG_ERROR("pubnub_add_message_action(pbcc=%p) - "
"unknown action type = %d\n",
pb,
actype);
return PNR_INVALID_PARAMETERS;
}

return pubnub_add_message_action_str(pb, channel, message_timetoken, type_literal, value);
}


enum pubnub_res pubnub_add_message_action_str(pubnub_t* pb,
char const* channel,
char const* message_timetoken,
enum pubnub_action_type actype,
char const* value)
{
char const* actype,
char const* value) {
enum pubnub_res rslt;
char obj_buffer[PUBNUB_BUF_MAXLEN];

Expand All @@ -34,7 +62,7 @@ enum pubnub_res pubnub_add_message_action(pubnub_t* pb,
pubnub_mutex_unlock(pb->monitor);
return PNR_IN_PROGRESS;
}
rslt = pbcc_form_the_action_object(&pb->core,
rslt = pbcc_form_the_action_object_str(&pb->core,
obj_buffer,
sizeof obj_buffer,
actype,
Expand Down
35 changes: 34 additions & 1 deletion core/pubnub_actions_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "lib/pb_extern.h"
#include "pbcc_actions_api.h"
#include "lib/pb_deprecated.h"

#include <stdbool.h>

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


/** Adds new type of message called action as a support for user reactions on a published
messages.
Json string @p value is checked for its quotation marks at its ends. If any of the
quotation marks is missing function returns parameter error.
If the transaction is finished successfully response will have 'data' field with
added action data. If there is no data, nor error description in the response,
response parsing function returns format error.
@deprecated This function is deprecated. Use pubnub_add_message_action_str() instead.
@see pubnub_add_message_action_str()
@param pb The pubnub context. Can't be NULL
@param channel The channel on which action is referring to.
@param message_timetoken The timetoken(unquoted) of a published message action is
applying to
@param action_type Jsoned string describing the action type (Max 15 characters without whitespaces)
@param value Json string describing the action that is to be added
@return #PNR_STARTED on success, an error otherwise
*/
PUBNUB_EXTERN enum pubnub_res pubnub_add_message_action_str(pubnub_t* pb,
char const* channel,
char const* message_timetoken,
char const* action_type,
char const* value);


/** Searches the response(if previous transaction on the @p pb context had been
pubnub_add_message_action and was accomplished successfully) and retrieves timetoken of
a message action was added on.
Expand Down
2 changes: 1 addition & 1 deletion core/pubnub_version_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define INC_PUBNUB_VERSION_INTERNAL


#define PUBNUB_SDK_VERSION "4.9.0"
#define PUBNUB_SDK_VERSION "4.10.0"


#endif /* !defined INC_PUBNUB_VERSION_INTERNAL */

0 comments on commit c0910f6

Please sign in to comment.