< 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:93
Uncovered lines:11
Coverable lines:104
Total lines:236
Line coverage:89.4% (93 of 104)
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.013088.89%
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 Channel = DCL.Chat.Channels.Channel;
 5
 6namespace DCL.Chat.HUD
 7{
 8    public class PublicChatWindowController : IHUD
 9    {
 10810        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;
 724        private bool showOnlyOnlineMembersOnPublicChannels => !dataStore.featureFlags.flags.Get().IsFeatureEnabled("matr
 25
 1826        private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 27
 1428        public PublicChatWindowController(IChatController chatController,
 29            IUserProfileBridge userProfileBridge,
 30            DataStore dataStore,
 31            IProfanityFilter profanityFilter,
 32            IMouseCatcher mouseCatcher,
 33            InputAction_Trigger toggleChatTrigger)
 34        {
 1435            this.chatController = chatController;
 1436            this.userProfileBridge = userProfileBridge;
 1437            this.dataStore = dataStore;
 1438            this.profanityFilter = profanityFilter;
 1439            this.mouseCatcher = mouseCatcher;
 1440            this.toggleChatTrigger = toggleChatTrigger;
 1441        }
 42
 43        public void Initialize(IPublicChatWindowView view = null)
 44        {
 1445            view ??= PublicChatWindowComponentView.Create();
 1446            View = view;
 1447            view.OnClose += HandleViewClosed;
 1448            view.OnBack += HandleViewBacked;
 1449            view.OnMuteChanged += MuteChannel;
 50
 1451            if (mouseCatcher != null)
 1352                mouseCatcher.OnMouseLock += Hide;
 53
 1454            chatHudController = new ChatHUDController(dataStore,
 55                userProfileBridge,
 56                true,
 57                profanityFilter);
 1458            chatHudController.Initialize(view.ChatHUD);
 1459            chatHudController.OnSendMessage += SendChatMessage;
 1460            chatHudController.OnMessageSentBlockedBySpam += HandleMessageBlockedBySpam;
 61
 1462            chatController.OnAddMessage -= HandleMessageReceived;
 1463            chatController.OnAddMessage += HandleMessageReceived;
 1464            chatController.OnChannelUpdated -= HandleChannelUpdated;
 1465            chatController.OnChannelUpdated += HandleChannelUpdated;
 66
 1467            toggleChatTrigger.OnTriggered += HandleChatInputTriggered;
 1468        }
 69
 70        public void Setup(string channelId)
 71        {
 672            if (string.IsNullOrEmpty(channelId) || channelId == this.channelId) return;
 673            this.channelId = channelId;
 74
 675            var channel = chatController.GetAllocatedChannel(channelId);
 676            View.Configure(ToPublicChatModel(channel));
 77
 678            chatHudController.ClearAllEntries();
 679        }
 80
 81        public void Dispose()
 82        {
 1383            View.OnClose -= HandleViewClosed;
 1384            View.OnBack -= HandleViewBacked;
 1385            View.OnMuteChanged -= MuteChannel;
 86
 1387            if (chatController != null)
 88            {
 1389                chatController.OnAddMessage -= HandleMessageReceived;
 1390                chatController.OnChannelUpdated -= HandleChannelUpdated;
 91            }
 92
 1393            chatHudController.OnSendMessage -= SendChatMessage;
 1394            chatHudController.OnMessageSentBlockedBySpam -= HandleMessageBlockedBySpam;
 95
 1396            if (mouseCatcher != null)
 1297                mouseCatcher.OnMouseLock -= Hide;
 98
 1399            toggleChatTrigger.OnTriggered -= HandleChatInputTriggered;
 100
 13101            if (View != null)
 102            {
 13103                View.Dispose();
 104            }
 13105        }
 106
 107        public void SetVisibility(bool visible, bool focusInputField)
 108        {
 9109            SetVisiblePanelList(visible);
 9110            if (visible)
 111            {
 4112                View.Show();
 4113                MarkChannelMessagesAsRead();
 114
 4115                if (focusInputField)
 0116                    chatHudController.FocusInputField();
 117            }
 118            else
 119            {
 5120                chatHudController.UnfocusInputField();
 5121                View.Hide();
 122            }
 9123        }
 124
 125        private void SendChatMessage(ChatMessage message)
 126        {
 2127            var isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body);
 2128            var isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE;
 129
 2130            if (isValidMessage)
 131            {
 2132                chatHudController.ResetInputField();
 2133                chatHudController.FocusInputField();
 134            }
 135            else
 136            {
 0137                HandleViewClosed();
 0138                SetVisibility(false);
 0139                return;
 140            }
 141
 2142            if (isPrivateMessage)
 1143                message.body = $"/w {message.recipient} {message.body}";
 144
 2145            chatController.Send(message);
 2146        }
 147
 9148        public void SetVisibility(bool visible) => SetVisibility(visible, false);
 149
 150        private void SetVisiblePanelList(bool visible)
 151        {
 9152            HashSet<string> newSet = visibleTaskbarPanels.Get();
 9153            if (visible)
 4154                newSet.Add("PublicChatChannel");
 155            else
 5156                newSet.Remove("PublicChatChannel");
 157
 9158            visibleTaskbarPanels.Set(newSet, true);
 9159        }
 160
 6161        private void MarkChannelMessagesAsRead() => chatController.MarkChannelMessagesAsSeen(channelId);
 162
 163        private void HandleViewClosed()
 164        {
 1165            OnClosed?.Invoke();
 1166        }
 167
 168        private void HandleViewBacked()
 169        {
 0170            OnBack?.Invoke();
 0171        }
 172
 173        private void HandleMessageReceived(ChatMessage[] messages)
 174        {
 4175            var messageLogUpdated = false;
 176
 18177            foreach (var message in messages)
 178            {
 5179                if (message.messageType != ChatMessage.Type.PUBLIC
 180                    && message.messageType != ChatMessage.Type.SYSTEM) continue;
 4181                if (!string.IsNullOrEmpty(message.recipient)) continue;
 182
 4183                chatHudController.AddChatMessage(message, View.IsActive);
 4184                messageLogUpdated = true;
 185            }
 186
 4187            if (View.IsActive && messageLogUpdated)
 2188                MarkChannelMessagesAsRead();
 4189        }
 190
 1191        private void Hide() => SetVisibility(false);
 192
 193        private void HandleChatInputTriggered(DCLAction_Trigger action)
 194        {
 0195            if (!View.IsActive) return;
 0196            chatHudController.FocusInputField();
 0197        }
 198
 199        private void HandleMessageBlockedBySpam(ChatMessage message)
 200        {
 0201            chatHudController.AddChatMessage(new ChatEntryModel
 202            {
 203                timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
 204                bodyText = "You sent too many messages in a short period of time. Please wait and try again later.",
 205                messageId = Guid.NewGuid().ToString(),
 206                messageType = ChatMessage.Type.SYSTEM,
 207                subType = ChatEntryModel.SubType.RECEIVED
 208            });
 0209        }
 210
 211        private void MuteChannel(bool muted)
 212        {
 2213            if (muted)
 1214                chatController.MuteChannel(channelId);
 215            else
 1216                chatController.UnmuteChannel(channelId);
 1217        }
 218
 219        private PublicChatModel ToPublicChatModel(Channel channel)
 220        {
 7221            return new PublicChatModel(channel.ChannelId,
 222                channel.Name,
 223                channel.Description,
 224                channel.Joined,
 225                channel.MemberCount,
 226                channel.Muted,
 227                showOnlyOnlineMembersOnPublicChannels);
 228        }
 229
 230        private void HandleChannelUpdated(Channel updatedChannel)
 231        {
 1232            if (updatedChannel.ChannelId != channelId) return;
 1233            View.Configure(ToPublicChatModel(updatedChannel));
 1234        }
 235    }
 236}