Stream Chat Unreal SDK
Loading...
Searching...
No Matches
StreamChatClientComponent.h
1// Copyright 2022 Stream.IO, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Channel/ChannelProperties.h"
6#include "Channel/ChannelSortOption.h"
7#include "Channel/ChatChannel.h"
8#include "Channel/Filter.h"
9#include "ChannelFlags.h"
10#include "ChatSocketEvents.h"
11#include "Components/ActorComponent.h"
12#include "CoreMinimal.h"
13#include "Engine/LatentActionManager.h"
14#include "Event/Notification/NotificationMutesUpdatedEvent.h"
15#include "IChatSocket.h"
16#include "Moderation/BanPaginationOptions.h"
17#include "PaginationOptions.h"
18#include "User/OwnUser.h"
19#include "User/User.h"
20
21#include "StreamChatClientComponent.generated.h"
22
23struct FBan;
24class FChatApi;
25class FTokenManager;
26class ITokenProvider;
27class UChatChannel;
28struct FUserRef;
31struct FMessage;
32struct FDevice;
34
40UCLASS(ClassGroup = (Stream), meta = (BlueprintSpawnableComponent))
41class STREAMCHAT_API UStreamChatClientComponent final : public UActorComponent
42{
43 GENERATED_BODY()
44
45public:
48
56 void ConnectUser(const FUser& User, TUniquePtr<ITokenProvider> TokenProvider, TFunction<void(const FOwnUser&)> Callback = {});
57
65 void ConnectUser(const FUser& User, const FString& Token, TFunction<void(const FOwnUser&)> Callback = {});
66
72 void ConnectAnonymousUser(TFunction<void(const FOwnUser&)> Callback = {});
73
80 void ConnectGuestUser(const FUser& User, TFunction<void(const FOwnUser&)> Callback = {});
81
85 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
86 void DisconnectUser();
87
91 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
92 void UpsertUsers(const TArray<FUser>& Users);
93
95 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
96 UChatChannel* NewChat();
98 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
99 void CancelNewChat();
100
112 void QueryChannels(
113 TOptional<FFilter> Filter = {},
114 const TArray<FChannelSortOption>& SortOptions = {},
116 const FPaginationOptions& PaginationOptions = {},
117 TFunction<void(const TArray<UChatChannel*>&)> Callback = {});
118
125 void CreateChannel(const FChannelProperties& ChannelProperties, TFunction<void(UChatChannel*)> Callback);
126
134 void WatchChannel(const FChannelProperties& ChannelProperties, TFunction<void(UChatChannel*)> Callback);
135
144 void QueryChannel(const FChannelProperties& ChannelProperties, const EChannelFlags Flags, TFunction<void(UChatChannel*)> Callback = {});
145
153 void QueryAdditionalChannels(int32 Limit = 10, TFunction<void()> Callback = {});
154
164 void QueryUsers(
165 const FFilter& Filter = {},
166 const TArray<FUserSortOption>& Sort = {},
167 bool bPresence = true,
168 const TOptional<FPaginationOptions> PaginationOptions = {},
169 TFunction<void(const TArray<FUserRef>&)> Callback = {}) const;
170
183 void SearchMessages(
184 const FFilter& ChannelFilter = {},
185 const TOptional<FString>& Query = {},
186 const TOptional<FFilter>& MessageFilter = {},
187 const TArray<FMessageSortOption>& Sort = {},
188 TOptional<uint32> MessageLimit = {},
189 TFunction<void(const TArray<FMessage>&)> Callback = {}) const;
190
194 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client")
195 void MarkAllRead() const;
196
197 UFUNCTION(BlueprintPure, Category = "Stream Chat|Client")
198 const TArray<UChatChannel*>& GetChannels() const;
199
200 UFUNCTION(BlueprintPure, Category = "Stream Chat|Client")
201 static FString DevToken(const FString& UserId);
202
208 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
209 void AddDevice(const FString& DeviceId, EPushProvider PushProvider) const;
210
215 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
216 void RemoveDevice(const FString& DeviceId) const;
217
222 void ListDevices(TFunction<void(TArray<FDevice>)> Callback) const;
223
224 UPROPERTY(EditAnywhere, Config, Category = "Stream Chat", meta = (DisplayName = "API Key"))
225 FString ApiKey;
226
227 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FChannelsUpdatedDelegate, const TArray<UChatChannel*>&, Channels);
228
230 UPROPERTY(BlueprintAssignable, Category = "Stream Chat|Channel")
231 FChannelsUpdatedDelegate ChannelsUpdated;
232
233private:
234 // Called when the game starts
235 virtual void BeginPlay() override;
236
237 void ConnectUserInternal(const FUser& User, TFunction<void(const FOwnUser&)> Callback);
238 UChatChannel* CreateChannelObject(const FChannelStateResponseFieldsDto&);
239
240 void OnConnectionRecovered(const FConnectionRecoveredEvent&);
241 void OnUserPresenceChanged(const FUserPresenceChangedEvent&);
242 void OnNewMessage(const FMessageNewEvent&);
243 void OnMutesUpdated(const FNotificationMutesUpdatedEvent&);
244
245 void AddChannels(const TArray<UChatChannel*>& InChannels);
246
247 TSharedPtr<FTokenManager> TokenManager;
248 TSharedPtr<FChatApi> Api;
249
250 UPROPERTY(Transient)
251 TArray<UChatChannel*> Channels;
252
253#pragma region Blueprint
258public:
265 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
266 void ConnectUser(const FUser& User, const FString& Token, const UObject* WorldContextObject, FLatentActionInfo LatentInfo, FOwnUser& OutUser);
267
272 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
273 void ConnectAnonymousUser(const UObject* WorldContextObject, FLatentActionInfo LatentInfo, FOwnUser& OutUser);
274
280 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
281 void ConnectGuestUser(const FUser& User, const UObject* WorldContextObject, FLatentActionInfo LatentInfo, FOwnUser& OutUser);
282
293 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo))
294 void QueryChannels(
295 FFilter Filter,
296 const TArray<FChannelSortOption>& SortOptions,
297 const UObject* WorldContextObject,
298 FLatentActionInfo LatentInfo,
299 TArray<UChatChannel*>& OutChannels,
300 UPARAM(meta = (Bitmask, BitmaskEnum = EChannelFlags)) const int32 Flags = 3);
301
309 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client", meta = (Latent, WorldContext = WorldContextObject, LatentInfo = LatentInfo, AdvancedDisplay = Id))
310 void WatchChannel(const FChannelProperties& ChannelProperties, const UObject* WorldContextObject, FLatentActionInfo LatentInfo, UChatChannel*& OutChannel);
311
313#pragma endregion Blueprint
314
315#pragma region Events
320public:
321 template <class TEvent>
322 using TEventMulticastDelegate = TMulticastDelegate<void(const TEvent& Event)>;
323 template <class TEvent>
324 using TEventDelegate = typename TEventMulticastDelegate<TEvent>::FDelegate;
325#if ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION <= 27
326 template <class TEvent, class UserClass>
327 using TEventDelegateUObjectMethodPtr = typename TEventDelegate<TEvent>::template TUObjectMethodDelegate<UserClass>::FMethodPtr;
328 template <class TEvent, class UserClass>
329 using TEventDelegateSpMethodPtr = typename TEventDelegate<TEvent>::template TSPMethodDelegate<UserClass>::FMethodPtr;
330#else
331 template <class TEvent, class UserClass>
332 using TEventDelegateUObjectMethodPtr = typename TEventDelegate<TEvent>::template TMethodPtr<UserClass>;
333 template <class TEvent, class UserClass>
334 using TEventDelegateSpMethodPtr = typename TEventDelegate<TEvent>::template TMethodPtr<UserClass>;
335#endif
336
338 template <class TEvent>
339 FDelegateHandle On(TEventDelegate<TEvent> Callback);
340
348 template <class TEvent, class UserClass>
349 typename TEnableIf<TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type On(
350 UserClass* Obj,
351 TEventDelegateUObjectMethodPtr<TEvent, UserClass> Method);
352
360 template <class TEvent, class UserClass>
361 typename TEnableIf<!TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type On(
362 UserClass* Obj,
363 TEventDelegateSpMethodPtr<TEvent, UserClass> Method);
364
373 template <class TEvent, typename FunctorType, typename... VarTypes>
374 typename TEnableIf<TIsInvocable<FunctorType, const TEvent&, VarTypes...>::Value, FDelegateHandle>::Type On(FunctorType&& Functor, VarTypes... Vars);
375
376 template <class TEvent>
377 bool Unsubscribe(FDelegateHandle) const;
378
379private:
380 TSharedPtr<IChatSocket> Socket;
381
383#pragma endregion Events
384
385#pragma region Moderation
390public:
400 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation", DisplayName = "Ban User")
401 void BanUserBP(const FUserRef& User, FTimespan Timeout, const FString& Reason, bool bIpBan) const;
402 void BanUser(const FUserRef& User, const TOptional<FTimespan>& Timeout = {}, const TOptional<FString>& Reason = {}, bool bIpBan = false) const;
403
408 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
409 void UnbanUser(const FUserRef& User) const;
410
418 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client|Moderation", DisplayName = "Shadow Ban User")
419 void ShadowBanUserBP(const FUserRef& User, FTimespan Timeout) const;
420 void ShadowBanUser(const FUserRef& User, const TOptional<FTimespan>& Timeout = {}) const;
421
426 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client|Moderation")
427 void ShadowUnbanUser(const FUserRef& User) const;
428
438 void QueryBannedUsers(
439 const FFilter& Filter,
440 const TArray<FBanSortOption>& SortOptions = {},
441 const TOptional<FBanPaginationOptions> PaginationOptions = {},
442 const TFunction<void(const TArray<FBan>&)> Callback = {});
443
449 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client")
450 void FlagMessage(const FMessage& Message) const;
451
457 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client")
458 void FlagUser(const FUserRef& User) const;
459
466 UFUNCTION(BlueprintCallable, BlueprintPure = false, Category = "Stream Chat|Client", DisplayName = "Mute User")
467 void MuteUserBP(const FUserRef& User, FTimespan Timeout) const;
468 void MuteUser(const FUserRef& User, const TOptional<FTimespan>& Timeout = {}) const;
469
475 UFUNCTION(BlueprintCallable, Category = "Stream Chat|Client")
476 void UnmuteUser(const FUserRef& User) const;
477
479#pragma endregion Moderation
480};
481
482#pragma region Events
483
484template <class TEvent>
485FDelegateHandle UStreamChatClientComponent::On(TEventDelegate<TEvent> Callback)
486{
487 return Socket->Events().SubscribeLambda<TEvent>(
488 [this, Callback](const TEvent& Event)
489 {
490 // TODO can we enforce client-only events?
491 Callback.ExecuteIfBound(Event);
492 });
493}
494
495template <class TEvent, class UserClass>
496typename TEnableIf<TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type UStreamChatClientComponent::On(
497 UserClass* Obj,
498 TEventDelegateUObjectMethodPtr<TEvent, UserClass> Method)
499{
500 const TEventDelegate<TEvent> Delegate = TEventDelegate<TEvent>::CreateUObject(Obj, Method);
501 return On<TEvent>(Delegate);
502}
503
504template <class TEvent, class UserClass>
505typename TEnableIf<!TIsDerivedFrom<UserClass, UObject>::IsDerived, FDelegateHandle>::Type UStreamChatClientComponent::On(
506 UserClass* Obj,
507 TEventDelegateSpMethodPtr<TEvent, UserClass> Method)
508{
509 const TEventDelegate<TEvent> Delegate = TEventDelegate<TEvent>::CreateSP(Obj, Method);
510 return On<TEvent>(Delegate);
511}
512
513template <class TEvent, typename FunctorType, typename... VarTypes>
514typename TEnableIf<TIsInvocable<FunctorType, const TEvent&, VarTypes...>::Value, FDelegateHandle>::Type UStreamChatClientComponent::On(
515 FunctorType&& Functor,
516 VarTypes... Vars)
517{
518 const TEventDelegate<TEvent> Delegate = TEventDelegate<TEvent>::CreateLambda(Forward<FunctorType>(Functor), Vars...);
519 return On<TEvent>(Delegate);
520}
521
522template <class TEvent>
523bool UStreamChatClientComponent::Unsubscribe(const FDelegateHandle Handle) const
524{
525 return Socket->Events().Unsubscribe<TEvent>(Handle);
526}
527#pragma endregion Events
All Stream Chat REST API requests can be made via this object.
Definition: ChatApi.h:59
The client-side representation of a Stream Chat channel.
Definition: ChatChannel.h:74
A component which allows for connecting to the Stream Chat API. Should be added to a client-side acto...
Definition: StreamChatClientComponent.h:42
TEnableIf<!TIsDerivedFrom< UserClass, UObject >::IsDerived, FDelegateHandle >::Type On(UserClass *Obj, TEventDelegateSpMethodPtr< TEvent, UserClass > Method)
void ConnectUser(const FUser &User, const FString &Token, TFunction< void(const FOwnUser &)> Callback={})
Create a connection to the API for the given user and credentials.
void WatchChannel(const FChannelProperties &ChannelProperties, TFunction< void(UChatChannel *)> Callback)
Create a channel if it does not exist yet (if this user has the right permissions),...
void ConnectUser(const FUser &User, TUniquePtr< ITokenProvider > TokenProvider, TFunction< void(const FOwnUser &)> Callback={})
Create a connection to the API for the given user and using a custom token provider.
FDelegateHandle On(TEventDelegate< TEvent > Callback)
Subscribe to a client event using your own delegate object.
Definition: StreamChatClientComponent.h:485
void ConnectAnonymousUser(TFunction< void(const FOwnUser &)> Callback={})
Create a connection to the API anonymously.
EChannelFlags
Additional channel actions to perform when requesting channel data from API.
Definition: ChannelFlags.h:15
@ State
Return channel state in response.
@ Watch
Start watching the channel (receive events regarding this channel via WebSocket)
@ UserId
The user ID of the user that sent the message.
@ Id
Message ID.
@ Type
Type of the message.
@ Query
Matches values by performing text search with the specified value.
A banned user.
Definition: Ban.h:17
The desired sort options for ban queries.
Definition: ChannelSortOption.h:188
Encapsulates the static properties of a channel.
Definition: ChannelProperties.h:21
The desired sort options for Channel API queries.
Definition: ChannelSortOption.h:44
#/components/schemas/ChannelStateResponseFields
Definition: ChannelStateResponseFieldsDto.h:19
Sent when the connection is recovered.
Definition: ConnectionRecoveredEvent.h:17
A push notification device.
Definition: Device.h:16
A filter used for querying channels.
Definition: Filter.h:75
Represents a Stream Chat message.
Definition: Message.h:66
A new message was created.
Definition: MessageNewEvent.h:17
Notification when a new message was created.
Definition: NotificationMutesUpdatedEvent.h:18
The currently connected Stream Chat user.
Definition: OwnUser.h:18
Options for pagination.
Definition: PaginationOptions.h:15
A Stream Chat user.
Definition: User.h:20
#/components/schemas/UserPresenceChangedEvent
Definition: UserPresenceChangedEvent.h:18
A reference to a unique User.
Definition: UserRef.h:23