Skip to content

Commit

Permalink
Create wrappers for ListChannelsFromGroup and ListUserSubscribedChannels
Browse files Browse the repository at this point in the history
  • Loading branch information
KGronek-Pubnub committed Oct 29, 2024
1 parent 804ec98 commit c42ccfa
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2024 PubNub Inc. All Rights Reserved.


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

FString UPubnubJsonUtilities::JsonObjectToString(TSharedPtr<FJsonObject> JsonObject)
{
FString JsonString;
TSharedRef< TJsonWriter<> > JsonWriter = TJsonWriterFactory<>::Create(&JsonString);
FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter);
return JsonString;
}

bool UPubnubJsonUtilities::StringToJsonObject(FString JsonString, TSharedPtr<FJsonObject>& JsonObject)
{
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonString);
bool Result = FJsonSerializer::Deserialize(JsonReader, JsonObject);
return Result;
}

void UPubnubJsonUtilities::ListChannelsFromGroupJsonToData(FString ResponseJson, bool& Error, int& Status, TArray<FString>& Channels)
{
Channels.Empty();

TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);

if(!StringToJsonObject(ResponseJson, JsonObject))
{
Error = true;
return;
}

if(JsonObject->HasField(ANSI_TO_TCHAR("error")))
{
Error = JsonObject->GetBoolField(ANSI_TO_TCHAR("error"));
}

if(JsonObject->HasField(ANSI_TO_TCHAR("status")))
{
Status = JsonObject->GetIntegerField(ANSI_TO_TCHAR("status"));
}

if(!JsonObject->HasField(ANSI_TO_TCHAR("payload")))
{
return;
}

TSharedPtr<FJsonObject> PayloadJsonObject = JsonObject->GetObjectField(ANSI_TO_TCHAR("payload"));

if(PayloadJsonObject->HasField(ANSI_TO_TCHAR("channels")))
{
TArray<TSharedPtr<FJsonValue>> ChannelsJsonValue = PayloadJsonObject->GetArrayField(ANSI_TO_TCHAR("channels"));

for(auto ChannelJsonValue : ChannelsJsonValue)
{
Channels.Add(ChannelJsonValue->AsString());
}
}
}

void UPubnubJsonUtilities::ListUserSubscribedChannelsJsonToData(FString ResponseJson, int& Status, FString& Message, TArray<FString>& Channels)
{
Channels.Empty();

TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);

if(!StringToJsonObject(ResponseJson, JsonObject))
{
return;
}

if(JsonObject->HasField(ANSI_TO_TCHAR("status")))
{
Status = JsonObject->GetIntegerField(ANSI_TO_TCHAR("status"));
}

if(JsonObject->HasField(ANSI_TO_TCHAR("message")))
{
Message = JsonObject->GetStringField(ANSI_TO_TCHAR("message"));
}

if(!JsonObject->HasField(ANSI_TO_TCHAR("payload")))
{
return;
}

TSharedPtr<FJsonObject> PayloadJsonObject = JsonObject->GetObjectField(ANSI_TO_TCHAR("payload"));

if(PayloadJsonObject->HasField(ANSI_TO_TCHAR("channels")))
{
TArray<TSharedPtr<FJsonValue>> ChannelsJsonValue = PayloadJsonObject->GetArrayField(ANSI_TO_TCHAR("channels"));

for(auto ChannelJsonValue : ChannelsJsonValue)
{
Channels.Add(ChannelJsonValue->AsString());
}
}
}
20 changes: 0 additions & 20 deletions Source/PubnubLibrary/Private/FunctionLibraries/PubnubUtilities.cpp

This file was deleted.

96 changes: 81 additions & 15 deletions Source/PubnubLibrary/Private/PubnubSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "Json.h"

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

Expand Down Expand Up @@ -202,14 +202,25 @@ void UPubnubSubsystem::RemoveChannelFromGroup(FString ChannelName, FString Chann
});
}

void UPubnubSubsystem::ListChannelsFromGroup(FString ChannelGroup, FOnPubnubResponse OnListChannelsResponse)
void UPubnubSubsystem::ListChannelsFromGroup(FString ChannelGroup, FOnListChannelsFromGroupResponse OnListChannelsResponse)
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this, ChannelGroup, OnListChannelsResponse]
{
ListChannelsFromGroup_priv(ChannelGroup, OnListChannelsResponse);
ListChannelsFromGroup_DATA_priv(ChannelGroup, OnListChannelsResponse);
});
}

