55#include " Runtime/Launch/Resources/Version.h"
66#include " Json.h"
77
8-
98FString UPubnubUtilities::AddQuotesToString (const FString InString, bool SkipIfHasQuotes)
109{
1110 if (InString.Left (1 ) != " \" " || InString.Right (1 ) != " \" " || !SkipIfHasQuotes)
@@ -29,4 +28,272 @@ FString UPubnubUtilities::PubnubCharMemBlockToString(const pubnub_char_mem_block
2928#else
3029 return FString::ConstructFromPtrSize (PnChar.ptr , PnChar.size );
3130#endif
31+ }
32+
33+ FString UPubnubUtilities::MembershipIncludeToString (const FPubnubMembershipInclude& MembershipInclude)
34+ {
35+ FString FinalString = " " ;
36+ if (MembershipInclude.IncludeCustom ) {FinalString.Append (" custom," );}
37+ if (MembershipInclude.IncludeStatus ) {FinalString.Append (" status," );}
38+ if (MembershipInclude.IncludeType ) {FinalString.Append (" type," );}
39+ if (MembershipInclude.IncludeChannel ) {FinalString.Append (" channel," );}
40+ if (MembershipInclude.IncludeChannelCustom ) {FinalString.Append (" channel.custom," );}
41+ if (MembershipInclude.IncludeChannelStatus ) {FinalString.Append (" channel.status," );}
42+ if (MembershipInclude.IncludeChannelType ) {FinalString.Append (" channel.type," );}
43+ // Total count is passed as a separate parameter, so it's not included directly in the final string
44+
45+ // If there was any include remove the last comma
46+ if (!FinalString.IsEmpty ())
47+ {
48+ FinalString.RemoveAt (FinalString.Len () - 1 );
49+ }
50+
51+ return FinalString;
52+ }
53+
54+ FString UPubnubUtilities::MemberIncludeToString (const FPubnubMemberInclude& MemberInclude)
55+ {
56+ FString FinalString = " " ;
57+ if (MemberInclude.IncludeCustom ) {FinalString.Append (" custom," );}
58+ if (MemberInclude.IncludeStatus ) {FinalString.Append (" status," );}
59+ if (MemberInclude.IncludeType ) {FinalString.Append (" type," );}
60+ if (MemberInclude.IncludeUser ) {FinalString.Append (" user," );}
61+ if (MemberInclude.IncludeUserCustom ) {FinalString.Append (" user.custom," );}
62+ if (MemberInclude.IncludeUserStatus ) {FinalString.Append (" user.status," );}
63+ if (MemberInclude.IncludeUserType ) {FinalString.Append (" user.type," );}
64+ // Total count is passed as a separate parameter, so it's not included directly in the final string
65+
66+ // If there was any include remove the last comma
67+ if (!FinalString.IsEmpty ())
68+ {
69+ FinalString.RemoveAt (FinalString.Len () - 1 );
70+ }
71+
72+ return FinalString;
73+ }
74+
75+ FString UPubnubUtilities::GetAllIncludeToString (const FPubnubGetAllInclude& GetAllInclude)
76+ {
77+ FString FinalString = " " ;
78+ if (GetAllInclude.IncludeCustom ) {FinalString.Append (" custom," );}
79+ if (GetAllInclude.IncludeStatus ) {FinalString.Append (" status," );}
80+ if (GetAllInclude.IncludeType ) {FinalString.Append (" type," );}
81+ // Total count is passed as a separate parameter, so it's not included directly in the final string
82+
83+ // If there was any include remove the last comma
84+ if (!FinalString.IsEmpty ())
85+ {
86+ FinalString.RemoveAt (FinalString.Len () - 1 );
87+ }
88+
89+ return FinalString;
90+ }
91+
92+ FString UPubnubUtilities::MembershipSortTypeToString (const EPubnubMembershipSortType SortType)
93+ {
94+ switch (SortType)
95+ {
96+ case EPubnubMembershipSortType::PMST_ChannelID:
97+ return " channel.id" ;
98+ case EPubnubMembershipSortType::PMST_ChannelName:
99+ return " channel.name" ;
100+ case EPubnubMembershipSortType::PMST_ChannelUpdated:
101+ return " channel.updated" ;
102+ case EPubnubMembershipSortType::PMST_ChannelStatus:
103+ return " channel.status" ;
104+ case EPubnubMembershipSortType::PMST_ChannelType:
105+ return " channel.type" ;
106+ case EPubnubMembershipSortType::PMST_Updated:
107+ return " updated" ;
108+ case EPubnubMembershipSortType::PMST_Status:
109+ return " status" ;
110+ case EPubnubMembershipSortType::PMST_Type:
111+ return " type" ;
112+ }
113+ return " " ;
114+ }
115+
116+ FString UPubnubUtilities::MemberSortTypeToString (const EPubnubMemberSortType SortType)
117+ {
118+ switch (SortType)
119+ {
120+ case EPubnubMemberSortType::PMeST_UserID:
121+ return " user.id" ;
122+ case EPubnubMemberSortType::PMeST_UserName:
123+ return " user.name" ;
124+ case EPubnubMemberSortType::PMeST_UserUpdated:
125+ return " user.updated" ;
126+ case EPubnubMemberSortType::PMeST_UserStatus:
127+ return " user.status" ;
128+ case EPubnubMemberSortType::PMeST_UserType:
129+ return " user.type" ;
130+ case EPubnubMemberSortType::PMeST_Updated:
131+ return " updated" ;
132+ case EPubnubMemberSortType::PMeST_Status:
133+ return " status" ;
134+ case EPubnubMemberSortType::PMeST_Type:
135+ return " type" ;
136+ }
137+ return " " ;
138+ }
139+
140+ FString UPubnubUtilities::GetAllSortTypeToString (const EPubnubGetAllSortType SortType)
141+ {
142+ switch (SortType)
143+ {
144+ case EPubnubGetAllSortType::PGAST_ID:
145+ return " id" ;
146+ case EPubnubGetAllSortType::PGAST_Name:
147+ return " name" ;
148+ case EPubnubGetAllSortType::PGAST_Updated:
149+ return " updated" ;
150+ case EPubnubGetAllSortType::PGAST_Status:
151+ return " status" ;
152+ case EPubnubGetAllSortType::PGAST_Type:
153+ return " type" ;
154+ }
155+ return " " ;
156+ }
157+
158+ FString UPubnubUtilities::MembershipSortToString (const FPubnubMembershipSort& MemberInclude)
159+ {
160+ FString FinalString = " " ;
161+ // Form comma separated string of sorts
162+ for (auto SingleSort : MemberInclude.MembershipSort )
163+ {
164+ if (!FinalString.IsEmpty ()) {FinalString.Append (" ," );}
165+ FinalString.Append (MembershipSortTypeToString (SingleSort.SortType ));
166+ // Default sort is ascending, so we only specify order when it's descending
167+ if (SingleSort.SortOrder )
168+ {
169+ FinalString.Append (" :desc" );
170+ }
171+ }
172+
173+ return FinalString;
174+ }
175+
176+ FString UPubnubUtilities::MemberSortToString (const FPubnubMemberSort& MemberInclude)
177+ {
178+ FString FinalString = " " ;
179+ // Form comma separated string of sorts
180+ for (auto SingleSort : MemberInclude.MemberSort )
181+ {
182+ if (!FinalString.IsEmpty ()) {FinalString.Append (" ," );}
183+ FinalString.Append (MemberSortTypeToString (SingleSort.SortType ));
184+ // Default sort is ascending, so we only specify order when it's descending
185+ if (SingleSort.SortOrder )
186+ {
187+ FinalString.Append (" :desc" );
188+ }
189+ }
190+
191+ return FinalString;
192+ }
193+
194+ FString UPubnubUtilities::GetAllSortToString (const FPubnubGetAllSort& GetAllInclude)
195+ {
196+ FString FinalString = " " ;
197+ // Form comma separated string of sorts
198+ for (auto SingleSort : GetAllInclude.GetAllSort )
199+ {
200+ if (!FinalString.IsEmpty ()) {FinalString.Append (" ," );}
201+ FinalString.Append (GetAllSortTypeToString (SingleSort.SortType ));
202+ // Default sort is ascending, so we only specify order when it's descending
203+ if (SingleSort.SortOrder )
204+ {
205+ FinalString.Append (" :desc" );
206+ }
207+ }
208+
209+ return FinalString;
210+ }
211+
212+
213+ pubnub_subscription_t * UPubnubUtilities::EEGetSubscriptionForChannel (pubnub_t * Context, FString Channel, FPubnubSubscribeSettings Options)
214+ {
215+ pubnub_subscription_options_t PnOptions = pubnub_subscription_options_defopts ();
216+ PnOptions.receive_presence_events = Options.ReceivePresenceEvents ;
217+
218+ pubnub_channel_t * PubnubChannel = pubnub_channel_alloc (Context, TCHAR_TO_ANSI (*Channel));
219+
220+ pubnub_subscription_t * Subscription = pubnub_subscription_alloc ((pubnub_entity_t *)PubnubChannel, &PnOptions);
221+
222+ pubnub_entity_free ((void **)&PubnubChannel);
223+
224+ return Subscription;
225+ }
226+
227+ pubnub_subscription_t * UPubnubUtilities::EEGetSubscriptionForChannelGroup (pubnub_t * Context, FString ChannelGroup, FPubnubSubscribeSettings Options)
228+ {
229+ pubnub_subscription_options_t PnOptions = pubnub_subscription_options_defopts ();
230+ PnOptions.receive_presence_events = Options.ReceivePresenceEvents ;
231+
232+ pubnub_channel_group_t * PubnubChannelGroup = pubnub_channel_group_alloc (Context, TCHAR_TO_ANSI (*ChannelGroup));
233+
234+ pubnub_subscription_t * Subscription = pubnub_subscription_alloc ((pubnub_entity_t *)PubnubChannelGroup, &PnOptions);
235+
236+ pubnub_entity_free ((void **)&PubnubChannelGroup);
237+
238+ return Subscription;
239+ }
240+
241+ bool UPubnubUtilities::EEAddListenerAndSubscribe (pubnub_subscription_t * Subscription, pubnub_subscribe_message_callback_t Callback, UPubnubSubsystem* PubnubSubsystem)
242+ {
243+ if (!PubnubSubsystem)
244+ {
245+ UE_LOG (PubnubLog, Error, TEXT (" EEAddListenerAndSubscribe Failed, PubnubSubsystem is invalid" ));
246+ return false ;
247+ }
248+
249+ enum pubnub_res AddListenerResult = pubnub_subscribe_add_subscription_listener (Subscription, PBSL_LISTENER_ON_MESSAGE, Callback, PubnubSubsystem);
250+ if (PNR_OK != AddListenerResult)
251+ {
252+ FString ResultString (pubnub_res_2_string (AddListenerResult));
253+ PubnubSubsystem->PubnubError (" Failed to subscribe. Add_subscription_listener failed with error: " + ResultString);
254+ return false ;
255+ }
256+
257+ enum pubnub_res SubscribeResult = pubnub_subscribe_with_subscription (Subscription, nullptr );
258+ if (PNR_OK != SubscribeResult)
259+ {
260+ FString ResultString (pubnub_res_2_string (AddListenerResult));
261+ PubnubSubsystem->PubnubError (" Failed to subscribe. Subscribe_with_subscription failed with error: " + ResultString);
262+ return false ;
263+ }
264+
265+ return true ;
266+ }
267+
268+ bool UPubnubUtilities::EERemoveListenerAndUnsubscribe (pubnub_subscription_t ** SubscriptionPtr, pubnub_subscribe_message_callback_t Callback, UPubnubSubsystem* PubnubSubsystem)
269+ {
270+ if (!PubnubSubsystem)
271+ {
272+ UE_LOG (PubnubLog, Error, TEXT (" EERemoveListenerAndUnsubscribe Failed, PubnubSubsystem is invalid" ));
273+ return false ;
274+ }
275+
276+ if (!SubscriptionPtr)
277+ {
278+ PubnubSubsystem->PubnubError (" Failed to unsubscribe. Passed subscription pointer is invalid" );
279+ return false ;
280+ }
281+
282+ enum pubnub_res RemoveListenerResult = pubnub_subscribe_remove_subscription_listener (*SubscriptionPtr, PBSL_LISTENER_ON_MESSAGE, Callback, PubnubSubsystem);
283+ if (PNR_OK != RemoveListenerResult)
284+ {
285+ FString ResultString (pubnub_res_2_string (RemoveListenerResult));
286+ PubnubSubsystem->PubnubError (" Failed to subscribe. Remove_subscription_listener failed with error: " + ResultString);
287+ return false ;
288+ }
289+
290+ enum pubnub_res UnsubscribeResult = pubnub_unsubscribe_with_subscription (SubscriptionPtr);
291+ if (PNR_OK != UnsubscribeResult)
292+ {
293+ FString ResultString (pubnub_res_2_string (UnsubscribeResult));
294+ PubnubSubsystem->PubnubError (" Failed to unsubscribe. Unsubscribe_with_subscription failed with error: " + ResultString);
295+ return false ;
296+ }
297+
298+ return true ;
32299}
0 commit comments