From 93dc0e4f9ffc59f92fe15ca7a800a23954e344f8 Mon Sep 17 00:00:00 2001 From: Kamil Gronek Date: Mon, 4 Nov 2024 14:07:46 +0100 Subject: [PATCH] Add quotes automatically to the fields that require them. --- .../FunctionLibraries/PubnubUtilities.cpp | 16 +++++++++++++ .../PubnubLibrary/Private/PubnubSubsystem.cpp | 23 +++++++++++++------ .../FunctionLibraries/PubnubUtilities.h | 19 +++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 Source/PubnubLibrary/Private/FunctionLibraries/PubnubUtilities.cpp create mode 100644 Source/PubnubLibrary/Public/FunctionLibraries/PubnubUtilities.h diff --git a/Source/PubnubLibrary/Private/FunctionLibraries/PubnubUtilities.cpp b/Source/PubnubLibrary/Private/FunctionLibraries/PubnubUtilities.cpp new file mode 100644 index 0000000..572bf24 --- /dev/null +++ b/Source/PubnubLibrary/Private/FunctionLibraries/PubnubUtilities.cpp @@ -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; +} diff --git a/Source/PubnubLibrary/Private/PubnubSubsystem.cpp b/Source/PubnubLibrary/Private/PubnubSubsystem.cpp index a44c915..1081302 100644 --- a/Source/PubnubLibrary/Private/PubnubSubsystem.cpp +++ b/Source/PubnubLibrary/Private/PubnubSubsystem.cpp @@ -5,6 +5,7 @@ #include "Config/PubnubSettings.h" #include "FunctionLibraries/PubnubJsonUtilities.h" +#include "FunctionLibraries/PubnubUtilities.h" #include "Threads/PubnubFunctionThread.h" #include "Threads/PubnubLoopingThread.h" @@ -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) { @@ -2258,24 +2263,28 @@ void UPubnubSubsystem::RemoveMessageAction_priv(FString ChannelName, FString Mes || CheckIsFieldEmpty(ActionTimetoken, "ActionTimetoken", "RemoveMessageAction")) {return;} - auto MessageTimetokenConverter = StringCast(*MessageTimetoken); - auto ActionTimetokenConverter = StringCast(*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(*FinalMessageTimetoken); + auto ActionTimetokenConverter = StringCast(*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); diff --git a/Source/PubnubLibrary/Public/FunctionLibraries/PubnubUtilities.h b/Source/PubnubLibrary/Public/FunctionLibraries/PubnubUtilities.h new file mode 100644 index 0000000..9e3a8ee --- /dev/null +++ b/Source/PubnubLibrary/Public/FunctionLibraries/PubnubUtilities.h @@ -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); +};