< Summary

Class:DCL.Social.Friends.ReceivedFriendRequestHUDController
Assembly:FriendRequestHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendRequestHUD/ReceivedFriendRequestHUDController.cs
Covered lines:0
Uncovered lines:89
Coverable lines:89
Total lines:184
Line coverage:0% (0 of 89)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:11
Method coverage:0% (0 of 11)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ReceivedFriendRequestHUDController(...)0%2100%
Dispose()0%2100%
ShowOrHide(...)0%6200%
<Show()0%72800%
Show(...)0%2100%
Hide()0%2100%
OpenProfile()0%6200%
<Reject()0%72800%
Reject()0%2100%
<Confirm()0%72800%
Confirm()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendRequestHUD/ReceivedFriendRequestHUDController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Tasks;
 3using SocialFeaturesAnalytics;
 4using System;
 5using System.Threading;
 6using UnityEngine;
 7
 8namespace 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
 023        private CancellationTokenSource showCancellationToken = new ();
 024        private CancellationTokenSource friendOperationsCancellationToken = new ();
 25        private string friendRequestId;
 26
 027        public ReceivedFriendRequestHUDController(DataStore dataStore,
 28            IReceivedFriendRequestHUDView view,
 29            FriendRequestHUDController friendRequestHUDController,
 30            IFriendsController friendsController,
 31            IUserProfileBridge userProfileBridge,
 32            ISocialAnalytics socialAnalytics)
 33        {
 034            this.dataStore = dataStore;
 035            this.view = view;
 036            this.friendRequestHUDController = friendRequestHUDController;
 037            this.friendsController = friendsController;
 038            this.userProfileBridge = userProfileBridge;
 039            this.openPassportVariable = dataStore.HUDs.currentPlayerId;
 040            this.socialAnalytics = socialAnalytics;
 41
 042            view.OnClose += Hide;
 043            view.OnOpenProfile += OpenProfile;
 044            view.OnRejectFriendRequest += Reject;
 045            view.OnConfirmFriendRequest += Confirm;
 046            view.Close();
 47
 048            dataStore.HUDs.openReceivedFriendRequestDetail.OnChange += ShowOrHide;
 049        }
 50
 51        public void Dispose()
 52        {
 053            friendOperationsCancellationToken.SafeCancelAndDispose();
 054            showCancellationToken.SafeCancelAndDispose();
 055            dataStore.HUDs.openReceivedFriendRequestDetail.OnChange -= ShowOrHide;
 056            friendRequestHUDController.Dispose();
 057        }
 58
 59        private void ShowOrHide(string current, string previous)
 60        {
 061            if (string.IsNullOrEmpty(current))
 062                Hide();
 63            else
 064                Show(current);
 065        }
 66
 67        private void Show(string friendRequestId)
 68        {
 69            async UniTaskVoid ShowAsync(string friendRequestId, CancellationToken cancellationToken)
 70            {
 071                this.friendRequestId = friendRequestId;
 72
 073                bool wasFound = friendsController.TryGetAllocatedFriendRequest(this.friendRequestId, out FriendRequest f
 74
 075                if (!wasFound)
 76                {
 077                    Debug.LogError($"Cannot display friend request {friendRequestId}, is not allocated");
 078                    return;
 79                }
 80
 081                view.SetBodyMessage(friendRequest.MessageBody);
 082                view.SetTimestamp(friendRequest.Timestamp);
 83
 084                UserProfile recipientProfile = userProfileBridge.Get(friendRequest.From);
 85
 086                try { recipientProfile ??= await userProfileBridge.RequestFullUserProfileAsync(friendRequest.From, cance
 087                catch (Exception e) when (e is not OperationCanceledException)
 88                {
 089                    view.SetSenderName(friendRequest.From);
 090                    throw;
 91                }
 92
 093                view.SetSenderName(recipientProfile.userName);
 094                view.SetSenderProfilePicture(recipientProfile.face256SnapshotURL);
 95
 096                var ownProfile = userProfileBridge.GetOwn();
 097                view.SetRecipientProfilePicture(ownProfile.face256SnapshotURL);
 098                view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() + 1);
 099                view.Show();
 0100            }
 101
 0102            showCancellationToken = showCancellationToken.SafeRestart();
 0103            ShowAsync(friendRequestId, showCancellationToken.Token).Forget();
 0104        }
 105
 106        private void Hide()
 107        {
 0108            dataStore.HUDs.openReceivedFriendRequestDetail.Set(null, false);
 0109            friendRequestHUDController.Hide();
 0110        }
 111
 112        private void OpenProfile()
 113        {
 0114            if (!friendsController.TryGetAllocatedFriendRequest(friendRequestId, out FriendRequest friendRequest))
 115            {
 0116                Debug.LogError($"Cannot open passport {friendRequestId}, is not allocated");
 0117                return;
 118            }
 119
 0120            openPassportVariable.Set((friendRequest.From, OPEN_PASSPORT_SOURCE));
 0121            view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() - 1);
 0122        }
 123
 124        private void Reject()
 125        {
 126            async UniTaskVoid RejectAsync(CancellationToken cancellationToken = default)
 127            {
 0128                cancellationToken.ThrowIfCancellationRequested();
 129
 0130                view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Pending);
 131
 132                try
 133                {
 0134                    FriendRequest request = await friendsController.RejectFriendshipAsync(friendRequestId, cancellationT
 135
 0136                    socialAnalytics.SendFriendRequestRejected(request.From, request.To, "modal", request.HasBodyMessage,
 137
 0138                    view.SetState(ReceivedFriendRequestHUDModel.LayoutState.RejectSuccess);
 0139                    await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken);
 0140                }
 0141                catch (Exception e) when (e is not OperationCanceledException)
 142                {
 0143                    e.ReportFriendRequestErrorToAnalyticsByRequestId(friendRequestId, "modal", friendsController, social
 0144                    dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true);
 0145                    view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Default);
 0146                    throw;
 147                }
 0148            }
 149
 0150            friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart();
 0151            RejectAsync(friendOperationsCancellationToken.Token).Forget();
 0152        }
 153
 154        private void Confirm()
 155        {
 156            async UniTaskVoid ConfirmAsync(CancellationToken cancellationToken = default)
 157            {
 0158                cancellationToken.ThrowIfCancellationRequested();
 159
 0160                view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Pending);
 161
 162                try
 163                {
 0164                    FriendRequest request = await friendsController.AcceptFriendshipAsync(friendRequestId, cancellationT
 165
 0166                    socialAnalytics.SendFriendRequestApproved(request.From, request.To, "modal", request.HasBodyMessage,
 167
 0168                    view.SetState(ReceivedFriendRequestHUDModel.LayoutState.ConfirmSuccess);
 0169                    await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken);
 0170                }
 0171                catch (Exception e) when (e is not OperationCanceledException)
 172                {
 0173                    e.ReportFriendRequestErrorToAnalyticsByRequestId(friendRequestId, "modal", friendsController, social
 0174                    dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true);
 0175                    view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Default);
 0176                    throw;
 177                }
 0178            }
 179
 0180            friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart();
 0181            ConfirmAsync(friendOperationsCancellationToken.Token).Forget();
 0182        }
 183    }
 184}