| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Interface; |
| | 6 | | using SocialFeaturesAnalytics; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | public class PublicChatChannelController : IHUD |
| | 10 | | { |
| 0 | 11 | | public IChannelChatWindowView View { get; private set; } |
| | 12 | |
|
| | 13 | | private enum ChatWindowVisualState { NONE_VISIBLE, INPUT_MODE, PREVIEW_MODE } |
| | 14 | |
|
| | 15 | | public event Action OnBack; |
| | 16 | | public event Action OnClosed; |
| | 17 | | public event Action<bool> OnPreviewModeChanged; |
| | 18 | |
|
| | 19 | | private readonly IChatController chatController; |
| | 20 | | private readonly ILastReadMessagesService lastReadMessagesService; |
| | 21 | | private readonly IUserProfileBridge userProfileBridge; |
| | 22 | | private readonly DataStore dataStore; |
| | 23 | | private readonly IProfanityFilter profanityFilter; |
| | 24 | | private readonly ISocialAnalytics socialAnalytics; |
| | 25 | | private readonly IMouseCatcher mouseCatcher; |
| | 26 | | private readonly InputAction_Trigger toggleChatTrigger; |
| | 27 | | private ChatHUDController chatHudController; |
| | 28 | | private double initTimeInSeconds; |
| | 29 | | private string channelId; |
| | 30 | | private ChatWindowVisualState currentState; |
| 16 | 31 | | private CancellationTokenSource deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 16 | 32 | | private CancellationTokenSource deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 33 | |
|
| | 34 | | private bool skipChatInputTrigger; |
| 16 | 35 | | private string lastPrivateMessageRecipient = string.Empty; |
| | 36 | |
|
| 2 | 37 | | private UserProfile ownProfile => userProfileBridge.GetOwn(); |
| | 38 | |
|
| 16 | 39 | | public PublicChatChannelController(IChatController chatController, |
| | 40 | | ILastReadMessagesService lastReadMessagesService, |
| | 41 | | IUserProfileBridge userProfileBridge, |
| | 42 | | DataStore dataStore, |
| | 43 | | IProfanityFilter profanityFilter, |
| | 44 | | ISocialAnalytics socialAnalytics, |
| | 45 | | IMouseCatcher mouseCatcher, |
| | 46 | | InputAction_Trigger toggleChatTrigger) |
| | 47 | | { |
| 16 | 48 | | this.chatController = chatController; |
| 16 | 49 | | this.lastReadMessagesService = lastReadMessagesService; |
| 16 | 50 | | this.userProfileBridge = userProfileBridge; |
| 16 | 51 | | this.dataStore = dataStore; |
| 16 | 52 | | this.profanityFilter = profanityFilter; |
| 16 | 53 | | this.socialAnalytics = socialAnalytics; |
| 16 | 54 | | this.mouseCatcher = mouseCatcher; |
| 16 | 55 | | this.toggleChatTrigger = toggleChatTrigger; |
| 16 | 56 | | } |
| | 57 | |
|
| | 58 | | public void Initialize(IChannelChatWindowView view = null) |
| | 59 | | { |
| 16 | 60 | | view ??= PublicChatChannelComponentView.Create(); |
| 16 | 61 | | View = view; |
| 16 | 62 | | view.OnClose += HandleViewClosed; |
| 16 | 63 | | view.OnBack += HandleViewBacked; |
| 16 | 64 | | view.OnFocused += HandleViewFocused; |
| 16 | 65 | | View.OnClickOverWindow += HandleViewClicked; |
| | 66 | |
|
| | 67 | |
|
| 16 | 68 | | chatHudController = new ChatHUDController(dataStore, |
| | 69 | | userProfileBridge, |
| | 70 | | true, |
| | 71 | | profanityFilter); |
| 16 | 72 | | chatHudController.Initialize(view.ChatHUD); |
| 16 | 73 | | chatHudController.OnSendMessage += SendChatMessage; |
| 16 | 74 | | chatHudController.OnMessageUpdated += HandleMessageInputUpdated; |
| 16 | 75 | | chatHudController.OnInputFieldSelected -= HandleInputFieldSelected; |
| 16 | 76 | | chatHudController.OnInputFieldSelected += HandleInputFieldSelected; |
| 16 | 77 | | chatHudController.OnInputFieldDeselected -= HandleInputFieldDeselected; |
| 16 | 78 | | chatHudController.OnInputFieldDeselected += HandleInputFieldDeselected; |
| | 79 | |
|
| 16 | 80 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 16 | 81 | | chatController.OnAddMessage += HandleMessageReceived; |
| | 82 | |
|
| 16 | 83 | | if (mouseCatcher != null) |
| 16 | 84 | | mouseCatcher.OnMouseLock += ActivatePreview; |
| | 85 | |
|
| 16 | 86 | | toggleChatTrigger.OnTriggered += HandleChatInputTriggered; |
| | 87 | |
|
| 16 | 88 | | initTimeInSeconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0; |
| | 89 | |
|
| 16 | 90 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 16 | 91 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| 16 | 92 | | } |
| | 93 | |
|
| | 94 | | public void Setup(string channelId) |
| | 95 | | { |
| 0 | 96 | | if (string.IsNullOrEmpty(channelId) || channelId == this.channelId) return; |
| 0 | 97 | | this.channelId = channelId; |
| | 98 | |
|
| | 99 | | // TODO: retrieve data from a channel provider |
| 0 | 100 | | View.Configure(new PublicChatChannelModel(this.channelId, "nearby", |
| | 101 | | "Talk to the people around you. If you move far away from someone you will lose contact. All whispers will b |
| | 102 | |
|
| 0 | 103 | | ReloadAllChats().Forget(); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public void Dispose() |
| | 107 | | { |
| 15 | 108 | | View.OnClose -= HandleViewClosed; |
| 15 | 109 | | View.OnBack -= HandleViewBacked; |
| | 110 | |
|
| 15 | 111 | | if (chatController != null) |
| 15 | 112 | | chatController.OnAddMessage -= HandleMessageReceived; |
| | 113 | |
|
| 15 | 114 | | chatHudController.OnSendMessage -= SendChatMessage; |
| 15 | 115 | | chatHudController.OnMessageUpdated -= HandleMessageInputUpdated; |
| 15 | 116 | | chatHudController.OnInputFieldSelected -= HandleInputFieldSelected; |
| 15 | 117 | | chatHudController.OnInputFieldDeselected -= HandleInputFieldDeselected; |
| | 118 | |
|
| 15 | 119 | | if (mouseCatcher != null) |
| 15 | 120 | | mouseCatcher.OnMouseLock -= ActivatePreview; |
| | 121 | |
|
| 15 | 122 | | toggleChatTrigger.OnTriggered -= HandleChatInputTriggered; |
| | 123 | |
|
| 15 | 124 | | if (View != null) |
| | 125 | | { |
| 15 | 126 | | View.OnFocused -= HandleViewFocused; |
| 15 | 127 | | View.OnClickOverWindow -= HandleViewClicked; |
| 15 | 128 | | View.Dispose(); |
| | 129 | | } |
| 15 | 130 | | } |
| | 131 | |
|
| | 132 | | private void SendChatMessage(ChatMessage message) |
| | 133 | | { |
| 3 | 134 | | var isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body); |
| 3 | 135 | | var isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE; |
| | 136 | |
|
| 3 | 137 | | if (isPrivateMessage && isValidMessage) |
| 1 | 138 | | lastPrivateMessageRecipient = message.recipient; |
| | 139 | | else |
| 2 | 140 | | lastPrivateMessageRecipient = null; |
| | 141 | |
|
| 3 | 142 | | if (isValidMessage) |
| | 143 | | { |
| 2 | 144 | | chatHudController.ResetInputField(); |
| 2 | 145 | | chatHudController.FocusInputField(); |
| 2 | 146 | | } |
| | 147 | | else |
| | 148 | | { |
| 1 | 149 | | skipChatInputTrigger = true; |
| 1 | 150 | | chatHudController.ResetInputField(true); |
| 1 | 151 | | ActivatePreview(); |
| 1 | 152 | | return; |
| | 153 | | } |
| | 154 | |
|
| 2 | 155 | | if (isPrivateMessage) |
| 1 | 156 | | message.body = $"/w {message.recipient} {message.body}"; |
| | 157 | |
|
| 2 | 158 | | chatController.Send(message); |
| 2 | 159 | | } |
| | 160 | |
|
| | 161 | | public void SetVisibility(bool visible, bool focusInputField) |
| | 162 | | { |
| 11 | 163 | | if (visible) |
| | 164 | | { |
| 8 | 165 | | View.Show(); |
| 8 | 166 | | MarkChatMessagesAsRead(); |
| | 167 | |
|
| 8 | 168 | | if (focusInputField) |
| 1 | 169 | | chatHudController.FocusInputField(); |
| 1 | 170 | | } |
| | 171 | | else |
| | 172 | | { |
| 3 | 173 | | chatHudController.UnfocusInputField(); |
| 3 | 174 | | View.Hide(); |
| | 175 | | } |
| 10 | 176 | | } |
| | 177 | |
|
| 9 | 178 | | public void SetVisibility(bool visible) => SetVisibility(visible, false); |
| | 179 | |
|
| | 180 | | private async UniTaskVoid ReloadAllChats() |
| | 181 | | { |
| 0 | 182 | | chatHudController.ClearAllEntries(); |
| | 183 | |
|
| | 184 | | const int entriesPerFrame = 10; |
| | 185 | | // TODO: filter entries by channelId |
| 0 | 186 | | var list = chatController.GetEntries(); |
| 0 | 187 | | if (list.Count == 0) return; |
| | 188 | |
|
| 0 | 189 | | for (var i = list.Count - 1; i >= 0; i--) |
| | 190 | | { |
| 0 | 191 | | var message = list[i]; |
| 0 | 192 | | if (i % entriesPerFrame == 0) await UniTask.NextFrame(); |
| 0 | 193 | | HandleMessageReceived(message); |
| 0 | 194 | | } |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | public void ActivatePreviewModeInstantly() |
| | 198 | | { |
| 3 | 199 | | deactivatePreviewCancellationToken.Cancel(); |
| 3 | 200 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 3 | 201 | | deactivateFadeOutCancellationToken.Cancel(); |
| 3 | 202 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 203 | |
|
| 3 | 204 | | View.ActivatePreviewInstantly(); |
| 3 | 205 | | chatHudController.ActivatePreview(); |
| 3 | 206 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 3 | 207 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| 3 | 208 | | OnPreviewModeChanged?.Invoke(true); |
| 2 | 209 | | } |
| | 210 | |
|
| 8 | 211 | | private void MarkChatMessagesAsRead() => lastReadMessagesService.MarkAllRead(channelId); |
| | 212 | |
|
| 1 | 213 | | private void HandleViewClosed() => OnClosed?.Invoke(); |
| | 214 | |
|
| 0 | 215 | | private void HandleViewBacked() => OnBack?.Invoke(); |
| | 216 | |
|
| | 217 | | private void HandleMessageInputUpdated(string message) |
| | 218 | | { |
| 1 | 219 | | if (!string.IsNullOrEmpty(lastPrivateMessageRecipient) && message == "/r ") |
| 1 | 220 | | chatHudController.SetInputFieldText($"/w {lastPrivateMessageRecipient} "); |
| 1 | 221 | | } |
| | 222 | |
|
| | 223 | | private bool IsOldPrivateMessage(ChatMessage message) |
| | 224 | | { |
| 5 | 225 | | if (message.messageType != ChatMessage.Type.PRIVATE) return false; |
| 3 | 226 | | var timestampInSeconds = message.timestamp / 1000.0; |
| 3 | 227 | | return timestampInSeconds < initTimeInSeconds; |
| | 228 | | } |
| | 229 | |
|
| | 230 | | private void HandleMessageReceived(ChatMessage message) |
| | 231 | | { |
| 5 | 232 | | if (IsOldPrivateMessage(message)) return; |
| | 233 | |
|
| 3 | 234 | | chatHudController.AddChatMessage(message, View.IsActive); |
| | 235 | |
|
| 3 | 236 | | if (View.IsActive) |
| 0 | 237 | | MarkChatMessagesAsRead(); |
| | 238 | |
|
| 3 | 239 | | if (message.messageType == ChatMessage.Type.PRIVATE && message.recipient == ownProfile.userId) |
| 1 | 240 | | lastPrivateMessageRecipient = userProfileBridge.Get(message.sender).userName; |
| | 241 | |
|
| 3 | 242 | | deactivatePreviewCancellationToken.Cancel(); |
| 3 | 243 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 3 | 244 | | deactivateFadeOutCancellationToken.Cancel(); |
| 3 | 245 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 246 | |
|
| 3 | 247 | | if (currentState.Equals(ChatWindowVisualState.NONE_VISIBLE)) |
| | 248 | | { |
| 0 | 249 | | ActivatePreview(); |
| 0 | 250 | | } |
| 3 | 251 | | else if (currentState.Equals(ChatWindowVisualState.PREVIEW_MODE)) |
| | 252 | | { |
| 3 | 253 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| | 254 | | } |
| 3 | 255 | | } |
| | 256 | |
|
| | 257 | | private void HandleInputFieldSelected() |
| | 258 | | { |
| 1 | 259 | | deactivatePreviewCancellationToken.Cancel(); |
| 1 | 260 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 1 | 261 | | DeactivatePreview(); |
| 1 | 262 | | } |
| | 263 | |
|
| | 264 | | private void HandleInputFieldDeselected() |
| | 265 | | { |
| 1 | 266 | | if (View.IsFocused) |
| 0 | 267 | | return; |
| 1 | 268 | | WaitThenActivatePreview(deactivatePreviewCancellationToken.Token).Forget(); |
| 1 | 269 | | } |
| | 270 | |
|
| | 271 | | private void HandleViewFocused(bool focused) |
| | 272 | | { |
| 0 | 273 | | if (focused) |
| | 274 | | { |
| 0 | 275 | | deactivatePreviewCancellationToken.Cancel(); |
| 0 | 276 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 0 | 277 | | deactivateFadeOutCancellationToken.Cancel(); |
| 0 | 278 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| 0 | 279 | | if (currentState.Equals(ChatWindowVisualState.NONE_VISIBLE)) |
| | 280 | | { |
| 0 | 281 | | ActivatePreviewOnMessages(); |
| | 282 | | } |
| 0 | 283 | | } |
| | 284 | | else |
| | 285 | | { |
| 0 | 286 | | if (chatHudController.IsInputSelected) |
| 0 | 287 | | return; |
| | 288 | |
|
| 0 | 289 | | if (currentState.Equals(ChatWindowVisualState.INPUT_MODE)) |
| | 290 | | { |
| 0 | 291 | | WaitThenActivatePreview(deactivatePreviewCancellationToken.Token).Forget(); |
| 0 | 292 | | return; |
| | 293 | | } |
| | 294 | |
|
| 0 | 295 | | if (currentState.Equals(ChatWindowVisualState.PREVIEW_MODE)) |
| | 296 | | { |
| 0 | 297 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| | 298 | | } |
| | 299 | | } |
| 0 | 300 | | } |
| | 301 | |
|
| | 302 | | private void HandleViewClicked() |
| | 303 | | { |
| 0 | 304 | | if (currentState.Equals(ChatWindowVisualState.INPUT_MODE)) |
| 0 | 305 | | return; |
| 0 | 306 | | DeactivatePreview(); |
| 0 | 307 | | } |
| | 308 | |
|
| | 309 | | private async UniTaskVoid WaitThenActivatePreview(CancellationToken cancellationToken) |
| | 310 | | { |
| 3 | 311 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 1 | 312 | | await UniTask.SwitchToMainThread(cancellationToken); |
| 1 | 313 | | if (cancellationToken.IsCancellationRequested) return; |
| 1 | 314 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 1 | 315 | | ActivatePreview(); |
| 1 | 316 | | } |
| | 317 | |
|
| | 318 | | private async UniTaskVoid WaitThenFadeOutMessages(CancellationToken cancellationToken) |
| | 319 | | { |
| 62 | 320 | | await UniTask.Delay(30000, cancellationToken: cancellationToken); |
| 1 | 321 | | await UniTask.SwitchToMainThread(cancellationToken); |
| 1 | 322 | | if (cancellationToken.IsCancellationRequested) return; |
| 1 | 323 | | chatHudController.FadeOutMessages(); |
| 1 | 324 | | currentState = ChatWindowVisualState.NONE_VISIBLE; |
| 1 | 325 | | } |
| | 326 | |
|
| | 327 | | public void ActivatePreview() |
| | 328 | | { |
| 4 | 329 | | View.ActivatePreview(); |
| 4 | 330 | | chatHudController.ActivatePreview(); |
| 4 | 331 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 4 | 332 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| 4 | 333 | | OnPreviewModeChanged?.Invoke(true); |
| 4 | 334 | | } |
| | 335 | |
|
| | 336 | | public void ActivatePreviewOnMessages() |
| | 337 | | { |
| 0 | 338 | | chatHudController.ActivatePreview(); |
| 0 | 339 | | OnPreviewModeChanged?.Invoke(true); |
| 0 | 340 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 0 | 341 | | } |
| | 342 | |
|
| | 343 | | public void DeactivatePreview() |
| | 344 | | { |
| 3 | 345 | | deactivatePreviewCancellationToken.Cancel(); |
| 3 | 346 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 3 | 347 | | deactivateFadeOutCancellationToken.Cancel(); |
| 3 | 348 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 349 | |
|
| 3 | 350 | | View.DeactivatePreview(); |
| 3 | 351 | | chatHudController.DeactivatePreview(); |
| 3 | 352 | | OnPreviewModeChanged?.Invoke(false); |
| 3 | 353 | | currentState = ChatWindowVisualState.INPUT_MODE; |
| 3 | 354 | | } |
| | 355 | |
|
| | 356 | | private void HandleChatInputTriggered(DCLAction_Trigger action) |
| | 357 | | { |
| | 358 | | // race condition patch caused by unfocusing input field from invalid message on SendChatMessage |
| | 359 | | // chat input trigger is the same key as sending the chat message from the input field |
| 0 | 360 | | if (skipChatInputTrigger) |
| | 361 | | { |
| 0 | 362 | | skipChatInputTrigger = false; |
| 0 | 363 | | return; |
| | 364 | | } |
| 0 | 365 | | if (!View.IsActive) return; |
| 0 | 366 | | chatHudController.FocusInputField(); |
| 0 | 367 | | } |
| | 368 | | } |