| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Chat; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.ProfanityFiltering; |
| | 5 | | using DCL.Social.Chat.Mentions; |
| | 6 | | using DCLServices.CopyPaste.Analytics; |
| | 7 | | using SocialFeaturesAnalytics; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using Channel = DCL.Chat.Channels.Channel; |
| | 11 | |
|
| | 12 | | namespace DCL.Social.Chat |
| | 13 | | { |
| | 14 | | public class PublicChatWindowController : IHUD |
| | 15 | | { |
| 171 | 16 | | public IPublicChatWindowView View { get; private set; } |
| | 17 | |
|
| | 18 | | public event Action OnBack; |
| | 19 | | public event Action OnClosed; |
| | 20 | |
|
| | 21 | | private readonly IChatController chatController; |
| | 22 | | private readonly IUserProfileBridge userProfileBridge; |
| | 23 | | private readonly DataStore dataStore; |
| | 24 | | private readonly IProfanityFilter profanityFilter; |
| | 25 | | private readonly IMouseCatcher mouseCatcher; |
| | 26 | | private readonly IChatMentionSuggestionProvider chatMentionSuggestionProvider; |
| | 27 | | private readonly ISocialAnalytics socialAnalytics; |
| | 28 | | private readonly IClipboard clipboard; |
| | 29 | | private readonly ICopyPasteAnalyticsService copyPasteAnalyticsService; |
| | 30 | | private ChatHUDController chatHudController; |
| | 31 | | private string channelId; |
| | 32 | | private bool skipChatInputTrigger; |
| | 33 | | private NearbyMembersHUDController nearbyMembersHUDController; |
| | 34 | |
|
| 19 | 35 | | private bool showOnlyOnlineMembersOnPublicChannels => !dataStore.featureFlags.flags.Get().IsFeatureEnabled("matr |
| 95 | 36 | | private BaseDictionary<string, Player> nearbyPlayers => dataStore.player.otherPlayers; |
| | 37 | |
|
| | 38 | | private bool isVisible; |
| | 39 | | private Channel channel; |
| | 40 | |
|
| 18 | 41 | | private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| | 42 | |
|
| 19 | 43 | | public PublicChatWindowController(IChatController chatController, |
| | 44 | | IUserProfileBridge userProfileBridge, |
| | 45 | | DataStore dataStore, |
| | 46 | | IProfanityFilter profanityFilter, |
| | 47 | | IMouseCatcher mouseCatcher, |
| | 48 | | IChatMentionSuggestionProvider chatMentionSuggestionProvider, |
| | 49 | | ISocialAnalytics socialAnalytics, |
| | 50 | | IClipboard clipboard, |
| | 51 | | ICopyPasteAnalyticsService copyPasteAnalyticsService) |
| | 52 | | { |
| 19 | 53 | | this.chatController = chatController; |
| 19 | 54 | | this.userProfileBridge = userProfileBridge; |
| 19 | 55 | | this.dataStore = dataStore; |
| 19 | 56 | | this.profanityFilter = profanityFilter; |
| 19 | 57 | | this.mouseCatcher = mouseCatcher; |
| 19 | 58 | | this.chatMentionSuggestionProvider = chatMentionSuggestionProvider; |
| 19 | 59 | | this.socialAnalytics = socialAnalytics; |
| 19 | 60 | | this.clipboard = clipboard; |
| 19 | 61 | | this.copyPasteAnalyticsService = copyPasteAnalyticsService; |
| 19 | 62 | | } |
| | 63 | |
|
| | 64 | | public void Initialize(IPublicChatWindowView view, bool isVisible = true) |
| | 65 | | { |
| 19 | 66 | | View = view; |
| 19 | 67 | | view.OnClose += HandleViewClosed; |
| 19 | 68 | | view.OnBack += HandleViewBacked; |
| 19 | 69 | | view.OnMuteChanged += MuteChannel; |
| 19 | 70 | | view.OnGoToCrowd += GoToCrowd; |
| | 71 | |
|
| 19 | 72 | | if (mouseCatcher != null) |
| 18 | 73 | | mouseCatcher.OnMouseLock += Hide; |
| | 74 | |
|
| 19 | 75 | | chatHudController = new ChatHUDController(dataStore, |
| | 76 | | userProfileBridge, |
| | 77 | | true, |
| 3 | 78 | | (name, count, ct) => chatMentionSuggestionProvider.GetNearbyProfilesStartingWith(name, count, ct), |
| | 79 | | socialAnalytics, |
| | 80 | | chatController, |
| | 81 | | clipboard, |
| | 82 | | copyPasteAnalyticsService, |
| | 83 | | profanityFilter); |
| | 84 | | // dont set any message's sorting strategy, just add them sequentally |
| | 85 | | // comms cannot calculate a server timestamp for each message |
| 19 | 86 | | chatHudController.Initialize(view.ChatHUD); |
| 19 | 87 | | chatHudController.OnSendMessage += SendChatMessage; |
| 19 | 88 | | chatHudController.OnMessageSentBlockedBySpam += HandleMessageBlockedBySpam; |
| | 89 | |
|
| 19 | 90 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 19 | 91 | | chatController.OnAddMessage += HandleMessageReceived; |
| 19 | 92 | | chatController.OnChannelUpdated -= HandleChannelUpdated; |
| 19 | 93 | | chatController.OnChannelUpdated += HandleChannelUpdated; |
| | 94 | |
|
| 19 | 95 | | dataStore.mentions.someoneMentionedFromContextMenu.OnChange += SomeoneMentionedFromContextMenu; |
| 19 | 96 | | nearbyPlayers.OnAdded += UpdateMembersCount; |
| 19 | 97 | | nearbyPlayers.OnRemoved += UpdateMembersCount; |
| | 98 | |
|
| 19 | 99 | | view.OnShowMembersList += ShowMembersList; |
| 19 | 100 | | view.OnHideMembersList += HideMembersList; |
| 19 | 101 | | nearbyMembersHUDController = new NearbyMembersHUDController(view.ChannelMembersHUD, dataStore.player, userPr |
| | 102 | |
|
| 19 | 103 | | SetVisibility(isVisible); |
| 19 | 104 | | this.isVisible = isVisible; |
| 19 | 105 | | } |
| | 106 | |
|
| | 107 | | public void Setup(string channelId) |
| | 108 | | { |
| 18 | 109 | | if (string.IsNullOrEmpty(channelId) || channelId == this.channelId) return; |
| 18 | 110 | | this.channelId = channelId; |
| | 111 | |
|
| 18 | 112 | | channel = chatController.GetAllocatedChannel(channelId); |
| 18 | 113 | | View.Configure(ToPublicChatModel(channel)); |
| | 114 | |
|
| 18 | 115 | | chatHudController.ClearAllEntries(); |
| 18 | 116 | | } |
| | 117 | |
|
| | 118 | | public void Dispose() |
| | 119 | | { |
| 19 | 120 | | View.OnClose -= HandleViewClosed; |
| 19 | 121 | | View.OnBack -= HandleViewBacked; |
| 19 | 122 | | View.OnMuteChanged -= MuteChannel; |
| 19 | 123 | | View.OnGoToCrowd -= GoToCrowd; |
| | 124 | |
|
| 19 | 125 | | if (chatController != null) |
| | 126 | | { |
| 19 | 127 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 19 | 128 | | chatController.OnChannelUpdated -= HandleChannelUpdated; |
| | 129 | | } |
| | 130 | |
|
| 19 | 131 | | chatHudController.OnSendMessage -= SendChatMessage; |
| 19 | 132 | | chatHudController.OnMessageSentBlockedBySpam -= HandleMessageBlockedBySpam; |
| 19 | 133 | | chatHudController.Dispose(); |
| | 134 | |
|
| 19 | 135 | | if (mouseCatcher != null) |
| 18 | 136 | | mouseCatcher.OnMouseLock -= Hide; |
| | 137 | |
|
| 19 | 138 | | dataStore.mentions.someoneMentionedFromContextMenu.OnChange -= SomeoneMentionedFromContextMenu; |
| 19 | 139 | | nearbyPlayers.OnAdded -= UpdateMembersCount; |
| 19 | 140 | | nearbyPlayers.OnRemoved -= UpdateMembersCount; |
| | 141 | |
|
| 19 | 142 | | View?.Dispose(); |
| 19 | 143 | | nearbyMembersHUDController.Dispose(); |
| 19 | 144 | | } |
| | 145 | |
|
| | 146 | | public void SetVisibility(bool visible, bool focusInputField) |
| | 147 | | { |
| 28 | 148 | | if (isVisible == visible) |
| 19 | 149 | | return; |
| | 150 | |
|
| 9 | 151 | | isVisible = visible; |
| | 152 | |
|
| 9 | 153 | | SetVisiblePanelList(visible); |
| 9 | 154 | | chatHudController.SetVisibility(visible); |
| 9 | 155 | | dataStore.HUDs.chatInputVisible.Set(visible); |
| | 156 | |
|
| 9 | 157 | | if (visible) |
| | 158 | | { |
| 6 | 159 | | View.ChatHUD.ResetInputField(); |
| 6 | 160 | | View.Show(); |
| 6 | 161 | | MarkChannelMessagesAsRead(); |
| | 162 | |
|
| 6 | 163 | | if (focusInputField) |
| 0 | 164 | | chatHudController.FocusInputField(); |
| | 165 | |
|
| 6 | 166 | | nearbyMembersHUDController.ClearSearch(); |
| | 167 | | } |
| | 168 | | else |
| | 169 | | { |
| 3 | 170 | | chatHudController.UnfocusInputField(); |
| 3 | 171 | | View.Hide(); |
| | 172 | | } |
| 3 | 173 | | } |
| | 174 | |
|
| | 175 | | public void SetVisibility(bool visible) => |
| 28 | 176 | | SetVisibility(visible, false); |
| | 177 | |
|
| | 178 | | private void SendChatMessage(ChatMessage message) |
| | 179 | | { |
| 2 | 180 | | bool isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body); |
| 2 | 181 | | bool isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE; |
| | 182 | |
|
| 2 | 183 | | message.channelName = channel.Name; |
| | 184 | |
|
| 2 | 185 | | if (isValidMessage) |
| | 186 | | { |
| 2 | 187 | | chatHudController.ResetInputField(); |
| 2 | 188 | | chatHudController.FocusInputField(); |
| | 189 | | } |
| | 190 | | else |
| | 191 | | { |
| 0 | 192 | | HandleViewClosed(); |
| 0 | 193 | | SetVisibility(false); |
| 0 | 194 | | return; |
| | 195 | | } |
| | 196 | |
|
| 2 | 197 | | if (isPrivateMessage) |
| 1 | 198 | | message.body = $"/w {message.recipient} {message.body}"; |
| | 199 | |
|
| 2 | 200 | | chatController.Send(message); |
| 2 | 201 | | } |
| | 202 | |
|
| | 203 | | private void SetVisiblePanelList(bool visible) |
| | 204 | | { |
| 9 | 205 | | HashSet<string> newSet = visibleTaskbarPanels.Get(); |
| | 206 | |
|
| 9 | 207 | | if (visible) |
| 6 | 208 | | newSet.Add("PublicChatChannel"); |
| | 209 | | else |
| 3 | 210 | | newSet.Remove("PublicChatChannel"); |
| | 211 | |
|
| 9 | 212 | | visibleTaskbarPanels.Set(newSet, true); |
| 9 | 213 | | } |
| | 214 | |
|
| | 215 | | private void MarkChannelMessagesAsRead() => |
| 8 | 216 | | chatController.MarkChannelMessagesAsSeen(channelId); |
| | 217 | |
|
| | 218 | | private void HandleViewClosed() |
| | 219 | | { |
| 1 | 220 | | OnClosed?.Invoke(); |
| 1 | 221 | | } |
| | 222 | |
|
| | 223 | | private void HandleViewBacked() |
| | 224 | | { |
| 0 | 225 | | OnBack?.Invoke(); |
| 0 | 226 | | } |
| | 227 | |
|
| | 228 | | private void HandleMessageReceived(ChatMessage[] messages) |
| | 229 | | { |
| 6 | 230 | | var messageLogUpdated = false; |
| | 231 | |
|
| 6 | 232 | | var ownPlayerAlreadyMentioned = false; |
| | 233 | |
|
| 26 | 234 | | foreach (var message in messages) |
| | 235 | | { |
| 7 | 236 | | if (!ownPlayerAlreadyMentioned) |
| 7 | 237 | | ownPlayerAlreadyMentioned = CheckOwnPlayerMentionInNearBy(message); |
| | 238 | |
|
| 7 | 239 | | if (message.messageType != ChatMessage.Type.PUBLIC |
| | 240 | | && message.messageType != ChatMessage.Type.SYSTEM) continue; |
| | 241 | |
|
| 6 | 242 | | if (!string.IsNullOrEmpty(message.recipient)) continue; |
| | 243 | |
|
| 6 | 244 | | chatHudController.SetChatMessage(message, View.IsActive); |
| 6 | 245 | | messageLogUpdated = true; |
| | 246 | | } |
| | 247 | |
|
| 6 | 248 | | if (View.IsActive && messageLogUpdated) |
| 2 | 249 | | MarkChannelMessagesAsRead(); |
| 6 | 250 | | } |
| | 251 | |
|
| | 252 | | private bool CheckOwnPlayerMentionInNearBy(ChatMessage message) |
| | 253 | | { |
| 7 | 254 | | var ownUserProfile = userProfileBridge.GetOwn(); |
| | 255 | |
|
| 7 | 256 | | if (message.sender == ownUserProfile.userId || |
| | 257 | | message.messageType != ChatMessage.Type.PUBLIC || |
| | 258 | | !string.IsNullOrEmpty(message.recipient) || |
| | 259 | | View.IsActive || |
| | 260 | | !MentionsUtils.IsUserMentionedInText(ownUserProfile.userName, message.body)) |
| 6 | 261 | | return false; |
| | 262 | |
|
| 1 | 263 | | dataStore.mentions.ownPlayerMentionedInChannel.Set(ChatUtils.NEARBY_CHANNEL_ID, true); |
| 1 | 264 | | return true; |
| | 265 | | } |
| | 266 | |
|
| | 267 | | private void Hide() => |
| 1 | 268 | | SetVisibility(false); |
| | 269 | |
|
| | 270 | | private void HandleMessageBlockedBySpam(ChatMessage message) |
| | 271 | | { |
| 0 | 272 | | chatHudController.SetChatMessage(new ChatEntryModel |
| | 273 | | { |
| | 274 | | timestamp = (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| | 275 | | bodyText = "You sent too many messages in a short period of time. Please wait and try again later.", |
| | 276 | | messageId = Guid.NewGuid().ToString(), |
| | 277 | | messageType = ChatMessage.Type.SYSTEM, |
| | 278 | | subType = ChatEntryModel.SubType.RECEIVED |
| | 279 | | }).Forget(); |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | private void MuteChannel(bool muted) |
| | 283 | | { |
| 2 | 284 | | if (muted) |
| 1 | 285 | | chatController.MuteChannel(channelId); |
| | 286 | | else |
| 1 | 287 | | chatController.UnmuteChannel(channelId); |
| 1 | 288 | | } |
| | 289 | |
|
| | 290 | | private PublicChatModel ToPublicChatModel(Channel channel) => |
| 19 | 291 | | new (channel.ChannelId, |
| | 292 | | channel.Name, |
| | 293 | | channel.Description, |
| | 294 | | channel.Joined, |
| | 295 | | nearbyPlayers.Count(), |
| | 296 | | channel.Muted, |
| | 297 | | showOnlyOnlineMembersOnPublicChannels); |
| | 298 | |
|
| | 299 | | private void HandleChannelUpdated(Channel updatedChannel) |
| | 300 | | { |
| 1 | 301 | | if (updatedChannel.ChannelId != channelId) return; |
| 1 | 302 | | View.Configure(ToPublicChatModel(updatedChannel)); |
| 1 | 303 | | } |
| | 304 | |
|
| | 305 | | private void SomeoneMentionedFromContextMenu(string mention, string _) |
| | 306 | | { |
| 0 | 307 | | if (!View.IsActive) |
| 0 | 308 | | return; |
| | 309 | |
|
| 0 | 310 | | View.ChatHUD.AddTextIntoInputField(mention); |
| 0 | 311 | | } |
| | 312 | |
|
| | 313 | | private void ShowMembersList() => |
| 1 | 314 | | nearbyMembersHUDController.SetVisibility(true); |
| | 315 | |
|
| | 316 | | private void HideMembersList() => |
| 0 | 317 | | nearbyMembersHUDController.SetVisibility(false); |
| | 318 | |
|
| | 319 | | private void GoToCrowd() |
| | 320 | | { |
| | 321 | | // Requested temporally by product team since the "go to crowd" approach was always redirecting to the casin |
| | 322 | | //Environment.i.world.teleportController.GoToCrowd(); |
| 0 | 323 | | Environment.i.world.teleportController.Teleport(0, 0); |
| 0 | 324 | | } |
| | 325 | |
|
| | 326 | | private void UpdateMembersCount(string userId, Player player) => |
| 0 | 327 | | View.UpdateMembersCount(nearbyPlayers.Count()); |
| | 328 | | } |
| | 329 | | } |