< Summary

Class:UserContextMenu
Assembly:UserContextMenu
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/UserContextMenu/Scripts/UserContextMenu.cs
Covered lines:117
Uncovered lines:57
Coverable lines:174
Total lines:429
Line coverage:67.2% (117 of 174)
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%
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")]
 21040    [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    /// <summary>
 120    /// Shows/Hides the friendship container
 121    /// </summary>
 2122    public void SetFriendshipContentActive(bool isActive) => friendshipContainer.SetActive(isActive);
 123
 124    private void Awake()
 125    {
 10126        if (!currentPlayerId)
 127        {
 1128            currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID);
 129        }
 130
 10131        rectTransform = GetComponent<RectTransform>();
 132
 10133        passportButton.onClick.AddListener(OnPassportButtonPressed);
 10134        blockButton.onClick.AddListener(OnBlockUserButtonPressed);
 10135        reportButton.onClick.AddListener(OnReportUserButtonPressed);
 10136        deleteFriendButton.onClick.AddListener(OnDeleteUserButtonPressed);
 10137        addFriendButton.onClick.AddListener(OnAddFriendButtonPressed);
 10138        cancelFriendButton.onClick.AddListener(OnCancelFriendRequestButtonPressed);
 10139        messageButton.onClick.AddListener(OnMessageButtonPressed);
 10140    }
 141
 0142    private void Update() { HideIfClickedOutside(); }
 143
 144    private void OnDisable()
 145    {
 10146        if (FriendsController.i)
 147        {
 9148            FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 149        }
 10150    }
 151
 0152    public void ClickReportButton() => reportButton.onClick.Invoke();
 153
 154    private void OnPassportButtonPressed()
 155    {
 1156        OnPassport?.Invoke(userId);
 1157        currentPlayerId.Set(userId);
 1158        Hide();
 159
 1160        AudioScriptableObjects.dialogOpen.Play(true);
 1161    }
 162
 163    private void OnReportUserButtonPressed()
 164    {
 1165        OnReport?.Invoke(userId);
 1166        WebInterface.SendReportPlayer(userId, UserProfileController.userProfilesCatalog.Get(userId)?.userName);
 1167        GetSocialAnalytics().SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.ProfileContextMenu);
 1168        Hide();
 1169    }
 170
 171    private void OnDeleteUserButtonPressed()
 172    {
 0173        OnUnfriend?.Invoke(userId);
 174
 0175        if (currentConfirmationDialog != null)
 176        {
 0177            currentConfirmationDialog.SetText(string.Format(DELETE_MSG_PATTERN, UserProfileController.userProfilesCatalo
 0178            currentConfirmationDialog.Show(() =>
 179            {
 0180                UnfriendUser();
 0181            });
 0182        }
 183        else
 184        {
 0185            UnfriendUser();
 186        }
 187
 0188        GetSocialAnalytics().SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSource.Profil
 0189        Hide();
 0190    }
 191
 192    private void UnfriendUser()
 193    {
 0194        FriendsController.i.RemoveFriend(userId);
 0195    }
 196
 197    private void OnAddFriendButtonPressed()
 198    {
 0199        OnAddFriend?.Invoke(userId);
 200
 0201        if (!FriendsController.i)
 202        {
 0203            return;
 204        }
 205
 206        // NOTE: if we don't add this, the friend request has strange behaviors
 0207        UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel()
 208        {
 209            userId = userId,
 210            name = UserProfileController.userProfilesCatalog.Get(userId)?.userName
 211        });
 212
 0213        FriendsController.i.RequestFriendship(userId);
 214
 0215        GetSocialAnalytics().SendFriendRequestSent(UserProfile.GetOwnUserProfile().userId, userId, 0, PlayerActionSource
 0216    }
 217
 218    private void OnCancelFriendRequestButtonPressed()
 219    {
 0220        OnCancelFriend?.Invoke(userId);
 221
 0222        if (!FriendsController.i)
 223        {
 0224            return;
 225        }
 226
 0227        FriendsController.i.CancelRequest(userId);
 228
 0229        GetSocialAnalytics().SendFriendRequestCancelled(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSour
 0230    }
 231
 232    private void OnMessageButtonPressed()
 233    {
 0234        OnMessage?.Invoke(userId);
 0235        OnOpenPrivateChatRequest?.Invoke(userId);
 0236        Hide();
 0237    }
 238
 239    private void OnBlockUserButtonPressed()
 240    {
 1241        bool blockUser = !isBlocked;
 1242        OnBlock?.Invoke(userId, blockUser);
 1243        if (blockUser)
 244        {
 1245            WebInterface.SendBlockPlayer(userId);
 1246            GetSocialAnalytics().SendPlayerBlocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileConte
 1247        }
 248        else
 249        {
 0250            WebInterface.SendUnblockPlayer(userId);
 0251            GetSocialAnalytics().SendPlayerUnblocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileCon
 252        }
 1253        Hide();
 1254    }
 255
 10256    private void UpdateBlockButton() { blockText.text = isBlocked ? BLOCK_BTN_UNBLOCK_TEXT : BLOCK_BTN_BLOCK_TEXT; }
 257
 258    private void HideIfClickedOutside()
 259    {
 0260        if (!Input.GetMouseButtonDown(0)) return;
 0261        var pointerEventData = new PointerEventData(EventSystem.current)
 262        {
 263            position = Input.mousePosition
 264        };
 0265        var raycastResults = new List<RaycastResult>();
 0266        EventSystem.current.RaycastAll(pointerEventData, raycastResults);
 267
 0268        if (raycastResults.All(result => result.gameObject != gameObject))
 0269            Hide();
 0270    }
 271
 272    private void ProcessActiveElements(MenuConfigFlags flags)
 273    {
 27274        headerContainer.SetActive((flags & headerFlags) != 0);
 27275        userName.gameObject.SetActive((flags & MenuConfigFlags.Name) != 0);
 27276        friendshipContainer.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 27277        deleteFriendButton.gameObject.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 27278        passportButton.gameObject.SetActive((flags & MenuConfigFlags.Passport) != 0);
 27279        blockButton.gameObject.SetActive((flags & MenuConfigFlags.Block) != 0);
 27280        reportButton.gameObject.SetActive((flags & MenuConfigFlags.Report) != 0);
 27281        messageButton.gameObject.SetActive((flags & MenuConfigFlags.Message) != 0);
 27282    }
 283
 284    private void Setup(string userId, MenuConfigFlags configFlags)
 285    {
 7286        this.userId = userId;
 287
 7288        UserProfile profile = UserProfileController.userProfilesCatalog.Get(userId);
 7289        bool userHasWallet = profile?.hasConnectedWeb3 ?? false;
 290
 7291        if (!userHasWallet || !UserProfile.GetOwnUserProfile().hasConnectedWeb3)
 292        {
 1293            configFlags &= ~usesFriendsApiFlags;
 294        }
 295
 7296        currentConfigFlags = configFlags;
 7297        ProcessActiveElements(configFlags);
 298
 7299        if ((configFlags & MenuConfigFlags.Block) != 0)
 300        {
 5301            isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId);
 5302            UpdateBlockButton();
 303        }
 7304        if ((configFlags & MenuConfigFlags.Name) != 0)
 305        {
 5306            string name = profile?.userName;
 5307            userName.text = name;
 308        }
 7309        if ((configFlags & usesFriendsApiFlags) != 0 && FriendsController.i)
 310        {
 6311            if (FriendsController.i.friends.TryGetValue(userId, out UserStatus status))
 312            {
 0313                SetupFriendship(status.friendshipStatus);
 0314            }
 315            else
 316            {
 6317                SetupFriendship(FriendshipStatus.NOT_FRIEND);
 318            }
 6319            FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 6320            FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate;
 321        }
 7322    }
 323
 324    private void SetupFriendship(FriendshipStatus friendshipStatus)
 325    {
 12326        bool friendshipEnabled = (currentConfigFlags & MenuConfigFlags.Friendship) != 0;
 12327        bool messageEnabled = (currentConfigFlags & MenuConfigFlags.Message) != 0;
 328
 12329        if (friendshipStatus == FriendshipStatus.FRIEND)
 330        {
 2331            if (friendshipEnabled)
 332            {
 1333                friendAddContainer.SetActive(false);
 1334                friendRemoveContainer.SetActive(true);
 1335                friendRequestedContainer.SetActive(false);
 1336                deleteFriendButton.gameObject.SetActive(true);
 337            }
 2338            if (messageEnabled)
 339            {
 1340                messageButton.gameObject.SetActive(true);
 341            }
 1342        }
 10343        else if (friendshipStatus == FriendshipStatus.REQUESTED_TO)
 344        {
 2345            if (friendshipEnabled)
 346            {
 1347                friendAddContainer.SetActive(false);
 1348                friendRemoveContainer.SetActive(false);
 1349                friendRequestedContainer.SetActive(true);
 1350                deleteFriendButton.gameObject.SetActive(false);
 351            }
 2352            if (messageEnabled)
 353            {
 1354                messageButton.gameObject.SetActive(false);
 355            }
 1356        }
 8357        else if (friendshipStatus == FriendshipStatus.NOT_FRIEND)
 358        {
 8359            if (friendshipEnabled)
 360            {
 6361                friendAddContainer.SetActive(true);
 6362                friendRemoveContainer.SetActive(false);
 6363                friendRequestedContainer.SetActive(false);
 6364                deleteFriendButton.gameObject.SetActive(false);
 365            }
 8366            if (messageEnabled)
 367            {
 6368                messageButton.gameObject.SetActive(false);
 369            }
 6370        }
 0371        else if (friendshipStatus == FriendshipStatus.REQUESTED_FROM)
 372        {
 0373            if (friendshipEnabled)
 374            {
 0375                friendAddContainer.SetActive(true);
 0376                friendRemoveContainer.SetActive(false);
 0377                friendRequestedContainer.SetActive(false);
 0378                deleteFriendButton.gameObject.SetActive(false);
 379            }
 0380            if (messageEnabled)
 381            {
 0382                messageButton.gameObject.SetActive(false);
 383            }
 384        }
 4385    }
 386
 387    private void OnFriendActionUpdate(string userId, FriendshipAction action)
 388    {
 6389        if (this.userId != userId)
 390        {
 0391            return;
 392        }
 393
 6394        if (action == FriendshipAction.APPROVED)
 395        {
 2396            SetupFriendship(FriendshipStatus.FRIEND);
 2397        }
 4398        else if (action == FriendshipAction.REQUESTED_TO)
 399        {
 2400            SetupFriendship(FriendshipStatus.REQUESTED_TO);
 2401        }
 2402        else if (action == FriendshipAction.DELETED || action == FriendshipAction.CANCELLED || action == FriendshipActio
 403        {
 2404            SetupFriendship(FriendshipStatus.NOT_FRIEND);
 405        }
 2406    }
 407
 408    private ISocialAnalytics GetSocialAnalytics()
 409    {
 2410        if (socialAnalytics == null)
 411        {
 0412            socialAnalytics = new SocialAnalytics(
 413                DCL.Environment.i.platform.serviceProviders.analytics,
 414                new UserProfileWebInterfaceBridge());
 415        }
 416
 2417        return socialAnalytics;
 418    }
 419
 420#if UNITY_EDITOR
 421    //This is just to process buttons and container visibility on editor
 422    private void OnValidate()
 423    {
 13424        if (headerContainer == null)
 0425            return;
 13426        ProcessActiveElements(menuConfigFlags);
 13427    }
 428#endif
 429}