void UPubnubSubsystem::ListChannelsFromGroup_JSON(FString ChannelGroup, FOnPubnubResponse OnListChannelsResponse)
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this, ChannelGroup, OnListChannelsResponse]
{
ListChannelsFromGroup_JSON_priv(ChannelGroup, OnListChannelsResponse);
});
}

Expand All @@ -235,14 +246,25 @@ void UPubnubSubsystem::ListUsersFromChannel(FString ChannelName, FOnPubnubRespon
});
}

void UPubnubSubsystem::ListUserSubscribedChannels(FString UserID, FOnPubnubResponse ListUserSubscribedChannelsResponse)
void UPubnubSubsystem::ListUserSubscribedChannels(FString UserID, FOnListUsersSubscribedChannelsResponse ListUserSubscribedChannelsResponse)
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this, UserID, ListUserSubscribedChannelsResponse]
{
ListUserSubscribedChannels_priv(UserID, ListUserSubscribedChannelsResponse);
ListUserSubscribedChannels_DATA_priv(UserID, ListUserSubscribedChannelsResponse);
});
}

void UPubnubSubsystem::ListUserSubscribedChannels_JSON(FString UserID, FOnPubnubResponse ListUserSubscribedChannelsResponse)
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this, UserID, ListUserSubscribedChannelsResponse]
{
ListUserSubscribedChannels_JSON_priv(UserID, ListUserSubscribedChannelsResponse);
});
}

Expand Down Expand Up @@ -654,7 +676,7 @@ FString UPubnubSubsystem::GrantTokenStructureToJsonString(FPubnubGrantTokenStruc
PermissionsJsonObject->SetObjectField("permissions", TokenStructureJsonObject);

//Convert created Json object to string
return UPubnubUtilities::JsonObjectToString(PermissionsJsonObject);
return UPubnubJsonUtilities::JsonObjectToString(PermissionsJsonObject);
}

void UPubnubSubsystem::SystemPublish(FString ChannelOpt)
Expand Down Expand Up @@ -1182,18 +1204,21 @@ void UPubnubSubsystem::RemoveChannelFromGroup_priv(FString ChannelName, FString
pubnub_remove_channel_from_group(ctx_pub, TCHAR_TO_ANSI(*ChannelName), TCHAR_TO_ANSI(*ChannelGroup));
}

void UPubnubSubsystem::ListChannelsFromGroup_priv(FString ChannelGroup,
FOnPubnubResponse OnListChannelsResponse)
FString UPubnubSubsystem::ListChannelsFromGroup_pn(FString ChannelGroup)
{
if(!CheckIsUserIDSet())
{return;}
{return "";}

if(CheckIsFieldEmpty(ChannelGroup, "ChannelGroup", "ListChannelsFromGroup"))
{return;}
{return "";}

pubnub_list_channel_group(ctx_pub, TCHAR_TO_ANSI(*ChannelGroup));
FString JsonResponse = GetLastChannelResponse(ctx_pub);
return GetLastChannelResponse(ctx_pub);
}

void UPubnubSubsystem::ListChannelsFromGroup_JSON_priv(FString ChannelGroup, FOnPubnubResponse OnListChannelsResponse)
{
FString JsonResponse = ListChannelsFromGroup_pn(ChannelGroup);

//Delegate needs to be executed back on Game Thread
AsyncTask(ENamedThreads::GameThread, [this, OnListChannelsResponse, JsonResponse]()
Expand All @@ -1203,6 +1228,24 @@ void UPubnubSubsystem::ListChannelsFromGroup_priv(FString ChannelGroup,
});
}

void UPubnubSubsystem::ListChannelsFromGroup_DATA_priv(FString ChannelGroup, FOnListChannelsFromGroupResponse OnListChannelsResponse)
{
FString JsonResponse = ListChannelsFromGroup_pn(ChannelGroup);

//Delegate needs to be executed back on Game Thread
AsyncTask(ENamedThreads::GameThread, [this, OnListChannelsResponse, JsonResponse]()
{
//Parse Json response into data
bool Error;
int Status;
TArray<FString> Channels;
UPubnubJsonUtilities::ListChannelsFromGroupJsonToData(JsonResponse, Error, Status, Channels);

//Broadcast bound delegate with parsed response
OnListChannelsResponse.ExecuteIfBound(Error, Status, Channels);
});
}

