< Summary

Class:UserContextMenu
Assembly:UserContextMenu
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/UserContextMenu/Scripts/UserContextMenu.cs
Covered lines:114
Uncovered lines:50
Coverable lines:164
Total lines:423
Line coverage:69.5% (114 of 164)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UserContextMenu()0%110100%
Show(...)0%110100%
Show(...)0%4.034087.5%
SetConfirmationDialog(...)0%2100%
Hide()0%2.152066.67%
SetFriendshipContentActive(...)0%110100%
Awake()0%220100%
Update()0%110100%
OnDisable()0%110100%
ClickReportButton()0%2100%
OnPassportButtonPressed()0%220100%
OnReportUserButtonPressed()0%440100%
OnDeleteUserButtonPressed()0%30500%
UnfriendUser()0%2100%
OnAddFriendButtonPressed()0%20400%
OnCancelFriendRequestButtonPressed()0%6200%
OnMessageButtonPressed()0%12300%
OnBlockUserButtonPressed()0%3.13077.78%
UpdateBlockButton()0%330100%
HideIfClickedOutside()0%6.83025%
ProcessActiveElements(...)0%110100%
Setup(...)0%11.0211095%
SetupFriendship(...)0%15.0213077.14%
OnFriendActionUpdate(...)0%7.077088.89%
GetSocialAnalytics()0%2.152066.67%
OnValidate()0%2.062075%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/UserContextMenu/Scripts/UserContextMenu.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using DCL.Interface;
 4using DCL.Social.Friends;
 5using SocialFeaturesAnalytics;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.EventSystems;
 9using UnityEngine.UI;
 10
 11/// <summary>
 12/// Contextual menu with different options about an user.
 13/// </summary>
 14[RequireComponent(typeof(RectTransform))]
 15public class UserContextMenu : MonoBehaviour
 16{
 17    internal const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId";
 18
 19    const string BLOCK_BTN_BLOCK_TEXT = "Block";
 20    const string BLOCK_BTN_UNBLOCK_TEXT = "Unblock";
 21    const string DELETE_MSG_PATTERN = "Are you sure you want to delete {0} as a friend?";
 22
 23    [System.Flags]
 24    public enum MenuConfigFlags
 25    {
 26        Name = 1,
 27        Friendship = 2,
 28        Message = 4,
 29        Passport = 8,
 30        Block = 16,
 31        Report = 32
 32    }
 33
 34    const MenuConfigFlags headerFlags = MenuConfigFlags.Name | MenuConfigFlags.Friendship;
 35    const MenuConfigFlags usesFriendsApiFlags = MenuConfigFlags.Friendship | MenuConfigFlags.Message;
 36
 37    [Header("Optional: Set Confirmation Dialog")]
 38    [SerializeField] internal UserContextConfirmationDialog confirmationDialog;
 39
 40    [Header("Enable Actions")]
 22841    [SerializeField] internal MenuConfigFlags menuConfigFlags = MenuConfigFlags.Passport | MenuConfigFlags.Block | MenuC
 42
 43    [Header("Containers")]
 44    [SerializeField] internal GameObject headerContainer;
 45    [SerializeField] internal GameObject bodyContainer;
 46    [SerializeField] internal GameObject friendshipContainer;
 47    [SerializeField] internal GameObject friendAddContainer;
 48    [SerializeField] internal GameObject friendRemoveContainer;
 49    [SerializeField] internal GameObject friendRequestedContainer;
 50
 51    [Header("Texts")]
 52    [SerializeField] internal TextMeshProUGUI userName;
 53    [SerializeField] internal TextMeshProUGUI blockText;
 54
 55    [Header("Buttons")]
 56    [SerializeField] internal Button passportButton;
 57    [SerializeField] internal Button blockButton;
 58    [SerializeField] internal Button reportButton;
 59    [SerializeField] internal Button addFriendButton;
 60    [SerializeField] internal Button cancelFriendButton;
 61    [SerializeField] internal Button deleteFriendButton;
 62    [SerializeField] internal Button messageButton;
 63
 64    public static event System.Action<string> OnOpenPrivateChatRequest;
 65
 2366    public bool isVisible => gameObject.activeSelf;
 067    public string UserId => userId;
 68
 69    public event System.Action OnShowMenu;
 70    public event System.Action<string> OnPassport;
 71    public event System.Action<string> OnReport;
 72    public event System.Action<string, bool> OnBlock;
 73    public event System.Action<string> OnUnfriend;
 74    public event System.Action<string> OnAddFriend;
 75    public event System.Action<string> OnCancelFriend;
 76    public event System.Action<string> OnMessage;
 77    public event System.Action OnHide;
 78
 79    private static StringVariable currentPlayerId = null;
 80    private RectTransform rectTransform;
 81    private string userId;
 82    private bool isBlocked;
 83    private MenuConfigFlags currentConfigFlags;
 84    private IConfirmationDialog currentConfirmationDialog;
 85    internal ISocialAnalytics socialAnalytics;
 86
 87    /// <summary>
 88    /// Show context menu
 89    /// </summary>
 90    /// <param name="userId"> user id</param>
 1091    public void Show(string userId) { Show(userId, menuConfigFlags); }
 92
 93    /// <summary>
 94    /// Show context menu
 95    /// </summary>
 96    /// <param name="userId"> user id</param>
 97    /// <param name="configFlags">set buttons to enable in menu</param>
 98    public void Show(string userId, MenuConfigFlags configFlags)
 99    {
 7100        this.userId = userId;
 7101        ProcessActiveElements(configFlags);
 7102        Setup(userId, configFlags);
 7103        if (currentConfirmationDialog == null && confirmationDialog != null)
 104        {
 0105            SetConfirmationDialog(confirmationDialog);
 106        }
 7107        gameObject.SetActive(true);
 7108        OnShowMenu?.Invoke();
 1109    }
 110
 111    /// <summary>
 112    /// Set confirmation popup to reference use
 113    /// </summary>
 114    /// <param name="confirmationPopup">confirmation popup reference</param>
 0115    public void SetConfirmationDialog(IConfirmationDialog confirmationPopup) { this.currentConfirmationDialog = confirma
 116
 117    /// <summary>
 118    /// Hides the context menu.
 119    /// </summary>
 120    public void Hide()
 121    {
 20122        gameObject.SetActive(false);
 20123        OnHide?.Invoke();
 0124    }
 125
 126    /// <summary>
 127    /// Shows/Hides the friendship container
 128    /// </summary>
 2129    public void SetFriendshipContentActive(bool isActive) => friendshipContainer.SetActive(isActive);
 130
 131    private void Awake()
 132    {
 14133        if (!currentPlayerId)
 134        {
 1135            currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID);
 136        }
 137
 14138        rectTransform = GetComponent<RectTransform>();
 139
 14140        passportButton.onClick.AddListener(OnPassportButtonPressed);
 14141        blockButton.onClick.AddListener(OnBlockUserButtonPressed);
 14142        reportButton.onClick.AddListener(OnReportUserButtonPressed);
 14143        deleteFriendButton.onClick.AddListener(OnDeleteUserButtonPressed);
 14144        addFriendButton.onClick.AddListener(OnAddFriendButtonPressed);
 14145        cancelFriendButton.onClick.AddListener(OnCancelFriendRequestButtonPressed);
 14146        messageButton.onClick.AddListener(OnMessageButtonPressed);
 14147    }
 148
 120149    private void Update() { HideIfClickedOutside(); }
 150
 151    private void OnDisable()
 152    {
 14153        FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 14154    }
 155
 0156    public void ClickReportButton() => reportButton.onClick.Invoke();
 157
 158    private void OnPassportButtonPressed()
 159    {
 1160        OnPassport?.Invoke(userId);
 1161        currentPlayerId.Set(userId);
 1162        Hide();
 163
 1164        AudioScriptableObjects.dialogOpen.Play(true);
 1165    }
 166
 167    private void OnReportUserButtonPressed()
 168    {
 1169        OnReport?.Invoke(userId);
 1170        WebInterface.SendReportPlayer(userId, UserProfileController.userProfilesCatalog.Get(userId)?.userName);
 1171        GetSocialAnalytics().SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.ProfileContextMenu);
 1172        Hide();
 1173    }
 174
 175    private void OnDeleteUserButtonPressed()
 176    {
 0177        OnUnfriend?.Invoke(userId);
 178
 0179        if (currentConfirmationDialog != null)
 180        {
 0181            currentConfirmationDialog.SetText(string.Format(DELETE_MSG_PATTERN, UserProfileController.userProfilesCatalo
 0182            currentConfirmationDialog.Show(() =>
 183            {
 0184                UnfriendUser();
 0185            });
 186        }
 187        else
 188        {
 0189            UnfriendUser();
 190        }
 191
 0192        GetSocialAnalytics().SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSource.Profil
 0193        Hide();
 0194    }
 195
 196    private void UnfriendUser()
 197    {
 0198        FriendsController.i.RemoveFriend(userId);
 0199    }
 200
 201    private void OnAddFriendButtonPressed()
 202    {
 0203        OnAddFriend?.Invoke(userId);
 204
 205        // NOTE: if we don't add this, the friend request has strange behaviors
 0206        UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel()
 207        {
 208            userId = userId,
 209            name = UserProfileController.userProfilesCatalog.Get(userId)?.userName
 210        });
 211
 0212        FriendsController.i.RequestFriendship(userId);
 213
 0214        GetSocialAnalytics().SendFriendRequestSent(UserProfile.GetOwnUserProfile().userId, userId, 0, PlayerActionSource
 0215    }
 216
 217    private void OnCancelFriendRequestButtonPressed()
 218    {
 0219        OnCancelFriend?.Invoke(userId);
 220
 0221        FriendsController.i.CancelRequest(userId);
 222
 0223        GetSocialAnalytics().SendFriendRequestCancelled(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSour
 0224    }
 225
 226    private void OnMessageButtonPressed()
 227    {
 0228        OnMessage?.Invoke(userId);
 0229        OnOpenPrivateChatRequest?.Invoke(userId);
 0230        Hide();
 0231    }
 232
 233    private void OnBlockUserButtonPressed()
 234    {
 1235        bool blockUser = !isBlocked;
 1236        OnBlock?.Invoke(userId, blockUser);
 1237        if (blockUser)
 238        {
 1239            WebInterface.SendBlockPlayer(userId);
 1240            GetSocialAnalytics().SendPlayerBlocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileConte
 241        }
 242        else
 243        {
 0244            WebInterface.SendUnblockPlayer(userId);
 0245            GetSocialAnalytics().SendPlayerUnblocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileCon
 246        }
 1247        Hide();
 1248    }
 249
 10250    private void UpdateBlockButton() { blockText.text = isBlocked ? BLOCK_BTN_UNBLOCK_TEXT : BLOCK_BTN_BLOCK_TEXT; }
 251
 252    private void HideIfClickedOutside()
 253    {
 120254        if (!Input.GetMouseButtonDown(0)) return;
 0255        var pointerEventData = new PointerEventData(EventSystem.current)
 256        {
 257            position = Input.mousePosition
 258        };
 0259        var raycastResults = new List<RaycastResult>();
 0260        EventSystem.current.RaycastAll(pointerEventData, raycastResults);
 261
 0262        if (raycastResults.All(result => result.gameObject != gameObject))
 0263            Hide();
 0264    }
 265
 266    private void ProcessActiveElements(MenuConfigFlags flags)
 267    {
 31268        headerContainer.SetActive((flags & headerFlags) != 0);
 31269        userName.gameObject.SetActive((flags & MenuConfigFlags.Name) != 0);
 31270        friendshipContainer.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 31271        deleteFriendButton.gameObject.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 31272        passportButton.gameObject.SetActive((flags & MenuConfigFlags.Passport) != 0);
 31273        blockButton.gameObject.SetActive((flags & MenuConfigFlags.Block) != 0);
 31274        reportButton.gameObject.SetActive((flags & MenuConfigFlags.Report) != 0);
 31275        messageButton.gameObject.SetActive((flags & MenuConfigFlags.Message) != 0);
 31276    }
 277
 278    private void Setup(string userId, MenuConfigFlags configFlags)
 279    {
 7280        this.userId = userId;
 281
 7282        UserProfile profile = UserProfileController.userProfilesCatalog.Get(userId);
 7283        bool userHasWallet = profile?.hasConnectedWeb3 ?? false;
 284
 7285        if (!userHasWallet || !UserProfile.GetOwnUserProfile().hasConnectedWeb3)
 286        {
 1287            configFlags &= ~usesFriendsApiFlags;
 288        }
 289
 7290        currentConfigFlags = configFlags;
 7291        ProcessActiveElements(configFlags);
 292
 7293        if ((configFlags & MenuConfigFlags.Block) != 0)
 294        {
 5295            isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId);
 5296            UpdateBlockButton();
 297        }
 7298        if ((configFlags & MenuConfigFlags.Name) != 0)
 299        {
 5300            string name = profile?.userName;
 5301            userName.text = name;
 302        }
 7303        if ((configFlags & usesFriendsApiFlags) != 0)
 304        {
 6305            if (FriendsController.i.friends.TryGetValue(userId, out UserStatus status))
 306            {
 0307                SetupFriendship(status.friendshipStatus);
 308            }
 309            else
 310            {
 6311                SetupFriendship(FriendshipStatus.NOT_FRIEND);
 312            }
 6313            FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 6314            FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate;
 315        }
 7316    }
 317
 318    private void SetupFriendship(FriendshipStatus friendshipStatus)
 319    {
 12320        bool friendshipEnabled = (currentConfigFlags & MenuConfigFlags.Friendship) != 0;
 12321        bool messageEnabled = (currentConfigFlags & MenuConfigFlags.Message) != 0;
 322
 12323        if (friendshipStatus == FriendshipStatus.FRIEND)
 324        {
 2325            if (friendshipEnabled)
 326            {
 1327                friendAddContainer.SetActive(false);
 1328                friendRemoveContainer.SetActive(true);
 1329                friendRequestedContainer.SetActive(false);
 1330                deleteFriendButton.gameObject.SetActive(true);
 331            }
 2332            if (messageEnabled)
 333            {
 1334                messageButton.gameObject.SetActive(true);
 335            }
 336        }
 10337        else if (friendshipStatus == FriendshipStatus.REQUESTED_TO)
 338        {
 2339            if (friendshipEnabled)
 340            {
 1341                friendAddContainer.SetActive(false);
 1342                friendRemoveContainer.SetActive(false);
 1343                friendRequestedContainer.SetActive(true);
 1344                deleteFriendButton.gameObject.SetActive(false);
 345            }
 2346            if (messageEnabled)
 347            {
 1348                messageButton.gameObject.SetActive(false);
 349            }
 350        }
 8351        else if (friendshipStatus == FriendshipStatus.NOT_FRIEND)
 352        {
 8353            if (friendshipEnabled)
 354            {
 6355                friendAddContainer.SetActive(true);
 6356                friendRemoveContainer.SetActive(false);
 6357                friendRequestedContainer.SetActive(false);
 6358                deleteFriendButton.gameObject.SetActive(false);
 359            }
 8360            if (messageEnabled)
 361            {
 6362                messageButton.gameObject.SetActive(false);
 363            }
 364        }
 0365        else if (friendshipStatus == FriendshipStatus.REQUESTED_FROM)
 366        {
 0367            if (friendshipEnabled)
 368            {
 0369                friendAddContainer.SetActive(true);
 0370                friendRemoveContainer.SetActive(false);
 0371                friendRequestedContainer.SetActive(false);
 0372                deleteFriendButton.gameObject.SetActive(false);
 373            }
 0374            if (messageEnabled)
 375            {
 0376                messageButton.gameObject.SetActive(false);
 377            }
 378        }
 4379    }
 380
 381    private void OnFriendActionUpdate(string userId, FriendshipAction action)
 382    {
 6383        if (this.userId != userId)
 384        {
 0385            return;
 386        }
 387
 6388        if (action == FriendshipAction.APPROVED)
 389        {
 2390            SetupFriendship(FriendshipStatus.FRIEND);
 391        }
 4392        else if (action == FriendshipAction.REQUESTED_TO)
 393        {
 2394            SetupFriendship(FriendshipStatus.REQUESTED_TO);
 395        }
 2396        else if (action == FriendshipAction.DELETED || action == FriendshipAction.CANCELLED || action == FriendshipActio
 397        {
 2398            SetupFriendship(FriendshipStatus.NOT_FRIEND);
 399        }
 2400    }
 401
 402    private ISocialAnalytics GetSocialAnalytics()
 403    {
 2404        if (socialAnalytics == null)
 405        {
 0406            socialAnalytics = new SocialAnalytics(
 407                DCL.Environment.i.platform.serviceProviders.analytics,
 408                new UserProfileWebInterfaceBridge());
 409        }
 410
 2411        return socialAnalytics;
 412    }
 413
 414#if UNITY_EDITOR
 415    //This is just to process buttons and container visibility on editor
 416    private void OnValidate()
 417    {
 17418        if (headerContainer == null)
 0419            return;
 17420        ProcessActiveElements(menuConfigFlags);
 17421    }
 422#endif
 423}