< Summary

Class:UserContextMenu
Assembly:UserContextMenu
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/UserContextMenu/Scripts/UserContextMenu.cs
Covered lines:119
Uncovered lines:58
Coverable lines:177
Total lines:435
Line coverage:67.2% (119 of 177)
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%2100%
OnDisable()0%220100%
ClickReportButton()0%2100%
OnPassportButtonPressed()0%220100%
OnReportUserButtonPressed()0%440100%
OnDeleteUserButtonPressed()0%30500%
UnfriendUser()0%2100%
OnAddFriendButtonPressed()0%30500%
OnCancelFriendRequestButtonPressed()0%12300%
OnMessageButtonPressed()0%12300%
OnBlockUserButtonPressed()0%3.073080%
UpdateBlockButton()0%330100%
HideIfClickedOutside()0%12300%
ProcessActiveElements(...)0%110100%
Setup(...)0%12.1212090.48%
SetupFriendship(...)0%14.5813078.95%
OnFriendActionUpdate(...)0%7.047090.91%
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 SocialFeaturesAnalytics;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.EventSystems;
 8using UnityEngine.UI;
 9
 10/// <summary>
 11/// Contextual menu with different options about an user.
 12/// </summary>
 13[RequireComponent(typeof(RectTransform))]
 14public class UserContextMenu : MonoBehaviour
 15{
 16    internal const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId";
 17
 18    const string BLOCK_BTN_BLOCK_TEXT = "Block";
 19    const string BLOCK_BTN_UNBLOCK_TEXT = "Unblock";
 20    const string DELETE_MSG_PATTERN = "Are you sure you want to delete {0} as a friend?";
 21
 22    [System.Flags]
 23    public enum MenuConfigFlags
 24    {
 25        Name = 1,
 26        Friendship = 2,
 27        Message = 4,
 28        Passport = 8,
 29        Block = 16,
 30        Report = 32
 31    }
 32
 33    const MenuConfigFlags headerFlags = MenuConfigFlags.Name | MenuConfigFlags.Friendship;
 34    const MenuConfigFlags usesFriendsApiFlags = MenuConfigFlags.Friendship | MenuConfigFlags.Message;
 35
 36    [Header("Optional: Set Confirmation Dialog")]
 37    [SerializeField] internal UserContextConfirmationDialog confirmationDialog;
 38
 39    [Header("Enable Actions")]
 22340    [SerializeField] internal MenuConfigFlags menuConfigFlags = MenuConfigFlags.Passport | MenuConfigFlags.Block | MenuC
 41
 42    [Header("Containers")]
 43    [SerializeField] internal GameObject headerContainer;
 44    [SerializeField] internal GameObject bodyContainer;
 45    [SerializeField] internal GameObject friendshipContainer;
 46    [SerializeField] internal GameObject friendAddContainer;
 47    [SerializeField] internal GameObject friendRemoveContainer;
 48    [SerializeField] internal GameObject friendRequestedContainer;
 49
 50    [Header("Texts")]
 51    [SerializeField] internal TextMeshProUGUI userName;
 52    [SerializeField] internal TextMeshProUGUI blockText;
 53
 54    [Header("Buttons")]
 55    [SerializeField] internal Button passportButton;
 56    [SerializeField] internal Button blockButton;
 57    [SerializeField] internal Button reportButton;
 58    [SerializeField] internal Button addFriendButton;
 59    [SerializeField] internal Button cancelFriendButton;
 60    [SerializeField] internal Button deleteFriendButton;
 61    [SerializeField] internal Button messageButton;
 62
 63    public static event System.Action<string> OnOpenPrivateChatRequest;
 64
 2365    public bool isVisible => gameObject.activeSelf;
 066    public string UserId => userId;
 67
 68    public event System.Action OnShowMenu;
 69    public event System.Action<string> OnPassport;
 70    public event System.Action<string> OnReport;
 71    public event System.Action<string, bool> OnBlock;
 72    public event System.Action<string> OnUnfriend;
 73    public event System.Action<string> OnAddFriend;
 74    public event System.Action<string> OnCancelFriend;
 75    public event System.Action<string> OnMessage;
 76    public event System.Action OnHide;
 77
 78    private static StringVariable currentPlayerId = null;
 79    private RectTransform rectTransform;
 80    private string userId;
 81    private bool isBlocked;
 82    private MenuConfigFlags currentConfigFlags;
 83    private IConfirmationDialog currentConfirmationDialog;
 84    internal ISocialAnalytics socialAnalytics;
 85
 86    /// <summary>
 87    /// Show context menu
 88    /// </summary>
 89    /// <param name="userId"> user id</param>
 1090    public void Show(string userId) { Show(userId, menuConfigFlags); }
 91
 92    /// <summary>
 93    /// Show context menu
 94    /// </summary>
 95    /// <param name="userId"> user id</param>
 96    /// <param name="configFlags">set buttons to enable in menu</param>
 97    public void Show(string userId, MenuConfigFlags configFlags)
 98    {
 799        this.userId = userId;
 7100        ProcessActiveElements(configFlags);
 7101        Setup(userId, configFlags);
 7102        if (currentConfirmationDialog == null && confirmationDialog != null)
 103        {
 0104            SetConfirmationDialog(confirmationDialog);
 105        }
 7106        gameObject.SetActive(true);
 7107        OnShowMenu?.Invoke();
 1108    }
 109
 110    /// <summary>
 111    /// Set confirmation popup to reference use
 112    /// </summary>
 113    /// <param name="confirmationPopup">confirmation popup reference</param>
 0114    public void SetConfirmationDialog(IConfirmationDialog confirmationPopup) { this.currentConfirmationDialog = confirma
 115
 116    /// <summary>
 117    /// Hides the context menu.
 118    /// </summary>
 119    public void Hide()
 120    {
 20121        gameObject.SetActive(false);
 20122        OnHide?.Invoke();
 0123    }
 124
 125    /// <summary>
 126    /// Shows/Hides the friendship container
 127    /// </summary>
 2128    public void SetFriendshipContentActive(bool isActive) => friendshipContainer.SetActive(isActive);
 129
 130    private void Awake()
 131    {
 10132        if (!currentPlayerId)
 133        {
 1134            currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID);
 135        }
 136
 10137        rectTransform = GetComponent<RectTransform>();
 138
 10139        passportButton.onClick.AddListener(OnPassportButtonPressed);
 10140        blockButton.onClick.AddListener(OnBlockUserButtonPressed);
 10141        reportButton.onClick.AddListener(OnReportUserButtonPressed);
 10142        deleteFriendButton.onClick.AddListener(OnDeleteUserButtonPressed);
 10143        addFriendButton.onClick.AddListener(OnAddFriendButtonPressed);
 10144        cancelFriendButton.onClick.AddListener(OnCancelFriendRequestButtonPressed);
 10145        messageButton.onClick.AddListener(OnMessageButtonPressed);
 10146    }
 147
 0148    private void Update() { HideIfClickedOutside(); }
 149
 150    private void OnDisable()
 151    {
 10152        if (FriendsController.i)
 153        {
 9154            FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 155        }
 10156    }
 157
 0158    public void ClickReportButton() => reportButton.onClick.Invoke();
 159
 160    private void OnPassportButtonPressed()
 161    {
 1162        OnPassport?.Invoke(userId);
 1163        currentPlayerId.Set(userId);
 1164        Hide();
 165
 1166        AudioScriptableObjects.dialogOpen.Play(true);
 1167    }
 168
 169    private void OnReportUserButtonPressed()
 170    {
 1171        OnReport?.Invoke(userId);
 1172        WebInterface.SendReportPlayer(userId, UserProfileController.userProfilesCatalog.Get(userId)?.userName);
 1173        GetSocialAnalytics().SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.ProfileContextMenu);
 1174        Hide();
 1175    }
 176
 177    private void OnDeleteUserButtonPressed()
 178    {
 0179        OnUnfriend?.Invoke(userId);
 180
 0181        if (currentConfirmationDialog != null)
 182        {
 0183            currentConfirmationDialog.SetText(string.Format(DELETE_MSG_PATTERN, UserProfileController.userProfilesCatalo
 0184            currentConfirmationDialog.Show(() =>
 185            {
 0186                UnfriendUser();
 0187            });
 0188        }
 189        else
 190        {
 0191            UnfriendUser();
 192        }
 193
 0194        GetSocialAnalytics().SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSource.Profil
 0195        Hide();
 0196    }
 197
 198    private void UnfriendUser()
 199    {
 0200        FriendsController.i.RemoveFriend(userId);
 0201    }
 202
 203    private void OnAddFriendButtonPressed()
 204    {
 0205        OnAddFriend?.Invoke(userId);
 206
 0207        if (!FriendsController.i)
 208        {
 0209            return;
 210        }
 211
 212        // NOTE: if we don't add this, the friend request has strange behaviors
 0213        UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel()
 214        {
 215            userId = userId,
 216            name = UserProfileController.userProfilesCatalog.Get(userId)?.userName
 217        });
 218
 0219        FriendsController.i.RequestFriendship(userId);
 220
 0221        GetSocialAnalytics().SendFriendRequestSent(UserProfile.GetOwnUserProfile().userId, userId, 0, PlayerActionSource
 0222    }
 223
 224    private void OnCancelFriendRequestButtonPressed()
 225    {
 0226        OnCancelFriend?.Invoke(userId);
 227
 0228        if (!FriendsController.i)
 229        {
 0230            return;
 231        }
 232
 0233        FriendsController.i.CancelRequest(userId);
 234
 0235        GetSocialAnalytics().SendFriendRequestCancelled(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSour
 0236    }
 237
 238    private void OnMessageButtonPressed()
 239    {
 0240        OnMessage?.Invoke(userId);
 0241        OnOpenPrivateChatRequest?.Invoke(userId);
 0242        Hide();
 0243    }
 244
 245    private void OnBlockUserButtonPressed()
 246    {
 1247        bool blockUser = !isBlocked;
 1248        OnBlock?.Invoke(userId, blockUser);
 1249        if (blockUser)
 250        {
 1251            WebInterface.SendBlockPlayer(userId);
 1252            GetSocialAnalytics().SendPlayerBlocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileConte
 1253        }
 254        else
 255        {
 0256            WebInterface.SendUnblockPlayer(userId);
 0257            GetSocialAnalytics().SendPlayerUnblocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileCon
 258        }
 1259        Hide();
 1260    }
 261
 10262    private void UpdateBlockButton() { blockText.text = isBlocked ? BLOCK_BTN_UNBLOCK_TEXT : BLOCK_BTN_BLOCK_TEXT; }
 263
 264    private void HideIfClickedOutside()
 265    {
 0266        if (!Input.GetMouseButtonDown(0)) return;
 0267        var pointerEventData = new PointerEventData(EventSystem.current)
 268        {
 269            position = Input.mousePosition
 270        };
 0271        var raycastResults = new List<RaycastResult>();
 0272        EventSystem.current.RaycastAll(pointerEventData, raycastResults);
 273
 0274        if (raycastResults.All(result => result.gameObject != gameObject))
 0275            Hide();
 0276    }
 277
 278    private void ProcessActiveElements(MenuConfigFlags flags)
 279    {
 30280        headerContainer.SetActive((flags & headerFlags) != 0);
 30281        userName.gameObject.SetActive((flags & MenuConfigFlags.Name) != 0);
 30282        friendshipContainer.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 30283        deleteFriendButton.gameObject.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 30284        passportButton.gameObject.SetActive((flags & MenuConfigFlags.Passport) != 0);
 30285        blockButton.gameObject.SetActive((flags & MenuConfigFlags.Block) != 0);
 30286        reportButton.gameObject.SetActive((flags & MenuConfigFlags.Report) != 0);
 30287        messageButton.gameObject.SetActive((flags & MenuConfigFlags.Message) != 0);
 30288    }
 289
 290    private void Setup(string userId, MenuConfigFlags configFlags)
 291    {
 7292        this.userId = userId;
 293
 7294        UserProfile profile = UserProfileController.userProfilesCatalog.Get(userId);
 7295        bool userHasWallet = profile?.hasConnectedWeb3 ?? false;
 296
 7297        if (!userHasWallet || !UserProfile.GetOwnUserProfile().hasConnectedWeb3)
 298        {
 1299            configFlags &= ~usesFriendsApiFlags;
 300        }
 301
 7302        currentConfigFlags = configFlags;
 7303        ProcessActiveElements(configFlags);
 304
 7305        if ((configFlags & MenuConfigFlags.Block) != 0)
 306        {
 5307            isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId);
 5308            UpdateBlockButton();
 309        }
 7310        if ((configFlags & MenuConfigFlags.Name) != 0)
 311        {
 5312            string name = profile?.userName;
 5313            userName.text = name;
 314        }
 7315        if ((configFlags & usesFriendsApiFlags) != 0 && FriendsController.i)
 316        {
 6317            if (FriendsController.i.friends.TryGetValue(userId, out UserStatus status))
 318            {
 0319                SetupFriendship(status.friendshipStatus);
 0320            }
 321            else
 322            {
 6323                SetupFriendship(FriendshipStatus.NOT_FRIEND);
 324            }
 6325            FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 6326            FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate;
 327        }
 7328    }
 329
 330    private void SetupFriendship(FriendshipStatus friendshipStatus)
 331    {
 12332        bool friendshipEnabled = (currentConfigFlags & MenuConfigFlags.Friendship) != 0;
 12333        bool messageEnabled = (currentConfigFlags & MenuConfigFlags.Message) != 0;
 334
 12335        if (friendshipStatus == FriendshipStatus.FRIEND)
 336        {
 2337            if (friendshipEnabled)
 338            {
 1339                friendAddContainer.SetActive(false);
 1340                friendRemoveContainer.SetActive(true);
 1341                friendRequestedContainer.SetActive(false);
 1342                deleteFriendButton.gameObject.SetActive(true);
 343            }
 2344            if (messageEnabled)
 345            {
 1346                messageButton.gameObject.SetActive(true);
 347            }
 1348        }
 10349        else if (friendshipStatus == FriendshipStatus.REQUESTED_TO)
 350        {
 2351            if (friendshipEnabled)
 352            {
 1353                friendAddContainer.SetActive(false);
 1354                friendRemoveContainer.SetActive(false);
 1355                friendRequestedContainer.SetActive(true);
 1356                deleteFriendButton.gameObject.SetActive(false);
 357            }
 2358            if (messageEnabled)
 359            {
 1360                messageButton.gameObject.SetActive(false);
 361            }
 1362        }
 8363        else if (friendshipStatus == FriendshipStatus.NOT_FRIEND)
 364        {
 8365            if (friendshipEnabled)
 366            {
 6367                friendAddContainer.SetActive(true);
 6368                friendRemoveContainer.SetActive(false);
 6369                friendRequestedContainer.SetActive(false);
 6370                deleteFriendButton.gameObject.SetActive(false);
 371            }
 8372            if (messageEnabled)
 373            {
 6374                messageButton.gameObject.SetActive(false);
 375            }
 6376        }
 0377        else if (friendshipStatus == FriendshipStatus.REQUESTED_FROM)
 378        {
 0379            if (friendshipEnabled)
 380            {
 0381                friendAddContainer.SetActive(true);
 0382                friendRemoveContainer.SetActive(false);
 0383                friendRequestedContainer.SetActive(false);
 0384                deleteFriendButton.gameObject.SetActive(false);
 385            }
 0386            if (messageEnabled)
 387            {
 0388                messageButton.gameObject.SetActive(false);
 389            }
 390        }
 4391    }
 392
 393    private void OnFriendActionUpdate(string userId, FriendshipAction action)
 394    {
 6395        if (this.userId != userId)
 396        {
 0397            return;
 398        }
 399
 6400        if (action == FriendshipAction.APPROVED)
 401        {
 2402            SetupFriendship(FriendshipStatus.FRIEND);
 2403        }
 4404        else if (action == FriendshipAction.REQUESTED_TO)
 405        {
 2406            SetupFriendship(FriendshipStatus.REQUESTED_TO);
 2407        }
 2408        else if (action == FriendshipAction.DELETED || action == FriendshipAction.CANCELLED || action == FriendshipActio
 409        {
 2410            SetupFriendship(FriendshipStatus.NOT_FRIEND);
 411        }
 2412    }
 413
 414    private ISocialAnalytics GetSocialAnalytics()
 415    {
 2416        if (socialAnalytics == null)
 417        {
 0418            socialAnalytics = new SocialAnalytics(
 419                DCL.Environment.i.platform.serviceProviders.analytics,
 420                new UserProfileWebInterfaceBridge());
 421        }
 422
 2423        return socialAnalytics;
 424    }
 425
 426#if UNITY_EDITOR
 427    //This is just to process buttons and container visibility on editor
 428    private void OnValidate()
 429    {
 16430        if (headerContainer == null)
 0431            return;
 16432        ProcessActiveElements(menuConfigFlags);
 16433    }
 434#endif
 435}