< Summary

Class:DCL.Social.Passports.PassportPlayerInfoComponentController
Assembly:PassportHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Passport/Passport/PlayerInfo/PassportPlayerInfoComponentController.cs
Covered lines:0
Uncovered lines:130
Coverable lines:130
Total lines:302
Line coverage:0% (0 of 130)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:24
Method coverage:0% (0 of 24)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PassportPlayerInfoComponentController(...)0%2100%
Dispose()0%12300%
WalletCopy(...)0%2100%
UsernameCopy(...)0%2100%
JumpInUser()0%6200%
UpdateWithUserProfile(...)0%12300%
ClosePassport()0%2100%
UpdateWithUserProfileAsync()0%56700%
FilterName()0%20400%
IsProfanityFilteringEnabled()0%2100%
AddPlayerAsFriend()0%6200%
RemoveFriend()0%12300%
CancelFriendRequest()0%12300%
CancelFriendRequestAsync()0%42600%
AcceptFriendRequest()0%12300%
AcceptFriendRequestAsync()0%42600%
BlockUser()0%20400%
UnblockUser()0%6200%
ReportUser()0%2100%
WhisperUser(...)0%6200%
UpdateFriendshipStatus(...)0%6200%
ToFriendshipStatus(...)0%30500%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Passport/Passport/PlayerInfo/PassportPlayerInfoComponentController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.ProfanityFiltering;
 3using DCL.Social.Friends;
 4using DCL.Tasks;
 5using SocialFeaturesAnalytics;
 6using System;
 7using System.Threading;
 8
 9namespace DCL.Social.Passports
 10{
 11    public class PassportPlayerInfoComponentController : IDisposable
 12    {
 13        private readonly IPassportPlayerInfoComponentView view;
 14        private readonly DataStore dataStore;
 15        private readonly IProfanityFilter profanityFilter;
 16        private readonly IFriendsController friendsController;
 17        private readonly IUserProfileBridge userProfileBridge;
 18        private readonly ISocialAnalytics socialAnalytics;
 19        private readonly BaseVariable<(string playerId, string source)> currentPlayerId;
 20        private readonly IClipboard clipboard;
 21        private readonly IPassportApiBridge passportApiBridge;
 22
 23        private CancellationTokenSource cancellationTokenSource;
 24        private CancellationTokenSource removeFriendCancellationToken;
 025        private UserProfile ownUserProfile => userProfileBridge.GetOwn();
 26        private string name;
 027        private bool isFriendsEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled("friends_enabled");
 28        public event Action OnClosePassport;
 29
 030        public PassportPlayerInfoComponentController(
 31            IPassportPlayerInfoComponentView view,
 32            DataStore dataStore,
 33            IProfanityFilter profanityFilter,
 34            IFriendsController friendsController,
 35            IUserProfileBridge userProfileBridge,
 36            ISocialAnalytics socialAnalytics,
 37            IClipboard clipboard,
 38            IPassportApiBridge passportApiBridge)
 39        {
 040            this.view = view;
 041            this.dataStore = dataStore;
 042            this.profanityFilter = profanityFilter;
 043            this.friendsController = friendsController;
 044            this.userProfileBridge = userProfileBridge;
 045            this.socialAnalytics = socialAnalytics;
 046            this.clipboard = clipboard;
 047            this.passportApiBridge = passportApiBridge;
 048            this.currentPlayerId = dataStore.HUDs.currentPlayerId;
 49
 050            view.OnAddFriend += AddPlayerAsFriend;
 051            view.OnRemoveFriend += RemoveFriend;
 052            view.OnCancelFriendRequest += CancelFriendRequest;
 053            view.OnAcceptFriendRequest += AcceptFriendRequest;
 054            view.OnBlockUser += BlockUser;
 055            view.OnUnblockUser += UnblockUser;
 056            view.OnReportUser += ReportUser;
 057            view.OnWhisperUser += WhisperUser;
 058            view.OnJumpInUser += JumpInUser;
 059            view.OnWalletCopy += WalletCopy;
 060            view.OnUsernameCopy += UsernameCopy;
 61
 062            friendsController.OnUpdateFriendship += UpdateFriendshipStatus;
 063        }
 64
 65        public void Dispose()
 66        {
 067            view.Dispose();
 068            cancellationTokenSource?.Cancel();
 069            cancellationTokenSource?.Dispose();
 070            cancellationTokenSource = null;
 071            friendsController.OnUpdateFriendship -= UpdateFriendshipStatus;
 072        }
 73
 74        private void WalletCopy(string address)
 75        {
 076            clipboard.WriteText(address);
 077            socialAnalytics.SendCopyWallet(PlayerActionSource.Passport);
 078        }
 79
 80        private void UsernameCopy(string username)
 81        {
 082            clipboard.WriteText(username);
 083            socialAnalytics.SendCopyUsername(PlayerActionSource.Passport);
 084        }
 85
 86        private void JumpInUser()
 87        {
 088            socialAnalytics.SendJumpInToPlayer(PlayerActionSource.Passport, currentPlayerId.Get().playerId);
 089            OnClosePassport?.Invoke();
 090        }
 91
 92        public void UpdateWithUserProfile(UserProfile userProfile)
 93        {
 094            cancellationTokenSource?.Cancel();
 095            cancellationTokenSource?.Dispose();
 096            cancellationTokenSource = new CancellationTokenSource();
 97
 098            UpdateWithUserProfileAsync(userProfile, cancellationTokenSource.Token).Forget();
 099        }
 100
 101        public void ClosePassport() =>
 0102            view.ResetCopyToast();
 103
 104        private async UniTask UpdateWithUserProfileAsync(UserProfile userProfile, CancellationToken cancellationToken)
 105        {
 0106            name = userProfile.name;
 0107            string filteredName = await FilterName(userProfile);
 108            PlayerPassportModel playerPassportModel;
 109
 0110            if (userProfile.isGuest)
 111            {
 0112                playerPassportModel = new PlayerPassportModel
 113                {
 114                    name = filteredName,
 115                    isGuest = userProfile.isGuest,
 116                    userId = userProfile.userId,
 117                    isBlocked = ownUserProfile.IsBlocked(userProfile.userId),
 118                    hasBlocked = userProfile.IsBlocked(ownUserProfile.userId),
 119                };
 120            }
 121            else
 122            {
 0123                playerPassportModel = new PlayerPassportModel
 124                {
 125                    name = filteredName,
 126                    userId = userProfile.userId,
 127                    presenceStatus = friendsController.GetUserStatus(userProfile.userId).presence,
 128                    isGuest = userProfile.isGuest,
 129                    isBlocked = ownUserProfile.IsBlocked(userProfile.userId),
 130                    hasBlocked = userProfile.IsBlocked(ownUserProfile.userId),
 131                    friendshipStatus = await friendsController.GetFriendshipStatus(userProfile.userId, cancellationToken
 132                    isFriendshipVisible = isFriendsEnabled && friendsController.IsInitialized,
 133                };
 134            }
 135
 0136            view.SetModel(playerPassportModel);
 0137            view.InitializeJumpInButton(friendsController, userProfile.userId, socialAnalytics);
 0138            view.SetActionsActive(userProfile.userId != ownUserProfile.userId);
 0139        }
 140
 141        private async UniTask<string> FilterName(UserProfile userProfile)
 142        {
 0143            return IsProfanityFilteringEnabled()
 144                ? await profanityFilter.Filter(userProfile.userName)
 145                : userProfile.userName;
 0146        }
 147
 148        private bool IsProfanityFilteringEnabled()
 149        {
 0150            return dataStore.settings.profanityChatFilteringEnabled.Get();
 151        }
 152
 153        private void AddPlayerAsFriend()
 154        {
 0155            if (userProfileBridge.GetOwn().isGuest)
 156            {
 0157                dataStore.HUDs.connectWalletModalVisible.Set(true);
 0158                return;
 159            }
 160
 0161            string userId = currentPlayerId.Get().playerId;
 0162            dataStore.HUDs.sendFriendRequest.Set(userId, true);
 0163        }
 164
 165        private void RemoveFriend()
 166        {
 0167            string userId = currentPlayerId.Get().playerId;
 168
 0169            dataStore.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateUnFriendData(
 170                UserProfileController.userProfilesCatalog.Get(userId)?.userName,
 171                () =>
 172                {
 0173                    removeFriendCancellationToken = removeFriendCancellationToken.SafeRestart();
 0174                    friendsController.RemoveFriendAsync(userId, removeFriendCancellationToken.Token);
 0175                    socialAnalytics.SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSource
 0176                }), true);
 0177        }
 178
 179        private void CancelFriendRequest()
 180        {
 0181            cancellationTokenSource?.Cancel();
 0182            cancellationTokenSource?.Dispose();
 0183            cancellationTokenSource = new CancellationTokenSource();
 0184            CancelFriendRequestAsync(cancellationTokenSource.Token).Forget();
 0185        }
 186
 187        private async UniTaskVoid CancelFriendRequestAsync(CancellationToken cancellationToken)
 188        {
 0189            string userId = currentPlayerId.Get().playerId;
 190
 191            try
 192            {
 0193                var friendRequest = await friendsController.CancelRequestByUserIdAsync(userId, cancellationToken);
 0194                dataStore.HUDs.openSentFriendRequestDetail.Set(null, true);
 195
 0196                socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, userId,
 197                    PlayerActionSource.Passport.ToString(), friendRequest.FriendRequestId);
 0198            }
 0199            catch (Exception e) when (e is not OperationCanceledException)
 200            {
 0201                e.ReportFriendRequestErrorToAnalyticsByUserId(userId, "modal",
 202                    friendsController, socialAnalytics);
 203
 0204                throw;
 205            }
 0206        }
 207
 208        private void AcceptFriendRequest()
 209        {
 0210            cancellationTokenSource?.Cancel();
 0211            cancellationTokenSource?.Dispose();
 0212            cancellationTokenSource = new CancellationTokenSource();
 0213            AcceptFriendRequestAsync(cancellationTokenSource.Token).Forget();
 0214        }
 215
 216        private async UniTaskVoid AcceptFriendRequestAsync(CancellationToken cancellationToken)
 217        {
 0218            string userId = currentPlayerId.Get().playerId;
 219
 220            try
 221            {
 0222                FriendRequest request = friendsController.GetAllocatedFriendRequestByUser(userId);
 0223                await friendsController.AcceptFriendshipAsync(request.FriendRequestId, cancellationToken);
 0224                dataStore.HUDs.openReceivedFriendRequestDetail.Set(null, true);
 225
 0226                socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, userId, PlayerActionSource.Passport.ToS
 227                    request.HasBodyMessage, request.FriendRequestId);
 0228            }
 0229            catch (Exception e) when (e is not OperationCanceledException)
 230            {
 0231                e.ReportFriendRequestErrorToAnalyticsByUserId(userId, "modal",
 232                    friendsController, socialAnalytics);
 233
 0234                throw;
 235            }
 0236        }
 237
 238        private void BlockUser()
 239        {
 0240            string userId = currentPlayerId.Get().playerId;
 0241            if (ownUserProfile.IsBlocked(userId)) return;
 242
 0243            dataStore.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateBlockUserData(
 244                userProfileBridge.Get(userId)?.userName,
 245                () =>
 246                {
 0247                    ownUserProfile.Block(userId);
 0248                    view.SetIsBlocked(true);
 0249                    passportApiBridge.SendBlockPlayer(userId);
 0250                    socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(userId), PlayerActionSource.Passport, c
 0251                }), true);
 0252        }
 253
 254        private void UnblockUser()
 255        {
 0256            string userId = currentPlayerId.Get().playerId;
 0257            if (!ownUserProfile.IsBlocked(userId)) return;
 0258            ownUserProfile.Unblock(userId);
 0259            view.SetIsBlocked(false);
 0260            passportApiBridge.SendUnblockPlayer(userId);
 0261            socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(userId), PlayerActionSource.Passport, current
 0262        }
 263
 264        private void ReportUser()
 265        {
 0266            passportApiBridge.SendReportPlayer(currentPlayerId.Get().playerId, name);
 0267            socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport, currentPlayerId
 0268        }
 269
 270        private void WhisperUser(string userId)
 271        {
 0272            dataStore.HUDs.openChat.Set(userId, true);
 0273            socialAnalytics.SendStartedConversation(PlayerActionSource.Passport, currentPlayerId.Get().playerId);
 0274            OnClosePassport?.Invoke();
 0275        }
 276
 277        private void UpdateFriendshipStatus(string userId, FriendshipAction action)
 278        {
 0279            if (userId != currentPlayerId.Get().playerId) return;
 0280            view.SetFriendStatus(ToFriendshipStatus(action));
 0281        }
 282
 283        private FriendshipStatus ToFriendshipStatus(FriendshipAction action)
 284        {
 285            switch (action)
 286            {
 287                case FriendshipAction.APPROVED:
 0288                    return FriendshipStatus.FRIEND;
 289                case FriendshipAction.REQUESTED_TO:
 0290                    return FriendshipStatus.REQUESTED_TO;
 291                case FriendshipAction.REQUESTED_FROM:
 0292                    return FriendshipStatus.REQUESTED_FROM;
 293                case FriendshipAction.NONE:
 294                case FriendshipAction.DELETED:
 295                case FriendshipAction.REJECTED:
 296                case FriendshipAction.CANCELLED:
 297                default:
 0298                    return FriendshipStatus.NOT_FRIEND;
 299            }
 300        }
 301    }
 302}