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