< Summary

Class:DCL.Social.Chat.PublicChatWindowController
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/PublicChatWindowController.cs
Covered lines:126
Uncovered lines:16
Coverable lines:142
Total lines:329
Line coverage:88.7% (126 of 142)
Covered branches:0
Total branches:0
Covered methods:22
Total methods:28
Method coverage:78.5% (22 of 28)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PublicChatWindowController(...)0%110100%
Initialize(...)0%220100%
Setup(...)0%3.033085.71%
Dispose()0%440100%
SetVisibility(...)0%44093.75%
SetVisibility(...)0%110100%
SendChatMessage(...)0%4.24076.92%
SetVisiblePanelList(...)0%220100%
MarkChannelMessagesAsRead()0%110100%
HandleViewClosed()0%220100%
HandleViewBacked()0%6200%
HandleMessageReceived(...)0%770100%
CheckOwnPlayerMentionInNearBy(...)0%660100%
Hide()0%110100%
HandleMessageBlockedBySpam(...)0%2100%
MuteChannel(...)0%220100%
ToPublicChatModel(...)0%110100%
HandleChannelUpdated(...)0%2.062075%
SomeoneMentionedFromContextMenu(...)0%6200%
ShowMembersList()0%110100%
HideMembersList()0%2100%
GoToCrowd()0%2100%
UpdateMembersCount(...)0%2100%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Chat;
 3using DCL.Interface;
 4using DCL.ProfanityFiltering;
 5using DCL.Social.Chat.Mentions;
 6using DCLServices.CopyPaste.Analytics;
 7using SocialFeaturesAnalytics;
 8using System;
 9using System.Collections.Generic;
 10using Channel = DCL.Chat.Channels.Channel;
 11
 12namespace DCL.Social.Chat
 13{
 14    public class PublicChatWindowController : IHUD
 15    {
 17116        public IPublicChatWindowView View { get; private set; }
 17
 18        public event Action OnBack;
 19        public event Action OnClosed;
 20
 21        private readonly IChatController chatController;
 22        private readonly IUserProfileBridge userProfileBridge;
 23        private readonly DataStore dataStore;
 24        private readonly IProfanityFilter profanityFilter;
 25        private readonly IMouseCatcher mouseCatcher;
 26        private readonly IChatMentionSuggestionProvider chatMentionSuggestionProvider;
 27        private readonly ISocialAnalytics socialAnalytics;
 28        private readonly IClipboard clipboard;
 29        private readonly ICopyPasteAnalyticsService copyPasteAnalyticsService;
 30        private ChatHUDController chatHudController;
 31        private string channelId;
 32        private bool skipChatInputTrigger;
 33        private NearbyMembersHUDController nearbyMembersHUDController;
 34
 1935        private bool showOnlyOnlineMembersOnPublicChannels => !dataStore.featureFlags.flags.Get().IsFeatureEnabled("matr
 9536        private BaseDictionary<string, Player> nearbyPlayers => dataStore.player.otherPlayers;
 37
 38        private bool isVisible;
 39        private Channel channel;
 40
 1841        private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 42
 1943        public PublicChatWindowController(IChatController chatController,
 44            IUserProfileBridge userProfileBridge,
 45            DataStore dataStore,
 46            IProfanityFilter profanityFilter,
 47            IMouseCatcher mouseCatcher,
 48            IChatMentionSuggestionProvider chatMentionSuggestionProvider,
 49            ISocialAnalytics socialAnalytics,
 50            IClipboard clipboard,
 51            ICopyPasteAnalyticsService copyPasteAnalyticsService)
 52        {
 1953            this.chatController = chatController;
 1954            this.userProfileBridge = userProfileBridge;
 1955            this.dataStore = dataStore;
 1956            this.profanityFilter = profanityFilter;
 1957            this.mouseCatcher = mouseCatcher;
 1958            this.chatMentionSuggestionProvider = chatMentionSuggestionProvider;
 1959            this.socialAnalytics = socialAnalytics;
 1960            this.clipboard = clipboard;
 1961            this.copyPasteAnalyticsService = copyPasteAnalyticsService;
 1962        }
 63
 64        public void Initialize(IPublicChatWindowView view, bool isVisible = true)
 65        {
 1966            View = view;
 1967            view.OnClose += HandleViewClosed;
 1968            view.OnBack += HandleViewBacked;
 1969            view.OnMuteChanged += MuteChannel;
 1970            view.OnGoToCrowd += GoToCrowd;
 71
 1972            if (mouseCatcher != null)
 1873                mouseCatcher.OnMouseLock += Hide;
 74
 1975            chatHudController = new ChatHUDController(dataStore,
 76                userProfileBridge,
 77                true,
 378                (name, count, ct) => chatMentionSuggestionProvider.GetNearbyProfilesStartingWith(name, count, ct),
 79                socialAnalytics,
 80                chatController,
 81                clipboard,
 82                copyPasteAnalyticsService,
 83                profanityFilter);
 84            // dont set any message's sorting strategy, just add them sequentally
 85            // comms cannot calculate a server timestamp for each message
 1986            chatHudController.Initialize(view.ChatHUD);
 1987            chatHudController.OnSendMessage += SendChatMessage;
 1988            chatHudController.OnMessageSentBlockedBySpam += HandleMessageBlockedBySpam;
 89
 1990            chatController.OnAddMessage -= HandleMessageReceived;
 1991            chatController.OnAddMessage += HandleMessageReceived;
 1992            chatController.OnChannelUpdated -= HandleChannelUpdated;
 1993            chatController.OnChannelUpdated += HandleChannelUpdated;
 94
 1995            dataStore.mentions.someoneMentionedFromContextMenu.OnChange += SomeoneMentionedFromContextMenu;
 1996            nearbyPlayers.OnAdded += UpdateMembersCount;
 1997            nearbyPlayers.OnRemoved += UpdateMembersCount;
 98
 1999            view.OnShowMembersList += ShowMembersList;
 19100            view.OnHideMembersList += HideMembersList;
 19101            nearbyMembersHUDController = new NearbyMembersHUDController(view.ChannelMembersHUD, dataStore.player, userPr
 102
 19103            SetVisibility(isVisible);
 19104            this.isVisible = isVisible;
 19105        }
 106
 107        public void Setup(string channelId)
 108        {
 18109            if (string.IsNullOrEmpty(channelId) || channelId == this.channelId) return;
 18110            this.channelId = channelId;
 111
 18112            channel = chatController.GetAllocatedChannel(channelId);
 18113            View.Configure(ToPublicChatModel(channel));
 114
 18115            chatHudController.ClearAllEntries();
 18116        }
 117
 118        public void Dispose()
 119        {
 19120            View.OnClose -= HandleViewClosed;
 19121            View.OnBack -= HandleViewBacked;
 19122            View.OnMuteChanged -= MuteChannel;
 19123            View.OnGoToCrowd -= GoToCrowd;
 124
 19125            if (chatController != null)
 126            {
 19127                chatController.OnAddMessage -= HandleMessageReceived;
 19128                chatController.OnChannelUpdated -= HandleChannelUpdated;
 129            }
 130
 19131            chatHudController.OnSendMessage -= SendChatMessage;
 19132            chatHudController.OnMessageSentBlockedBySpam -= HandleMessageBlockedBySpam;
 19133            chatHudController.Dispose();
 134
 19135            if (mouseCatcher != null)
 18136                mouseCatcher.OnMouseLock -= Hide;
 137
 19138            dataStore.mentions.someoneMentionedFromContextMenu.OnChange -= SomeoneMentionedFromContextMenu;
 19139            nearbyPlayers.OnAdded -= UpdateMembersCount;
 19140            nearbyPlayers.OnRemoved -= UpdateMembersCount;
 141
 19142            View?.Dispose();
 19143            nearbyMembersHUDController.Dispose();
 19144        }
 145
 146        public void SetVisibility(bool visible, bool focusInputField)
 147        {
 28148            if (isVisible == visible)
 19149                return;
 150
 9151            isVisible = visible;
 152
 9153            SetVisiblePanelList(visible);
 9154            chatHudController.SetVisibility(visible);
 9155            dataStore.HUDs.chatInputVisible.Set(visible);
 156
 9157            if (visible)
 158            {
 6159                View.ChatHUD.ResetInputField();
 6160                View.Show();
 6161                MarkChannelMessagesAsRead();
 162
 6163                if (focusInputField)
 0164                    chatHudController.FocusInputField();
 165
 6166                nearbyMembersHUDController.ClearSearch();
 167            }
 168            else
 169            {
 3170                chatHudController.UnfocusInputField();
 3171                View.Hide();
 172            }
 3173        }
 174
 175        public void SetVisibility(bool visible) =>
 28176            SetVisibility(visible, false);
 177
 178        private void SendChatMessage(ChatMessage message)
 179        {
 2180            bool isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body);
 2181            bool isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE;
 182
 2183            message.channelName = channel.Name;
 184
 2185            if (isValidMessage)
 186            {
 2187                chatHudController.ResetInputField();
 2188                chatHudController.FocusInputField();
 189            }
 190            else
 191            {
 0192                HandleViewClosed();
 0193                SetVisibility(false);
 0194                return;
 195            }
 196
 2197            if (isPrivateMessage)
 1198                message.body = $"/w {message.recipient} {message.body}";
 199
 2200            chatController.Send(message);
 2201        }
 202
 203        private void SetVisiblePanelList(bool visible)
 204        {
 9205            HashSet<string> newSet = visibleTaskbarPanels.Get();
 206
 9207            if (visible)
 6208                newSet.Add("PublicChatChannel");
 209            else
 3210                newSet.Remove("PublicChatChannel");
 211
 9212            visibleTaskbarPanels.Set(newSet, true);
 9213        }
 214
 215        private void MarkChannelMessagesAsRead() =>
 8216            chatController.MarkChannelMessagesAsSeen(channelId);
 217
 218        private void HandleViewClosed()
 219        {
 1220            OnClosed?.Invoke();
 1221        }
 222
 223        private void HandleViewBacked()
 224        {
 0225            OnBack?.Invoke();
 0226        }
 227
 228        private void HandleMessageReceived(ChatMessage[] messages)
 229        {
 6230            var messageLogUpdated = false;
 231
 6232            var ownPlayerAlreadyMentioned = false;
 233
 26234            foreach (var message in messages)
 235            {
 7236                if (!ownPlayerAlreadyMentioned)
 7237                    ownPlayerAlreadyMentioned = CheckOwnPlayerMentionInNearBy(message);
 238
 7239                if (message.messageType != ChatMessage.Type.PUBLIC
 240                    && message.messageType != ChatMessage.Type.SYSTEM) continue;
 241
 6242                if (!string.IsNullOrEmpty(message.recipient)) continue;
 243
 6244                chatHudController.SetChatMessage(message, View.IsActive);
 6245                messageLogUpdated = true;
 246            }
 247
 6248            if (View.IsActive && messageLogUpdated)
 2249                MarkChannelMessagesAsRead();
 6250        }
 251
 252        private bool CheckOwnPlayerMentionInNearBy(ChatMessage message)
 253        {
 7254            var ownUserProfile = userProfileBridge.GetOwn();
 255
 7256            if (message.sender == ownUserProfile.userId ||
 257                message.messageType != ChatMessage.Type.PUBLIC ||
 258                !string.IsNullOrEmpty(message.recipient) ||
 259                View.IsActive ||
 260                !MentionsUtils.IsUserMentionedInText(ownUserProfile.userName, message.body))
 6261                return false;
 262
 1263            dataStore.mentions.ownPlayerMentionedInChannel.Set(ChatUtils.NEARBY_CHANNEL_ID, true);
 1264            return true;
 265        }
 266
 267        private void Hide() =>
 1268            SetVisibility(false);
 269
 270        private void HandleMessageBlockedBySpam(ChatMessage message)
 271        {
 0272            chatHudController.SetChatMessage(new ChatEntryModel
 273            {
 274                timestamp = (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
 275                bodyText = "You sent too many messages in a short period of time. Please wait and try again later.",
 276                messageId = Guid.NewGuid().ToString(),
 277                messageType = ChatMessage.Type.SYSTEM,
 278                subType = ChatEntryModel.SubType.RECEIVED
 279            }).Forget();
 0280        }
 281
 282        private void MuteChannel(bool muted)
 283        {
 2284            if (muted)
 1285                chatController.MuteChannel(channelId);
 286            else
 1287                chatController.UnmuteChannel(channelId);
 1288        }
 289
 290        private PublicChatModel ToPublicChatModel(Channel channel) =>
 19291            new (channel.ChannelId,
 292                channel.Name,
 293                channel.Description,
 294                channel.Joined,
 295                nearbyPlayers.Count(),
 296                channel.Muted,
 297                showOnlyOnlineMembersOnPublicChannels);
 298
 299        private void HandleChannelUpdated(Channel updatedChannel)
 300        {
 1301            if (updatedChannel.ChannelId != channelId) return;
 1302            View.Configure(ToPublicChatModel(updatedChannel));
 1303        }
 304
 305        private void SomeoneMentionedFromContextMenu(string mention, string _)
 306        {
 0307            if (!View.IsActive)
 0308                return;
 309
 0310            View.ChatHUD.AddTextIntoInputField(mention);
 0311        }
 312
 313        private void ShowMembersList() =>
 1314            nearbyMembersHUDController.SetVisibility(true);
 315
 316        private void HideMembersList() =>
 0317            nearbyMembersHUDController.SetVisibility(false);
 318
 319        private void GoToCrowd()
 320        {
 321            // Requested temporally by product team since the "go to crowd" approach was always redirecting to the casin
 322            //Environment.i.world.teleportController.GoToCrowd();
 0323            Environment.i.world.teleportController.Teleport(0, 0);
 0324        }
 325
 326        private void UpdateMembersCount(string userId, Player player) =>
 0327            View.UpdateMembersCount(nearbyPlayers.Count());
 328    }
 329}