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