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