< Summary

Class:PublicChatWindowController
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/PublicChatWindowController.cs
Covered lines:95
Uncovered lines:17
Coverable lines:112
Total lines:244
Line coverage:84.8% (95 of 112)
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%330100%
SendChatMessage(...)0%4.24076.92%
SetVisibility(...)0%110100%
SetVisiblePanelList(...)0%220100%
ReloadAllChats()0%16.676033.33%
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 Cysharp.Threading.Tasks;
 4using DCL;
 5using DCL.Interface;
 6using Channel = DCL.Chat.Channels.Channel;
 7
 8public 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
 2225    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        ReloadAllChats().Forget();
 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    {
 11108        SetVisiblePanelList(visible);
 11109        if (visible)
 110        {
 6111            View.Show();
 6112            MarkChannelMessagesAsRead();
 113
 6114            if (focusInputField)
 1115                chatHudController.FocusInputField();
 1116        }
 117        else
 118        {
 5119            chatHudController.UnfocusInputField();
 5120            View.Hide();
 121        }
 10122    }
 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
 10147    public void SetVisibility(bool visible) => SetVisibility(visible, false);
 148
 149    private void SetVisiblePanelList(bool visible)
 150    {
 11151        HashSet<string> newSet = visibleTaskbarPanels.Get();
 11152        if (visible)
 6153            newSet.Add("PublicChatChannel");
 154        else
 5155            newSet.Remove("PublicChatChannel");
 156
 11157        visibleTaskbarPanels.Set(newSet, true);
 11158    }
 159
 160    private async UniTaskVoid ReloadAllChats()
 161    {
 4162        chatHudController.ClearAllEntries();
 163
 164        const int entriesPerFrame = 10;
 165        // TODO: filter entries by channelId
 4166        var list = chatController.GetAllocatedEntries();
 8167        if (list.Count == 0) return;
 168
 0169        for (var i = list.Count - 1; i >= 0; i--)
 170        {
 0171            var message = list[i];
 0172            if (i % entriesPerFrame == 0) await UniTask.NextFrame();
 0173            HandleMessageReceived(message);
 0174        }
 4175    }
 176
 7177    internal void MarkChannelMessagesAsRead() => chatController.MarkChannelMessagesAsSeen(channelId);
 178
 179    private void HandleViewClosed()
 180    {
 1181        OnClosed?.Invoke();
 1182    }
 183
 184    private void HandleViewBacked()
 185    {
 0186        OnBack?.Invoke();
 0187    }
 188
 189    private void HandleMessageReceived(ChatMessage message)
 190    {
 2191        if (message.messageType != ChatMessage.Type.PUBLIC
 1192            && message.messageType != ChatMessage.Type.SYSTEM) return;
 1193        if (!string.IsNullOrEmpty(message.recipient)) return;
 194
 1195        chatHudController.AddChatMessage(message, View.IsActive);
 196
 1197        if (View.IsActive)
 0198            MarkChannelMessagesAsRead();
 1199    }
 200
 1201    private void Hide() => SetVisibility(false);
 202
 203    private void HandleChatInputTriggered(DCLAction_Trigger action)
 204    {
 0205        if (!View.IsActive) return;
 0206        chatHudController.FocusInputField();
 0207    }
 208
 209    private void HandleMessageBlockedBySpam(ChatMessage message)
 210    {
 0211        chatHudController.AddChatMessage(new ChatEntryModel
 212        {
 213            timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
 214            bodyText = "You sent too many messages in a short period of time. Please wait and try again later.",
 215            messageId = Guid.NewGuid().ToString(),
 216            messageType = ChatMessage.Type.SYSTEM,
 217            subType = ChatEntryModel.SubType.RECEIVED
 218        });
 0219    }
 220
 221    private void MuteChannel(bool muted)
 222    {
 2223        if (muted)
 1224            chatController.MuteChannel(channelId);
 225        else
 1226            chatController.UnmuteChannel(channelId);
 1227    }
 228
 229    private PublicChatModel ToPublicChatModel(Channel channel)
 230    {
 5231        return new PublicChatModel(channel.ChannelId,
 232            channel.Name,
 233            channel.Description,
 234            channel.Joined,
 235            channel.MemberCount,
 236            channel.Muted);
 237    }
 238
 239    private void HandleChannelUpdated(Channel updatedChannel)
 240    {
 1241        if (updatedChannel.ChannelId != channelId) return;
 1242        View.Configure(ToPublicChatModel(updatedChannel));
 1243    }
 244}