void UPubnubSubsystem::RemoveChannelGroup_priv(FString ChannelGroup)
{
if(!CheckIsUserIDSet())
Expand Down Expand Up @@ -1242,17 +1285,22 @@ void UPubnubSubsystem::ListUsersFromChannel_priv(FString ChannelName, FOnPubnubR
});
}

void UPubnubSubsystem::ListUserSubscribedChannels_priv(FString UserID, FOnPubnubResponse ListUserSubscribedChannelsResponse)
FString UPubnubSubsystem::ListUserSubscribedChannels_pn(FString UserID)
{
if(!CheckIsUserIDSet())
{return;}
{return "";}

if(CheckIsFieldEmpty(UserID, "UserID", "ListUserSubscribedChannels"))
{return;}
{return "";}

pubnub_where_now(ctx_pub, TCHAR_TO_ANSI(*UserID));
FString JsonResponse = GetLastResponse(ctx_pub);
return GetLastResponse(ctx_pub);
}

void UPubnubSubsystem::ListUserSubscribedChannels_JSON_priv(FString UserID, FOnPubnubResponse ListUserSubscribedChannelsResponse)
{
FString JsonResponse = ListUserSubscribedChannels_pn(UserID);

//Delegate needs to be executed back on Game Thread
AsyncTask(ENamedThreads::GameThread, [this, ListUserSubscribedChannelsResponse, JsonResponse]()
{
Expand All @@ -1261,6 +1309,24 @@ void UPubnubSubsystem::ListUserSubscribedChannels_priv(FString UserID, FOnPubnub
});
}

void UPubnubSubsystem::ListUserSubscribedChannels_DATA_priv(FString UserID, FOnListUsersSubscribedChannelsResponse ListUserSubscribedChannelsResponse)
{
FString JsonResponse = ListUserSubscribedChannels_pn(UserID);

//Delegate needs to be executed back on Game Thread
AsyncTask(ENamedThreads::GameThread, [this, ListUserSubscribedChannelsResponse, JsonResponse]()
{
//Parse Json response into data
int Status;
FString Message;
TArray<FString> Channels;
UPubnubJsonUtilities::ListUserSubscribedChannelsJsonToData(JsonResponse, Status, Message, Channels);

//Broadcast bound delegate with parsed response
ListUserSubscribedChannelsResponse.ExecuteIfBound(Status, Message, Channels);
});
}

void UPubnubSubsystem::SetState_priv(FString ChannelName, FString StateJson, FPubnubSetStateSettings SetStateSettings)
{
if(!CheckIsUserIDSet())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 PubNub Inc. All Rights Reserved.

#pragma once

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

class FJsonObject;

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

static FString JsonObjectToString(TSharedPtr<FJsonObject> JsonObject);
//Convert FString to JsonObject. Returns true if conversion was successful
static bool StringToJsonObject(FString JsonString, TSharedPtr<FJsonObject> &JsonObject);

/* JSON CONVERTERS*/

UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities")
static void ListChannelsFromGroupJsonToData(FString ResponseJson, bool &Error, int &Status, TArray<FString>& Channels);

UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities")
static void ListUserSubscribedChannelsJsonToData(FString ResponseJson, int &Status, FString &Message, TArray<FString>& Channels);
};
23 changes: 0 additions & 23 deletions Source/PubnubLibrary/Public/FunctionLibraries/PubnubUtilities.h

This file was deleted.

4 changes: 2 additions & 2 deletions Source/PubnubLibrary/Public/PubnubStructLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ struct FPubnubGrantTokenStructure
{
GENERATED_BODY()

UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") int TTLMinutes;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString AuthorizedUUID;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") int TTLMinutes = 0;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString AuthorizedUUID = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") TArray<FString> Channels;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") TArray<FPubnubChannelPermissions> ChannelPermissions;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") TArray<FString> ChannelGroups;
Expand Down
Loading

0 comments on commit c42ccfa

Please sign in to comment.