| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Interface; |
| | 4 | | using SocialFeaturesAnalytics; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Threading; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | public class PrivateChatWindowController : IHUD |
| | 12 | | { |
| | 13 | | internal const int USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_INITIAL_LOAD = 30; |
| | 14 | | internal const int USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_SHOW_MORE = 10; |
| | 15 | | internal const float REQUEST_MESSAGES_TIME_OUT = 2; |
| | 16 | |
|
| 0 | 17 | | public IPrivateChatComponentView View { get; private set; } |
| | 18 | |
|
| | 19 | | private enum ChatWindowVisualState |
| | 20 | | { |
| | 21 | | NONE_VISIBLE, |
| | 22 | | INPUT_MODE, |
| | 23 | | PREVIEW_MODE |
| | 24 | | } |
| | 25 | |
|
| | 26 | | private readonly DataStore dataStore; |
| | 27 | | private readonly IUserProfileBridge userProfileBridge; |
| | 28 | | private readonly IChatController chatController; |
| | 29 | | private readonly IFriendsController friendsController; |
| | 30 | | private readonly InputAction_Trigger closeWindowTrigger; |
| | 31 | | private readonly ISocialAnalytics socialAnalytics; |
| | 32 | | private readonly IMouseCatcher mouseCatcher; |
| | 33 | | private readonly InputAction_Trigger toggleChatTrigger; |
| | 34 | | private ChatHUDController chatHudController; |
| | 35 | | private UserProfile conversationProfile; |
| | 36 | | private float lastRequestTime; |
| | 37 | | private ChatWindowVisualState currentState; |
| 19 | 38 | | private CancellationTokenSource deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 19 | 39 | | private CancellationTokenSource deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| 19 | 40 | | private CancellationTokenSource markMessagesAsSeenCancellationToken = new CancellationTokenSource(); |
| | 41 | | private bool shouldRequestMessages; |
| | 42 | |
|
| 19 | 43 | | internal string ConversationUserId { get; set; } = string.Empty; |
| | 44 | |
|
| | 45 | | public event Action OnPressBack; |
| | 46 | | public event Action OnClosed; |
| | 47 | | public event Action<bool> OnPreviewModeChanged; |
| | 48 | |
|
| 19 | 49 | | public PrivateChatWindowController(DataStore dataStore, |
| | 50 | | IUserProfileBridge userProfileBridge, |
| | 51 | | IChatController chatController, |
| | 52 | | IFriendsController friendsController, |
| | 53 | | InputAction_Trigger closeWindowTrigger, |
| | 54 | | ISocialAnalytics socialAnalytics, |
| | 55 | | IMouseCatcher mouseCatcher, |
| | 56 | | InputAction_Trigger toggleChatTrigger) |
| | 57 | | { |
| 19 | 58 | | this.dataStore = dataStore; |
| 19 | 59 | | this.userProfileBridge = userProfileBridge; |
| 19 | 60 | | this.chatController = chatController; |
| 19 | 61 | | this.friendsController = friendsController; |
| 19 | 62 | | this.closeWindowTrigger = closeWindowTrigger; |
| 19 | 63 | | this.socialAnalytics = socialAnalytics; |
| 19 | 64 | | this.mouseCatcher = mouseCatcher; |
| 19 | 65 | | this.toggleChatTrigger = toggleChatTrigger; |
| 19 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Initialize(IPrivateChatComponentView view = null) |
| | 69 | | { |
| 20 | 70 | | view ??= PrivateChatWindowComponentView.Create(); |
| 20 | 71 | | View = view; |
| 20 | 72 | | View.Initialize(friendsController, socialAnalytics); |
| 20 | 73 | | view.OnPressBack -= HandlePressBack; |
| 20 | 74 | | view.OnPressBack += HandlePressBack; |
| 20 | 75 | | view.OnClose -= Hide; |
| 20 | 76 | | view.OnClose += Hide; |
| 20 | 77 | | view.OnMinimize += MinimizeView; |
| 20 | 78 | | view.OnUnfriend += Unfriend; |
| 20 | 79 | | view.OnFocused += HandleViewFocused; |
| 20 | 80 | | view.OnRequireMoreMessages += RequestOldConversations; |
| 20 | 81 | | view.OnClickOverWindow += HandleViewClicked; |
| | 82 | |
|
| 20 | 83 | | closeWindowTrigger.OnTriggered -= HandleCloseInputTriggered; |
| 20 | 84 | | closeWindowTrigger.OnTriggered += HandleCloseInputTriggered; |
| | 85 | |
|
| 20 | 86 | | chatHudController = new ChatHUDController(dataStore, userProfileBridge, false); |
| 20 | 87 | | chatHudController.Initialize(view.ChatHUD); |
| 20 | 88 | | chatHudController.OnInputFieldSelected -= HandleInputFieldSelected; |
| 20 | 89 | | chatHudController.OnInputFieldSelected += HandleInputFieldSelected; |
| 20 | 90 | | chatHudController.OnInputFieldDeselected -= HandleInputFieldDeselected; |
| 20 | 91 | | chatHudController.OnInputFieldDeselected += HandleInputFieldDeselected; |
| 20 | 92 | | chatHudController.OnSendMessage += HandleSendChatMessage; |
| | 93 | |
|
| 20 | 94 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 20 | 95 | | chatController.OnAddMessage += HandleMessageReceived; |
| | 96 | |
|
| 20 | 97 | | if (mouseCatcher != null) |
| 20 | 98 | | mouseCatcher.OnMouseLock += ActivatePreview; |
| | 99 | |
|
| 20 | 100 | | toggleChatTrigger.OnTriggered += HandleChatInputTriggered; |
| | 101 | |
|
| 20 | 102 | | currentState = ChatWindowVisualState.INPUT_MODE; |
| 20 | 103 | | } |
| | 104 | |
|
| | 105 | | public void Setup(string newConversationUserId) |
| | 106 | | { |
| 17 | 107 | | if (string.IsNullOrEmpty(newConversationUserId) || newConversationUserId == ConversationUserId) |
| 1 | 108 | | return; |
| | 109 | |
|
| 16 | 110 | | var newConversationUserProfile = userProfileBridge.Get(newConversationUserId); |
| | 111 | |
|
| 16 | 112 | | ConversationUserId = newConversationUserId; |
| 16 | 113 | | conversationProfile = newConversationUserProfile; |
| 16 | 114 | | chatHudController.ClearAllEntries(); |
| 16 | 115 | | shouldRequestMessages = true; |
| 16 | 116 | | } |
| | 117 | |
|
| | 118 | | public void SetVisibility(bool visible) |
| | 119 | | { |
| 19 | 120 | | if (View.IsActive == visible) |
| 3 | 121 | | return; |
| | 122 | |
|
| 16 | 123 | | if (visible) |
| | 124 | | { |
| 12 | 125 | | View?.SetLoadingMessagesActive(false); |
| 12 | 126 | | View?.SetOldMessagesLoadingActive(false); |
| | 127 | |
|
| 12 | 128 | | if (conversationProfile != null) |
| | 129 | | { |
| 11 | 130 | | var userStatus = friendsController.GetUserStatus(ConversationUserId); |
| 11 | 131 | | View.Setup(conversationProfile, |
| | 132 | | userStatus.presence == PresenceStatus.ONLINE, |
| | 133 | | userProfileBridge.GetOwn().IsBlocked(ConversationUserId)); |
| | 134 | |
|
| 11 | 135 | | if (shouldRequestMessages) |
| | 136 | | { |
| 10 | 137 | | RequestPrivateMessages( |
| | 138 | | ConversationUserId, |
| | 139 | | USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_INITIAL_LOAD, |
| | 140 | | null); |
| | 141 | |
|
| 10 | 142 | | shouldRequestMessages = false; |
| | 143 | | } |
| | 144 | | } |
| | 145 | |
|
| 12 | 146 | | View.Show(); |
| 12 | 147 | | Focus(); |
| 12 | 148 | | } |
| | 149 | | else |
| | 150 | | { |
| 4 | 151 | | chatHudController.UnfocusInputField(); |
| 4 | 152 | | View.Hide(); |
| | 153 | | } |
| 4 | 154 | | } |
| | 155 | |
|
| | 156 | | public void Focus() |
| | 157 | | { |
| 12 | 158 | | chatHudController.FocusInputField(); |
| 12 | 159 | | MarkUserChatMessagesAsRead(); |
| 12 | 160 | | } |
| | 161 | |
|
| | 162 | | public void Dispose() |
| | 163 | | { |
| 19 | 164 | | if (chatHudController != null) |
| | 165 | | { |
| 19 | 166 | | chatHudController.OnInputFieldSelected -= HandleInputFieldSelected; |
| 19 | 167 | | chatHudController.OnInputFieldDeselected -= HandleInputFieldDeselected; |
| | 168 | | } |
| | 169 | |
|
| 19 | 170 | | if (chatController != null) |
| 19 | 171 | | chatController.OnAddMessage -= HandleMessageReceived; |
| | 172 | |
|
| 19 | 173 | | if (mouseCatcher != null) |
| 19 | 174 | | mouseCatcher.OnMouseLock -= ActivatePreview; |
| | 175 | |
|
| 19 | 176 | | toggleChatTrigger.OnTriggered -= HandleChatInputTriggered; |
| | 177 | |
|
| 19 | 178 | | if (View != null) |
| | 179 | | { |
| 19 | 180 | | View.OnPressBack -= HandlePressBack; |
| 19 | 181 | | View.OnClose -= Hide; |
| 19 | 182 | | View.OnMinimize -= MinimizeView; |
| 19 | 183 | | View.OnUnfriend -= Unfriend; |
| 19 | 184 | | View.OnFocused -= HandleViewFocused; |
| 19 | 185 | | View.OnRequireMoreMessages -= RequestOldConversations; |
| 19 | 186 | | View.OnClickOverWindow -= HandleViewClicked; |
| 19 | 187 | | View.Dispose(); |
| | 188 | | } |
| 19 | 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 | | chatHudController.ResetInputField(true); |
| 0 | 212 | | ActivatePreview(); |
| 0 | 213 | | return; |
| | 214 | | } |
| | 215 | |
|
| | 216 | | // If Kernel allowed for private messages without the whisper param we could avoid this line |
| 1 | 217 | | message.body = $"/w {message.recipient} {message.body}"; |
| | 218 | |
|
| 1 | 219 | | chatController.Send(message); |
| 1 | 220 | | } |
| | 221 | |
|
| 0 | 222 | | private void HandleCloseInputTriggered(DCLAction_Trigger action) => Hide(); |
| | 223 | |
|
| 0 | 224 | | private void MinimizeView() => SetVisibility(false); |
| | 225 | |
|
| | 226 | | private void HandleMessageReceived(ChatMessage message) |
| | 227 | | { |
| 3 | 228 | | if (!IsMessageFomCurrentConversation(message)) |
| 0 | 229 | | return; |
| | 230 | |
|
| 3 | 231 | | chatHudController.AddChatMessage(message, limitMaxEntries: false); |
| | 232 | |
|
| 3 | 233 | | if (View.IsActive) |
| | 234 | | { |
| 0 | 235 | | markMessagesAsSeenCancellationToken.Cancel(); |
| 0 | 236 | | markMessagesAsSeenCancellationToken = new CancellationTokenSource(); |
| | 237 | | // since there could be many messages coming in a row, avoid making the call instantly for each message |
| | 238 | | // instead make just one call after the iteration finishes |
| 0 | 239 | | MarkMessagesAsSeenDelayed(markMessagesAsSeenCancellationToken.Token).Forget(); |
| | 240 | | } |
| | 241 | |
|
| 3 | 242 | | View?.SetLoadingMessagesActive(false); |
| 3 | 243 | | View?.SetOldMessagesLoadingActive(false); |
| | 244 | |
|
| 3 | 245 | | deactivatePreviewCancellationToken.Cancel(); |
| 3 | 246 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 3 | 247 | | deactivateFadeOutCancellationToken.Cancel(); |
| 3 | 248 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 249 | |
|
| 3 | 250 | | switch (currentState) |
| | 251 | | { |
| | 252 | | case ChatWindowVisualState.NONE_VISIBLE: |
| 0 | 253 | | ActivatePreview(); |
| 0 | 254 | | break; |
| | 255 | | case ChatWindowVisualState.PREVIEW_MODE: |
| 0 | 256 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| | 257 | | break; |
| | 258 | | } |
| 0 | 259 | | } |
| | 260 | |
|
| | 261 | | private void Hide() |
| | 262 | | { |
| 1 | 263 | | SetVisibility(false); |
| 1 | 264 | | OnClosed?.Invoke(); |
| 0 | 265 | | } |
| | 266 | |
|
| 1 | 267 | | private void HandlePressBack() => OnPressBack?.Invoke(); |
| | 268 | |
|
| | 269 | | private void Unfriend(string friendId) |
| | 270 | | { |
| 0 | 271 | | friendsController.RemoveFriend(friendId); |
| 0 | 272 | | Hide(); |
| 0 | 273 | | } |
| | 274 | |
|
| | 275 | | private bool IsMessageFomCurrentConversation(ChatMessage message) |
| | 276 | | { |
| 3 | 277 | | return message.messageType == ChatMessage.Type.PRIVATE && |
| | 278 | | (message.sender == ConversationUserId || message.recipient == ConversationUserId); |
| | 279 | | } |
| | 280 | |
|
| | 281 | | private void MarkUserChatMessagesAsRead() => |
| 13 | 282 | | chatController.MarkMessagesAsSeen(ConversationUserId); |
| | 283 | |
|
| | 284 | | private async UniTask MarkMessagesAsSeenDelayed(CancellationToken cancellationToken) |
| | 285 | | { |
| 0 | 286 | | await UniTask.NextFrame(cancellationToken); |
| 0 | 287 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 288 | | MarkUserChatMessagesAsRead(); |
| 0 | 289 | | } |
| | 290 | |
|
| | 291 | | private void HandleInputFieldSelected() |
| | 292 | | { |
| 1 | 293 | | deactivatePreviewCancellationToken.Cancel(); |
| 1 | 294 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 1 | 295 | | DeactivatePreview(); |
| | 296 | | // The messages from 'conversationUserId' are marked as read if the player clicks on the input field of the priv |
| 1 | 297 | | MarkUserChatMessagesAsRead(); |
| 1 | 298 | | } |
| | 299 | |
|
| | 300 | | private void HandleInputFieldDeselected() |
| | 301 | | { |
| 1 | 302 | | if (View.IsFocused) |
| 0 | 303 | | return; |
| 1 | 304 | | WaitThenActivatePreview(deactivatePreviewCancellationToken.Token).Forget(); |
| 1 | 305 | | } |
| | 306 | |
|
| | 307 | | private void HandleViewFocused(bool focused) |
| | 308 | | { |
| 1 | 309 | | if (focused) |
| | 310 | | { |
| 0 | 311 | | deactivatePreviewCancellationToken.Cancel(); |
| 0 | 312 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 0 | 313 | | deactivateFadeOutCancellationToken.Cancel(); |
| 0 | 314 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 315 | |
|
| 0 | 316 | | if (currentState.Equals(ChatWindowVisualState.NONE_VISIBLE)) |
| | 317 | | { |
| 0 | 318 | | ActivatePreviewOnMessages(); |
| | 319 | | } |
| 0 | 320 | | } |
| | 321 | | else |
| | 322 | | { |
| 1 | 323 | | if (chatHudController.IsInputSelected) |
| 0 | 324 | | return; |
| | 325 | |
|
| 1 | 326 | | if (currentState.Equals(ChatWindowVisualState.INPUT_MODE)) |
| | 327 | | { |
| 1 | 328 | | WaitThenActivatePreview(deactivatePreviewCancellationToken.Token).Forget(); |
| 1 | 329 | | return; |
| | 330 | | } |
| | 331 | |
|
| 0 | 332 | | if (currentState.Equals(ChatWindowVisualState.PREVIEW_MODE)) |
| | 333 | | { |
| 0 | 334 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| | 335 | | } |
| | 336 | | } |
| 0 | 337 | | } |
| | 338 | |
|
| | 339 | | private void HandleViewClicked() |
| | 340 | | { |
| 0 | 341 | | if (currentState.Equals(ChatWindowVisualState.INPUT_MODE)) |
| 0 | 342 | | return; |
| 0 | 343 | | DeactivatePreview(); |
| 0 | 344 | | } |
| | 345 | |
|
| | 346 | | private async UniTaskVoid WaitThenActivatePreview(CancellationToken cancellationToken) |
| | 347 | | { |
| 6 | 348 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 2 | 349 | | await UniTask.SwitchToMainThread(cancellationToken); |
| 2 | 350 | | if (cancellationToken.IsCancellationRequested) |
| 0 | 351 | | return; |
| 2 | 352 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 2 | 353 | | ActivatePreview(); |
| 2 | 354 | | } |
| | 355 | |
|
| | 356 | | private async UniTaskVoid WaitThenFadeOutMessages(CancellationToken cancellationToken) |
| | 357 | | { |
| 12 | 358 | | await UniTask.Delay(30000, cancellationToken: cancellationToken); |
| 4 | 359 | | await UniTask.SwitchToMainThread(cancellationToken); |
| 4 | 360 | | if (cancellationToken.IsCancellationRequested) |
| 0 | 361 | | return; |
| 4 | 362 | | chatHudController.FadeOutMessages(); |
| 4 | 363 | | currentState = ChatWindowVisualState.NONE_VISIBLE; |
| 4 | 364 | | } |
| | 365 | |
|
| | 366 | | public void ActivatePreview() |
| | 367 | | { |
| 4 | 368 | | View.ActivatePreview(); |
| 4 | 369 | | chatHudController.ActivatePreview(); |
| 4 | 370 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 4 | 371 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| 4 | 372 | | OnPreviewModeChanged?.Invoke(true); |
| 3 | 373 | | } |
| | 374 | |
|
| | 375 | | public void ActivatePreviewOnMessages() |
| | 376 | | { |
| 0 | 377 | | chatHudController.ActivatePreview(); |
| 0 | 378 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 0 | 379 | | OnPreviewModeChanged?.Invoke(true); |
| 0 | 380 | | } |
| | 381 | |
|
| | 382 | | public void DeactivatePreview() |
| | 383 | | { |
| 2 | 384 | | deactivatePreviewCancellationToken.Cancel(); |
| 2 | 385 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 2 | 386 | | deactivateFadeOutCancellationToken.Cancel(); |
| 2 | 387 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 388 | |
|
| 2 | 389 | | View.DeactivatePreview(); |
| 2 | 390 | | chatHudController.DeactivatePreview(); |
| 2 | 391 | | OnPreviewModeChanged?.Invoke(false); |
| 2 | 392 | | currentState = ChatWindowVisualState.INPUT_MODE; |
| 2 | 393 | | } |
| | 394 | |
|
| | 395 | | private void HandleChatInputTriggered(DCLAction_Trigger action) |
| | 396 | | { |
| 0 | 397 | | if (!View.IsActive) |
| 0 | 398 | | return; |
| 0 | 399 | | chatHudController.FocusInputField(); |
| 0 | 400 | | } |
| | 401 | |
|
| | 402 | | internal void RequestPrivateMessages(string userId, int limit, string fromMessageId) |
| | 403 | | { |
| 11 | 404 | | View?.SetLoadingMessagesActive(true); |
| 11 | 405 | | chatController.GetPrivateMessages(userId, limit, fromMessageId); |
| 11 | 406 | | WaitForRequestTimeOutThenHideLoadingFeedback().Forget(); |
| 11 | 407 | | } |
| | 408 | |
|
| | 409 | | internal void RequestOldConversations() |
| | 410 | | { |
| 1 | 411 | | if (IsLoadingMessages()) return; |
| | 412 | |
|
| 1 | 413 | | var currentPrivateMessages = chatController.GetPrivateAllocatedEntriesByUser(ConversationUserId); |
| | 414 | |
|
| 1 | 415 | | var oldestMessageId = currentPrivateMessages |
| 3 | 416 | | .OrderBy(x => x.timestamp) |
| 1 | 417 | | .Select(x => x.messageId) |
| | 418 | | .FirstOrDefault(); |
| | 419 | |
|
| 1 | 420 | | chatController.GetPrivateMessages( |
| | 421 | | ConversationUserId, |
| | 422 | | USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_SHOW_MORE, |
| | 423 | | oldestMessageId); |
| | 424 | |
|
| 1 | 425 | | lastRequestTime = Time.realtimeSinceStartup; |
| 1 | 426 | | View?.SetOldMessagesLoadingActive(true); |
| 1 | 427 | | WaitForRequestTimeOutThenHideLoadingFeedback().Forget(); |
| 1 | 428 | | } |
| | 429 | |
|
| | 430 | | private bool IsLoadingMessages() => |
| 1 | 431 | | Time.realtimeSinceStartup - lastRequestTime < REQUEST_MESSAGES_TIME_OUT; |
| | 432 | |
|
| | 433 | | private async UniTaskVoid WaitForRequestTimeOutThenHideLoadingFeedback() |
| | 434 | | { |
| 12 | 435 | | lastRequestTime = Time.realtimeSinceStartup; |
| | 436 | |
|
| 2928 | 437 | | await UniTask.WaitUntil(() => Time.realtimeSinceStartup - lastRequestTime > REQUEST_MESSAGES_TIME_OUT); |
| | 438 | |
|
| 12 | 439 | | View?.SetLoadingMessagesActive(false); |
| 12 | 440 | | View?.SetOldMessagesLoadingActive(false); |
| 12 | 441 | | } |
| | 442 | | } |