< 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:89
Uncovered lines:14
Coverable lines:103
Total lines:228
Line coverage:86.4% (89 of 103)
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%3.073080%
SendChatMessage(...)0%4.24076.92%
SetVisibility(...)0%110100%
SetVisiblePanelList(...)0%220100%
MarkChannelMessagesAsRead()0%110100%
HandleViewClosed()0%220100%
HandleViewBacked()0%6200%
HandleMessageReceived(...)0%5.395075%
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 Channel = DCL.Chat.Channels.Channel;
 5
 6namespace DCL.Chat.HUD
 7{
 8    public class PublicChatWindowController : IHUD
 9    {
 010        public IPublicChatWindowView View { get; private set; }
 11
 12        public event Action OnBack;
 13        public event Action OnClosed;
 14
 15        private readonly IChatController chatController;
 16        private readonly IUserProfileBridge userProfileBridge;
 17        private readonly DataStore dataStore;
 18        private readonly IProfanityFilter profanityFilter;
 19        private readonly IMouseCatcher mouseCatcher;
 20        private readonly InputAction_Trigger toggleChatTrigger;
 21        private ChatHUDController chatHudController;
 22        private string channelId;
 23        private bool skipChatInputTrigger;
 24
 1825        private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 26
 1327        public PublicChatWindowController(IChatController chatController,
 28            IUserProfileBridge userProfileBridge,
 29            DataStore dataStore,
 30            IProfanityFilter profanityFilter,
 31            IMouseCatcher mouseCatcher,
 32            InputAction_Trigger toggleChatTrigger)
 33        {
 1334            this.chatController = chatController;
 1335            this.userProfileBridge = userProfileBridge;
 1336            this.dataStore = dataStore;
 1337            this.profanityFilter = profanityFilter;
 1338            this.mouseCatcher = mouseCatcher;
 1339            this.toggleChatTrigger = toggleChatTrigger;
 1340        }
 41
 42        public void Initialize(IPublicChatWindowView view = null)
 43        {
 1344            view ??= PublicChatWindowComponentView.Create();
 1345            View = view;
 1346            view.OnClose += HandleViewClosed;
 1347            view.OnBack += HandleViewBacked;
 1348            view.OnMuteChanged += MuteChannel;
 49
 1350            if (mouseCatcher != null)
 1351                mouseCatcher.OnMouseLock += Hide;
 52
 1353            chatHudController = new ChatHUDController(dataStore,
 54                userProfileBridge,
 55                true,
 56                profanityFilter);
 1357            chatHudController.Initialize(view.ChatHUD);
 1358            chatHudController.OnSendMessage += SendChatMessage;
 1359            chatHudController.OnMessageSentBlockedBySpam += HandleMessageBlockedBySpam;
 60
 1361            chatController.OnAddMessage -= HandleMessageReceived;
 1362            chatController.OnAddMessage += HandleMessageReceived;
 1363            chatController.OnChannelUpdated -= HandleChannelUpdated;
 1364            chatController.OnChannelUpdated += HandleChannelUpdated;
 65
 1366            toggleChatTrigger.OnTriggered += HandleChatInputTriggered;
 1367        }
 68
 69        public void Setup(string channelId)
 70        {
 471            if (string.IsNullOrEmpty(channelId) || channelId == this.channelId) return;
 472            this.channelId = channelId;
 73
 474            var channel = chatController.GetAllocatedChannel(channelId);
 475            View.Configure(ToPublicChatModel(channel));
 76
 477            chatHudController.ClearAllEntries();
 478        }
 79
 80        public void Dispose()
 81        {
 1282            View.OnClose -= HandleViewClosed;
 1283            View.OnBack -= HandleViewBacked;
 1284            View.OnMuteChanged -= MuteChannel;
 85
 1286            if (chatController != null)
 87            {
 1288                chatController.OnAddMessage -= HandleMessageReceived;
 1289                chatController.OnChannelUpdated -= HandleChannelUpdated;
 90            }
 91
 1292            chatHudController.OnSendMessage -= SendChatMessage;
 1293            chatHudController.OnMessageSentBlockedBySpam -= HandleMessageBlockedBySpam;
 94
 1295            if (mouseCatcher != null)
 1296                mouseCatcher.OnMouseLock -= Hide;
 97
 1298            toggleChatTrigger.OnTriggered -= HandleChatInputTriggered;
 99
 12100            if (View != null)
 101            {
 12102                View.Dispose();
 103            }
 12104        }
 105
 106        public void SetVisibility(bool visible, bool focusInputField)
 107        {
 9108            SetVisiblePanelList(visible);
 9109            if (visible)
 110            {
 4111                View.Show();
 4112                MarkChannelMessagesAsRead();
 113
 4114                if (focusInputField)
 0115                    chatHudController.FocusInputField();
 0116            }
 117            else
 118            {
 5119                chatHudController.UnfocusInputField();
 5120                View.Hide();
 121            }
 9122        }
 123
 124        private void SendChatMessage(ChatMessage message)
 125        {
 2126            var isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body);
 2127            var isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE;
 128
 2129            if (isValidMessage)
 130            {
 2131                chatHudController.ResetInputField();
 2132                chatHudController.FocusInputField();
 2133            }
 134            else
 135            {
 0136                HandleViewClosed();
 0137                SetVisibility(false);
 0138                return;
 139            }
 140
 2141            if (isPrivateMessage)
 1142                message.body = $"/w {message.recipient} {message.body}";
 143
 2144            chatController.Send(message);
 2145        }
 146
 9147        public void SetVisibility(bool visible) => SetVisibility(visible, false);
 148
 149        private void SetVisiblePanelList(bool visible)
 150        {
 9151            HashSet<string> newSet = visibleTaskbarPanels.Get();
 9152            if (visible)
 4153                newSet.Add("PublicChatChannel");
 154            else
 5155                newSet.Remove("PublicChatChannel");
 156
 9157            visibleTaskbarPanels.Set(newSet, true);
 9158        }
 159
 5160        internal void MarkChannelMessagesAsRead() => chatController.MarkChannelMessagesAsSeen(channelId);
 161
 162        private void HandleViewClosed()
 163        {
 1164            OnClosed?.Invoke();
 1165        }
 166
 167        private void HandleViewBacked()
 168        {
 0169            OnBack?.Invoke();
 0170        }
 171
 172        private void HandleMessageReceived(ChatMessage message)
 173        {
 2174            if (message.messageType != ChatMessage.Type.PUBLIC
 1175                && message.messageType != ChatMessage.Type.SYSTEM) return;
 1176            if (!string.IsNullOrEmpty(message.recipient)) return;
 177
 1178            chatHudController.AddChatMessage(message, View.IsActive);
 179
 1180            if (View.IsActive)
 0181                MarkChannelMessagesAsRead();
 1182        }
 183
 1184        private void Hide() => SetVisibility(false);
 185
 186        private void HandleChatInputTriggered(DCLAction_Trigger action)
 187        {
 0188            if (!View.IsActive) return;
 0189            chatHudController.FocusInputField();
 0190        }
 191
 192        private void HandleMessageBlockedBySpam(ChatMessage message)
 193        {
 0194            chatHudController.AddChatMessage(new ChatEntryModel
 195            {
 196                timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
 197                bodyText = "You sent too many messages in a short period of time. Please wait and try again later.",
 198                messageId = Guid.NewGuid().ToString(),
 199                messageType = ChatMessage.Type.SYSTEM,
 200                subType = ChatEntryModel.SubType.RECEIVED
 201            });
 0202        }
 203
 204        private void MuteChannel(bool muted)
 205        {
 2206            if (muted)
 1207                chatController.MuteChannel(channelId);
 208            else
 1209                chatController.UnmuteChannel(channelId);
 1210        }
 211
 212        private PublicChatModel ToPublicChatModel(Channel channel)
 213        {
 5214            return new PublicChatModel(channel.ChannelId,
 215                channel.Name,
 216                channel.Description,
 217                channel.Joined,
 218                channel.MemberCount,
 219                channel.Muted);
 220        }
 221
 222        private void HandleChannelUpdated(Channel updatedChannel)
 223        {
 1224            if (updatedChannel.ChannelId != channelId) return;
 1225            View.Configure(ToPublicChatModel(updatedChannel));
 1226        }
 227    }
 228}