Skip to content

Commit

Permalink
Add wrappers to GetMemberships and GetChannelMembers.
Browse files Browse the repository at this point in the history
  • Loading branch information
KGronek-Pubnub committed Nov 4, 2024
1 parent 61264f6 commit 641f3db
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

FString UPubnubJsonUtilities::JsonObjectToString(TSharedPtr<FJsonObject> JsonObject)
{
if(!JsonObject)
{
return "";
}

FString JsonString;
TSharedRef< TJsonWriter<> > JsonWriter = TJsonWriterFactory<>::Create(&JsonString);
FJsonSerializer::Serialize(JsonObject.ToSharedRef(), JsonWriter);
Expand Down Expand Up @@ -309,6 +314,90 @@ void UPubnubJsonUtilities::GetMessageActionsJsonToData(FString ResponseJson, int
}
}

void UPubnubJsonUtilities::GetMembershipsJsonToData(FString ResponseJson, int& Status, TArray<FPubnubGetMembershipsWrapper>& MembershipsData, FString& PageNext, FString& PagePrev)
{
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);

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

JsonObject->TryGetNumberField(ANSI_TO_TCHAR("status"), Status);
JsonObject->TryGetStringField(ANSI_TO_TCHAR("next"), PageNext);
JsonObject->TryGetStringField(ANSI_TO_TCHAR("prev"), PagePrev);

if(JsonObject->HasField(ANSI_TO_TCHAR("data")))
{
TArray<TSharedPtr<FJsonValue>> MembershipsJsonValue = JsonObject->GetArrayField(ANSI_TO_TCHAR("data"));

for(auto MembershipJsonValue : MembershipsJsonValue)
{
FPubnubGetMembershipsWrapper CurrentMembership;
const TSharedPtr<FJsonObject>* ChannelJsonObject;
if(MembershipJsonValue->AsObject()->TryGetObjectField(ANSI_TO_TCHAR("channel"), ChannelJsonObject))
{
CurrentMembership.Channel = GetChannelDataFromJson(JsonObjectToString(*ChannelJsonObject));
}

const TSharedPtr<FJsonObject>* CustomJsonObject;
if(MembershipJsonValue->AsObject()->TryGetObjectField(ANSI_TO_TCHAR("custom"), CustomJsonObject))
{
CurrentMembership.Custom = JsonObjectToString(*CustomJsonObject);
}

MembershipJsonValue->AsObject()->TryGetStringField(ANSI_TO_TCHAR("status"), CurrentMembership.Status);
MembershipJsonValue->AsObject()->TryGetStringField(ANSI_TO_TCHAR("type"), CurrentMembership.Type);
MembershipJsonValue->AsObject()->TryGetStringField(ANSI_TO_TCHAR("updated"), CurrentMembership.Updated);
MembershipJsonValue->AsObject()->TryGetStringField(ANSI_TO_TCHAR("eTag"), CurrentMembership.ETag);

MembershipsData.Add(CurrentMembership);
}
}
}

void UPubnubJsonUtilities::GetChannelMembersJsonToData(FString ResponseJson, int& Status, TArray<FPubnubGetChannelMembersWrapper>& MembershipsData, FString& PageNext, FString& PagePrev)
{
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);

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

JsonObject->TryGetNumberField(ANSI_TO_TCHAR("status"), Status);
JsonObject->TryGetStringField(ANSI_TO_TCHAR("next"), PageNext);
JsonObject->TryGetStringField(ANSI_TO_TCHAR("prev"), PagePrev);

