< Summary

Class:DCL.Chat.Notifications.ChatNotificationController
Assembly:NotificationMessagesHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationMessagesHUD/ChatNotificationController.cs
Covered lines:87
Uncovered lines:13
Coverable lines:100
Total lines:200
Line coverage:87% (87 of 100)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChatNotificationController(...)0%110100%
SetVisibility(...)0%4.774063.64%
Dispose()0%110100%
VisiblePanelsChanged(...)0%2100%
HandleMessageAdded(...)0%14.2512075%
AddNotification()0%29.6527084.62%
ResetFadeOut(...)0%330100%
TogglePanelBackground(...)0%6200%
WaitThenFadeOutNotifications()0%10.378066.67%
ExtractPeerId(...)0%220100%
ResetVisibility(...)0%110100%
IsProfanityFilteringEnabled()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NotificationMessagesHUD/ChatNotificationController.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Threading;
 3using Cysharp.Threading.Tasks;
 4using DCL.Interface;
 5using DCL.Helpers;
 6using UnityEngine;
 7using System;
 8using Channel = DCL.Chat.Channels.Channel;
 9
 10namespace DCL.Chat.Notifications
 11{
 12    public class ChatNotificationController : IHUD
 13    {
 14        private const int FADEOUT_DELAY = 8000;
 15
 16        private readonly DataStore dataStore;
 17        private readonly IChatController chatController;
 18        private readonly IMainChatNotificationsComponentView mainChatNotificationView;
 19        private readonly ITopNotificationsComponentView topNotificationView;
 20        private readonly IUserProfileBridge userProfileBridge;
 21        private readonly IProfanityFilter profanityFilter;
 822        private readonly TimeSpan maxNotificationInterval = new TimeSpan(0, 1, 0);
 823        private readonly HashSet<string> notificationEntries = new HashSet<string>();
 2424        private BaseVariable<bool> shouldShowNotificationPanel => dataStore.HUDs.shouldShowNotificationPanel;
 825        private BaseVariable<Transform> notificationPanelTransform => dataStore.HUDs.notificationPanelTransform;
 3126        private BaseVariable<Transform> topNotificationPanelTransform => dataStore.HUDs.topNotificationPanelTransform;
 1627        private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 728        private BaseVariable<string> openedChat => dataStore.HUDs.openedChat;
 829        private CancellationTokenSource fadeOutCT = new CancellationTokenSource();
 30        private UserProfile ownUserProfile;
 31
 832        public ChatNotificationController(DataStore dataStore,
 33            IMainChatNotificationsComponentView mainChatNotificationView,
 34            ITopNotificationsComponentView topNotificationView,
 35            IChatController chatController,
 36            IUserProfileBridge userProfileBridge,
 37            IProfanityFilter profanityFilter)
 38        {
 839            this.dataStore = dataStore;
 840            this.chatController = chatController;
 841            this.userProfileBridge = userProfileBridge;
 842            this.profanityFilter = profanityFilter;
 843            this.mainChatNotificationView = mainChatNotificationView;
 844            this.topNotificationView = topNotificationView;
 845            mainChatNotificationView.OnResetFade += ResetFadeOut;
 846            topNotificationView.OnResetFade += ResetFadeOut;
 847            mainChatNotificationView.OnPanelFocus += TogglePanelBackground;
 848            chatController.OnAddMessage += HandleMessageAdded;
 849            notificationPanelTransform.Set(mainChatNotificationView.GetPanelTransform());
 850            topNotificationPanelTransform.Set(topNotificationView.GetPanelTransform());
 851            visibleTaskbarPanels.OnChange += VisiblePanelsChanged;
 852            shouldShowNotificationPanel.OnChange += ResetVisibility;
 853            ResetVisibility(shouldShowNotificationPanel.Get(), false);
 854        }
 55
 56        public void SetVisibility(bool visible)
 57        {
 858            ResetFadeOut(visible);
 59
 860            if (visible)
 61            {
 862                if (shouldShowNotificationPanel.Get())
 863                    mainChatNotificationView.Show();
 64
 865                topNotificationView.Hide();
 866                mainChatNotificationView.ShowNotifications();
 867            }
 68            else
 69            {
 070                mainChatNotificationView.Hide();
 071                if (!visibleTaskbarPanels.Get().Contains("WorldChatPanel"))
 072                    topNotificationView.Show();
 73            }
 074        }
 75
 76        public void Dispose()
 77        {
 878            chatController.OnAddMessage -= HandleMessageAdded;
 879            visibleTaskbarPanels.OnChange -= VisiblePanelsChanged;
 880            mainChatNotificationView.OnResetFade -= ResetFadeOut;
 881            topNotificationView.OnResetFade -= ResetFadeOut;
 882        }
 83
 84        private void VisiblePanelsChanged(HashSet<string> newList, HashSet<string> oldList)
 85        {
 086            SetVisibility(newList.Count == 0);
 087        }
 88
 89        private void HandleMessageAdded(ChatMessage message)
 90        {
 891            if (message.messageType != ChatMessage.Type.PRIVATE &&
 092                message.messageType != ChatMessage.Type.PUBLIC) return;
 893            ownUserProfile ??= userProfileBridge.GetOwn();
 894            if (message.sender == ownUserProfile.userId) return;
 95
 896            var span = Utils.UnixToDateTimeWithTime((ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) -
 97                       Utils.UnixToDateTimeWithTime(message.timestamp);
 98
 999            if (span >= maxNotificationInterval) return;
 100
 7101            var channel = chatController.GetAllocatedChannel(
 102                string.IsNullOrEmpty(message.recipient) && message.messageType == ChatMessage.Type.PUBLIC
 103                    ? "nearby"
 104                    : message.recipient);
 7105            if (channel?.Muted ?? false) return;
 106
 107            // TODO: entries may have an inconsistent state. We should update the entry with new data
 7108            if (notificationEntries.Contains(message.messageId)) return;
 7109            notificationEntries.Add(message.messageId);
 110
 7111            AddNotification(message, channel).Forget();
 7112        }
 113
 114        private async UniTaskVoid AddNotification(ChatMessage message, Channel channel = null)
 115        {
 7116            var peerId = ExtractPeerId(message);
 7117            var peerProfile = userProfileBridge.Get(peerId);
 7118            var peerName = peerProfile?.userName ?? peerId;
 7119            var peerProfilePicture = peerProfile?.face256SnapshotURL;
 7120            var body = message.body;
 121
 7122            switch (message.messageType)
 123            {
 124                case ChatMessage.Type.PRIVATE:
 2125                    var privateModel = new PrivateChatMessageNotificationModel(message.messageId,
 126                        message.sender, body, message.timestamp, peerName, peerProfilePicture);
 127
 2128                    if (message.sender != openedChat.Get())
 129                    {
 2130                        mainChatNotificationView.AddNewChatNotification(privateModel);
 2131                        if (topNotificationPanelTransform.Get().gameObject.activeInHierarchy)
 2132                            topNotificationView.AddNewChatNotification(privateModel);
 133                    }
 134
 2135                    break;
 136                case ChatMessage.Type.PUBLIC:
 5137                    if (IsProfanityFilteringEnabled())
 138                    {
 2139                        peerName = await profanityFilter.Filter(peerProfile?.userName ?? peerId);
 2140                        body = await profanityFilter.Filter(message.body);
 141                    }
 5142                    var publicModel = new PublicChannelMessageNotificationModel(message.messageId,
 143                        body, channel?.Name ?? message.recipient, channel?.ChannelId, message.timestamp,
 144                        peerName);
 145
 5146                    if (channel?.ChannelId != openedChat.Get())
 147                    {
 5148                        mainChatNotificationView.AddNewChatNotification(publicModel);
 5149                        if (topNotificationPanelTransform.Get().gameObject.activeInHierarchy)
 5150                            topNotificationView.AddNewChatNotification(publicModel);
 151                    }
 152
 7153                    break;
 154            }
 7155        }
 156
 157        private void ResetFadeOut(bool fadeOutAfterDelay = false)
 158        {
 8159            mainChatNotificationView.ShowNotifications();
 8160            if (topNotificationPanelTransform.Get().gameObject.activeInHierarchy)
 8161                topNotificationView.ShowNotification();
 162
 8163            fadeOutCT.Cancel();
 8164            fadeOutCT = new CancellationTokenSource();
 165
 8166            if (fadeOutAfterDelay)
 8167                WaitThenFadeOutNotifications(fadeOutCT.Token).Forget();
 8168        }
 169
 170        private void TogglePanelBackground(bool isInFocus)
 171        {
 0172            if (isInFocus)
 0173                mainChatNotificationView.ShowPanel();
 174            else
 0175                mainChatNotificationView.HidePanel();
 0176        }
 177
 178        private async UniTaskVoid WaitThenFadeOutNotifications(CancellationToken cancellationToken)
 179        {
 24180            await UniTask.Delay(FADEOUT_DELAY, cancellationToken: cancellationToken);
 8181            await UniTask.SwitchToMainThread(cancellationToken);
 8182            if (cancellationToken.IsCancellationRequested)
 0183                return;
 184
 8185            mainChatNotificationView.HideNotifications();
 186
 8187            if (topNotificationPanelTransform.Get() != null &&
 188                topNotificationPanelTransform.Get().gameObject.activeInHierarchy)
 0189                topNotificationView.HideNotification();
 8190        }
 191
 192        private string ExtractPeerId(ChatMessage message) =>
 7193            message.sender != ownUserProfile.userId ? message.sender : message.recipient;
 194
 8195        private void ResetVisibility(bool current, bool previous) => SetVisibility(current);
 196
 197        private bool IsProfanityFilteringEnabled() =>
 5198            dataStore.settings.profanityChatFilteringEnabled.Get();
 199    }
 200}