| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL.Interface; |
| | 6 | | using DCL.Social.Chat.Mentions; |
| | 7 | | using DCL.Social.Friends; |
| | 8 | | using DCLServices.CopyPaste.Analytics; |
| | 9 | | using SocialFeaturesAnalytics; |
| | 10 | | using UnityEngine; |
| | 11 | |
|
| | 12 | | namespace DCL.Social.Chat |
| | 13 | | { |
| | 14 | | public class PrivateChatWindowController : IHUD |
| | 15 | | { |
| | 16 | | private const int USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_INITIAL_LOAD = 30; |
| | 17 | | private const float REQUEST_MESSAGES_TIME_OUT = 2; |
| | 18 | | internal const int USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_SHOW_MORE = 10; |
| | 19 | |
|
| 417 | 20 | | public IPrivateChatComponentView View { get; private set; } |
| | 21 | |
|
| | 22 | | private readonly DataStore dataStore; |
| | 23 | | private readonly IUserProfileBridge userProfileBridge; |
| | 24 | | private readonly IChatController chatController; |
| | 25 | | private readonly IFriendsController friendsController; |
| | 26 | | private readonly ISocialAnalytics socialAnalytics; |
| | 27 | | private readonly IMouseCatcher mouseCatcher; |
| | 28 | | private readonly IChatMentionSuggestionProvider chatMentionSuggestionProvider; |
| | 29 | | private readonly IClipboard clipboard; |
| | 30 | | private readonly ICopyPasteAnalyticsService copyPasteAnalyticsService; |
| | 31 | | private ChatHUDController chatHudController; |
| | 32 | | private UserProfile conversationProfile; |
| | 33 | | private float lastRequestTime; |
| 22 | 34 | | private CancellationTokenSource deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 35 | | private bool shouldRequestMessages; |
| 22 | 36 | | private ulong oldestTimestamp = ulong.MaxValue; |
| | 37 | | private string oldestMessageId; |
| | 38 | | private string conversationUserId; |
| 32 | 39 | | private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| 0 | 40 | | private UserProfile ownUserProfile => userProfileBridge.GetOwn(); |
| | 41 | |
|
| | 42 | | public event Action OnBack; |
| | 43 | | public event Action OnClosed; |
| | 44 | |
|
| 22 | 45 | | public PrivateChatWindowController(DataStore dataStore, |
| | 46 | | IUserProfileBridge userProfileBridge, |
| | 47 | | IChatController chatController, |
| | 48 | | IFriendsController friendsController, |
| | 49 | | ISocialAnalytics socialAnalytics, |
| | 50 | | IMouseCatcher mouseCatcher, |
| | 51 | | IChatMentionSuggestionProvider chatMentionSuggestionProvider, |
| | 52 | | IClipboard clipboard, |
| | 53 | | ICopyPasteAnalyticsService copyPasteAnalyticsService) |
| | 54 | | { |
| 22 | 55 | | this.dataStore = dataStore; |
| 22 | 56 | | this.userProfileBridge = userProfileBridge; |
| 22 | 57 | | this.chatController = chatController; |
| 22 | 58 | | this.friendsController = friendsController; |
| 22 | 59 | | this.socialAnalytics = socialAnalytics; |
| 22 | 60 | | this.mouseCatcher = mouseCatcher; |
| 22 | 61 | | this.chatMentionSuggestionProvider = chatMentionSuggestionProvider; |
| 22 | 62 | | this.clipboard = clipboard; |
| 22 | 63 | | this.copyPasteAnalyticsService = copyPasteAnalyticsService; |
| 22 | 64 | | } |
| | 65 | |
|
| | 66 | | public void Initialize(IPrivateChatComponentView view) |
| | 67 | | { |
| 23 | 68 | | View = view; |
| 23 | 69 | | View.Initialize(friendsController, socialAnalytics); |
| 23 | 70 | | view.OnPressBack -= HandlePressBack; |
| 23 | 71 | | view.OnPressBack += HandlePressBack; |
| 23 | 72 | | view.OnClose -= Hide; |
| 23 | 73 | | view.OnClose += Hide; |
| 23 | 74 | | view.OnMinimize += MinimizeView; |
| 23 | 75 | | view.OnUnfriend += Unfriend; |
| | 76 | |
|
| 23 | 77 | | if (mouseCatcher != null) |
| 22 | 78 | | mouseCatcher.OnMouseLock += Hide; |
| | 79 | |
|
| 23 | 80 | | view.OnRequireMoreMessages += RequestOldConversations; |
| 23 | 81 | | view.ChatHUD.OnUnblockUser += UnblockUser; |
| | 82 | |
|
| 23 | 83 | | dataStore.mentions.someoneMentionedFromContextMenu.OnChange += SomeoneMentionedFromContextMenu; |
| | 84 | |
|
| 23 | 85 | | chatHudController = new ChatHUDController(dataStore, userProfileBridge, false, |
| | 86 | | async (name, count, ct) => |
| | 87 | | { |
| 3 | 88 | | return await chatMentionSuggestionProvider.GetProfilesStartingWith(name, count, new[] |
| | 89 | | { |
| | 90 | | userProfileBridge.GetOwn(), |
| | 91 | | userProfileBridge.Get(conversationUserId) |
| | 92 | | }, ct); |
| 3 | 93 | | }, |
| | 94 | | socialAnalytics, |
| | 95 | | chatController, |
| | 96 | | clipboard, |
| | 97 | | copyPasteAnalyticsService); |
| | 98 | |
|
| 23 | 99 | | chatHudController.Initialize(view.ChatHUD); |
| 23 | 100 | | chatHudController.SortingStrategy = new ChatEntrySortingByTimestamp(); |
| 23 | 101 | | chatHudController.OnInputFieldSelected += HandleInputFieldSelected; |
| 23 | 102 | | chatHudController.OnSendMessage += HandleSendChatMessage; |
| 23 | 103 | | chatHudController.OnMessageSentBlockedBySpam += HandleMessageBlockedBySpam; |
| | 104 | |
|
| 23 | 105 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 23 | 106 | | chatController.OnAddMessage += HandleMessageReceived; |
| 23 | 107 | | } |
| | 108 | |
|
| | 109 | | public void Setup(string newConversationUserId) |
| | 110 | | { |
| 20 | 111 | | if (string.IsNullOrEmpty(newConversationUserId) || newConversationUserId == conversationUserId) |
| 1 | 112 | | return; |
| | 113 | |
|
| 19 | 114 | | var newConversationUserProfile = userProfileBridge.Get(newConversationUserId); |
| | 115 | |
|
| 19 | 116 | | conversationUserId = newConversationUserId; |
| 19 | 117 | | conversationProfile = newConversationUserProfile; |
| 19 | 118 | | chatHudController.ClearAllEntries(); |
| 19 | 119 | | View.ChatHUD.SetConversationUserId(newConversationUserId); |
| 19 | 120 | | shouldRequestMessages = true; |
| 19 | 121 | | } |
| | 122 | |
|
| | 123 | | public void SetVisibility(bool visible) |
| | 124 | | { |
| 19 | 125 | | if (View.IsActive == visible) |
| 3 | 126 | | return; |
| | 127 | |
|
| 16 | 128 | | SetVisiblePanelList(visible); |
| 16 | 129 | | chatHudController.SetVisibility(visible); |
| 16 | 130 | | dataStore.HUDs.chatInputVisible.Set(visible); |
| | 131 | |
|
| 16 | 132 | | if (visible) |
| | 133 | | { |
| 11 | 134 | | View?.SetLoadingMessagesActive(false); |
| 11 | 135 | | View?.SetOldMessagesLoadingActive(false); |
| | 136 | |
|
| 11 | 137 | | if (conversationProfile != null) |
| | 138 | | { |
| 10 | 139 | | var userStatus = friendsController.GetUserStatus(conversationUserId); |
| | 140 | |
|
| 10 | 141 | | View.Setup(conversationProfile, |
| | 142 | | userStatus.presence == PresenceStatus.ONLINE, |
| | 143 | | userProfileBridge.GetOwn().IsBlocked(conversationUserId)); |
| | 144 | |
|
| 10 | 145 | | if (shouldRequestMessages) |
| | 146 | | { |
| 9 | 147 | | ResetPagination(); |
| | 148 | |
|
| 9 | 149 | | RequestPrivateMessages( |
| | 150 | | conversationUserId, |
| | 151 | | USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_INITIAL_LOAD, |
| | 152 | | null); |
| | 153 | |
|
| 9 | 154 | | shouldRequestMessages = false; |
| | 155 | | } |
| | 156 | | } |
| | 157 | |
|
| 11 | 158 | | View.Show(); |
| 11 | 159 | | Focus(); |
| | 160 | | } |
| | 161 | | else |
| | 162 | | { |
| 5 | 163 | | chatHudController.UnfocusInputField(); |
| 5 | 164 | | View.Hide(); |
| | 165 | | } |
| 5 | 166 | | } |
| | 167 | |
|
| | 168 | | public void Dispose() |
| | 169 | | { |
| 22 | 170 | | if (chatHudController != null) |
| | 171 | | { |
| 22 | 172 | | chatHudController.OnInputFieldSelected -= HandleInputFieldSelected; |
| 22 | 173 | | chatHudController.OnSendMessage -= HandleSendChatMessage; |
| 22 | 174 | | chatHudController.OnMessageSentBlockedBySpam -= HandleMessageBlockedBySpam; |
| 22 | 175 | | chatHudController.Dispose(); |
| | 176 | | } |
| | 177 | |
|
| 22 | 178 | | if (chatController != null) |
| 22 | 179 | | chatController.OnAddMessage -= HandleMessageReceived; |
| | 180 | |
|
| 22 | 181 | | if (mouseCatcher != null) |
| 21 | 182 | | mouseCatcher.OnMouseLock -= Hide; |
| | 183 | |
|
| 22 | 184 | | if (View != null) |
| | 185 | | { |
| 22 | 186 | | View.OnPressBack -= HandlePressBack; |
| 22 | 187 | | View.OnClose -= Hide; |
| 22 | 188 | | View.OnMinimize -= MinimizeView; |
| 22 | 189 | | View.OnUnfriend -= Unfriend; |
| 22 | 190 | | View.OnFocused -= HandleViewFocused; |
| 22 | 191 | | View.OnRequireMoreMessages -= RequestOldConversations; |
| | 192 | |
|
| 44 | 193 | | if (View.ChatHUD != null) View.ChatHUD.OnUnblockUser -= UnblockUser; |
| | 194 | |
|
| 22 | 195 | | View.Dispose(); |
| | 196 | | } |
| | 197 | |
|
| 22 | 198 | | dataStore.mentions.someoneMentionedFromContextMenu.OnChange -= SomeoneMentionedFromContextMenu; |
| 22 | 199 | | } |
| | 200 | |
|
| | 201 | | private void HandleSendChatMessage(ChatMessage message) |
| | 202 | | { |
| 1 | 203 | | if (string.IsNullOrEmpty(conversationProfile.userName)) |
| 0 | 204 | | return; |
| | 205 | |
|
| 1 | 206 | | message.messageType = ChatMessage.Type.PRIVATE; |
| 1 | 207 | | message.recipient = conversationProfile.userName; |
| 1 | 208 | | message.receiverId = conversationProfile.userId; |
| | 209 | |
|
| 1 | 210 | | bool isValidMessage = !string.IsNullOrEmpty(message.body) |
| | 211 | | && !string.IsNullOrWhiteSpace(message.body) |
| | 212 | | && !string.IsNullOrEmpty(message.recipient); |
| | 213 | |
|
| 1 | 214 | | if (isValidMessage) |
| | 215 | | { |
| 1 | 216 | | chatHudController.ResetInputField(); |
| 1 | 217 | | chatHudController.FocusInputField(); |
| | 218 | | } |
| | 219 | | else |
| | 220 | | { |
| 0 | 221 | | SetVisibility(false); |
| 0 | 222 | | OnClosed?.Invoke(); |
| 0 | 223 | | return; |
| | 224 | | } |
| | 225 | |
|
| | 226 | | // If Kernel allowed for private messages without the whisper param we could avoid this line |
| 1 | 227 | | message.body = $"/w {message.recipient} {message.body}"; |
| | 228 | |
|
| 1 | 229 | | chatController.Send(message); |
| 1 | 230 | | } |
| | 231 | |
|
| | 232 | | private void MinimizeView() => |
| 0 | 233 | | SetVisibility(false); |
| | 234 | |
|
| | 235 | | private void HandleMessageReceived(ChatMessage[] messages) |
| | 236 | | { |
| 6 | 237 | | var messageLogUpdated = false; |
| | 238 | |
|
| 6 | 239 | | var ownPlayerAlreadyMentioned = false; |
| | 240 | |
|
| 26 | 241 | | foreach (var message in messages) |
| | 242 | | { |
| 7 | 243 | | if (!ownPlayerAlreadyMentioned) |
| 7 | 244 | | ownPlayerAlreadyMentioned = CheckOwnPlayerMentionInDMs(message); |
| | 245 | |
|
| 7 | 246 | | if (!IsMessageFomCurrentConversation(message)) continue; |
| | 247 | |
|
| 7 | 248 | | chatHudController.SetChatMessage(message, limitMaxEntries: false); |
| | 249 | |
|
| 7 | 250 | | if (message.timestamp < oldestTimestamp) |
| | 251 | | { |
| 4 | 252 | | oldestTimestamp = message.timestamp; |
| 4 | 253 | | oldestMessageId = message.messageId; |
| | 254 | | } |
| | 255 | |
|
| 7 | 256 | | View?.SetLoadingMessagesActive(false); |
| 7 | 257 | | View?.SetOldMessagesLoadingActive(false); |
| | 258 | |
|
| 7 | 259 | | messageLogUpdated = true; |
| | 260 | | } |
| | 261 | |
|
| 6 | 262 | | deactivateFadeOutCancellationToken.Cancel(); |
| 6 | 263 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 264 | |
|
| 6 | 265 | | if (View.IsActive && messageLogUpdated) |
| 1 | 266 | | MarkUserChatMessagesAsRead(); |
| 6 | 267 | | } |
| | 268 | |
|
| | 269 | | private bool CheckOwnPlayerMentionInDMs(ChatMessage message) |
| | 270 | | { |
| 7 | 271 | | var ownUserProfile = userProfileBridge.GetOwn(); |
| | 272 | |
|
| 7 | 273 | | if (message.sender == ownUserProfile.userId || |
| | 274 | | (message.sender == conversationUserId && View.IsActive) || |
| | 275 | | message.messageType != ChatMessage.Type.PRIVATE || |
| | 276 | | !MentionsUtils.IsUserMentionedInText(ownUserProfile.userName, message.body)) |
| 6 | 277 | | return false; |
| | 278 | |
|
| 1 | 279 | | dataStore.mentions.ownPlayerMentionedInDM.Set(message.sender, true); |
| 1 | 280 | | return true; |
| | 281 | | } |
| | 282 | |
|
| | 283 | | private void Hide() |
| | 284 | | { |
| 2 | 285 | | SetVisibility(false); |
| 2 | 286 | | OnClosed?.Invoke(); |
| 0 | 287 | | } |
| | 288 | |
|
| | 289 | | private void Show() |
| | 290 | | { |
| 0 | 291 | | SetVisibility(true); |
| 0 | 292 | | } |
| | 293 | |
|
| | 294 | | private void HandlePressBack() => |
| 1 | 295 | | OnBack?.Invoke(); |
| | 296 | |
|
| | 297 | | private void Unfriend(string friendId) |
| | 298 | | { |
| 0 | 299 | | Hide(); |
| 0 | 300 | | } |
| | 301 | |
|
| | 302 | | private bool IsMessageFomCurrentConversation(ChatMessage message) |
| | 303 | | { |
| 7 | 304 | | return message.messageType == ChatMessage.Type.PRIVATE && |
| | 305 | | (message.sender == conversationUserId || message.recipient == conversationUserId); |
| | 306 | | } |
| | 307 | |
|
| | 308 | | private void MarkUserChatMessagesAsRead() => |
| 12 | 309 | | chatController.MarkMessagesAsSeen(conversationUserId); |
| | 310 | |
|
| | 311 | | private void HandleInputFieldSelected() |
| | 312 | | { |
| 0 | 313 | | Show(); |
| | 314 | |
|
| | 315 | | // The messages from 'conversationUserId' are marked as read if the player clicks on the input field of the |
| | 316 | | //MarkUserChatMessagesAsRead(); |
| 0 | 317 | | } |
| | 318 | |
|
| | 319 | | private void HandleViewFocused(bool focused) |
| | 320 | | { |
| 0 | 321 | | if (focused) |
| | 322 | | { |
| 0 | 323 | | deactivateFadeOutCancellationToken.Cancel(); |
| 0 | 324 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 325 | | } |
| 0 | 326 | | } |
| | 327 | |
|
| | 328 | | private void SetVisiblePanelList(bool visible) |
| | 329 | | { |
| 16 | 330 | | HashSet<string> newSet = visibleTaskbarPanels.Get(); |
| | 331 | |
|
| 16 | 332 | | if (visible) |
| 11 | 333 | | newSet.Add("PrivateChatChannel"); |
| | 334 | | else |
| 5 | 335 | | newSet.Remove("PrivateChatChannel"); |
| | 336 | |
|
| 16 | 337 | | visibleTaskbarPanels.Set(newSet, true); |
| 16 | 338 | | } |
| | 339 | |
|
| | 340 | | internal void RequestPrivateMessages(string userId, int limit, string fromMessageId) |
| | 341 | | { |
| 10 | 342 | | View?.SetLoadingMessagesActive(true); |
| 10 | 343 | | chatController.GetPrivateMessages(userId, limit, fromMessageId); |
| 10 | 344 | | WaitForRequestTimeOutThenHideLoadingFeedback().Forget(); |
| 10 | 345 | | } |
| | 346 | |
|
| | 347 | | internal void RequestOldConversations() |
| | 348 | | { |
| 1 | 349 | | if (IsLoadingMessages()) return; |
| | 350 | |
|
| 1 | 351 | | chatController.GetPrivateMessages( |
| | 352 | | conversationUserId, |
| | 353 | | USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_SHOW_MORE, |
| | 354 | | oldestMessageId); |
| | 355 | |
|
| 1 | 356 | | lastRequestTime = Time.realtimeSinceStartup; |
| 1 | 357 | | View?.SetOldMessagesLoadingActive(true); |
| 1 | 358 | | WaitForRequestTimeOutThenHideLoadingFeedback().Forget(); |
| 1 | 359 | | } |
| | 360 | |
|
| | 361 | | private bool IsLoadingMessages() => |
| 1 | 362 | | Time.realtimeSinceStartup - lastRequestTime < REQUEST_MESSAGES_TIME_OUT; |
| | 363 | |
|
| | 364 | | private async UniTaskVoid WaitForRequestTimeOutThenHideLoadingFeedback() |
| | 365 | | { |
| 11 | 366 | | lastRequestTime = Time.realtimeSinceStartup; |
| | 367 | |
|
| 273 | 368 | | await UniTask.WaitUntil(() => Time.realtimeSinceStartup - lastRequestTime > REQUEST_MESSAGES_TIME_OUT); |
| | 369 | |
|
| 11 | 370 | | View?.SetLoadingMessagesActive(false); |
| 11 | 371 | | View?.SetOldMessagesLoadingActive(false); |
| 11 | 372 | | } |
| | 373 | |
|
| | 374 | | private void HandleMessageBlockedBySpam(ChatMessage message) |
| | 375 | | { |
| 0 | 376 | | chatHudController.SetChatMessage(new ChatEntryModel |
| | 377 | | { |
| | 378 | | timestamp = (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| | 379 | | bodyText = "You sent too many messages in a short period of time. Please wait and try |
| | 380 | | messageId = Guid.NewGuid().ToString(), |
| | 381 | | messageType = ChatMessage.Type.SYSTEM, |
| | 382 | | subType = ChatEntryModel.SubType.RECEIVED |
| | 383 | | }) |
| | 384 | | .Forget(); |
| 0 | 385 | | } |
| | 386 | |
|
| | 387 | | private void ResetPagination() |
| | 388 | | { |
| 9 | 389 | | oldestTimestamp = long.MaxValue; |
| 9 | 390 | | oldestMessageId = null; |
| 9 | 391 | | } |
| | 392 | |
|
| | 393 | | private void Focus() |
| | 394 | | { |
| 11 | 395 | | chatHudController.FocusInputField(); |
| 11 | 396 | | MarkUserChatMessagesAsRead(); |
| 11 | 397 | | } |
| | 398 | |
|
| | 399 | | private void SomeoneMentionedFromContextMenu(string mention, string _) |
| | 400 | | { |
| 0 | 401 | | if (!View.IsActive) |
| 0 | 402 | | return; |
| | 403 | |
|
| 0 | 404 | | View.ChatHUD.AddTextIntoInputField(mention); |
| 0 | 405 | | } |
| | 406 | |
|
| | 407 | | private void UnblockUser(string userId) |
| | 408 | | { |
| 0 | 409 | | if (!ownUserProfile.IsBlocked(userId)) return; |
| | 410 | |
|
| 0 | 411 | | dataStore.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateUnBlockUserData( |
| | 412 | | userProfileBridge.Get(userId)?.userName, |
| | 413 | | () => |
| | 414 | | { |
| 0 | 415 | | ownUserProfile.Unblock(userId); |
| 0 | 416 | | WebInterface.SendUnblockPlayer(userId); |
| 0 | 417 | | socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(userId), PlayerActionSource.MyProfile |
| | 418 | |
|
| 0 | 419 | | }), true); |
| 0 | 420 | | } |
| | 421 | | } |
| | 422 | | } |