Skip to content

Commit

Permalink
Add quotes automatically to the fields that require them.
Browse files Browse the repository at this point in the history
  • Loading branch information
KGronek-Pubnub committed Nov 4, 2024
1 parent 641f3db commit 93dc0e4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
16 changes: 16 additions & 0 deletions Source/PubnubLibrary/Private/FunctionLibraries/PubnubUtilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 PubNub Inc. All Rights Reserved.


#include "FunctionLibraries/PubnubUtilities.h"
#include "Json.h"


FString UPubnubUtilities::AddQuotesToString(const FString InString, bool SkipIfHasQuotes)
{
if(InString.Left(1) != "\"" || InString.Right(1) != "\"" || !SkipIfHasQuotes)
{
return "\"" + InString + "\"";
}

return InString;
}
23 changes: 16 additions & 7 deletions Source/PubnubLibrary/Private/PubnubSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "Config/PubnubSettings.h"
#include "FunctionLibraries/PubnubJsonUtilities.h"
#include "FunctionLibraries/PubnubUtilities.h"
#include "Threads/PubnubFunctionThread.h"
#include "Threads/PubnubLoopingThread.h"

Expand Down Expand Up @@ -2223,8 +2224,12 @@ void UPubnubSubsystem::AddMessageAction_priv(FString ChannelName, FString Messag

if(CheckIsFieldEmpty(ChannelName, "ChannelName", "AddMessageAction") || CheckIsFieldEmpty(MessageTimetoken, "MessageTimetoken", "AddMessageAction"))
{return;}

//Add quotes to these fields as they are required by C-Core
FString FinalActionType = UPubnubUtilities::AddQuotesToString(ActionType);
FString FinalValue = UPubnubUtilities::AddQuotesToString(Value);

pubnub_add_message_action_str(ctx_pub, TCHAR_TO_ANSI(*ChannelName), TCHAR_TO_ANSI(*MessageTimetoken), TCHAR_TO_ANSI(*ActionType), TCHAR_TO_ANSI(*Value));
pubnub_add_message_action_str(ctx_pub, TCHAR_TO_ANSI(*ChannelName), TCHAR_TO_ANSI(*MessageTimetoken), TCHAR_TO_ANSI(*FinalActionType), TCHAR_TO_ANSI(*FinalValue));
pubnub_res PubnubResponse = pubnub_await(ctx_pub);
if(PubnubResponse != PNR_OK)
{
Expand Down Expand Up @@ -2258,24 +2263,28 @@ void UPubnubSubsystem::RemoveMessageAction_priv(FString ChannelName, FString Mes
|| CheckIsFieldEmpty(ActionTimetoken, "ActionTimetoken", "RemoveMessageAction"))
{return;}

auto MessageTimetokenConverter = StringCast<ANSICHAR>(*MessageTimetoken);
auto ActionTimetokenConverter = StringCast<ANSICHAR>(*ActionTimetoken);
//Add quotes to these fields as they are required by C-Core
FString FinalMessageTimetoken = UPubnubUtilities::AddQuotesToString(MessageTimetoken);
FString FinalActionTimetoken = UPubnubUtilities::AddQuotesToString(ActionTimetoken);

auto MessageTimetokenConverter = StringCast<ANSICHAR>(*FinalMessageTimetoken);
auto ActionTimetokenConverter = StringCast<ANSICHAR>(*FinalActionTimetoken);

// Allocate memory for message_timetoken_char and copy the content
char* message_timetoken_char = new char[MessageTimetoken.Len() + 1];
char* message_timetoken_char = new char[FinalMessageTimetoken.Len() + 1];
std::strcpy(message_timetoken_char, MessageTimetokenConverter.Get());

pubnub_chamebl_t message_timetoken_chamebl;
message_timetoken_chamebl.ptr = message_timetoken_char;
message_timetoken_chamebl.size = MessageTimetoken.Len();
message_timetoken_chamebl.size = FinalMessageTimetoken.Len();

// Allocate memory for action_timetoken_char and copy the content
char* action_timetoken_char = new char[ActionTimetoken.Len() + 1];
char* action_timetoken_char = new char[FinalActionTimetoken.Len() + 1];
std::strcpy(action_timetoken_char, ActionTimetokenConverter.Get());

pubnub_chamebl_t action_timetoken_chamebl;
action_timetoken_chamebl.ptr = action_timetoken_char;
action_timetoken_chamebl.size = ActionTimetoken.Len();
action_timetoken_chamebl.size = FinalActionTimetoken.Len();

pubnub_remove_message_action(ctx_pub, TCHAR_TO_ANSI(*ChannelName), message_timetoken_chamebl, action_timetoken_chamebl);

Expand Down
19 changes: 19 additions & 0 deletions Source/PubnubLibrary/Public/FunctionLibraries/PubnubUtilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 PubNub Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "PubnubUtilities.generated.h"

/**
*
*/
UCLASS()
class PUBNUBLIBRARY_API UPubnubUtilities : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:

static FString AddQuotesToString(const FString InString, bool SkipIfHasQuotes = true);
};

0 comments on commit 93dc0e4

Please sign in to comment.