if(JsonObject->HasField(ANSI_TO_TCHAR("data")))
{
TArray<TSharedPtr<FJsonValue>> MembershipsJsonValue = JsonObject->GetArrayField(ANSI_TO_TCHAR("data"));

for(auto MembershipJsonValue : MembershipsJsonValue)
{
FPubnubGetChannelMembersWrapper CurrentMembership;
const TSharedPtr<FJsonObject>* UserJsonObject;
if(MembershipJsonValue->AsObject()->TryGetObjectField(ANSI_TO_TCHAR("uuid"), UserJsonObject))
{
CurrentMembership.User = GetUserDataFromJson(JsonObjectToString(*UserJsonObject));
}

const TSharedPtr<FJsonObject>* CustomJsonObject;
if(MembershipJsonValue->AsObject()->TryGetObjectField(ANSI_TO_TCHAR("custom"), CustomJsonObject))
{
CurrentMembership.Custom = JsonObjectToString(*CustomJsonObject);
}

MembershipJsonValue->AsObject()->TryGetStringField(ANSI_TO_TCHAR("status"), CurrentMembership.Status);
MembershipJsonValue->AsObject()->TryGetStringField(ANSI_TO_TCHAR("type"), CurrentMembership.Type);
MembershipJsonValue->AsObject()->TryGetStringField(ANSI_TO_TCHAR("updated"), CurrentMembership.Updated);
MembershipJsonValue->AsObject()->TryGetStringField(ANSI_TO_TCHAR("eTag"), CurrentMembership.ETag);

MembershipsData.Add(CurrentMembership);
}
}
}

FPubnubUserData UPubnubJsonUtilities::GetUserDataFromJson(FString ResponseJson)
{
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);
Expand Down
127 changes: 107 additions & 20 deletions Source/PubnubLibrary/Private/PubnubSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,25 @@ void UPubnubSubsystem::RemoveChannelMetadata(FString ChannelMetadataID)
});
}

void UPubnubSubsystem::GetMemberships(FString UUIDMetadataID, FOnPubnubResponse OnGetMembershipResponse, FString Include, int Limit, FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
void UPubnubSubsystem::GetMemberships(FString UUIDMetadataID, FOnGetMembershipsResponse OnGetMembershipResponse, FString Include, int Limit, FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this, UUIDMetadataID, OnGetMembershipResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count]
{
GetMemberships_priv(UUIDMetadataID, OnGetMembershipResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);
GetMemberships_DATA_priv(UUIDMetadataID, OnGetMembershipResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);
});
}

void UPubnubSubsystem::GetMemberships_JSON(FString UUIDMetadataID, FOnPubnubResponse OnGetMembershipResponse, FString Include, int Limit, FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this, UUIDMetadataID, OnGetMembershipResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count]
{
GetMemberships_JSON_priv(UUIDMetadataID, OnGetMembershipResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);
});
}

Expand All @@ -554,14 +565,25 @@ void UPubnubSubsystem::RemoveMemberships(FString UUIDMetadataID, FString RemoveO
});
}

void UPubnubSubsystem::GetChannelMembers(FString ChannelMetadataID, FOnPubnubResponse OnGetMembersResponse, FString Include, int Limit, FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
void UPubnubSubsystem::GetChannelMembers(FString ChannelMetadataID, FOnGetChannelMembersResponse OnGetMembersResponse, FString Include, int Limit, FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this, ChannelMetadataID, OnGetMembersResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count]
{
GetChannelMembers_priv(ChannelMetadataID, OnGetMembersResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);
GetChannelMembers_DATA_priv(ChannelMetadataID, OnGetMembersResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);
});
}

void UPubnubSubsystem::GetChannelMembers_JSON(FString ChannelMetadataID, FOnPubnubResponse OnGetMembersResponse, FString Include, int Limit, FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsPubnubInitialized() || !CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this, ChannelMetadataID, OnGetMembersResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count]
{
GetChannelMembers_JSON_priv(ChannelMetadataID, OnGetMembersResponse, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);
});
}

Expand Down Expand Up @@ -1972,15 +1994,9 @@ void UPubnubSubsystem::RemoveChannelMetadata_priv(FString ChannelMetadataID)
}
}

