| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Interface; |
| | 4 | | using Channel = DCL.Chat.Channels.Channel; |
| | 5 | |
|
| | 6 | | namespace DCL.Chat.HUD |
| | 7 | | { |
| | 8 | | public class PublicChatWindowController : IHUD |
| | 9 | | { |
| 108 | 10 | | 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; |
| 7 | 24 | | private bool showOnlyOnlineMembersOnPublicChannels => !dataStore.featureFlags.flags.Get().IsFeatureEnabled("matr |
| | 25 | |
|
| 18 | 26 | | private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| | 27 | |
|
| 14 | 28 | | public PublicChatWindowController(IChatController chatController, |
| | 29 | | IUserProfileBridge userProfileBridge, |
| | 30 | | DataStore dataStore, |
| | 31 | | IProfanityFilter profanityFilter, |
| | 32 | | IMouseCatcher mouseCatcher, |
| | 33 | | InputAction_Trigger toggleChatTrigger) |
| | 34 | | { |
| 14 | 35 | | this.chatController = chatController; |
| 14 | 36 | | this.userProfileBridge = userProfileBridge; |
| 14 | 37 | | this.dataStore = dataStore; |
| 14 | 38 | | this.profanityFilter = profanityFilter; |
| 14 | 39 | | this.mouseCatcher = mouseCatcher; |
| 14 | 40 | | this.toggleChatTrigger = toggleChatTrigger; |
| 14 | 41 | | } |
| | 42 | |
|
| | 43 | | public void Initialize(IPublicChatWindowView view = null) |
| | 44 | | { |
| 14 | 45 | | view ??= PublicChatWindowComponentView.Create(); |
| 14 | 46 | | View = view; |
| 14 | 47 | | view.OnClose += HandleViewClosed; |
| 14 | 48 | | view.OnBack += HandleViewBacked; |
| 14 | 49 | | view.OnMuteChanged += MuteChannel; |
| | 50 | |
|
| 14 | 51 | | if (mouseCatcher != null) |
| 13 | 52 | | mouseCatcher.OnMouseLock += Hide; |
| | 53 | |
|
| 14 | 54 | | chatHudController = new ChatHUDController(dataStore, |
| | 55 | | userProfileBridge, |
| | 56 | | true, |
| | 57 | | profanityFilter); |
| 14 | 58 | | chatHudController.Initialize(view.ChatHUD); |
| 14 | 59 | | chatHudController.OnSendMessage += SendChatMessage; |
| 14 | 60 | | chatHudController.OnMessageSentBlockedBySpam += HandleMessageBlockedBySpam; |
| | 61 | |
|
| 14 | 62 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 14 | 63 | | chatController.OnAddMessage += HandleMessageReceived; |
| 14 | 64 | | chatController.OnChannelUpdated -= HandleChannelUpdated; |
| 14 | 65 | | chatController.OnChannelUpdated += HandleChannelUpdated; |
| | 66 | |
|
| 14 | 67 | | toggleChatTrigger.OnTriggered += HandleChatInputTriggered; |
| 14 | 68 | | } |
| | 69 | |
|
| | 70 | | public void Setup(string channelId) |
| | 71 | | { |
| 6 | 72 | | if (string.IsNullOrEmpty(channelId) || channelId == this.channelId) return; |
| 6 | 73 | | this.channelId = channelId; |
| | 74 | |
|
| 6 | 75 | | var channel = chatController.GetAllocatedChannel(channelId); |
| 6 | 76 | | View.Configure(ToPublicChatModel(channel)); |
| | 77 | |
|
| 6 | 78 | | chatHudController.ClearAllEntries(); |
| 6 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Dispose() |
| | 82 | | { |
| 13 | 83 | | View.OnClose -= HandleViewClosed; |
| 13 | 84 | | View.OnBack -= HandleViewBacked; |
| 13 | 85 | | View.OnMuteChanged -= MuteChannel; |
| | 86 | |
|
| 13 | 87 | | if (chatController != null) |
| | 88 | | { |
| 13 | 89 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 13 | 90 | | chatController.OnChannelUpdated -= HandleChannelUpdated; |
| | 91 | | } |
| | 92 | |
|
| 13 | 93 | | chatHudController.OnSendMessage -= SendChatMessage; |
| 13 | 94 | | chatHudController.OnMessageSentBlockedBySpam -= HandleMessageBlockedBySpam; |
| | 95 | |
|
| 13 | 96 | | if (mouseCatcher != null) |
| 12 | 97 | | mouseCatcher.OnMouseLock -= Hide; |
| | 98 | |
|
| 13 | 99 | | toggleChatTrigger.OnTriggered -= HandleChatInputTriggered; |
| | 100 | |
|
| 13 | 101 | | if (View != null) |
| | 102 | | { |
| 13 | 103 | | View.Dispose(); |
| | 104 | | } |
| 13 | 105 | | } |
| | 106 | |
|
| | 107 | | public void SetVisibility(bool visible, bool focusInputField) |
| | 108 | | { |
| 9 | 109 | | SetVisiblePanelList(visible); |
| 9 | 110 | | if (visible) |
| | 111 | | { |
| 4 | 112 | | View.Show(); |
| 4 | 113 | | MarkChannelMessagesAsRead(); |
| | 114 | |
|
| 4 | 115 | | if (focusInputField) |
| 0 | 116 | | chatHudController.FocusInputField(); |
| | 117 | | } |
| | 118 | | else |
| | 119 | | { |
| 5 | 120 | | chatHudController.UnfocusInputField(); |
| 5 | 121 | | View.Hide(); |
| | 122 | | } |
| 9 | 123 | | } |
| | 124 | |
|
| | 125 | | private void SendChatMessage(ChatMessage message) |
| | 126 | | { |
| 2 | 127 | | var isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body); |
| 2 | 128 | | var isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE; |
| | 129 | |
|
| 2 | 130 | | if (isValidMessage) |
| | 131 | | { |
| 2 | 132 | | chatHudController.ResetInputField(); |
| 2 | 133 | | chatHudController.FocusInputField(); |
| | 134 | | } |
| | 135 | | else |
| | 136 | | { |
| 0 | 137 | | HandleViewClosed(); |
| 0 | 138 | | SetVisibility(false); |
| 0 | 139 | | return; |
| | 140 | | } |
| | 141 | |
|
| 2 | 142 | | if (isPrivateMessage) |
| 1 | 143 | | message.body = $"/w {message.recipient} {message.body}"; |
| | 144 | |
|
| 2 | 145 | | chatController.Send(message); |
| 2 | 146 | | } |
| | 147 | |
|
| 9 | 148 | | public void SetVisibility(bool visible) => SetVisibility(visible, false); |
| | 149 | |
|
| | 150 | | private void SetVisiblePanelList(bool visible) |
| | 151 | | { |
| 9 | 152 | | HashSet<string> newSet = visibleTaskbarPanels.Get(); |
| 9 | 153 | | if (visible) |
| 4 | 154 | | newSet.Add("PublicChatChannel"); |
| | 155 | | else |
| 5 | 156 | | newSet.Remove("PublicChatChannel"); |
| | 157 | |
|
| 9 | 158 | | visibleTaskbarPanels.Set(newSet, true); |
| 9 | 159 | | } |
| | 160 | |
|
| 6 | 161 | | private void MarkChannelMessagesAsRead() => chatController.MarkChannelMessagesAsSeen(channelId); |
| | 162 | |
|
| | 163 | | private void HandleViewClosed() |
| | 164 | | { |
| 1 | 165 | | OnClosed?.Invoke(); |
| 1 | 166 | | } |
| | 167 | |
|
| | 168 | | private void HandleViewBacked() |
| | 169 | | { |
| 0 | 170 | | OnBack?.Invoke(); |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | private void HandleMessageReceived(ChatMessage[] messages) |
| | 174 | | { |
| 4 | 175 | | var messageLogUpdated = false; |
| | 176 | |
|
| 18 | 177 | | foreach (var message in messages) |
| | 178 | | { |
| 5 | 179 | | if (message.messageType != ChatMessage.Type.PUBLIC |
| | 180 | | && message.messageType != ChatMessage.Type.SYSTEM) continue; |
| 4 | 181 | | if (!string.IsNullOrEmpty(message.recipient)) continue; |
| | 182 | |
|
| 4 | 183 | | chatHudController.AddChatMessage(message, View.IsActive); |
| 4 | 184 | | messageLogUpdated = true; |
| | 185 | | } |
| | 186 | |
|
| 4 | 187 | | if (View.IsActive && messageLogUpdated) |
| 2 | 188 | | MarkChannelMessagesAsRead(); |
| 4 | 189 | | } |
| | 190 | |
|
| 1 | 191 | | private void Hide() => SetVisibility(false); |
| | 192 | |
|
| | 193 | | private void HandleChatInputTriggered(DCLAction_Trigger action) |
| | 194 | | { |
| 0 | 195 | | if (!View.IsActive) return; |
| 0 | 196 | | chatHudController.FocusInputField(); |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | private void HandleMessageBlockedBySpam(ChatMessage message) |
| | 200 | | { |
| 0 | 201 | | 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 | | }); |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | private void MuteChannel(bool muted) |
| | 212 | | { |
| 2 | 213 | | if (muted) |
| 1 | 214 | | chatController.MuteChannel(channelId); |
| | 215 | | else |
| 1 | 216 | | chatController.UnmuteChannel(channelId); |
| 1 | 217 | | } |
| | 218 | |
|
| | 219 | | private PublicChatModel ToPublicChatModel(Channel channel) |
| | 220 | | { |
| 7 | 221 | | 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 | | { |
| 1 | 232 | | if (updatedChannel.ChannelId != channelId) return; |
| 1 | 233 | | View.Configure(ToPublicChatModel(updatedChannel)); |
| 1 | 234 | | } |
| | 235 | | } |
| | 236 | | } |