Skip to content

Commit f18d7a4

Browse files
Add iterative app context samples.
1 parent 8a5d916 commit f18d7a4

File tree

2 files changed

+167
-1
lines changed

2 files changed

+167
-1
lines changed

Source/PubnubLibraryTests/Private/Samples/Sample_AppContext.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void ASample_AppContext::RunSamples()
3535
SetUserMetadataWithResultSample();
3636
SetUserMetadataWithLambdaSample();
3737
SetUserMetadataRawSample();
38+
UpdateUserMetadataIterativelySample();
3839
GetAllUserMetadataSample();
3940
GetAllUserMetadataWithSettingsSample();
4041
GetAllUserMetadataWithAllIncludesSample();
@@ -51,6 +52,7 @@ void ASample_AppContext::RunSamples()
5152
SetChannelMetadataWithResultSample();
5253
SetChannelMetadataWithLambdaSample();
5354
SetChannelMetadataRawSample();
55+
UpdateChannelMetadataIterativelySample();
5456
GetAllChannelMetadataSample();
5557
GetAllChannelMetadataWithSettingsSample();
5658
GetAllChannelMetadataWithAllIncludesSample();
@@ -198,6 +200,77 @@ void ASample_AppContext::SetUserMetadataRawSample()
198200
PubnubSubsystem->SetUserMetadataRaw(UserID, UserMetadataJson, OnSetUserMetadataResponse, Include);
199201
}
200202

203+
// snippet.update_user_metadata_iteratively
204+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
205+
void ASample_AppContext::UpdateUserMetadataIterativelySample()
206+
{
207+
//Get PubnubSubsystem from GameInstance
208+
UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
209+
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();
210+
211+
//Set UserID
212+
FString UserID = TEXT("Player_005");
213+
PubnubSubsystem->SetUserID(UserID);
214+
215+
// Create initial user metadata object
216+
FPubnubUserData UserMetadata;
217+
UserMetadata.UserName = "Player Two";
218+
UserMetadata.Status = "active";
219+
UserMetadata.Custom = "{\"inventory_slots\": 20, \"guild_id\": \"G2\"}";
220+
221+
// Bind response delegate
222+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
223+
FOnSetUserMetadataResponse OnSetUserMetadataResponse;
224+
OnSetUserMetadataResponse.BindDynamic(this, &ASample_AppContext::OnInitialSetUserMetadataResponse);
225+
226+
// Set initial user metadata
227+
FPubnubGetMetadataInclude Include = FPubnubGetMetadataInclude::FromValue(true);
228+
PubnubSubsystem->SetUserMetadata(UserID, UserMetadata, OnSetUserMetadataResponse);
229+
}
230+
231+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
232+
void ASample_AppContext::OnInitialSetUserMetadataResponse(FPubnubOperationResult Result, FPubnubUserData UserData)
233+
{
234+
if(Result.Error)
235+
{
236+
UE_LOG(LogTemp, Error, TEXT("Failed to set initial user metadata. Status: %d, Reason: %s"), Result.Status, *Result.ErrorMessage);
237+
return;
238+
}
239+
240+
//Get PubnubSubsystem from GameInstance
241+
UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
242+
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();
243+
244+
FString UserID = TEXT("Player_005");
245+
246+
// Create updated version of user metadata object - change status to "inactive"
247+
FPubnubUserData UpdatedUserMetadata = UserData;
248+
UpdatedUserMetadata.Status = "inactive";
249+
250+
// Bind response delegate
251+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
252+
FOnSetUserMetadataResponse OnSetUserMetadataResponse;
253+
OnSetUserMetadataResponse.BindDynamic(this, &ASample_AppContext::OnUpdateUserMetadataResponse);
254+
255+
// Update user metadata
256+
FPubnubGetMetadataInclude Include;
257+
Include.IncludeStatus = true;
258+
PubnubSubsystem->SetUserMetadata(UserID, UpdatedUserMetadata, OnSetUserMetadataResponse, Include);
259+
}
260+
261+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
262+
void ASample_AppContext::OnUpdateUserMetadataResponse(FPubnubOperationResult Result, FPubnubUserData UserData)
263+
{
264+
if(Result.Error)
265+
{
266+
UE_LOG(LogTemp, Error, TEXT("Failed to update user metadata. Status: %d, Reason: %s"), Result.Status, *Result.ErrorMessage);
267+
}
268+
else
269+
{
270+
UE_LOG(LogTemp, Log, TEXT("Successfully updated user metadata. New status is: %s"), *UserData.Status);
271+
}
272+
}
273+
201274
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
202275
void ASample_AppContext::OnSetUserMetadataRawResponse(FPubnubOperationResult Result, FPubnubUserData UserData)
203276
{
@@ -779,6 +852,78 @@ void ASample_AppContext::SetChannelMetadataRawSample()
779852
PubnubSubsystem->SetChannelMetadataRaw(Channel, ChannelMetadataJson, OnSetChannelMetadataResponse, Include);
780853
}
781854

