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