< Summary

Class:ChatHUDController
Assembly:ChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ChatWidgetHUD/ChatHUDController.cs
Covered lines:57
Uncovered lines:6
Coverable lines:63
Total lines:139
Line coverage:90.4% (57 of 63)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChatHUDController()0%110100%
ChatHUDController(...)0%110100%
Initialize(...)0%330100%
View_OnPressPrivateMessage(...)0%6200%
ContextMenu_OnShowMenu()0%2100%
OnCloseButtonPressed(...)0%6200%
AddChatMessage(...)0%660100%
Dispose()0%220100%
ChatMessageToChatEntry(...)0%10100100%
IsProfanityFilteringEnabled()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ChatWidgetHUD/ChatHUDController.cs

#LineLine coverage
 1using System;
 2using DCL;
 3using DCL.Interface;
 4using UnityEngine;
 5using UnityEngine.Events;
 6using Object = UnityEngine.Object;
 7
 8public class ChatHUDController : IDisposable
 9{
 110    public static int MAX_CHAT_ENTRIES { internal set; get; } = 30;
 11
 12    public ChatHUDView view;
 13
 14    public event UnityAction<string> OnPressPrivateMessage;
 15
 16    private readonly DataStore dataStore;
 17    private readonly RegexProfanityFilter profanityFilter;
 18    private InputAction_Trigger closeWindowTrigger;
 19
 3220    public ChatHUDController(DataStore dataStore, RegexProfanityFilter profanityFilter = null)
 21    {
 3222        this.dataStore = dataStore;
 3223        this.profanityFilter = profanityFilter;
 3224    }
 25
 26    public void Initialize(ChatHUDView view = null, UnityAction<ChatMessage> onSendMessage = null)
 27    {
 3228        this.view = view ?? ChatHUDView.Create();
 29
 3230        this.view.Initialize(this, onSendMessage);
 31
 3232        this.view.OnPressPrivateMessage -= View_OnPressPrivateMessage;
 3233        this.view.OnPressPrivateMessage += View_OnPressPrivateMessage;
 34
 3235        if (this.view.contextMenu != null)
 36        {
 2437            this.view.contextMenu.OnShowMenu -= ContextMenu_OnShowMenu;
 2438            this.view.contextMenu.OnShowMenu += ContextMenu_OnShowMenu;
 39        }
 40
 3241        closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 3242        closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 3243        closeWindowTrigger.OnTriggered += OnCloseButtonPressed;
 3244    }
 45
 046    void View_OnPressPrivateMessage(string friendUserId) { OnPressPrivateMessage?.Invoke(friendUserId); }
 47
 048    private void ContextMenu_OnShowMenu() { view.OnMessageCancelHover(); }
 49
 50    private void OnCloseButtonPressed(DCLAction_Trigger action)
 51    {
 052        if (view.contextMenu != null)
 53        {
 054            view.contextMenu.Hide();
 055            view.confirmationDialog.Hide();
 56        }
 057    }
 58
 59    public void AddChatMessage(ChatEntry.Model chatEntryModel, bool setScrollPositionToBottom = false)
 60    {
 3861        if (IsProfanityFilteringEnabled() && chatEntryModel.messageType != ChatMessage.Type.PRIVATE)
 62        {
 2463            chatEntryModel.bodyText = profanityFilter.Filter(chatEntryModel.bodyText);
 2464            if (!string.IsNullOrEmpty(chatEntryModel.senderName))
 2465                chatEntryModel.senderName = profanityFilter.Filter(chatEntryModel.senderName);
 2466            if (!string.IsNullOrEmpty(chatEntryModel.recipientName))
 267                chatEntryModel.recipientName = profanityFilter.Filter(chatEntryModel.recipientName);
 68        }
 69
 3870        view.AddEntry(chatEntryModel, setScrollPositionToBottom);
 71
 3872        if (view.entries.Count > MAX_CHAT_ENTRIES)
 73        {
 574            Object.Destroy(view.entries[0].gameObject);
 575            view.entries.Remove(view.entries[0]);
 76        }
 3877    }
 78
 79    public void Dispose()
 80    {
 1481        view.OnPressPrivateMessage -= View_OnPressPrivateMessage;
 1482        if (view.contextMenu != null)
 83        {
 1484            view.contextMenu.OnShowMenu -= ContextMenu_OnShowMenu;
 85        }
 1486        closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 1487        Object.Destroy(view.gameObject);
 1488    }
 89
 90    public static ChatEntry.Model ChatMessageToChatEntry(ChatMessage message)
 91    {
 1392        ChatEntry.Model model = new ChatEntry.Model();
 93
 1394        var ownProfile = UserProfile.GetOwnUserProfile();
 95
 1396        model.messageType = message.messageType;
 1397        model.bodyText = message.body;
 1398        model.timestamp = message.timestamp;
 99
 13100        if (message.recipient != null)
 101        {
 5102            var recipientProfile = UserProfileController.userProfilesCatalog.Get(message.recipient);
 5103            model.recipientName = recipientProfile != null ? recipientProfile.userName : message.recipient;
 104        }
 105
 13106        if (message.sender != null)
 107        {
 13108            var senderProfile = UserProfileController.userProfilesCatalog.Get(message.sender);
 13109            model.senderName = senderProfile != null ? senderProfile.userName : message.sender;
 13110            model.senderId = message.sender;
 111        }
 112
 13113        if (model.messageType == ChatMessage.Type.PRIVATE)
 114        {
 11115            if (message.recipient == ownProfile.userId)
 116            {
 4117                model.subType = ChatEntry.Model.SubType.PRIVATE_FROM;
 4118                model.otherUserId = message.sender;
 4119            }
 7120            else if (message.sender == ownProfile.userId)
 121            {
 1122                model.subType = ChatEntry.Model.SubType.PRIVATE_TO;
 1123                model.otherUserId = message.recipient;
 1124            }
 125            else
 126            {
 6127                model.subType = ChatEntry.Model.SubType.NONE;
 128            }
 129        }
 130
 13131        return model;
 132    }
 133
 134    private bool IsProfanityFilteringEnabled()
 135    {
 38136        return dataStore.settings.profanityChatFilteringEnabled.Get()
 137            && profanityFilter != null;
 138    }
 139}