< Summary

Class:UserContextMenu
Assembly:UserContextMenu
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/UserContextMenu/Scripts/UserContextMenu.cs
Covered lines:116
Uncovered lines:61
Coverable lines:177
Total lines:449
Line coverage:65.5% (116 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%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")]
 19640    [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
 065    public bool isVisible => gameObject.activeSelf;
 66
 67    public event System.Action OnShowMenu;
 68    public event System.Action<string> OnPassport;
 69    public event System.Action<string> OnReport;
 70    public event System.Action<string, bool> OnBlock;
 71    public event System.Action<string> OnUnfriend;
 72    public event System.Action<string> OnAddFriend;
 73    public event System.Action<string> OnCancelFriend;
 74    public event System.Action<string> OnMessage;
 75
 76    private static StringVariable currentPlayerId = null;
 77    private RectTransform rectTransform;
 78    private string userId;
 79    private bool isBlocked;
 80    private MenuConfigFlags currentConfigFlags;
 81    private IConfirmationDialog currentConfirmationDialog;
 82    internal ISocialAnalytics socialAnalytics;
 83
 84    /// <summary>
 85    /// Show context menu
 86    /// </summary>
 87    /// <param name="userId"> user id</param>
 1088    public void Show(string userId) { Show(userId, menuConfigFlags); }
 89
 90    /// <summary>
 91    /// Show context menu
 92    /// </summary>
 93    /// <param name="userId"> user id</param>
 94    /// <param name="configFlags">set buttons to enable in menu</param>
 95    public void Show(string userId, MenuConfigFlags configFlags)
 96    {
 797        this.userId = userId;
 798        ProcessActiveElements(configFlags);
 799        Setup(userId, configFlags);
 7100        if (currentConfirmationDialog == null && confirmationDialog != null)
 101        {
 0102            SetConfirmationDialog(confirmationDialog);
 103        }
 7104        gameObject.SetActive(true);
 7105        OnShowMenu?.Invoke();
 1106    }
 107
 108    /// <summary>
 109    /// Set confirmation popup to reference use
 110    /// </summary>
 111    /// <param name="confirmationPopup">confirmation popup reference</param>
 0112    public void SetConfirmationDialog(IConfirmationDialog confirmationPopup) { this.currentConfirmationDialog = confirma
 113
 114    /// <summary>
 115    /// Hides the context menu.
 116    /// </summary>
 40117    public void Hide() { gameObject.SetActive(false); }
 118
 119    private void Awake()
 120    {
 8121        if (!currentPlayerId)
 122        {
 1123            currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID);
 124        }
 125
 8126        rectTransform = GetComponent<RectTransform>();
 127
 8128        passportButton.onClick.AddListener(OnPassportButtonPressed);
 8129        blockButton.onClick.AddListener(OnBlockUserButtonPressed);
 8130        reportButton.onClick.AddListener(OnReportUserButtonPressed);
 8131        deleteFriendButton.onClick.AddListener(OnDeleteUserButtonPressed);
 8132        addFriendButton.onClick.AddListener(OnAddFriendButtonPressed);
 8133        cancelFriendButton.onClick.AddListener(OnCancelFriendRequestButtonPressed);
 8134        messageButton.onClick.AddListener(OnMessageButtonPressed);
 8135    }
 136
 0137    private void Update() { HideIfClickedOutside(); }
 138
 139    private void OnDisable()
 140    {
 8141        if (FriendsController.i)
 142        {
 7143            FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 144        }
 8145    }
 146
 0147    public void ClickReportButton() => reportButton.onClick.Invoke();
 148
 149    private void OnPassportButtonPressed()
 150    {
 1151        OnPassport?.Invoke(userId);
 1152        currentPlayerId.Set(userId);
 1153        Hide();
 154
 1155        AudioScriptableObjects.dialogOpen.Play(true);
 1156    }
 157
 158    private void OnReportUserButtonPressed()
 159    {
 1160        OnReport?.Invoke(userId);
 1161        WebInterface.SendReportPlayer(userId, UserProfileController.userProfilesCatalog.Get(userId)?.userName);
 1162        GetSocialAnalytics().SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.ProfileContextMenu);
 1163        Hide();
 1164    }
 165
 166    private void OnDeleteUserButtonPressed()
 167    {
 0168        OnUnfriend?.Invoke(userId);
 169
 0170        if (currentConfirmationDialog != null)
 171        {
 0172            currentConfirmationDialog.SetText(string.Format(DELETE_MSG_PATTERN, UserProfileController.userProfilesCatalo
 0173            currentConfirmationDialog.Show(() =>
 174            {
 0175                UnfriendUser();
 0176            });
 0177        }
 178        else
 179        {
 0180            UnfriendUser();
 181        }
 182
 0183        GetSocialAnalytics().SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSource.Profil
 0184        Hide();
 0185    }
 186
 187    private void UnfriendUser()
 188    {
 0189        FriendsController.FriendshipUpdateStatusMessage newFriendshipStatusMessage = new FriendsController.FriendshipUpd
 190        {
 191            userId = userId,
 192            action = FriendshipAction.DELETED
 193        };
 194
 0195        FriendsController.i.UpdateFriendshipStatus(newFriendshipStatusMessage);
 0196        WebInterface.UpdateFriendshipStatus(newFriendshipStatusMessage);
 0197    }
 198
 199    private void OnAddFriendButtonPressed()
 200    {
 0201        OnAddFriend?.Invoke(userId);
 202
 0203        if (!FriendsController.i)
 204        {
 0205            return;
 206        }
 207
 208        // NOTE: if we don't add this, the friend request has strange behaviors
 0209        UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel()
 210        {
 211            userId = userId,
 212            name = UserProfileController.userProfilesCatalog.Get(userId)?.userName
 213        });
 214
 0215        FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 216        {
 217            userId = userId,
 218            action = FriendshipAction.REQUESTED_TO
 219        });
 220
 0221        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 222        {
 223            userId = userId, action = FriendshipAction.REQUESTED_TO
 224        });
 225
 0226        GetSocialAnalytics().SendFriendRequestSent(UserProfile.GetOwnUserProfile().userId, userId, 0, PlayerActionSource
 0227    }
 228
 229    private void OnCancelFriendRequestButtonPressed()
 230    {
 0231        OnCancelFriend?.Invoke(userId);
 232
 0233        if (!FriendsController.i)
 234        {
 0235            return;
 236        }
 237
 0238        FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 239        {
 240            userId = userId,
 241            action = FriendshipAction.CANCELLED
 242        });
 243
 0244        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 245        {
 246            userId = userId, action = FriendshipAction.CANCELLED
 247        });
 248
 0249        GetSocialAnalytics().SendFriendRequestCancelled(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSour
 0250    }
 251
 252    private void OnMessageButtonPressed()
 253    {
 0254        OnMessage?.Invoke(userId);
 0255        OnOpenPrivateChatRequest?.Invoke(userId);
 0256        Hide();
 0257    }
 258
 259    private void OnBlockUserButtonPressed()
 260    {
 1261        bool blockUser = !isBlocked;
 1262        OnBlock?.Invoke(userId, blockUser);
 1263        if (blockUser)
 264        {
 1265            WebInterface.SendBlockPlayer(userId);
 1266            GetSocialAnalytics().SendPlayerBlocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileConte
 1267        }
 268        else
 269        {
 0270            WebInterface.SendUnblockPlayer(userId);
 0271            GetSocialAnalytics().SendPlayerUnblocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileCon
 272        }
 1273        Hide();
 1274    }
 275
 10276    private void UpdateBlockButton() { blockText.text = isBlocked ? BLOCK_BTN_UNBLOCK_TEXT : BLOCK_BTN_BLOCK_TEXT; }
 277
 278    private void HideIfClickedOutside()
 279    {
 0280        if (!Input.GetMouseButtonDown(0)) return;
 0281        var pointerEventData = new PointerEventData(EventSystem.current)
 282        {
 283            position = Input.mousePosition
 284        };
 0285        var raycastResults = new List<RaycastResult>();
 0286        EventSystem.current.RaycastAll(pointerEventData, raycastResults);
 287
 0288        if (raycastResults.All(result => result.gameObject != gameObject))
 0289            Hide();
 0290    }
 291
 292    private void ProcessActiveElements(MenuConfigFlags flags)
 293    {
 27294        headerContainer.SetActive((flags & headerFlags) != 0);
 27295        userName.gameObject.SetActive((flags & MenuConfigFlags.Name) != 0);
 27296        friendshipContainer.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 27297        deleteFriendButton.gameObject.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 27298        passportButton.gameObject.SetActive((flags & MenuConfigFlags.Passport) != 0);
 27299        blockButton.gameObject.SetActive((flags & MenuConfigFlags.Block) != 0);
 27300        reportButton.gameObject.SetActive((flags & MenuConfigFlags.Report) != 0);
 27301        messageButton.gameObject.SetActive((flags & MenuConfigFlags.Message) != 0);
 27302    }
 303
 304    private void Setup(string userId, MenuConfigFlags configFlags)
 305    {
 7306        this.userId = userId;
 307
 7308        UserProfile profile = UserProfileController.userProfilesCatalog.Get(userId);
 7309        bool userHasWallet = profile?.hasConnectedWeb3 ?? false;
 310
 7311        if (!userHasWallet || !UserProfile.GetOwnUserProfile().hasConnectedWeb3)
 312        {
 1313            configFlags &= ~usesFriendsApiFlags;
 314        }
 315
 7316        currentConfigFlags = configFlags;
 7317        ProcessActiveElements(configFlags);
 318
 7319        if ((configFlags & MenuConfigFlags.Block) != 0)
 320        {
 5321            isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId);
 5322            UpdateBlockButton();
 323        }
 7324        if ((configFlags & MenuConfigFlags.Name) != 0)
 325        {
 5326            string name = profile?.userName;
 5327            userName.text = name;
 328        }
 7329        if ((configFlags & usesFriendsApiFlags) != 0 && FriendsController.i)
 330        {
 6331            if (FriendsController.i.friends.TryGetValue(userId, out FriendsController.UserStatus status))
 332            {
 0333                SetupFriendship(status.friendshipStatus);
 0334            }
 335            else
 336            {
 6337                SetupFriendship(FriendshipStatus.NOT_FRIEND);
 338            }
 6339            FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 6340            FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate;
 341        }
 7342    }
 343
 344    private void SetupFriendship(FriendshipStatus friendshipStatus)
 345    {
 12346        bool friendshipEnabled = (currentConfigFlags & MenuConfigFlags.Friendship) != 0;
 12347        bool messageEnabled = (currentConfigFlags & MenuConfigFlags.Message) != 0;
 348
 12349        if (friendshipStatus == FriendshipStatus.FRIEND)
 350        {
 2351            if (friendshipEnabled)
 352            {
 1353                friendAddContainer.SetActive(false);
 1354                friendRemoveContainer.SetActive(true);
 1355                friendRequestedContainer.SetActive(false);
 1356                deleteFriendButton.gameObject.SetActive(true);
 357            }
 2358            if (messageEnabled)
 359            {
 1360                messageButton.gameObject.SetActive(true);
 361            }
 1362        }
 10363        else if (friendshipStatus == FriendshipStatus.REQUESTED_TO)
 364        {
 2365            if (friendshipEnabled)
 366            {
 1367                friendAddContainer.SetActive(false);
 1368                friendRemoveContainer.SetActive(false);
 1369                friendRequestedContainer.SetActive(true);
 1370                deleteFriendButton.gameObject.SetActive(false);
 371            }
 2372            if (messageEnabled)
 373            {
 1374                messageButton.gameObject.SetActive(false);
 375            }
 1376        }
 8377        else if (friendshipStatus == FriendshipStatus.NOT_FRIEND)
 378        {
 8379            if (friendshipEnabled)
 380            {
 6381                friendAddContainer.SetActive(true);
 6382                friendRemoveContainer.SetActive(false);
 6383                friendRequestedContainer.SetActive(false);
 6384                deleteFriendButton.gameObject.SetActive(false);
 385            }
 8386            if (messageEnabled)
 387            {
 6388                messageButton.gameObject.SetActive(false);
 389            }
 6390        }
 0391        else if (friendshipStatus == FriendshipStatus.REQUESTED_FROM)
 392        {
 0393            if (friendshipEnabled)
 394            {
 0395                friendAddContainer.SetActive(true);
 0396                friendRemoveContainer.SetActive(false);
 0397                friendRequestedContainer.SetActive(false);
 0398                deleteFriendButton.gameObject.SetActive(false);
 399            }
 0400            if (messageEnabled)
 401            {
 0402                messageButton.gameObject.SetActive(false);
 403            }
 404        }
 4405    }
 406
 407    private void OnFriendActionUpdate(string userId, FriendshipAction action)
 408    {
 6409        if (this.userId != userId)
 410        {
 0411            return;
 412        }
 413
 6414        if (action == FriendshipAction.APPROVED)
 415        {
 2416            SetupFriendship(FriendshipStatus.FRIEND);
 2417        }
 4418        else if (action == FriendshipAction.REQUESTED_TO)
 419        {
 2420            SetupFriendship(FriendshipStatus.REQUESTED_TO);
 2421        }
 2422        else if (action == FriendshipAction.DELETED || action == FriendshipAction.CANCELLED || action == FriendshipActio
 423        {
 2424            SetupFriendship(FriendshipStatus.NOT_FRIEND);
 425        }
 2426    }
 427
 428    private ISocialAnalytics GetSocialAnalytics()
 429    {
 2430        if (socialAnalytics == null)
 431        {
 0432            socialAnalytics = new SocialAnalytics(
 433                DCL.Environment.i.platform.serviceProviders.analytics,
 434                new UserProfileWebInterfaceBridge());
 435        }
 436
 2437        return socialAnalytics;
 438    }
 439
 440#if UNITY_EDITOR
 441    //This is just to process buttons and container visibility on editor
 442    private void OnValidate()
 443    {
 13444        if (headerContainer == null)
 0445            return;
 13446        ProcessActiveElements(menuConfigFlags);
 13447    }
 448#endif
 449}