| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using SocialFeaturesAnalytics; |
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Social.Friends |
| | 9 | | { |
| | 10 | | public class ReceivedFriendRequestHUDController |
| | 11 | | { |
| | 12 | | private const string PROCESS_REQUEST_ERROR_MESSAGE = "There was an error while trying to process your request. P |
| | 13 | | private const string OPEN_PASSPORT_SOURCE = "FriendRequest"; |
| | 14 | |
|
| | 15 | | private readonly DataStore dataStore; |
| | 16 | | private readonly IReceivedFriendRequestHUDView view; |
| | 17 | | private readonly FriendRequestHUDController friendRequestHUDController; |
| | 18 | | private readonly IFriendsController friendsController; |
| | 19 | | private readonly IUserProfileBridge userProfileBridge; |
| | 20 | | private readonly BaseVariable<(string playerId, string source)> openPassportVariable; |
| | 21 | | private readonly ISocialAnalytics socialAnalytics; |
| | 22 | |
|
| 0 | 23 | | private CancellationTokenSource showCancellationToken = new (); |
| 0 | 24 | | private CancellationTokenSource friendOperationsCancellationToken = new (); |
| | 25 | | private string friendRequestId; |
| | 26 | |
|
| 0 | 27 | | public ReceivedFriendRequestHUDController(DataStore dataStore, |
| | 28 | | IReceivedFriendRequestHUDView view, |
| | 29 | | FriendRequestHUDController friendRequestHUDController, |
| | 30 | | IFriendsController friendsController, |
| | 31 | | IUserProfileBridge userProfileBridge, |
| | 32 | | ISocialAnalytics socialAnalytics) |
| | 33 | | { |
| 0 | 34 | | this.dataStore = dataStore; |
| 0 | 35 | | this.view = view; |
| 0 | 36 | | this.friendRequestHUDController = friendRequestHUDController; |
| 0 | 37 | | this.friendsController = friendsController; |
| 0 | 38 | | this.userProfileBridge = userProfileBridge; |
| 0 | 39 | | this.openPassportVariable = dataStore.HUDs.currentPlayerId; |
| 0 | 40 | | this.socialAnalytics = socialAnalytics; |
| | 41 | |
|
| 0 | 42 | | view.OnClose += Hide; |
| 0 | 43 | | view.OnOpenProfile += OpenProfile; |
| 0 | 44 | | view.OnRejectFriendRequest += Reject; |
| 0 | 45 | | view.OnConfirmFriendRequest += Confirm; |
| 0 | 46 | | view.Close(); |
| | 47 | |
|
| 0 | 48 | | dataStore.HUDs.openReceivedFriendRequestDetail.OnChange += ShowOrHide; |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public void Dispose() |
| | 52 | | { |
| 0 | 53 | | friendOperationsCancellationToken.SafeCancelAndDispose(); |
| 0 | 54 | | showCancellationToken.SafeCancelAndDispose(); |
| 0 | 55 | | dataStore.HUDs.openReceivedFriendRequestDetail.OnChange -= ShowOrHide; |
| 0 | 56 | | friendRequestHUDController.Dispose(); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | private void ShowOrHide(string current, string previous) |
| | 60 | | { |
| 0 | 61 | | if (string.IsNullOrEmpty(current)) |
| 0 | 62 | | Hide(); |
| | 63 | | else |
| 0 | 64 | | Show(current); |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | private void Show(string friendRequestId) |
| | 68 | | { |
| | 69 | | async UniTaskVoid ShowAsync(string friendRequestId, CancellationToken cancellationToken) |
| | 70 | | { |
| 0 | 71 | | this.friendRequestId = friendRequestId; |
| | 72 | |
|
| 0 | 73 | | bool wasFound = friendsController.TryGetAllocatedFriendRequest(this.friendRequestId, out FriendRequest f |
| | 74 | |
|
| 0 | 75 | | if (!wasFound) |
| | 76 | | { |
| 0 | 77 | | Debug.LogError($"Cannot display friend request {friendRequestId}, is not allocated"); |
| 0 | 78 | | return; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | view.SetBodyMessage(friendRequest.MessageBody); |
| 0 | 82 | | view.SetTimestamp(friendRequest.Timestamp); |
| | 83 | |
|
| 0 | 84 | | UserProfile recipientProfile = userProfileBridge.Get(friendRequest.From); |
| | 85 | |
|
| 0 | 86 | | try { recipientProfile ??= await userProfileBridge.RequestFullUserProfileAsync(friendRequest.From, cance |
| 0 | 87 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 88 | | { |
| 0 | 89 | | view.SetSenderName(friendRequest.From); |
| 0 | 90 | | throw; |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | view.SetSenderName(recipientProfile.userName); |
| 0 | 94 | | view.SetSenderProfilePicture(recipientProfile.face256SnapshotURL); |
| | 95 | |
|
| 0 | 96 | | var ownProfile = userProfileBridge.GetOwn(); |
| 0 | 97 | | view.SetRecipientProfilePicture(ownProfile.face256SnapshotURL); |
| 0 | 98 | | view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() + 1); |
| 0 | 99 | | view.Show(); |
| 0 | 100 | | } |
| | 101 | |
|
| 0 | 102 | | showCancellationToken = showCancellationToken.SafeRestart(); |
| 0 | 103 | | ShowAsync(friendRequestId, showCancellationToken.Token).Forget(); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | private void Hide() |
| | 107 | | { |
| 0 | 108 | | dataStore.HUDs.openReceivedFriendRequestDetail.Set(null, false); |
| 0 | 109 | | friendRequestHUDController.Hide(); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | private void OpenProfile() |
| | 113 | | { |
| 0 | 114 | | if (!friendsController.TryGetAllocatedFriendRequest(friendRequestId, out FriendRequest friendRequest)) |
| | 115 | | { |
| 0 | 116 | | Debug.LogError($"Cannot open passport {friendRequestId}, is not allocated"); |
| 0 | 117 | | return; |
| | 118 | | } |
| | 119 | |
|
| 0 | 120 | | openPassportVariable.Set((friendRequest.From, OPEN_PASSPORT_SOURCE)); |
| 0 | 121 | | view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() - 1); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | private void Reject() |
| | 125 | | { |
| | 126 | | async UniTaskVoid RejectAsync(CancellationToken cancellationToken = default) |
| | 127 | | { |
| 0 | 128 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 129 | |
|
| 0 | 130 | | view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Pending); |
| | 131 | |
|
| | 132 | | try |
| | 133 | | { |
| 0 | 134 | | FriendRequest request = await friendsController.RejectFriendshipAsync(friendRequestId, cancellationT |
| | 135 | |
|
| 0 | 136 | | socialAnalytics.SendFriendRequestRejected(request.From, request.To, "modal", request.HasBodyMessage, |
| | 137 | |
|
| 0 | 138 | | view.SetState(ReceivedFriendRequestHUDModel.LayoutState.RejectSuccess); |
| 0 | 139 | | await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken); |
| 0 | 140 | | } |
| 0 | 141 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 142 | | { |
| 0 | 143 | | e.ReportFriendRequestErrorToAnalyticsByRequestId(friendRequestId, "modal", friendsController, social |
| 0 | 144 | | dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true); |
| 0 | 145 | | view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Default); |
| 0 | 146 | | throw; |
| | 147 | | } |
| 0 | 148 | | } |
| | 149 | |
|
| 0 | 150 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| 0 | 151 | | RejectAsync(friendOperationsCancellationToken.Token).Forget(); |
| 0 | 152 | | } |
| | 153 | |
|
| | 154 | | private void Confirm() |
| | 155 | | { |
| | 156 | | async UniTaskVoid ConfirmAsync(CancellationToken cancellationToken = default) |
| | 157 | | { |
| 0 | 158 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 159 | |
|
| 0 | 160 | | view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Pending); |
| | 161 | |
|
| | 162 | | try |
| | 163 | | { |
| 0 | 164 | | FriendRequest request = await friendsController.AcceptFriendshipAsync(friendRequestId, cancellationT |
| | 165 | |
|
| 0 | 166 | | socialAnalytics.SendFriendRequestApproved(request.From, request.To, "modal", request.HasBodyMessage, |
| | 167 | |
|
| 0 | 168 | | view.SetState(ReceivedFriendRequestHUDModel.LayoutState.ConfirmSuccess); |
| 0 | 169 | | await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken); |
| 0 | 170 | | } |
| 0 | 171 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 172 | | { |
| 0 | 173 | | e.ReportFriendRequestErrorToAnalyticsByRequestId(friendRequestId, "modal", friendsController, social |
| 0 | 174 | | dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true); |
| 0 | 175 | | view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Default); |
| 0 | 176 | | throw; |
| | 177 | | } |
| 0 | 178 | | } |
| | 179 | |
|
| 0 | 180 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| 0 | 181 | | ConfirmAsync(friendOperationsCancellationToken.Token).Forget(); |
| 0 | 182 | | } |
| | 183 | | } |
| | 184 | | } |