| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using SocialFeaturesAnalytics; |
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | |
|
| | 7 | | namespace DCL.Social.Friends |
| | 8 | | { |
| | 9 | | public class SendFriendRequestHUDController |
| | 10 | | { |
| | 11 | | private const string PROCESS_REQUEST_ERROR_MESSAGE = "There was an error while trying to process your request. P |
| | 12 | |
|
| | 13 | | private readonly ISendFriendRequestHUDView view; |
| | 14 | | private readonly FriendRequestHUDController friendRequestHUDController; |
| | 15 | | private readonly DataStore dataStore; |
| | 16 | | private readonly IUserProfileBridge userProfileBridge; |
| | 17 | | private readonly IFriendsController friendsController; |
| | 18 | | private readonly ISocialAnalytics socialAnalytics; |
| | 19 | |
|
| | 20 | | private string messageBody; |
| | 21 | | private string recipientId; |
| 0 | 22 | | private CancellationTokenSource friendOperationsCancellationToken = new (); |
| 0 | 23 | | private CancellationTokenSource showCancellationToken = new (); |
| | 24 | |
|
| 0 | 25 | | public SendFriendRequestHUDController( |
| | 26 | | ISendFriendRequestHUDView view, |
| | 27 | | FriendRequestHUDController friendRequestHUDController, |
| | 28 | | DataStore dataStore, |
| | 29 | | IUserProfileBridge userProfileBridge, |
| | 30 | | IFriendsController friendsController, |
| | 31 | | ISocialAnalytics socialAnalytics) |
| | 32 | | { |
| 0 | 33 | | this.view = view; |
| 0 | 34 | | this.friendRequestHUDController = friendRequestHUDController; |
| 0 | 35 | | this.dataStore = dataStore; |
| 0 | 36 | | this.userProfileBridge = userProfileBridge; |
| 0 | 37 | | this.friendsController = friendsController; |
| 0 | 38 | | this.socialAnalytics = socialAnalytics; |
| | 39 | |
|
| 0 | 40 | | dataStore.HUDs.sendFriendRequest.OnChange += ShowOrHide; |
| | 41 | |
|
| 0 | 42 | | view.OnMessageBodyChanged += OnMessageBodyChanged; |
| 0 | 43 | | view.OnSend += Send; |
| 0 | 44 | | view.OnCancel += Hide; |
| | 45 | |
|
| 0 | 46 | | view.Close(); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void Dispose() |
| | 50 | | { |
| 0 | 51 | | friendOperationsCancellationToken.SafeCancelAndDispose(); |
| 0 | 52 | | showCancellationToken.SafeCancelAndDispose(); |
| 0 | 53 | | dataStore.HUDs.sendFriendRequest.OnChange -= ShowOrHide; |
| 0 | 54 | | view.OnMessageBodyChanged -= OnMessageBodyChanged; |
| 0 | 55 | | view.OnSend -= Send; |
| 0 | 56 | | view.OnCancel -= Hide; |
| 0 | 57 | | view.Dispose(); |
| 0 | 58 | | friendRequestHUDController.Dispose(); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void ShowOrHide(string current, string previous) |
| | 62 | | { |
| 0 | 63 | | if (string.IsNullOrEmpty(current)) |
| 0 | 64 | | Hide(); |
| | 65 | | else |
| 0 | 66 | | Show(current); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | private void Show(string recipient) |
| | 70 | | { |
| | 71 | | async UniTaskVoid ShowAsync(string recipient, CancellationToken cancellationToken) |
| | 72 | | { |
| 0 | 73 | | messageBody = ""; |
| 0 | 74 | | recipientId = recipient; |
| 0 | 75 | | var userProfile = userProfileBridge.Get(recipient); |
| | 76 | |
|
| 0 | 77 | | try { userProfile ??= await userProfileBridge.RequestFullUserProfileAsync(recipient, cancellationToken); |
| 0 | 78 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 79 | | { |
| 0 | 80 | | view.SetName(recipient); |
| 0 | 81 | | view.ClearInputField(); |
| 0 | 82 | | view.Show(); |
| 0 | 83 | | throw; |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | view.SetName(userProfile.userName); |
| | 87 | | // must send the snapshot observer, otherwise the faceUrl is invalid and the texture never loads |
| 0 | 88 | | view.SetProfilePicture(userProfile.snapshotObserver); |
| 0 | 89 | | view.ClearInputField(); |
| 0 | 90 | | view.Show(); |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | showCancellationToken = showCancellationToken.SafeRestart(); |
| 0 | 94 | | ShowAsync(recipient, showCancellationToken.Token).Forget(); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | private void Send() |
| | 98 | | { |
| 0 | 99 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| | 100 | |
|
| | 101 | | async UniTaskVoid SendAsync(CancellationToken cancellationToken) |
| | 102 | | { |
| 0 | 103 | | view.ShowPendingToSend(); |
| | 104 | |
|
| | 105 | | try |
| | 106 | | { |
| 0 | 107 | | var friendRequest = await friendsController.RequestFriendshipAsync(recipientId, messageBody, cancell |
| | 108 | |
|
| 0 | 109 | | socialAnalytics.SendFriendRequestSent(userProfileBridge.GetOwn().userId, |
| | 110 | | recipientId, messageBody.Length, |
| | 111 | | (PlayerActionSource)dataStore.HUDs.sendFriendRequestSource.Get(), friendRequest.FriendRequestId) |
| | 112 | |
|
| 0 | 113 | | view.ShowSendSuccess(); |
| | 114 | |
|
| 0 | 115 | | await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken); |
| 0 | 116 | | } |
| 0 | 117 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 118 | | { |
| 0 | 119 | | e.ReportFriendRequestErrorToAnalyticsAsSender(recipientId, dataStore.HUDs.sendFriendRequestSource.Ge |
| | 120 | | userProfileBridge, socialAnalytics); |
| | 121 | |
|
| 0 | 122 | | view.Show(); |
| 0 | 123 | | dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true); |
| 0 | 124 | | throw; |
| | 125 | | } |
| 0 | 126 | | } |
| | 127 | |
|
| 0 | 128 | | SendAsync(friendOperationsCancellationToken.Token) |
| | 129 | | .Forget(); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | private void Hide() |
| | 133 | | { |
| 0 | 134 | | showCancellationToken.SafeCancelAndDispose(); |
| 0 | 135 | | dataStore.HUDs.sendFriendRequest.Set(null, false); |
| 0 | 136 | | friendRequestHUDController.Hide(); |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | private void OnMessageBodyChanged(string body) => |
| 0 | 140 | | messageBody = body; |
| | 141 | | } |
| | 142 | | } |