void UPubnubSubsystem::GetMemberships_priv(FString UUIDMetadataID, FOnPubnubResponse OnGetMembershipResponse, FString Include, int Limit,
FString UPubnubSubsystem::GetMemberships_pn(FString UUIDMetadataID, FString Include, int Limit,
FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsUserIDSet())
{return;}

if(CheckIsFieldEmpty(UUIDMetadataID, "UUIDMetadataID", "GetMemberships"))
{return;}

pubnub_membership_opts PubnubOptions = pubnub_membership_opts();
auto CharConverterUuid = StringCast<ANSICHAR>(*UUIDMetadataID);
PubnubOptions.uuid = CharConverterUuid.Get();
Expand All @@ -1999,7 +2015,19 @@ void UPubnubSubsystem::GetMemberships_priv(FString UUIDMetadataID, FOnPubnubResp

pubnub_get_memberships_ex(ctx_pub, PubnubOptions);

FString JsonResponse = GetLastResponse(ctx_pub);
return GetLastResponse(ctx_pub);
}

void UPubnubSubsystem::GetMemberships_JSON_priv(FString UUIDMetadataID, FOnPubnubResponse OnGetMembershipResponse, FString Include, int Limit,
FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsUserIDSet())
{return;}

if(CheckIsFieldEmpty(UUIDMetadataID, "UUIDMetadataID", "GetMemberships"))
{return;}

FString JsonResponse = GetMemberships_pn(UUIDMetadataID, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);

//Delegate needs to be executed back on Game Thread
AsyncTask(ENamedThreads::GameThread, [this, OnGetMembershipResponse, JsonResponse]()
Expand All @@ -2009,6 +2037,32 @@ void UPubnubSubsystem::GetMemberships_priv(FString UUIDMetadataID, FOnPubnubResp
});
}

void UPubnubSubsystem::GetMemberships_DATA_priv(FString UUIDMetadataID, FOnGetMembershipsResponse OnGetMembershipResponse, FString Include, int Limit,
FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsUserIDSet())
{return;}

if(CheckIsFieldEmpty(UUIDMetadataID, "UUIDMetadataID", "GetMemberships"))
{return;}

FString JsonResponse = GetMemberships_pn(UUIDMetadataID, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);

//Delegate needs to be executed back on Game Thread
AsyncTask(ENamedThreads::GameThread, [this, OnGetMembershipResponse, JsonResponse]()
{
//Parse Json response into data
int Status;
TArray<FPubnubGetMembershipsWrapper> MembershipsData;
FString PageNext;
FString PagePrev;
UPubnubJsonUtilities::GetMembershipsJsonToData(JsonResponse, Status, MembershipsData, PageNext, PagePrev);

//Broadcast bound delegate with parsed response
OnGetMembershipResponse.ExecuteIfBound(Status, MembershipsData, PageNext, PagePrev);
});
}

void UPubnubSubsystem::SetMemberships_priv(FString UUIDMetadataID, FString SetObj, FString Include)
{
if(!CheckIsUserIDSet())
Expand Down Expand Up @@ -2043,15 +2097,9 @@ void UPubnubSubsystem::RemoveMemberships_priv(FString UUIDMetadataID, FString Re
}
}

void UPubnubSubsystem::GetChannelMembers_priv(FString ChannelMetadataID, FOnPubnubResponse OnGetMembersResponse, FString Include, int Limit,
FString UPubnubSubsystem::GetChannelMembers_pn(FString ChannelMetadataID, FString Include, int Limit,
FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsUserIDSet())
{return;}

if(CheckIsFieldEmpty(ChannelMetadataID, "UUIDMetadataID", "GetChannelMembers"))
{return;}

pubnub_members_opts PubnubOptions = pubnub_members_opts();
auto CharConverterInclude = StringCast<ANSICHAR>(*Include);
PubnubOptions.include = CharConverterInclude.Get();
Expand All @@ -2068,7 +2116,19 @@ void UPubnubSubsystem::GetChannelMembers_priv(FString ChannelMetadataID, FOnPubn

pubnub_get_members_ex(ctx_pub,TCHAR_TO_ANSI(*ChannelMetadataID), PubnubOptions);

FString JsonResponse = GetLastResponse(ctx_pub);
return GetLastResponse(ctx_pub);
}

void UPubnubSubsystem::GetChannelMembers_JSON_priv(FString ChannelMetadataID, FOnPubnubResponse OnGetMembersResponse, FString Include, int Limit,
FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsUserIDSet())
{return;}

if(CheckIsFieldEmpty(ChannelMetadataID, "UUIDMetadataID", "GetChannelMembers"))
{return;}

FString JsonResponse = GetChannelMembers_pn(ChannelMetadataID, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);

//Delegate needs to be executed back on Game Thread
AsyncTask(ENamedThreads::GameThread, [this, OnGetMembersResponse, JsonResponse]()
Expand All @@ -2078,6 +2138,33 @@ void UPubnubSubsystem::GetChannelMembers_priv(FString ChannelMetadataID, FOnPubn
});
}

