< Summary

Class:DCL.Chat.HUD.PublicChatWindowController
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/PublicChatWindowController.cs
Covered lines:98
Uncovered lines:11
Coverable lines:109
Total lines:247
Line coverage:89.9% (98 of 109)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PublicChatWindowController(...)0%110100%
Initialize(...)0%330100%
Setup(...)0%3.033085.71%
Dispose()0%440100%
SetVisibility(...)0%4.014091.67%
SendChatMessage(...)0%4.254075%
SetVisibility(...)0%110100%
SetVisiblePanelList(...)0%220100%
MarkChannelMessagesAsRead()0%110100%
HandleViewClosed()0%220100%
HandleViewBacked()0%6200%
HandleMessageReceived(...)0%660100%
Hide()0%110100%
HandleChatInputTriggered(...)0%6200%
HandleMessageBlockedBySpam(...)0%2100%
MuteChannel(...)0%220100%
ToPublicChatModel(...)0%110100%
HandleChannelUpdated(...)0%2.062075%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/PublicChatWindowController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using DCL.Interface;
 4using DCL.ProfanityFiltering;
 5using Channel = DCL.Chat.Channels.Channel;
 6
 7namespace DCL.Chat.HUD
 8{
 9    public class PublicChatWindowController : IHUD
 10    {
 10811        public IPublicChatWindowView View { get; private set; }
 12
 13        public event Action OnBack;
 14        public event Action OnClosed;
 15
 16        private readonly IChatController chatController;
 17        private readonly IUserProfileBridge userProfileBridge;
 18        private readonly DataStore dataStore;
 19        private readonly IProfanityFilter profanityFilter;
 20        private readonly IMouseCatcher mouseCatcher;
 21        private readonly InputAction_Trigger toggleChatTrigger;
 22        private ChatHUDController chatHudController;
 23        private string channelId;
 24        private bool skipChatInputTrigger;
 725        private bool showOnlyOnlineMembersOnPublicChannels => !dataStore.featureFlags.flags.Get().IsFeatureEnabled("matr
 26
 27        private bool isVisible;
 28
 1829        private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 30
 1431        public PublicChatWindowController(IChatController chatController,
 32            IUserProfileBridge userProfileBridge,
 33            DataStore dataStore,
 34            IProfanityFilter profanityFilter,
 35            IMouseCatcher mouseCatcher,
 36            InputAction_Trigger toggleChatTrigger)
 37        {
 1438            this.chatController = chatController;
 1439            this.userProfileBridge = userProfileBridge;
 1440            this.dataStore = dataStore;
 1441            this.profanityFilter = profanityFilter;
 1442            this.mouseCatcher = mouseCatcher;
 1443            this.toggleChatTrigger = toggleChatTrigger;
 1444        }
 45
 46        public void Initialize(IPublicChatWindowView view = null, bool isVisible = true)
 47        {
 1448            view ??= PublicChatWindowComponentView.Create();
 1449            View = view;
 1450            view.OnClose += HandleViewClosed;
 1451            view.OnBack += HandleViewBacked;
 1452            view.OnMuteChanged += MuteChannel;
 53
 1454            if (mouseCatcher != null)
 1355                mouseCatcher.OnMouseLock += Hide;
 56
 1457            chatHudController = new ChatHUDController(dataStore,
 58                userProfileBridge,
 59                true,
 60                profanityFilter);
 1461            chatHudController.Initialize(view.ChatHUD);
 1462            chatHudController.OnSendMessage += SendChatMessage;
 1463            chatHudController.OnMessageSentBlockedBySpam += HandleMessageBlockedBySpam;
 64
 1465            chatController.OnAddMessage -= HandleMessageReceived;
 1466            chatController.OnAddMessage += HandleMessageReceived;
 1467            chatController.OnChannelUpdated -= HandleChannelUpdated;
 1468            chatController.OnChannelUpdated += HandleChannelUpdated;
 69
 1470            toggleChatTrigger.OnTriggered += HandleChatInputTriggered;
 71
 1472            SetVisibility(isVisible);
 1473            this.isVisible = isVisible;
 1474        }
 75
 76        public void Setup(string channelId)
 77        {
 678            if (string.IsNullOrEmpty(channelId) || channelId == this.channelId) return;
 679            this.channelId = channelId;
 80
 681            var channel = chatController.GetAllocatedChannel(channelId);
 682            View.Configure(ToPublicChatModel(channel));
 83
 684            chatHudController.ClearAllEntries();
 685        }
 86
 87        public void Dispose()
 88        {
 1389            View.OnClose -= HandleViewClosed;
 1390            View.OnBack -= HandleViewBacked;
 1391            View.OnMuteChanged -= MuteChannel;
 92
 1393            if (chatController != null)
 94            {
 1395                chatController.OnAddMessage -= HandleMessageReceived;
 1396                chatController.OnChannelUpdated -= HandleChannelUpdated;
 97            }
 98
 1399            chatHudController.OnSendMessage -= SendChatMessage;
 13100            chatHudController.OnMessageSentBlockedBySpam -= HandleMessageBlockedBySpam;
 101
 13102            if (mouseCatcher != null)
 12103                mouseCatcher.OnMouseLock -= Hide;
 104
 13105            toggleChatTrigger.OnTriggered -= HandleChatInputTriggered;
 106
 13107            if (View != null)
 108            {
 13109                View.Dispose();
 110            }
 13111        }
 112
 113        public void SetVisibility(bool visible, bool focusInputField)
 114        {
 23115            if(isVisible == visible)
 14116                return;
 117
 9118            isVisible = visible;
 119
 9120            SetVisiblePanelList(visible);
 9121            if (visible)
 122            {
 6123                View.Show();
 6124                MarkChannelMessagesAsRead();
 125
 6126                if (focusInputField)
 0127                    chatHudController.FocusInputField();
 128            }
 129            else
 130            {
 3131                chatHudController.UnfocusInputField();
 3132                View.Hide();
 133            }
 9134        }
 135
 136        private void SendChatMessage(ChatMessage message)
 137        {
 2138            var isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body);
 2139            var isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE;
 140
 2141            if (isValidMessage)
 142            {
 2143                chatHudController.ResetInputField();
 2144                chatHudController.FocusInputField();
 145            }
 146            else
 147            {
 0148                HandleViewClosed();
 0149                SetVisibility(false);
 0150                return;
 151            }
 152
 2153            if (isPrivateMessage)
 1154                message.body = $"/w {message.recipient} {message.body}";
 155
 2156            chatController.Send(message);
 2157        }
 158
 23159        public void SetVisibility(bool visible) => SetVisibility(visible, false);
 160
 161        private void SetVisiblePanelList(bool visible)
 162        {
 9163            HashSet<string> newSet = visibleTaskbarPanels.Get();
 9164            if (visible)
 6165                newSet.Add("PublicChatChannel");
 166            else
 3167                newSet.Remove("PublicChatChannel");
 168
 9169            visibleTaskbarPanels.Set(newSet, true);
 9170        }
 171
 8172        private void MarkChannelMessagesAsRead() => chatController.MarkChannelMessagesAsSeen(channelId);
 173
 174        private void HandleViewClosed()
 175        {
 1176            OnClosed?.Invoke();
 1177        }
 178
 179        private void HandleViewBacked()
 180        {
 0181            OnBack?.Invoke();
 0182        }
 183
 184        private void HandleMessageReceived(ChatMessage[] messages)
 185        {
 4186            var messageLogUpdated = false;
 187
 18188            foreach (var message in messages)
 189            {
 5190                if (message.messageType != ChatMessage.Type.PUBLIC
 191                    && message.messageType != ChatMessage.Type.SYSTEM) continue;
 4192                if (!string.IsNullOrEmpty(message.recipient)) continue;
 193
 4194                chatHudController.AddChatMessage(message, View.IsActive);
 4195                messageLogUpdated = true;
 196            }
 197
 4198            if (View.IsActive && messageLogUpdated)
 2199                MarkChannelMessagesAsRead();
 4200        }
 201
 1202        private void Hide() => SetVisibility(false);
 203
 204        private void HandleChatInputTriggered(DCLAction_Trigger action)
 205        {
 0206            if (!View.IsActive) return;
 0207            chatHudController.FocusInputField();
 0208        }
 209
 210        private void HandleMessageBlockedBySpam(ChatMessage message)
 211        {
 0212            chatHudController.AddChatMessage(new ChatEntryModel
 213            {
 214                timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
 215                bodyText = "You sent too many messages in a short period of time. Please wait and try again later.",
 216                messageId = Guid.NewGuid().ToString(),
 217                messageType = ChatMessage.Type.SYSTEM,
 218                subType = ChatEntryModel.SubType.RECEIVED
 219            });
 0220        }
 221
 222        private void MuteChannel(bool muted)
 223        {
 2224            if (muted)
 1225                chatController.MuteChannel(channelId);
 226            else
 1227                chatController.UnmuteChannel(channelId);
 1228        }
 229
 230        private PublicChatModel ToPublicChatModel(Channel channel)
 231        {
 7232            return new PublicChatModel(channel.ChannelId,
 233                channel.Name,
 234                channel.Description,
 235                channel.Joined,
 236                channel.MemberCount,
 237                channel.Muted,
 238                showOnlyOnlineMembersOnPublicChannels);
 239        }
 240
 241        private void HandleChannelUpdated(Channel updatedChannel)
 242        {
 1243            if (updatedChannel.ChannelId != channelId) return;
 1244            View.Configure(ToPublicChatModel(updatedChannel));
 1245        }
 246    }
 247}