< 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:129
Coverable lines:129
Total lines:305
Line coverage:0% (0 of 129)
Covered branches:0
Total branches:0

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%42600%
FilterName()0%20400%
IsProfanityFilteringEnabled()0%2100%
AddPlayerAsFriend()0%6200%
RemoveFriend()0%12300%
CancelFriendRequest()0%12300%
CancelFriendRequestAsync()0%56700%
AcceptFriendRequest()0%12300%
AcceptFriendRequestAsync()0%56700%
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.Interface;
 3using DCL.ProfanityFiltering;
 4using DCL.Social.Friends;
 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 StringVariable currentPlayerId;
 20        private readonly IClipboard clipboard;
 21        private readonly IPassportApiBridge passportApiBridge;
 22
 023        private UserProfile ownUserProfile => userProfileBridge.GetOwn();
 24        private string name;
 025        private bool isNewFriendRequestsEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled("new_friend_reque
 026        private bool isFriendsEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled("friends_enabled");
 27        public event Action OnClosePassport;
 28        private CancellationTokenSource cancellationTokenSource;
 29
 030        public PassportPlayerInfoComponentController(
 31            StringVariable currentPlayerId,
 32            IPassportPlayerInfoComponentView view,
 33            DataStore dataStore,
 34            IProfanityFilter profanityFilter,
 35            IFriendsController friendsController,
 36            IUserProfileBridge userProfileBridge,
 37            ISocialAnalytics socialAnalytics,
 38            IClipboard clipboard,
 39            IPassportApiBridge passportApiBridge)
 40        {
 041            this.currentPlayerId = currentPlayerId;
 042            this.view = view;
 043            this.dataStore = dataStore;
 044            this.profanityFilter = profanityFilter;
 045            this.friendsController = friendsController;
 046            this.userProfileBridge = userProfileBridge;
 047            this.socialAnalytics = socialAnalytics;
 048            this.clipboard = clipboard;
 049            this.passportApiBridge = passportApiBridge;
 50
 051            view.OnAddFriend += AddPlayerAsFriend;
 052            view.OnRemoveFriend += RemoveFriend;
 053            view.OnCancelFriendRequest += CancelFriendRequest;
 054            view.OnAcceptFriendRequest += AcceptFriendRequest;
 055            view.OnBlockUser += BlockUser;
 056            view.OnUnblockUser += UnblockUser;
 057            view.OnReportUser += ReportUser;
 058            view.OnWhisperUser += WhisperUser;
 059            view.OnJumpInUser += JumpInUser;
 060            view.OnWalletCopy += WalletCopy;
 061            view.OnUsernameCopy += UsernameCopy;
 62
 063            friendsController.OnUpdateFriendship += UpdateFriendshipStatus;
 064        }
 65
 66        public void Dispose()
 67        {
 068            view.Dispose();
 069            cancellationTokenSource?.Cancel();
 070            cancellationTokenSource?.Dispose();
 071            cancellationTokenSource = null;
 072            friendsController.OnUpdateFriendship -= UpdateFriendshipStatus;
 073        }
 74
 75        private void WalletCopy(string address)
 76        {
 077            clipboard.WriteText(address);
 078            socialAnalytics.SendCopyWallet(PlayerActionSource.Passport);
 079        }
 80
 81        private void UsernameCopy(string username)
 82        {
 083            clipboard.WriteText(username);
 084            socialAnalytics.SendCopyUsername(PlayerActionSource.Passport);
 085        }
 86
 87        private void JumpInUser()
 88        {
 089            socialAnalytics.SendJumpInToPlayer(PlayerActionSource.Passport);
 090            OnClosePassport?.Invoke();
 091        }
 92
 93        public void UpdateWithUserProfile(UserProfile userProfile)
 94        {
 095            cancellationTokenSource?.Cancel();
 096            cancellationTokenSource?.Dispose();
 097            cancellationTokenSource = new CancellationTokenSource();
 98
 099            UpdateWithUserProfileAsync(userProfile, cancellationTokenSource.Token).Forget();
 0100        }
 101
 102        public void ClosePassport() =>
 0103            view.ResetCopyToast();
 104
 105        private async UniTask UpdateWithUserProfileAsync(UserProfile userProfile, CancellationToken cancellationToken)
 106        {
 0107            name = userProfile.name;
 0108            string filteredName = await FilterName(userProfile);
 109            PlayerPassportModel playerPassportModel;
 110
 0111            if (userProfile.isGuest)
 112            {
 0113                playerPassportModel = new PlayerPassportModel
 114                {
 115                    name = filteredName,
 116                    isGuest = userProfile.isGuest,
 117                };
 118            }
 119            else
 120            {
 0121                playerPassportModel = new PlayerPassportModel
 122                {
 123                    name = filteredName,
 124                    userId = userProfile.userId,
 125                    presenceStatus = friendsController.GetUserStatus(userProfile.userId).presence,
 126                    isGuest = userProfile.isGuest,
 127                    isBlocked = ownUserProfile.IsBlocked(userProfile.userId),
 128                    hasBlocked = userProfile.IsBlocked(ownUserProfile.userId),
 129                    friendshipStatus = await friendsController.GetFriendshipStatus(userProfile.userId, cancellationToken
 130                    isFriendshipVisible = isFriendsEnabled,
 131                };
 132            }
 133
 0134            view.SetModel(playerPassportModel);
 0135            view.InitializeJumpInButton(friendsController, userProfile.userId, socialAnalytics);
 0136            view.SetActionsActive(userProfile.userId != ownUserProfile.userId);
 0137        }
 138
 139        private async UniTask<string> FilterName(UserProfile userProfile)
 140        {
 0141            return IsProfanityFilteringEnabled()
 142                ? await profanityFilter.Filter(userProfile.userName)
 143                : userProfile.userName;
 0144        }
 145
 146        private bool IsProfanityFilteringEnabled()
 147        {
 0148            return dataStore.settings.profanityChatFilteringEnabled.Get();
 149        }
 150
 151        private void AddPlayerAsFriend()
 152        {
 0153            if (isNewFriendRequestsEnabled)
 0154                dataStore.HUDs.sendFriendRequest.Set(currentPlayerId, true);
 155            else
 156            {
 0157                friendsController.RequestFriendship(currentPlayerId);
 0158                socialAnalytics.SendFriendRequestSent(ownUserProfile.userId, currentPlayerId, 0, PlayerActionSource.Pass
 159            }
 0160        }
 161
 162        private void RemoveFriend()
 163        {
 0164            dataStore.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateUnFriendData(
 165                UserProfileController.userProfilesCatalog.Get(currentPlayerId)?.userName,
 166                () =>
 167                {
 0168                    friendsController.RemoveFriend(currentPlayerId);
 0169                    socialAnalytics.SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, currentPlayerId, PlayerAct
 0170                }), true);
 0171        }
 172
 173        private void CancelFriendRequest()
 174        {
 0175            cancellationTokenSource?.Cancel();
 0176            cancellationTokenSource?.Dispose();
 0177            cancellationTokenSource = new CancellationTokenSource();
 0178            CancelFriendRequestAsync(cancellationTokenSource.Token).Forget();
 0179        }
 180
 181        private async UniTaskVoid CancelFriendRequestAsync(CancellationToken cancellationToken)
 182        {
 0183            if (isNewFriendRequestsEnabled)
 184            {
 185                try
 186                {
 0187                    await friendsController.CancelRequestByUserIdAsync(currentPlayerId, cancellationToken);
 0188                    dataStore.HUDs.openSentFriendRequestDetail.Set(null, true);
 0189                }
 0190                catch (Exception e) when (e is not OperationCanceledException)
 191                {
 0192                    e.ReportFriendRequestErrorToAnalyticsByUserId(currentPlayerId, "modal",
 193                        friendsController, socialAnalytics);
 194
 0195                    throw;
 196                }
 197            }
 198            else
 0199                friendsController.CancelRequestByUserId(currentPlayerId);
 200
 0201            socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, currentPlayerId,
 202                PlayerActionSource.Passport.ToString());
 0203        }
 204
 205        private void AcceptFriendRequest()
 206        {
 0207            cancellationTokenSource?.Cancel();
 0208            cancellationTokenSource?.Dispose();
 0209            cancellationTokenSource = new CancellationTokenSource();
 0210            AcceptFriendRequestAsync(cancellationTokenSource.Token).Forget();
 0211        }
 212
 213        private async UniTaskVoid AcceptFriendRequestAsync(CancellationToken cancellationToken)
 214        {
 0215            if (isNewFriendRequestsEnabled)
 216            {
 217                try
 218                {
 0219                    FriendRequest request = friendsController.GetAllocatedFriendRequestByUser(currentPlayerId);
 0220                    await friendsController.AcceptFriendshipAsync(request.FriendRequestId, cancellationToken);
 0221                    dataStore.HUDs.openReceivedFriendRequestDetail.Set(null, true);
 222
 0223                    socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, currentPlayerId, PlayerActionSource
 224                        request.HasBodyMessage);
 0225                }
 0226                catch (Exception e) when (e is not OperationCanceledException)
 227                {
 0228                    e.ReportFriendRequestErrorToAnalyticsByUserId(currentPlayerId, "modal",
 229                        friendsController, socialAnalytics);
 230
 0231                    throw;
 232                }
 233            }
 234            else
 235            {
 0236                friendsController.AcceptFriendship(currentPlayerId);
 237
 0238                socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Pas
 239                    false);
 240            }
 0241        }
 242
 243        private void BlockUser()
 244        {
 0245            if (ownUserProfile.IsBlocked(currentPlayerId)) return;
 246
 0247            dataStore.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateBlockUserData(
 248                userProfileBridge.Get(currentPlayerId)?.userName,
 249                () =>
 250                {
 0251                    ownUserProfile.Block(currentPlayerId);
 0252                    view.SetIsBlocked(true);
 0253                    passportApiBridge.SendBlockPlayer(currentPlayerId);
 0254                    socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(currentPlayerId), PlayerActionSource.Pa
 0255                }), true);
 0256        }
 257
 258        private void UnblockUser()
 259        {
 0260            if (!ownUserProfile.IsBlocked(currentPlayerId)) return;
 0261            ownUserProfile.Unblock(currentPlayerId);
 0262            view.SetIsBlocked(false);
 0263            passportApiBridge.SendUnblockPlayer(currentPlayerId);
 0264            socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(currentPlayerId), PlayerActionSource.Passport
 0265        }
 266
 267        private void ReportUser()
 268        {
 0269            passportApiBridge.SendReportPlayer(currentPlayerId, name);
 0270            socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport);
 0271        }
 272
 273        private void WhisperUser(string userId)
 274        {
 0275            dataStore.HUDs.openPrivateChat.Set(userId);
 0276            socialAnalytics.SendStartedConversation(PlayerActionSource.Passport);
 0277            OnClosePassport?.Invoke();
 0278        }
 279
 280        private void UpdateFriendshipStatus(string userId, FriendshipAction action)
 281        {
 0282            if (userId != currentPlayerId) return;
 0283            view.SetFriendStatus(ToFriendshipStatus(action));
 0284        }
 285
 286        private FriendshipStatus ToFriendshipStatus(FriendshipAction action)
 287        {
 288            switch (action)
 289            {
 290                case FriendshipAction.APPROVED:
 0291                    return FriendshipStatus.FRIEND;
 292                case FriendshipAction.REQUESTED_TO:
 0293                    return FriendshipStatus.REQUESTED_TO;
 294                case FriendshipAction.REQUESTED_FROM:
 0295                    return FriendshipStatus.REQUESTED_FROM;
 296                case FriendshipAction.NONE:
 297                case FriendshipAction.DELETED:
 298                case FriendshipAction.REJECTED:
 299                case FriendshipAction.CANCELLED:
 300                default:
 0301                    return FriendshipStatus.NOT_FRIEND;
 302            }
 303        }
 304    }
 305}