void UPubnubSubsystem::GetChannelMembers_DATA_priv(FString ChannelMetadataID, FOnGetChannelMembersResponse OnGetMembersResponse, FString Include, int Limit,
FString Filter, FString Sort, FString PageNext, FString PagePrev, EPubnubTribool Count)
{
if(!CheckIsUserIDSet())
{return;}

if(CheckIsFieldEmpty(ChannelMetadataID, "UUIDMetadataID", "GetChannelMembers"))
{return;}

FString JsonResponse = GetChannelMembers_pn(ChannelMetadataID, Include, Limit, Filter, Sort, PageNext, PagePrev, Count);

//Delegate needs to be executed back on Game Thread
AsyncTask(ENamedThreads::GameThread, [this, OnGetMembersResponse, JsonResponse]()
{
//Parse Json response into data
int Status;
TArray<FPubnubGetChannelMembersWrapper> MembersData;
FString PageNext;
FString PagePrev;
UPubnubJsonUtilities::GetChannelMembersJsonToData(JsonResponse, Status, MembersData, PageNext, PagePrev);

//Broadcast bound delegate with parsed response
OnGetMembersResponse.ExecuteIfBound(Status, MembersData, PageNext, PagePrev);
});
}


void UPubnubSubsystem::AddChannelMembers_priv(FString ChannelMetadataID, FString AddObj, FString Include)
{
if(!CheckIsUserIDSet())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class PUBNUBLIBRARY_API UPubnubJsonUtilities : public UBlueprintFunctionLibrary

UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities")
static void GetMessageActionsJsonToData(FString ResponseJson, int &Status, TArray<FPubnubMessageActionData> &MessageActions);

UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities")
static void GetMembershipsJsonToData(FString ResponseJson, int &Status, TArray<FPubnubGetMembershipsWrapper> &MembershipsData, FString &PageNext, FString &PagePrev);

UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities")
static void GetChannelMembersJsonToData(FString ResponseJson, int &Status, TArray<FPubnubGetChannelMembersWrapper> &MembershipsData, FString &PageNext, FString &PagePrev);

UFUNCTION(BlueprintCallable, BlueprintPure, Category="Pubnub|Json Utilities")
static FPubnubUserData GetUserDataFromJson(FString ResponseJson);
Expand Down
26 changes: 26 additions & 0 deletions Source/PubnubLibrary/Public/PubnubStructLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,30 @@ struct FPubnubChannelData
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Type = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Updated = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString ETag = "";
};

USTRUCT(BlueprintType)
struct FPubnubGetMembershipsWrapper
{
GENERATED_BODY()

UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FPubnubChannelData Channel;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Custom = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Status = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Type = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Updated = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString ETag = "";
};

USTRUCT(BlueprintType)
struct FPubnubGetChannelMembersWrapper
{
GENERATED_BODY()

UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FPubnubUserData User;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Custom = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Status = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Type = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString Updated = "";
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Pubnub") FString ETag = "";
};
Loading

0 comments on commit 641f3db

Please sign in to comment.