855+
// snippet.update_channel_metadata_iteratively
856+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
857+
void ASample_AppContext::UpdateChannelMetadataIterativelySample()
858+
{
859+
//Get PubnubSubsystem from GameInstance
860+
UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
861+
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();
862+
863+
//Set UserID
864+
FString UserID = TEXT("Player_001");
865+
PubnubSubsystem->SetUserID(UserID);
866+
867+
// Create initial channel metadata object
868+
FString ChannelID = TEXT("iterative-channel-update-test");
869+
FPubnubChannelData ChannelMetadata;
870+
ChannelMetadata.ChannelName = "Channel For Iterative Update";
871+
ChannelMetadata.Description = "This is the initial description.";
872+
ChannelMetadata.Custom = "{\"topic\": \"initial_topic\"}";
873+
874+
// Bind response delegate
875+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
876+
FOnSetChannelMetadataResponse OnSetChannelMetadataResponse;
877+
OnSetChannelMetadataResponse.BindDynamic(this, &ASample_AppContext::OnInitialSetChannelMetadataResponse);
878+
879+
// Set initial channel metadata
880+
PubnubSubsystem->SetChannelMetadata(ChannelID, ChannelMetadata, OnSetChannelMetadataResponse);
881+
}
882+
883+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
884+
void ASample_AppContext::OnInitialSetChannelMetadataResponse(FPubnubOperationResult Result, FPubnubChannelData ChannelData)
885+
{
886+
if(Result.Error)
887+
{
888+
UE_LOG(LogTemp, Error, TEXT("Failed to set initial channel metadata. Status: %d, Reason: %s"), Result.Status, *Result.ErrorMessage);
889+
return;
890+
}
891+
892+
UE_LOG(LogTemp, Log, TEXT("Successfully set initial channel metadata. Description: %s"), *ChannelData.Description);
893+
894+
//Get PubnubSubsystem from GameInstance
895+
UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this);
896+
UPubnubSubsystem* PubnubSubsystem = GameInstance->GetSubsystem<UPubnubSubsystem>();
897+
898+
// Use the ChannelID from the response
899+
FString ChannelID = ChannelData.ChannelID;
900+
901+
// Create updated version of channel metadata object - change description only
902+
FPubnubChannelData UpdatedChannelMetadata;
903+
UpdatedChannelMetadata.Description = "This is the updated description!";
904+
905+
// Bind response delegate
906+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
907+
FOnSetChannelMetadataResponse OnSetChannelMetadataResponse;
908+
OnSetChannelMetadataResponse.BindDynamic(this, &ASample_AppContext::OnUpdateChannelMetadataResponse);
909+
910+
// Update channel metadata
911+
PubnubSubsystem->SetChannelMetadata(ChannelID, UpdatedChannelMetadata, OnSetChannelMetadataResponse);
912+
}
913+
914+
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
915+
void ASample_AppContext::OnUpdateChannelMetadataResponse(FPubnubOperationResult Result, FPubnubChannelData ChannelData)
916+
{
917+
if(Result.Error)
918+
{
919+
UE_LOG(LogTemp, Error, TEXT("Failed to update channel metadata. Status: %d, Reason: %s"), Result.Status, *Result.ErrorMessage);
920+
}
921+
else
922+
{
923+
UE_LOG(LogTemp, Log, TEXT("Successfully updated channel metadata. New description is: '%s' and channel name is still: '%s'"), *ChannelData.Description, *ChannelData.ChannelName);
924+
}
925+
}
926+
782927
// ACTION REQUIRED: Replace ASample_AppContext with name of your Actor class
783928
void ASample_AppContext::OnSetChannelMetadataRawResponse(FPubnubOperationResult Result, FPubnubChannelData ChannelData)
784929
{

Source/PubnubLibraryTests/Public/Samples/Sample_AppContext.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ class PUBNUBLIBRARYTESTS_API ASample_AppContext : public APubnubSampleBase
4848
UFUNCTION()
4949
void OnSetUserMetadataRawResponse(FPubnubOperationResult Result, FPubnubUserData UserData);
5050

51+
//snippet.update_user_metadata_iteratively
52+
UFUNCTION(BlueprintCallable, Category = "Pubnub|Samples|App Context")
53+
void UpdateUserMetadataIterativelySample();
54+
55+
UFUNCTION()
56+
void OnInitialSetUserMetadataResponse(FPubnubOperationResult Result, FPubnubUserData UserData);
57+
58+
UFUNCTION()
59+
void OnUpdateUserMetadataResponse(FPubnubOperationResult Result, FPubnubUserData UserData);
60+
5161
// snippet.get_all_user_metadata
5262
UFUNCTION(BlueprintCallable, Category = "Pubnub|Samples|App Context")
5363
void GetAllUserMetadataSample();
@@ -138,6 +148,17 @@ class PUBNUBLIBRARYTESTS_API ASample_AppContext : public APubnubSampleBase
138148
// snippet.set_channel_metadata_raw
139149
UFUNCTION(BlueprintCallable, Category = "Pubnub|Samples|App Context")
140150
void SetChannelMetadataRawSample();
151+
152+
//snippet.update_channel_metadata_iteratively
153+
UFUNCTION(BlueprintCallable, Category = "Pubnub|Samples|App Context")
154+
void UpdateChannelMetadataIterativelySample();
155+
156+
UFUNCTION()
157+
void OnInitialSetChannelMetadataResponse(FPubnubOperationResult Result, FPubnubChannelData ChannelData);
158+
159+
UFUNCTION()
160+
void OnUpdateChannelMetadataResponse(FPubnubOperationResult Result, FPubnubChannelData ChannelData);
161+
141162

142163
UFUNCTION()
143164
void OnSetChannelMetadataRawResponse(FPubnubOperationResult Result, FPubnubChannelData ChannelData);
@@ -213,7 +234,7 @@ class PUBNUBLIBRARYTESTS_API ASample_AppContext : public APubnubSampleBase
213234
// snippet.remove_channel_metadata_with_result_lambda
214235
UFUNCTION(BlueprintCallable, Category = "Pubnub|Samples|App Context")
215236
void RemoveChannelMetadataWithResultLambdaSample();
216-
237+
217238
// snippet.end
218239
};
219240

0 commit comments

Comments
 (0)