< 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:78
Uncovered lines:5
Coverable lines:83
Total lines:176
Line coverage:93.9% (78 of 83)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ReceivedFriendRequestHUDController(...)0%110100%
Dispose()0%110100%
ShowOrHide(...)0%220100%
Show(...)0%3.053082.35%
Hide()0%110100%
OpenProfile()0%2.092071.43%
<Reject()0%8.18088.24%
Reject()0%110100%
<Confirm()0%8.18088.24%
Confirm()0%110100%

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
 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
 822        private CancellationTokenSource friendOperationsCancellationToken = new ();
 23        private string friendRequestId;
 24
 825        public ReceivedFriendRequestHUDController(DataStore dataStore,
 26            IReceivedFriendRequestHUDView view,
 27            FriendRequestHUDController friendRequestHUDController,
 28            IFriendsController friendsController,
 29            IUserProfileBridge userProfileBridge,
 30            StringVariable openPassportVariable,
 31            ISocialAnalytics socialAnalytics)
 32        {
 833            this.dataStore = dataStore;
 834            this.view = view;
 835            this.friendRequestHUDController = friendRequestHUDController;
 836            this.friendsController = friendsController;
 837            this.userProfileBridge = userProfileBridge;
 838            this.openPassportVariable = openPassportVariable;
 839            this.socialAnalytics = socialAnalytics;
 40
 841            view.OnClose += Hide;
 842            view.OnOpenProfile += OpenProfile;
 843            view.OnRejectFriendRequest += Reject;
 844            view.OnConfirmFriendRequest += Confirm;
 845            view.Close();
 46
 847            dataStore.HUDs.openReceivedFriendRequestDetail.OnChange += ShowOrHide;
 848        }
 49
 50        public void Dispose()
 51        {
 852            friendOperationsCancellationToken.SafeCancelAndDispose();
 853            dataStore.HUDs.openReceivedFriendRequestDetail.OnChange -= ShowOrHide;
 854            friendRequestHUDController.Dispose();
 855        }
 56
 57        private void ShowOrHide(string current, string previous)
 58        {
 959            if (string.IsNullOrEmpty(current))
 160                Hide();
 61            else
 862                Show(current);
 863        }
 64
 65        private void Show(string friendRequestId)
 66        {
 867            this.friendRequestId = friendRequestId;
 68
 869            FriendRequest friendRequest = friendsController.GetAllocatedFriendRequest(this.friendRequestId);
 70
 871            if (friendRequest == null)
 72            {
 073                Debug.LogError($"Cannot display friend request {friendRequestId}, is not allocated");
 074                return;
 75            }
 76
 877            view.SetBodyMessage(friendRequest.MessageBody);
 878            view.SetTimestamp(DateTimeOffset.FromUnixTimeMilliseconds(friendRequest.Timestamp).DateTime);
 79
 880            var recipientProfile = userProfileBridge.Get(friendRequest.From);
 81
 882            if (recipientProfile != null)
 83            {
 884                view.SetSenderName(recipientProfile.userName);
 885                view.SetSenderProfilePicture(recipientProfile.face256SnapshotURL);
 86            }
 87            else
 088                Debug.LogError($"Cannot display user profile {friendRequest.From}, is not allocated");
 89
 890            var ownProfile = userProfileBridge.GetOwn();
 891            view.SetRecipientProfilePicture(ownProfile.face256SnapshotURL);
 892            view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() + 1);
 893            view.Show();
 894        }
 95
 96        private void Hide()
 97        {
 298            dataStore.HUDs.openReceivedFriendRequestDetail.Set(null, false);
 299            friendRequestHUDController.Hide();
 2100        }
 101
 102        private void OpenProfile()
 103        {
 1104            FriendRequest friendRequest = friendsController.GetAllocatedFriendRequest(friendRequestId);
 105
 1106            if (friendRequest == null)
 107            {
 0108                Debug.LogError($"Cannot open passport {friendRequestId}, is not allocated");
 0109                return;
 110            }
 111
 1112            openPassportVariable.Set(friendRequest.From);
 1113            view.SetSortingOrder(dataStore.HUDs.currentPassportSortingOrder.Get() - 1);
 1114        }
 115
 116        private void Reject()
 117        {
 118            async UniTaskVoid RejectAsync(CancellationToken cancellationToken = default)
 119            {
 2120                cancellationToken.ThrowIfCancellationRequested();
 121
 2122                view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Pending);
 123
 124                try
 125                {
 2126                    FriendRequest request = await friendsController.RejectFriendshipAsync(friendRequestId, cancellationT
 127
 1128                    socialAnalytics.SendFriendRequestRejected(request.From, request.To, "modal", request.HasBodyMessage)
 129
 1130                    view.SetState(ReceivedFriendRequestHUDModel.LayoutState.RejectSuccess);
 3131                    await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken);
 1132                }
 1133                catch (Exception e) when (e is not OperationCanceledException)
 134                {
 1135                    e.ReportFriendRequestErrorToAnalyticsByRequestId(friendRequestId, "modal", friendsController, social
 1136                    dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true);
 1137                    view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Default);
 1138                    throw;
 139                }
 1140            }
 141
 2142            friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart();
 2143            RejectAsync(friendOperationsCancellationToken.Token).Forget();
 2144        }
 145
 146        private void Confirm()
 147        {
 148            async UniTaskVoid ConfirmAsync(CancellationToken cancellationToken = default)
 149            {
 2150                cancellationToken.ThrowIfCancellationRequested();
 151
 2152                view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Pending);
 153
 154                try
 155                {
 2156                    FriendRequest request = await friendsController.AcceptFriendshipAsync(friendRequestId, cancellationT
 157
 1158                    socialAnalytics.SendFriendRequestApproved(request.From, request.To, "modal", request.HasBodyMessage)
 159
 1160                    view.SetState(ReceivedFriendRequestHUDModel.LayoutState.ConfirmSuccess);
 3161                    await friendRequestHUDController.HideWithDelay(cancellationToken: cancellationToken);
 1162                }
 1163                catch (Exception e) when (e is not OperationCanceledException)
 164                {
 1165                    e.ReportFriendRequestErrorToAnalyticsByRequestId(friendRequestId, "modal", friendsController, social
 1166                    dataStore.notifications.DefaultErrorNotification.Set(PROCESS_REQUEST_ERROR_MESSAGE, true);
 1167                    view.SetState(ReceivedFriendRequestHUDModel.LayoutState.Default);
 1168                    throw;
 169                }
 1170            }
 171
 2172            friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart();
 2173            ConfirmAsync(friendOperationsCancellationToken.Token).Forget();
 2174        }
 175    }
 176}