| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL; |
| | 6 | | using DCL.Interface; |
| | 7 | | using SocialFeaturesAnalytics; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public class PrivateChatWindowController : IHUD |
| | 11 | | { |
| | 12 | | private const int USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_INITIAL_LOAD = 30; |
| | 13 | | private const float REQUEST_MESSAGES_TIME_OUT = 2; |
| | 14 | | internal const int USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_SHOW_MORE = 10; |
| | 15 | |
|
| 0 | 16 | | public IPrivateChatComponentView View { get; private set; } |
| | 17 | |
|
| | 18 | | private readonly DataStore dataStore; |
| | 19 | | private readonly IUserProfileBridge userProfileBridge; |
| | 20 | | private readonly IChatController chatController; |
| | 21 | | private readonly IFriendsController friendsController; |
| | 22 | | private readonly ISocialAnalytics socialAnalytics; |
| | 23 | | private readonly IMouseCatcher mouseCatcher; |
| | 24 | | private readonly InputAction_Trigger toggleChatTrigger; |
| | 25 | | private ChatHUDController chatHudController; |
| | 26 | | private UserProfile conversationProfile; |
| | 27 | | private bool skipChatInputTrigger; |
| | 28 | | private float lastRequestTime; |
| 16 | 29 | | private CancellationTokenSource deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| 16 | 30 | | private CancellationTokenSource markMessagesAsSeenCancellationToken = new CancellationTokenSource(); |
| | 31 | | private bool shouldRequestMessages; |
| 16 | 32 | | private ulong oldestTimestamp = ulong.MaxValue; |
| | 33 | | private string oldestMessageId; |
| | 34 | | private string conversationUserId; |
| 32 | 35 | | private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| | 36 | |
|
| | 37 | | public event Action OnBack; |
| | 38 | | public event Action OnClosed; |
| | 39 | |
|
| 16 | 40 | | public PrivateChatWindowController(DataStore dataStore, |
| | 41 | | IUserProfileBridge userProfileBridge, |
| | 42 | | IChatController chatController, |
| | 43 | | IFriendsController friendsController, |
| | 44 | | ISocialAnalytics socialAnalytics, |
| | 45 | | IMouseCatcher mouseCatcher, |
| | 46 | | InputAction_Trigger toggleChatTrigger) |
| | 47 | | { |
| 16 | 48 | | this.dataStore = dataStore; |
| 16 | 49 | | this.userProfileBridge = userProfileBridge; |
| 16 | 50 | | this.chatController = chatController; |
| 16 | 51 | | this.friendsController = friendsController; |
| 16 | 52 | | this.socialAnalytics = socialAnalytics; |
| 16 | 53 | | this.mouseCatcher = mouseCatcher; |
| 16 | 54 | | this.toggleChatTrigger = toggleChatTrigger; |
| 16 | 55 | | } |
| | 56 | |
|
| | 57 | | public void Initialize(IPrivateChatComponentView view = null) |
| | 58 | | { |
| 17 | 59 | | view ??= PrivateChatWindowComponentView.Create(); |
| 17 | 60 | | View = view; |
| 17 | 61 | | View.Initialize(friendsController, socialAnalytics); |
| 17 | 62 | | view.OnPressBack -= HandlePressBack; |
| 17 | 63 | | view.OnPressBack += HandlePressBack; |
| 17 | 64 | | view.OnClose -= Hide; |
| 17 | 65 | | view.OnClose += Hide; |
| 17 | 66 | | view.OnMinimize += MinimizeView; |
| 17 | 67 | | view.OnUnfriend += Unfriend; |
| | 68 | |
|
| 17 | 69 | | if (mouseCatcher != null) |
| 17 | 70 | | mouseCatcher.OnMouseLock += Hide; |
| | 71 | |
|
| 17 | 72 | | view.OnRequireMoreMessages += RequestOldConversations; |
| | 73 | |
|
| 17 | 74 | | chatHudController = new ChatHUDController(dataStore, userProfileBridge, false); |
| 17 | 75 | | chatHudController.Initialize(view.ChatHUD); |
| 17 | 76 | | chatHudController.OnInputFieldSelected += HandleInputFieldSelected; |
| 17 | 77 | | chatHudController.OnSendMessage += HandleSendChatMessage; |
| 17 | 78 | | chatHudController.OnMessageSentBlockedBySpam += HandleMessageBlockedBySpam; |
| | 79 | |
|
| 17 | 80 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 17 | 81 | | chatController.OnAddMessage += HandleMessageReceived; |
| | 82 | |
|
| 17 | 83 | | toggleChatTrigger.OnTriggered += HandleChatInputTriggered; |
| 17 | 84 | | } |
| | 85 | |
|
| | 86 | | public void Setup(string newConversationUserId) |
| | 87 | | { |
| 14 | 88 | | if (string.IsNullOrEmpty(newConversationUserId) || newConversationUserId == conversationUserId) |
| 1 | 89 | | return; |
| | 90 | |
|
| 13 | 91 | | var newConversationUserProfile = userProfileBridge.Get(newConversationUserId); |
| | 92 | |
|
| 13 | 93 | | conversationUserId = newConversationUserId; |
| 13 | 94 | | conversationProfile = newConversationUserProfile; |
| 13 | 95 | | chatHudController.ClearAllEntries(); |
| 13 | 96 | | shouldRequestMessages = true; |
| 13 | 97 | | } |
| | 98 | |
|
| | 99 | | public void SetVisibility(bool visible) |
| | 100 | | { |
| 19 | 101 | | if (View.IsActive == visible) |
| 3 | 102 | | return; |
| | 103 | |
|
| 16 | 104 | | SetVisiblePanelList(visible); |
| | 105 | |
|
| 16 | 106 | | if (visible) |
| | 107 | | { |
| 11 | 108 | | View?.SetLoadingMessagesActive(false); |
| 11 | 109 | | View?.SetOldMessagesLoadingActive(false); |
| | 110 | |
|
| 11 | 111 | | if (conversationProfile != null) |
| | 112 | | { |
| 10 | 113 | | var userStatus = friendsController.GetUserStatus(conversationUserId); |
| 10 | 114 | | View.Setup(conversationProfile, |
| | 115 | | userStatus.presence == PresenceStatus.ONLINE, |
| | 116 | | userProfileBridge.GetOwn().IsBlocked(conversationUserId)); |
| | 117 | |
|
| 10 | 118 | | if (shouldRequestMessages) |
| | 119 | | { |
| 9 | 120 | | ResetPagination(); |
| 9 | 121 | | RequestPrivateMessages( |
| | 122 | | conversationUserId, |
| | 123 | | USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_INITIAL_LOAD, |
| | 124 | | null); |
| | 125 | |
|
| 9 | 126 | | shouldRequestMessages = false; |
| | 127 | | } |
| | 128 | | } |
| | 129 | |
|
| 11 | 130 | | View.Show(); |
| 11 | 131 | | Focus(); |
| 11 | 132 | | } |
| | 133 | | else |
| | 134 | | { |
| 5 | 135 | | chatHudController.UnfocusInputField(); |
| 5 | 136 | | View.Hide(); |
| | 137 | | } |
| 5 | 138 | | } |
| | 139 | |
|
| | 140 | | public void Dispose() |
| | 141 | | { |
| 16 | 142 | | if (chatHudController != null) |
| | 143 | | { |
| 16 | 144 | | chatHudController.OnInputFieldSelected -= HandleInputFieldSelected; |
| 16 | 145 | | chatHudController.OnSendMessage -= HandleSendChatMessage; |
| 16 | 146 | | chatHudController.OnMessageSentBlockedBySpam -= HandleMessageBlockedBySpam; |
| | 147 | | } |
| | 148 | |
|
| 16 | 149 | | if (chatController != null) |
| 16 | 150 | | chatController.OnAddMessage -= HandleMessageReceived; |
| | 151 | |
|
| 16 | 152 | | if (mouseCatcher != null) |
| 16 | 153 | | mouseCatcher.OnMouseLock -= Hide; |
| | 154 | |
|
| 16 | 155 | | toggleChatTrigger.OnTriggered -= HandleChatInputTriggered; |
| | 156 | |
|
| 16 | 157 | | if (View != null) |
| | 158 | | { |
| 16 | 159 | | View.OnPressBack -= HandlePressBack; |
| 16 | 160 | | View.OnClose -= Hide; |
| 16 | 161 | | View.OnMinimize -= MinimizeView; |
| 16 | 162 | | View.OnUnfriend -= Unfriend; |
| 16 | 163 | | View.OnFocused -= HandleViewFocused; |
| 16 | 164 | | View.OnRequireMoreMessages -= RequestOldConversations; |
| 16 | 165 | | View.Dispose(); |
| | 166 | | } |
| 16 | 167 | | } |
| | 168 | |
|
| | 169 | | private void HandleSendChatMessage(ChatMessage message) |
| | 170 | | { |
| 1 | 171 | | if (string.IsNullOrEmpty(conversationProfile.userName)) |
| 0 | 172 | | return; |
| | 173 | |
|
| 1 | 174 | | message.messageType = ChatMessage.Type.PRIVATE; |
| 1 | 175 | | message.recipient = conversationProfile.userName; |
| | 176 | |
|
| 1 | 177 | | bool isValidMessage = !string.IsNullOrEmpty(message.body) |
| | 178 | | && !string.IsNullOrWhiteSpace(message.body) |
| | 179 | | && !string.IsNullOrEmpty(message.recipient); |
| | 180 | |
|
| 1 | 181 | | if (isValidMessage) |
| | 182 | | { |
| 1 | 183 | | chatHudController.ResetInputField(); |
| 1 | 184 | | chatHudController.FocusInputField(); |
| 1 | 185 | | } |
| | 186 | | else |
| | 187 | | { |
| 0 | 188 | | SetVisibility(false); |
| 0 | 189 | | OnClosed?.Invoke(); |
| 0 | 190 | | return; |
| | 191 | | } |
| | 192 | |
|
| | 193 | | // If Kernel allowed for private messages without the whisper param we could avoid this line |
| 1 | 194 | | message.body = $"/w {message.recipient} {message.body}"; |
| | 195 | |
|
| 1 | 196 | | chatController.Send(message); |
| 1 | 197 | | } |
| | 198 | |
|
| 0 | 199 | | private void MinimizeView() => SetVisibility(false); |
| | 200 | |
|
| | 201 | | private void HandleMessageReceived(ChatMessage message) |
| | 202 | | { |
| 3 | 203 | | if (!IsMessageFomCurrentConversation(message)) |
| 0 | 204 | | return; |
| | 205 | |
|
| 3 | 206 | | chatHudController.AddChatMessage(message, limitMaxEntries: false); |
| | 207 | |
|
| 3 | 208 | | if (message.timestamp < oldestTimestamp) |
| | 209 | | { |
| 1 | 210 | | oldestTimestamp = message.timestamp; |
| 1 | 211 | | oldestMessageId = message.messageId; |
| | 212 | | } |
| | 213 | |
|
| 3 | 214 | | if (View.IsActive) |
| | 215 | | { |
| 0 | 216 | | markMessagesAsSeenCancellationToken.Cancel(); |
| 0 | 217 | | markMessagesAsSeenCancellationToken = new CancellationTokenSource(); |
| | 218 | | // since there could be many messages coming in a row, avoid making the call instantly for each message |
| | 219 | | // instead make just one call after the iteration finishes |
| 0 | 220 | | MarkMessagesAsSeenDelayed(markMessagesAsSeenCancellationToken.Token).Forget(); |
| | 221 | | } |
| | 222 | |
|
| 3 | 223 | | View?.SetLoadingMessagesActive(false); |
| 3 | 224 | | View?.SetOldMessagesLoadingActive(false); |
| | 225 | |
|
| 3 | 226 | | deactivateFadeOutCancellationToken.Cancel(); |
| 3 | 227 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| 3 | 228 | | } |
| | 229 | |
|
| | 230 | | private void Hide() |
| | 231 | | { |
| 2 | 232 | | SetVisibility(false); |
| 2 | 233 | | OnClosed?.Invoke(); |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | private void Show() |
| | 237 | | { |
| 0 | 238 | | SetVisibility(true); |
| 0 | 239 | | } |
| | 240 | |
|
| 1 | 241 | | private void HandlePressBack() => OnBack?.Invoke(); |
| | 242 | |
|
| | 243 | | private void Unfriend(string friendId) |
| | 244 | | { |
| 0 | 245 | | friendsController.RemoveFriend(friendId); |
| 0 | 246 | | Hide(); |
| 0 | 247 | | } |
| | 248 | |
|
| | 249 | | private bool IsMessageFomCurrentConversation(ChatMessage message) |
| | 250 | | { |
| 3 | 251 | | return message.messageType == ChatMessage.Type.PRIVATE && |
| | 252 | | (message.sender == conversationUserId || message.recipient == conversationUserId); |
| | 253 | | } |
| | 254 | |
|
| | 255 | | private void MarkUserChatMessagesAsRead() => |
| 11 | 256 | | chatController.MarkMessagesAsSeen(conversationUserId); |
| | 257 | |
|
| | 258 | | private async UniTask MarkMessagesAsSeenDelayed(CancellationToken cancellationToken) |
| | 259 | | { |
| 0 | 260 | | await UniTask.NextFrame(cancellationToken); |
| 0 | 261 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 262 | | MarkUserChatMessagesAsRead(); |
| 0 | 263 | | } |
| | 264 | |
|
| | 265 | | private void HandleInputFieldSelected() |
| | 266 | | { |
| 0 | 267 | | Show(); |
| | 268 | | // The messages from 'conversationUserId' are marked as read if the player clicks on the input field of the priv |
| | 269 | | //MarkUserChatMessagesAsRead(); |
| 0 | 270 | | } |
| | 271 | |
|
| | 272 | | private void HandleViewFocused(bool focused) |
| | 273 | | { |
| 0 | 274 | | if (focused) |
| | 275 | | { |
| 0 | 276 | | deactivateFadeOutCancellationToken.Cancel(); |
| 0 | 277 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 278 | | } |
| 0 | 279 | | } |
| | 280 | |
|
| | 281 | | private void SetVisiblePanelList(bool visible) |
| | 282 | | { |
| 16 | 283 | | HashSet<string> newSet = visibleTaskbarPanels.Get(); |
| 16 | 284 | | if (visible) |
| 11 | 285 | | newSet.Add("PrivateChatChannel"); |
| | 286 | | else |
| 5 | 287 | | newSet.Remove("PrivateChatChannel"); |
| | 288 | |
|
| 16 | 289 | | visibleTaskbarPanels.Set(newSet, true); |
| 16 | 290 | | } |
| | 291 | |
|
| | 292 | | private void HandleChatInputTriggered(DCLAction_Trigger action) |
| | 293 | | { |
| | 294 | | // race condition patch caused by unfocusing input field from invalid message on SendChatMessage |
| | 295 | | // chat input trigger is the same key as sending the chat message from the input field |
| 0 | 296 | | if (skipChatInputTrigger) |
| | 297 | | { |
| 0 | 298 | | skipChatInputTrigger = false; |
| 0 | 299 | | return; |
| | 300 | | } |
| | 301 | |
|
| 0 | 302 | | if (!View.IsActive) |
| 0 | 303 | | return; |
| 0 | 304 | | chatHudController.FocusInputField(); |
| 0 | 305 | | } |
| | 306 | |
|
| | 307 | | internal void RequestPrivateMessages(string userId, int limit, string fromMessageId) |
| | 308 | | { |
| 10 | 309 | | View?.SetLoadingMessagesActive(true); |
| 10 | 310 | | chatController.GetPrivateMessages(userId, limit, fromMessageId); |
| 10 | 311 | | WaitForRequestTimeOutThenHideLoadingFeedback().Forget(); |
| 10 | 312 | | } |
| | 313 | |
|
| | 314 | | internal void RequestOldConversations() |
| | 315 | | { |
| 1 | 316 | | if (IsLoadingMessages()) return; |
| | 317 | |
|
| 1 | 318 | | chatController.GetPrivateMessages( |
| | 319 | | conversationUserId, |
| | 320 | | USER_PRIVATE_MESSAGES_TO_REQUEST_FOR_SHOW_MORE, |
| | 321 | | oldestMessageId); |
| | 322 | |
|
| 1 | 323 | | lastRequestTime = Time.realtimeSinceStartup; |
| 1 | 324 | | View?.SetOldMessagesLoadingActive(true); |
| 1 | 325 | | WaitForRequestTimeOutThenHideLoadingFeedback().Forget(); |
| 1 | 326 | | } |
| | 327 | |
|
| | 328 | | private bool IsLoadingMessages() => |
| 1 | 329 | | Time.realtimeSinceStartup - lastRequestTime < REQUEST_MESSAGES_TIME_OUT; |
| | 330 | |
|
| | 331 | | private async UniTaskVoid WaitForRequestTimeOutThenHideLoadingFeedback() |
| | 332 | | { |
| 11 | 333 | | lastRequestTime = Time.realtimeSinceStartup; |
| | 334 | |
|
| 1918 | 335 | | await UniTask.WaitUntil(() => Time.realtimeSinceStartup - lastRequestTime > REQUEST_MESSAGES_TIME_OUT); |
| | 336 | |
|
| 11 | 337 | | View?.SetLoadingMessagesActive(false); |
| 11 | 338 | | View?.SetOldMessagesLoadingActive(false); |
| 11 | 339 | | } |
| | 340 | |
|
| | 341 | | private void HandleMessageBlockedBySpam(ChatMessage message) |
| | 342 | | { |
| 0 | 343 | | chatHudController.AddChatMessage(new ChatEntryModel |
| | 344 | | { |
| | 345 | | timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| | 346 | | bodyText = "You sent too many messages in a short period of time. Please wait and try again later.", |
| | 347 | | messageId = Guid.NewGuid().ToString(), |
| | 348 | | messageType = ChatMessage.Type.SYSTEM, |
| | 349 | | subType = ChatEntryModel.SubType.RECEIVED |
| | 350 | | }); |
| 0 | 351 | | } |
| | 352 | |
|
| | 353 | | private void ResetPagination() |
| | 354 | | { |
| 9 | 355 | | oldestTimestamp = long.MaxValue; |
| 9 | 356 | | oldestMessageId = null; |
| 9 | 357 | | } |
| | 358 | |
|
| | 359 | | private void Focus() |
| | 360 | | { |
| 11 | 361 | | chatHudController.FocusInputField(); |
| 11 | 362 | | MarkUserChatMessagesAsRead(); |
| 11 | 363 | | } |
| | 364 | | } |