| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL.Social.Chat; |
| | 6 | | using DCL.Interface; |
| | 7 | | using DCL.ProfanityFiltering; |
| | 8 | | using DCL.Social.Chat.Mentions; |
| | 9 | | using DCLServices.CopyPaste.Analytics; |
| | 10 | | using SocialFeaturesAnalytics; |
| | 11 | | using UnityEngine; |
| | 12 | | using Channel = DCL.Chat.Channels.Channel; |
| | 13 | |
|
| | 14 | | namespace DCL.Social.Chat |
| | 15 | | { |
| | 16 | | public class ChatChannelHUDController : IHUD |
| | 17 | | { |
| | 18 | | private const int INITIAL_PAGE_SIZE = 30; |
| | 19 | | private const int SHOW_MORE_PAGE_SIZE = 10; |
| | 20 | | private const float REQUEST_MESSAGES_TIME_OUT = 2; |
| | 21 | |
|
| 184 | 22 | | public IChatChannelWindowView View { get; private set; } |
| | 23 | |
|
| | 24 | | private readonly DataStore dataStore; |
| 14 | 25 | | private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| | 26 | | private readonly IUserProfileBridge userProfileBridge; |
| | 27 | | private readonly IChatController chatController; |
| | 28 | | private readonly IMouseCatcher mouseCatcher; |
| | 29 | | private readonly ISocialAnalytics socialAnalytics; |
| | 30 | | private readonly IProfanityFilter profanityFilter; |
| | 31 | | private readonly IChatMentionSuggestionProvider chatMentionSuggestionProvider; |
| | 32 | | private readonly IClipboard clipboard; |
| | 33 | | private readonly ICopyPasteAnalyticsService copyPasteAnalyticsService; |
| | 34 | | private ChatHUDController chatHudController; |
| | 35 | | private ChannelMembersHUDController channelMembersHUDController; |
| 14 | 36 | | private CancellationTokenSource hideLoadingCancellationToken = new (); |
| | 37 | | private float lastRequestTime; |
| | 38 | | private string channelId; |
| | 39 | | private Channel channel; |
| | 40 | | private ChatMessage oldestMessage; |
| 14 | 41 | | private bool showOnlyOnlineMembersOnPublicChannels => !dataStore.featureFlags.flags.Get().IsFeatureEnabled("matr |
| | 42 | |
|
| | 43 | | private bool isVisible; |
| | 44 | |
|
| | 45 | | public event Action OnPressBack; |
| | 46 | | public event Action OnClosed; |
| | 47 | | public event Action<string> OnOpenChannelLeave; |
| | 48 | |
|
| 14 | 49 | | public ChatChannelHUDController(DataStore dataStore, |
| | 50 | | IUserProfileBridge userProfileBridge, |
| | 51 | | IChatController chatController, |
| | 52 | | IMouseCatcher mouseCatcher, |
| | 53 | | ISocialAnalytics socialAnalytics, |
| | 54 | | IProfanityFilter profanityFilter, |
| | 55 | | IChatMentionSuggestionProvider chatMentionSuggestionProvider, |
| | 56 | | IClipboard clipboard, |
| | 57 | | ICopyPasteAnalyticsService copyPasteAnalyticsService) |
| | 58 | | { |
| 14 | 59 | | this.dataStore = dataStore; |
| 14 | 60 | | this.userProfileBridge = userProfileBridge; |
| 14 | 61 | | this.chatController = chatController; |
| 14 | 62 | | this.mouseCatcher = mouseCatcher; |
| 14 | 63 | | this.socialAnalytics = socialAnalytics; |
| 14 | 64 | | this.profanityFilter = profanityFilter; |
| 14 | 65 | | this.chatMentionSuggestionProvider = chatMentionSuggestionProvider; |
| 14 | 66 | | this.clipboard = clipboard; |
| 14 | 67 | | this.copyPasteAnalyticsService = copyPasteAnalyticsService; |
| 14 | 68 | | } |
| | 69 | |
|
| | 70 | | public void Initialize(IChatChannelWindowView view, bool isVisible = true) |
| | 71 | | { |
| 14 | 72 | | View = view; |
| 14 | 73 | | view.OnBack -= HandlePressBack; |
| 14 | 74 | | view.OnBack += HandlePressBack; |
| 14 | 75 | | view.OnClose -= Hide; |
| 14 | 76 | | view.OnClose += Hide; |
| 14 | 77 | | view.OnRequireMoreMessages += RequestOldConversations; |
| 14 | 78 | | view.OnLeaveChannel += LeaveChannel; |
| 14 | 79 | | view.OnShowMembersList += ShowMembersList; |
| 14 | 80 | | view.OnHideMembersList += HideMembersList; |
| 14 | 81 | | view.OnMuteChanged += MuteChannel; |
| 14 | 82 | | view.OnCopyNameRequested += CopyNameToClipboard; |
| | 83 | |
|
| 14 | 84 | | dataStore.mentions.someoneMentionedFromContextMenu.OnChange += SomeoneMentionedFromContextMenu; |
| | 85 | |
|
| 14 | 86 | | chatHudController = new ChatHUDController(dataStore, userProfileBridge, false, |
| 3 | 87 | | (name, count, ct) => chatMentionSuggestionProvider.GetProfilesFromChatChannelsStartingWith(name, channelI |
| | 88 | | socialAnalytics, chatController, clipboard, copyPasteAnalyticsService, profanityFilter); |
| | 89 | |
|
| 14 | 90 | | chatHudController.Initialize(view.ChatHUD); |
| 14 | 91 | | chatHudController.SortingStrategy = new ChatEntrySortingByTimestamp(); |
| 14 | 92 | | chatHudController.OnSendMessage += HandleSendChatMessage; |
| 14 | 93 | | chatHudController.OnMessageSentBlockedBySpam += HandleMessageBlockedBySpam; |
| 14 | 94 | | chatController.OnAddMessage += HandleMessageReceived; |
| | 95 | |
|
| 14 | 96 | | if (mouseCatcher != null) |
| 13 | 97 | | mouseCatcher.OnMouseLock += Hide; |
| | 98 | |
|
| 14 | 99 | | channelMembersHUDController = new ChannelMembersHUDController(view.ChannelMembersHUD, chatController, userPr |
| | 100 | |
|
| 14 | 101 | | SetVisibility(isVisible); |
| 14 | 102 | | this.isVisible = isVisible; |
| 14 | 103 | | } |
| | 104 | |
|
| | 105 | | private void CopyNameToClipboard(string channelName) |
| | 106 | | { |
| 1 | 107 | | clipboard.WriteText(channelName); |
| 1 | 108 | | copyPasteAnalyticsService.Copy("channel_name"); |
| 1 | 109 | | } |
| | 110 | |
|
| | 111 | | public void Setup(string channelId) |
| | 112 | | { |
| 12 | 113 | | channelMembersHUDController.SetChannelId(channelId); |
| 12 | 114 | | this.channelId = channelId; |
| 12 | 115 | | lastRequestTime = 0; |
| | 116 | |
|
| 12 | 117 | | channel = chatController.GetAllocatedChannel(channelId); |
| 12 | 118 | | View.Setup(ToPublicChatModel(channel)); |
| | 119 | |
|
| 12 | 120 | | chatHudController.ClearAllEntries(); |
| 12 | 121 | | oldestMessage = null; |
| 12 | 122 | | } |
| | 123 | |
|
| | 124 | | public void SetVisibility(bool visible) |
| | 125 | | { |
| 23 | 126 | | if (isVisible != visible) |
| | 127 | | { |
| 7 | 128 | | isVisible = visible; |
| | 129 | |
|
| 7 | 130 | | SetVisiblePanelList(visible); |
| 7 | 131 | | chatHudController.SetVisibility(visible); |
| 7 | 132 | | dataStore.HUDs.chatInputVisible.Set(visible); |
| | 133 | | } |
| | 134 | |
|
| 23 | 135 | | if (visible) |
| | 136 | | { |
| 5 | 137 | | ClearChatControllerListeners(); |
| | 138 | |
|
| 5 | 139 | | chatController.OnChannelLeft += HandleChannelLeft; |
| 5 | 140 | | chatController.OnChannelUpdated += HandleChannelUpdated; |
| | 141 | |
|
| 5 | 142 | | if (channelMembersHUDController.IsVisible) |
| 1 | 143 | | channelMembersHUDController.SetAutomaticReloadingActive(true); |
| | 144 | |
|
| 5 | 145 | | View?.SetLoadingMessagesActive(false); |
| 5 | 146 | | View?.SetOldMessagesLoadingActive(false); |
| | 147 | |
|
| 5 | 148 | | if (!string.IsNullOrEmpty(channelId)) |
| | 149 | | { |
| 2 | 150 | | var channel = chatController.GetAllocatedChannel(channelId); |
| 2 | 151 | | View.Setup(ToPublicChatModel(channel)); |
| | 152 | |
|
| 2 | 153 | | RequestMessages( |
| | 154 | | channelId, |
| | 155 | | INITIAL_PAGE_SIZE); |
| | 156 | | } |
| | 157 | |
|
| 5 | 158 | | View?.ChatHUD.ResetInputField(); |
| 5 | 159 | | View?.Show(); |
| 5 | 160 | | Focus(); |
| | 161 | | } |
| | 162 | | else |
| | 163 | | { |
| 18 | 164 | | ClearChatControllerListeners(); |
| | 165 | |
|
| 18 | 166 | | channelMembersHUDController.SetAutomaticReloadingActive(false); |
| 18 | 167 | | chatHudController.UnfocusInputField(); |
| 18 | 168 | | OnClosed?.Invoke(); |
| 18 | 169 | | View.Hide(); |
| | 170 | | } |
| | 171 | |
|
| 23 | 172 | | dataStore.channels.channelToBeOpened.Set(null, notifyEvent: false); |
| 23 | 173 | | } |
| | 174 | |
|
| | 175 | | public void Dispose() |
| | 176 | | { |
| 14 | 177 | | ClearChatControllerListeners(); |
| | 178 | |
|
| 14 | 179 | | if (mouseCatcher != null) |
| 13 | 180 | | mouseCatcher.OnMouseLock -= Hide; |
| | 181 | |
|
| 14 | 182 | | chatHudController.OnSendMessage -= HandleSendChatMessage; |
| 14 | 183 | | chatHudController.OnMessageSentBlockedBySpam -= HandleMessageBlockedBySpam; |
| 14 | 184 | | chatHudController.Dispose(); |
| | 185 | |
|
| 14 | 186 | | if (View != null) |
| | 187 | | { |
| 14 | 188 | | View.OnBack -= HandlePressBack; |
| 14 | 189 | | View.OnClose -= Hide; |
| 14 | 190 | | View.OnRequireMoreMessages -= RequestOldConversations; |
| 14 | 191 | | View.OnLeaveChannel -= LeaveChannel; |
| 14 | 192 | | View.OnMuteChanged -= MuteChannel; |
| 14 | 193 | | View.Dispose(); |
| | 194 | | } |
| 14 | 195 | | dataStore.mentions.someoneMentionedFromContextMenu.OnChange -= SomeoneMentionedFromContextMenu; |
| | 196 | |
|
| 14 | 197 | | hideLoadingCancellationToken.Dispose(); |
| 14 | 198 | | channelMembersHUDController.Dispose(); |
| | 199 | |
|
| 14 | 200 | | if (chatController != null) |
| 14 | 201 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 14 | 202 | | } |
| | 203 | |
|
| | 204 | | private void HandleSendChatMessage(ChatMessage message) |
| | 205 | | { |
| 1 | 206 | | message.messageType = ChatMessage.Type.PUBLIC; |
| 1 | 207 | | message.recipient = channelId; |
| 1 | 208 | | message.channelName = channel.Name; |
| | 209 | |
|
| 1 | 210 | | var 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 | | return; |
| | 223 | | } |
| | 224 | |
|
| 1 | 225 | | if (message.body.ToLower().Equals("/leave")) |
| | 226 | | { |
| 1 | 227 | | LeaveChannelFromCommand(); |
| 1 | 228 | | return; |
| | 229 | | } |
| | 230 | |
|
| 0 | 231 | | chatController.Send(message); |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | private void HandleMessageReceived(ChatMessage[] messages) |
| | 235 | | { |
| 3 | 236 | | var messageLogUpdated = false; |
| | 237 | |
|
| 3 | 238 | | var ownPlayerAlreadyMentioned = false; |
| | 239 | |
|
| 14 | 240 | | foreach (var message in messages) |
| | 241 | | { |
| 4 | 242 | | if (!ownPlayerAlreadyMentioned) |
| 4 | 243 | | ownPlayerAlreadyMentioned = CheckOwnPlayerMentionInChannels(message); |
| | 244 | |
|
| 4 | 245 | | if (!isVisible) continue; |
| | 246 | |
|
| 2 | 247 | | if (!IsMessageFomCurrentChannel(message)) continue; |
| | 248 | |
|
| 2 | 249 | | UpdateOldestMessage(message); |
| | 250 | |
|
| | 251 | | // TODO: right now the channel history is disabled, but we must find a workaround to support history + m |
| | 252 | | // one approach could be to increment the max amount of messages depending on how many pages you loaded |
| | 253 | | // for example: 1 page = 30 messages, 2 pages = 60 messages, and so on.. |
| 2 | 254 | | chatHudController.SetChatMessage(message, limitMaxEntries: true); |
| | 255 | |
|
| 2 | 256 | | dataStore.channels.SetAvailableMemberInChannel(message.sender, channelId); |
| | 257 | |
|
| 2 | 258 | | View?.SetLoadingMessagesActive(false); |
| 2 | 259 | | View?.SetOldMessagesLoadingActive(false); |
| | 260 | |
|
| 2 | 261 | | messageLogUpdated = true; |
| | 262 | | } |
| | 263 | |
|
| 3 | 264 | | if (View.IsActive && messageLogUpdated) |
| | 265 | | { |
| | 266 | | // The messages from 'channelId' are marked as read if the channel window is currently open |
| 1 | 267 | | MarkChannelMessagesAsRead(); |
| | 268 | | } |
| 3 | 269 | | } |
| | 270 | |
|
| | 271 | | private bool CheckOwnPlayerMentionInChannels(ChatMessage message) |
| | 272 | | { |
| 4 | 273 | | var ownUserProfile = userProfileBridge.GetOwn(); |
| | 274 | |
|
| 4 | 275 | | if (message.sender == ownUserProfile.userId || |
| | 276 | | message.messageType != ChatMessage.Type.PUBLIC || |
| | 277 | | string.IsNullOrEmpty(message.recipient) || |
| | 278 | | (message.recipient == channelId && View.IsActive) || |
| | 279 | | !MentionsUtils.IsUserMentionedInText(ownUserProfile.userName, message.body)) |
| 3 | 280 | | return false; |
| | 281 | |
|
| 1 | 282 | | dataStore.mentions.ownPlayerMentionedInChannel.Set(message.recipient, true); |
| 1 | 283 | | return true; |
| | 284 | | } |
| | 285 | |
|
| | 286 | | private void UpdateOldestMessage(ChatMessage message) |
| | 287 | | { |
| 2 | 288 | | if (oldestMessage == null) |
| 1 | 289 | | oldestMessage = message; |
| 1 | 290 | | else if (message.timestamp < oldestMessage.timestamp) |
| 0 | 291 | | oldestMessage = message; |
| 1 | 292 | | } |
| | 293 | |
|
| | 294 | | private void Hide() |
| | 295 | | { |
| 0 | 296 | | SetVisibility(false); |
| 0 | 297 | | OnClosed?.Invoke(); |
| 0 | 298 | | } |
| | 299 | |
|
| | 300 | | private void HandlePressBack() => |
| 0 | 301 | | OnPressBack?.Invoke(); |
| | 302 | |
|
| | 303 | | private bool IsMessageFomCurrentChannel(ChatMessage message) => |
| 2 | 304 | | message.sender == channelId || message.recipient == channelId || (View.IsActive && message.messageType == Ch |
| | 305 | |
|
| | 306 | | private void MarkChannelMessagesAsRead() => |
| 6 | 307 | | chatController.MarkChannelMessagesAsSeen(channelId); |
| | 308 | |
|
| | 309 | | private void RequestMessages(string channelId, int limit, string fromMessageId = null) |
| | 310 | | { |
| 2 | 311 | | View?.SetLoadingMessagesActive(true); |
| 2 | 312 | | chatController.GetChannelMessages(channelId, limit, fromMessageId); |
| 2 | 313 | | hideLoadingCancellationToken.Cancel(); |
| 2 | 314 | | hideLoadingCancellationToken = new CancellationTokenSource(); |
| 2 | 315 | | WaitForRequestTimeOutThenHideLoadingFeedback(hideLoadingCancellationToken.Token).Forget(); |
| 2 | 316 | | } |
| | 317 | |
|
| | 318 | | private void RequestOldConversations() |
| | 319 | | { |
| 0 | 320 | | if (IsLoadingMessages()) return; |
| | 321 | |
|
| 0 | 322 | | View?.SetOldMessagesLoadingActive(true); |
| 0 | 323 | | lastRequestTime = Time.realtimeSinceStartup; |
| | 324 | |
|
| 0 | 325 | | chatController.GetChannelMessages( |
| | 326 | | channelId, |
| | 327 | | SHOW_MORE_PAGE_SIZE, |
| | 328 | | oldestMessage?.messageId); |
| | 329 | |
|
| 0 | 330 | | hideLoadingCancellationToken.Cancel(); |
| 0 | 331 | | hideLoadingCancellationToken = new CancellationTokenSource(); |
| 0 | 332 | | WaitForRequestTimeOutThenHideLoadingFeedback(hideLoadingCancellationToken.Token).Forget(); |
| 0 | 333 | | } |
| | 334 | |
|
| | 335 | | private bool IsLoadingMessages() => |
| 0 | 336 | | Time.realtimeSinceStartup - lastRequestTime < REQUEST_MESSAGES_TIME_OUT; |
| | 337 | |
|
| | 338 | | private async UniTaskVoid WaitForRequestTimeOutThenHideLoadingFeedback(CancellationToken cancellationToken) |
| | 339 | | { |
| 2 | 340 | | lastRequestTime = Time.realtimeSinceStartup; |
| | 341 | |
|
| 6 | 342 | | await UniTask.WaitUntil(() => |
| 45 | 343 | | Time.realtimeSinceStartup - lastRequestTime > REQUEST_MESSAGES_TIME_OUT, |
| | 344 | | cancellationToken: cancellationToken); |
| | 345 | |
|
| 2 | 346 | | if (cancellationToken.IsCancellationRequested) return; |
| | 347 | |
|
| 2 | 348 | | View?.SetLoadingMessagesActive(false); |
| 2 | 349 | | View?.SetOldMessagesLoadingActive(false); |
| 2 | 350 | | } |
| | 351 | |
|
| | 352 | | private void LeaveChannel() |
| | 353 | | { |
| 1 | 354 | | dataStore.channels.channelLeaveSource.Set(ChannelLeaveSource.Chat); |
| 1 | 355 | | OnOpenChannelLeave?.Invoke(channelId); |
| 1 | 356 | | } |
| | 357 | |
|
| | 358 | | private void LeaveChannelFromCommand() |
| | 359 | | { |
| 1 | 360 | | dataStore.channels.channelLeaveSource.Set(ChannelLeaveSource.Command); |
| 1 | 361 | | chatController.LeaveChannel(channelId); |
| 1 | 362 | | } |
| | 363 | |
|
| | 364 | | private void HandleChannelLeft(string channelId) |
| | 365 | | { |
| 1 | 366 | | if (channelId != this.channelId) return; |
| 1 | 367 | | OnPressBack?.Invoke(); |
| 1 | 368 | | } |
| | 369 | |
|
| | 370 | | private void HandleChannelUpdated(Channel updatedChannel) |
| | 371 | | { |
| 0 | 372 | | if (updatedChannel.ChannelId != channelId) |
| 0 | 373 | | return; |
| | 374 | |
|
| 0 | 375 | | View.Setup(ToPublicChatModel(updatedChannel)); |
| 0 | 376 | | channelMembersHUDController.SetMembersCount(updatedChannel.MemberCount); |
| 0 | 377 | | } |
| | 378 | |
|
| | 379 | | private void ShowMembersList() => |
| 1 | 380 | | channelMembersHUDController.SetVisibility(true); |
| | 381 | |
|
| | 382 | | private void HideMembersList() => |
| 0 | 383 | | channelMembersHUDController.SetVisibility(false); |
| | 384 | |
|
| | 385 | | private void MuteChannel(bool muted) |
| | 386 | | { |
| 2 | 387 | | if (muted) |
| 1 | 388 | | chatController.MuteChannel(channelId); |
| | 389 | | else |
| 1 | 390 | | chatController.UnmuteChannel(channelId); |
| 1 | 391 | | } |
| | 392 | |
|
| | 393 | | private void SetVisiblePanelList(bool visible) |
| | 394 | | { |
| 7 | 395 | | var newSet = visibleTaskbarPanels.Get(); |
| | 396 | |
|
| 7 | 397 | | if (visible) |
| 5 | 398 | | newSet.Add("ChatChannel"); |
| | 399 | | else |
| 2 | 400 | | newSet.Remove("ChatChannel"); |
| | 401 | |
|
| 7 | 402 | | visibleTaskbarPanels.Set(newSet, true); |
| 7 | 403 | | } |
| | 404 | |
|
| | 405 | | private PublicChatModel ToPublicChatModel(Channel channel) |
| | 406 | | { |
| 14 | 407 | | return new PublicChatModel(channelId, channel.Name, channel.Description, |
| | 408 | | channel.Joined, channel.MemberCount, channel.Muted, |
| | 409 | | showOnlyOnlineMembersOnPublicChannels); |
| | 410 | | } |
| | 411 | |
|
| | 412 | | private void ClearChatControllerListeners() |
| | 413 | | { |
| 37 | 414 | | if (chatController == null) return; |
| 37 | 415 | | chatController.OnChannelLeft -= HandleChannelLeft; |
| 37 | 416 | | chatController.OnChannelUpdated -= HandleChannelUpdated; |
| 37 | 417 | | } |
| | 418 | |
|
| | 419 | | private void Focus() |
| | 420 | | { |
| 5 | 421 | | chatHudController.FocusInputField(); |
| 5 | 422 | | MarkChannelMessagesAsRead(); |
| 5 | 423 | | } |
| | 424 | |
|
| | 425 | | private void HandleMessageBlockedBySpam(ChatMessage message) |
| | 426 | | { |
| 0 | 427 | | chatHudController.SetChatMessage(new ChatEntryModel |
| | 428 | | { |
| | 429 | | timestamp = (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| | 430 | | bodyText = "You sent too many messages in a short period of time. Please wait and try again later.", |
| | 431 | | messageId = Guid.NewGuid().ToString(), |
| | 432 | | messageType = ChatMessage.Type.SYSTEM, |
| | 433 | | subType = ChatEntryModel.SubType.RECEIVED |
| | 434 | | }).Forget(); |
| 0 | 435 | | } |
| | 436 | |
|
| | 437 | | private void SomeoneMentionedFromContextMenu(string mention, string _) |
| | 438 | | { |
| 0 | 439 | | if (!View.IsActive) |
| 0 | 440 | | return; |
| | 441 | |
|
| 0 | 442 | | View.ChatHUD.AddTextIntoInputField(mention); |
| 0 | 443 | | } |
| | 444 | | } |
| | 445 | | } |