| | 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 DCL.Tasks; |
| | 7 | | using DCLServices.CopyPaste.Analytics; |
| | 8 | | using SocialFeaturesAnalytics; |
| | 9 | | using System; |
| | 10 | | using System.Collections.Generic; |
| | 11 | | using System.Linq; |
| | 12 | | using System.Text.RegularExpressions; |
| | 13 | | using System.Threading; |
| | 14 | | using UnityEngine; |
| | 15 | |
|
| | 16 | | namespace DCL.Social.Chat |
| | 17 | | { |
| | 18 | | public class ChatHUDController : IHUD |
| | 19 | | { |
| | 20 | | public const int MAX_CHAT_ENTRIES = 30; |
| | 21 | | private const int TEMPORARILY_MUTE_MINUTES = 3; |
| | 22 | | private const int MAX_CONTINUOUS_MESSAGES = 10; |
| | 23 | | private const int MIN_MILLISECONDS_BETWEEN_MESSAGES = 1500; |
| | 24 | | private const int MAX_HISTORY_ITERATION = 10; |
| | 25 | | private const int MAX_MENTION_SUGGESTIONS = 5; |
| | 26 | |
|
| | 27 | | public delegate UniTask<List<UserProfile>> GetSuggestedUserProfiles(string name, int maxCount, CancellationToken |
| | 28 | |
|
| | 29 | | private readonly DataStore dataStore; |
| | 30 | | private readonly IUserProfileBridge userProfileBridge; |
| | 31 | | private readonly bool detectWhisper; |
| | 32 | | private readonly GetSuggestedUserProfiles getSuggestedUserProfiles; |
| | 33 | | private readonly InputAction_Trigger closeMentionSuggestionsTrigger; |
| | 34 | | private readonly IProfanityFilter profanityFilter; |
| | 35 | | private readonly ISocialAnalytics socialAnalytics; |
| | 36 | | private readonly IChatController chatController; |
| | 37 | | private readonly IClipboard clipboard; |
| | 38 | | private readonly ICopyPasteAnalyticsService copyPasteAnalyticsService; |
| 95 | 39 | | private readonly Regex mentionRegex = new (@"(\B@\w+)|(\B@+)"); |
| 95 | 40 | | private readonly Regex whisperRegex = new (@"(?i)^\/(whisper|w) (\S+)( *)(.*)"); |
| 95 | 41 | | private readonly Dictionary<string, ulong> temporarilyMutedSenders = new (); |
| 95 | 42 | | private readonly List<ChatEntryModel> spamMessages = new (); |
| 95 | 43 | | private readonly List<string> lastMessagesSent = new (); |
| 95 | 44 | | private readonly CancellationTokenSource profileFetchingCancellationToken = new (); |
| 95 | 45 | | private readonly CancellationTokenSource addMessagesCancellationToken = new (); |
| | 46 | |
|
| | 47 | | private int currentHistoryIteration; |
| | 48 | | private IChatHUDComponentView view; |
| 95 | 49 | | private CancellationTokenSource mentionSuggestionCancellationToken = new (); |
| | 50 | | private int mentionLength; |
| | 51 | | private int mentionFromIndex; |
| | 52 | | private Dictionary<string, UserProfile> mentionSuggestedProfiles; |
| | 53 | |
|
| 95 | 54 | | private bool useLegacySorting => dataStore.featureFlags.flags.Get().IsFeatureEnabled("legacy_chat_sorting_enable |
| 17 | 55 | | private bool isMentionsEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled("chat_mentions_enabled"); |
| | 56 | |
|
| | 57 | | public IComparer<ChatEntryModel> SortingStrategy |
| | 58 | | { |
| | 59 | | set |
| | 60 | | { |
| 37 | 61 | | if (view != null) |
| 37 | 62 | | view.SortingStrategy = value; |
| 37 | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public event Action OnInputFieldSelected; |
| | 67 | | public event Action<ChatMessage> OnSendMessage; |
| | 68 | | public event Action<ChatMessage> OnMessageSentBlockedBySpam; |
| | 69 | |
|
| 95 | 70 | | public ChatHUDController(DataStore dataStore, |
| | 71 | | IUserProfileBridge userProfileBridge, |
| | 72 | | bool detectWhisper, |
| | 73 | | GetSuggestedUserProfiles getSuggestedUserProfiles, |
| | 74 | | ISocialAnalytics socialAnalytics, |
| | 75 | | IChatController chatController, |
| | 76 | | IClipboard clipboard, |
| | 77 | | ICopyPasteAnalyticsService copyPasteAnalyticsService, |
| | 78 | | IProfanityFilter profanityFilter = null) |
| | 79 | | { |
| 95 | 80 | | this.dataStore = dataStore; |
| 95 | 81 | | this.userProfileBridge = userProfileBridge; |
| 95 | 82 | | this.detectWhisper = detectWhisper; |
| 95 | 83 | | this.getSuggestedUserProfiles = getSuggestedUserProfiles; |
| 95 | 84 | | this.socialAnalytics = socialAnalytics; |
| 95 | 85 | | this.chatController = chatController; |
| 95 | 86 | | this.clipboard = clipboard; |
| 95 | 87 | | this.copyPasteAnalyticsService = copyPasteAnalyticsService; |
| 95 | 88 | | this.profanityFilter = profanityFilter; |
| 95 | 89 | | } |
| | 90 | |
|
| | 91 | | public void Initialize(IChatHUDComponentView view) |
| | 92 | | { |
| 95 | 93 | | this.view = view; |
| 95 | 94 | | this.view.OnPreviousChatInHistory -= FillInputWithPreviousMessage; |
| 95 | 95 | | this.view.OnPreviousChatInHistory += FillInputWithPreviousMessage; |
| 95 | 96 | | this.view.OnNextChatInHistory -= FillInputWithNextMessage; |
| 95 | 97 | | this.view.OnNextChatInHistory += FillInputWithNextMessage; |
| 95 | 98 | | this.view.OnShowMenu -= ContextMenu_OnShowMenu; |
| 95 | 99 | | this.view.OnShowMenu += ContextMenu_OnShowMenu; |
| 95 | 100 | | this.view.OnInputFieldSelected -= HandleInputFieldSelected; |
| 95 | 101 | | this.view.OnInputFieldSelected += HandleInputFieldSelected; |
| 95 | 102 | | this.view.OnInputFieldDeselected -= HandleInputFieldDeselected; |
| 95 | 103 | | this.view.OnInputFieldDeselected += HandleInputFieldDeselected; |
| 95 | 104 | | this.view.OnSendMessage -= HandleSendMessage; |
| 95 | 105 | | this.view.OnSendMessage += HandleSendMessage; |
| 95 | 106 | | this.view.OnMessageUpdated -= HandleMessageUpdated; |
| 95 | 107 | | this.view.OnMessageUpdated += HandleMessageUpdated; |
| 95 | 108 | | this.view.OnMentionSuggestionSelected -= HandleMentionSuggestionSelected; |
| 95 | 109 | | this.view.OnMentionSuggestionSelected += HandleMentionSuggestionSelected; |
| 95 | 110 | | this.view.OnOpenedContextMenu -= OpenedContextMenu; |
| 95 | 111 | | this.view.OnOpenedContextMenu += OpenedContextMenu; |
| 95 | 112 | | this.view.OnCopyMessageRequested += HandleCopyMessageToClipboard; |
| 95 | 113 | | this.view.UseLegacySorting = useLegacySorting; |
| 95 | 114 | | } |
| | 115 | |
|
| | 116 | | public void Dispose() |
| | 117 | | { |
| 94 | 118 | | view.OnShowMenu -= ContextMenu_OnShowMenu; |
| 94 | 119 | | view.OnMessageUpdated -= HandleMessageUpdated; |
| 94 | 120 | | view.OnSendMessage -= HandleSendMessage; |
| 94 | 121 | | view.OnInputFieldSelected -= HandleInputFieldSelected; |
| 94 | 122 | | view.OnInputFieldDeselected -= HandleInputFieldDeselected; |
| 94 | 123 | | view.OnPreviousChatInHistory -= FillInputWithPreviousMessage; |
| 94 | 124 | | view.OnNextChatInHistory -= FillInputWithNextMessage; |
| 94 | 125 | | view.OnMentionSuggestionSelected -= HandleMentionSuggestionSelected; |
| 94 | 126 | | view.OnCopyMessageRequested -= HandleCopyMessageToClipboard; |
| 94 | 127 | | OnSendMessage = null; |
| 94 | 128 | | OnInputFieldSelected = null; |
| 94 | 129 | | view.Dispose(); |
| 94 | 130 | | mentionSuggestionCancellationToken.SafeCancelAndDispose(); |
| 94 | 131 | | profileFetchingCancellationToken.SafeCancelAndDispose(); |
| 94 | 132 | | addMessagesCancellationToken.SafeCancelAndDispose(); |
| 94 | 133 | | } |
| | 134 | |
|
| | 135 | | private void OpenedContextMenu(string userId) => |
| 0 | 136 | | socialAnalytics.SendClickedMention(userId); |
| | 137 | |
|
| | 138 | | public void SetVisibility(bool visible) |
| | 139 | | { |
| 32 | 140 | | if (!visible) |
| 10 | 141 | | HideMentionSuggestions(); |
| 32 | 142 | | } |
| | 143 | |
|
| | 144 | | public void SetChatMessage(ChatMessage message, bool setScrollPositionToBottom = false, |
| | 145 | | bool spamFiltering = true, bool limitMaxEntries = true) |
| | 146 | | { |
| | 147 | | async UniTaskVoid EnsureProfileThenUpdateMessage(string profileId, ChatEntryModel model, |
| | 148 | | Func<ChatEntryModel, UserProfile, ChatEntryModel> modificationCallback, |
| | 149 | | bool setScrollPositionToBottom, bool spamFiltering, bool limitMaxEntries, |
| | 150 | | CancellationToken cancellationToken) |
| | 151 | | { |
| | 152 | | try |
| | 153 | | { |
| 4 | 154 | | UserProfile requestedProfile = await userProfileBridge.RequestFullUserProfileAsync(profileId, cancel |
| | 155 | |
|
| 4 | 156 | | model = modificationCallback.Invoke(model, requestedProfile); |
| | 157 | |
|
| | 158 | | // avoid any possible race condition with the current AddChatMessage operation |
| 12 | 159 | | await UniTask.NextFrame(cancellationToken: cancellationToken); |
| | 160 | |
|
| 4 | 161 | | await SetChatMessage(model, setScrollPositionToBottom, spamFiltering, limitMaxEntries, cancellationT |
| 4 | 162 | | } |
| 0 | 163 | | catch (Exception e) when (e is not OperationCanceledException) { Debug.LogException(e); } |
| 4 | 164 | | } |
| | 165 | |
|
| | 166 | | string GetEllipsisFormat(string address) => |
| 4 | 167 | | address.Length <= 8 ? address : $"{address[..4]}...{address[^4..]}"; |
| | 168 | |
|
| 18 | 169 | | var model = new ChatEntryModel(); |
| 18 | 170 | | var ownProfile = userProfileBridge.GetOwn(); |
| | 171 | |
|
| 18 | 172 | | model.messageId = message.messageId; |
| 18 | 173 | | model.messageType = message.messageType; |
| 18 | 174 | | model.bodyText = message.body; |
| 18 | 175 | | model.timestamp = message.timestamp; |
| | 176 | |
|
| 18 | 177 | | if (!string.IsNullOrEmpty(message.recipient)) |
| | 178 | | { |
| 6 | 179 | | model.isChannelMessage = chatController.GetAllocatedChannel(message.recipient) != null; |
| | 180 | |
|
| 6 | 181 | | if (!model.isChannelMessage) |
| | 182 | | { |
| 4 | 183 | | UserProfile recipientProfile = userProfileBridge.Get(message.recipient); |
| | 184 | |
|
| 4 | 185 | | if (recipientProfile != null) |
| 2 | 186 | | model.recipientName = recipientProfile.userName; |
| | 187 | | else |
| | 188 | | { |
| 2 | 189 | | model.recipientName = GetEllipsisFormat(message.recipient); |
| 2 | 190 | | model.isLoadingNames = true; |
| | 191 | |
|
| | 192 | | // sometimes there is no cached profile, so we request it |
| | 193 | | // dont block the operation of showing the message immediately |
| | 194 | | // just update the message information after we get the profile |
| 2 | 195 | | EnsureProfileThenUpdateMessage(message.recipient, model, |
| | 196 | | (m, p) => |
| | 197 | | { |
| 2 | 198 | | m.recipientName = p.userName; |
| 2 | 199 | | return m; |
| | 200 | | }, |
| | 201 | | setScrollPositionToBottom, spamFiltering, |
| | 202 | | limitMaxEntries, |
| | 203 | | profileFetchingCancellationToken.Token) |
| | 204 | | .Forget(); |
| | 205 | | } |
| | 206 | | } |
| | 207 | | } |
| | 208 | |
|
| 18 | 209 | | if (!string.IsNullOrEmpty(message.sender)) |
| | 210 | | { |
| 18 | 211 | | model.senderId = message.sender; |
| 18 | 212 | | UserProfile senderProfile = userProfileBridge.Get(message.sender); |
| | 213 | |
|
| 18 | 214 | | if (senderProfile != null) |
| 16 | 215 | | model.senderName = senderProfile.userName; |
| | 216 | | else |
| | 217 | | { |
| 2 | 218 | | model.senderName = GetEllipsisFormat(message.sender); |
| 2 | 219 | | model.isLoadingNames = true; |
| | 220 | |
|
| | 221 | | // sometimes there is no cached profile, so we request it |
| | 222 | | // dont block the operation of showing the message immediately |
| | 223 | | // just update the message information after we get the profile |
| 2 | 224 | | EnsureProfileThenUpdateMessage(message.sender, model, |
| | 225 | | (m, p) => |
| | 226 | | { |
| 2 | 227 | | m.senderName = p.userName; |
| 2 | 228 | | return m; |
| | 229 | | }, |
| | 230 | | setScrollPositionToBottom, spamFiltering, |
| | 231 | | limitMaxEntries, |
| | 232 | | profileFetchingCancellationToken.Token) |
| | 233 | | .Forget(); |
| | 234 | | } |
| | 235 | | } |
| | 236 | |
|
| 18 | 237 | | if (message.messageType == ChatMessage.Type.PRIVATE) |
| | 238 | | { |
| 9 | 239 | | model.subType = message.sender == ownProfile.userId |
| | 240 | | ? ChatEntryModel.SubType.SENT |
| | 241 | | : ChatEntryModel.SubType.RECEIVED; |
| | 242 | | } |
| 9 | 243 | | else if (message.messageType == ChatMessage.Type.PUBLIC) |
| | 244 | | { |
| 9 | 245 | | model.subType = message.sender == ownProfile.userId |
| | 246 | | ? ChatEntryModel.SubType.SENT |
| | 247 | | : ChatEntryModel.SubType.RECEIVED; |
| | 248 | | } |
| | 249 | |
|
| 18 | 250 | | SetChatMessage(model, setScrollPositionToBottom, spamFiltering, limitMaxEntries, |
| | 251 | | addMessagesCancellationToken.Token) |
| | 252 | | .Forget(); |
| 18 | 253 | | } |
| | 254 | |
|
| | 255 | | public async UniTask SetChatMessage(ChatEntryModel chatEntryModel, bool setScrollPositionToBottom = false, bool |
| | 256 | | CancellationToken cancellationToken = default) |
| | 257 | | { |
| 34 | 258 | | if (IsSpamming(chatEntryModel.senderName) && spamFiltering) return; |
| | 259 | |
|
| 34 | 260 | | chatEntryModel.bodyText = ChatUtils.AddNoParse(chatEntryModel.bodyText); |
| | 261 | |
|
| 34 | 262 | | if (IsProfanityFilteringEnabled() && chatEntryModel.messageType != ChatMessage.Type.PRIVATE) |
| | 263 | | { |
| 12 | 264 | | chatEntryModel.bodyText = await profanityFilter.Filter(chatEntryModel.bodyText, cancellationToken); |
| | 265 | |
|
| 12 | 266 | | if (!string.IsNullOrEmpty(chatEntryModel.senderName)) |
| 12 | 267 | | chatEntryModel.senderName = await profanityFilter.Filter(chatEntryModel.senderName, cancellationToke |
| | 268 | |
|
| 12 | 269 | | if (!string.IsNullOrEmpty(chatEntryModel.recipientName)) |
| 2 | 270 | | chatEntryModel.recipientName = await profanityFilter.Filter(chatEntryModel.recipientName, cancellati |
| | 271 | | } |
| | 272 | |
|
| 34 | 273 | | await UniTask.SwitchToMainThread(cancellationToken: cancellationToken); |
| | 274 | |
|
| 34 | 275 | | view.SetEntry(chatEntryModel, setScrollPositionToBottom); |
| | 276 | |
|
| 34 | 277 | | if (limitMaxEntries && view.EntryCount > MAX_CHAT_ENTRIES) |
| 1 | 278 | | view.RemoveOldestEntry(); |
| | 279 | |
|
| 48 | 280 | | if (string.IsNullOrEmpty(chatEntryModel.senderId)) return; |
| | 281 | |
|
| 20 | 282 | | if (spamFiltering) |
| 20 | 283 | | UpdateSpam(chatEntryModel); |
| 34 | 284 | | } |
| | 285 | |
|
| | 286 | | public void ClearAllEntries() => |
| 50 | 287 | | view.ClearAllEntries(); |
| | 288 | |
|
| | 289 | | public void ResetInputField(bool loseFocus = false) => |
| 6 | 290 | | view.ResetInputField(loseFocus); |
| | 291 | |
|
| | 292 | | public void FocusInputField() => |
| 20 | 293 | | view.FocusInputField(); |
| | 294 | |
|
| | 295 | | public void SetInputFieldText(string setInputText) => |
| 3 | 296 | | view.SetInputFieldText(setInputText); |
| | 297 | |
|
| | 298 | | public void UnfocusInputField() => |
| 27 | 299 | | view.UnfocusInputField(); |
| | 300 | |
|
| | 301 | | private void ContextMenu_OnShowMenu() => |
| 0 | 302 | | view.OnMessageCancelHover(); |
| | 303 | |
|
| | 304 | | private bool IsProfanityFilteringEnabled() => |
| 34 | 305 | | dataStore.settings.profanityChatFilteringEnabled.Get() |
| | 306 | | && profanityFilter != null; |
| | 307 | |
|
| | 308 | | private void HandleMessageUpdated(string message, int cursorPosition) => |
| 17 | 309 | | UpdateMentions(message, cursorPosition); |
| | 310 | |
|
| | 311 | | private void UpdateMentions(string message, int cursorPosition) |
| | 312 | | { |
| 17 | 313 | | if (!isMentionsEnabled) return; |
| | 314 | |
|
| 17 | 315 | | if (string.IsNullOrEmpty(message)) |
| | 316 | | { |
| 0 | 317 | | HideMentionSuggestions(); |
| 0 | 318 | | return; |
| | 319 | | } |
| | 320 | |
|
| | 321 | | async UniTaskVoid ShowMentionSuggestionsAsync(string name, CancellationToken cancellationToken) |
| | 322 | | { |
| | 323 | | try |
| | 324 | | { |
| 13 | 325 | | List<UserProfile> suggestions = await getSuggestedUserProfiles.Invoke(name, MAX_MENTION_SUGGESTIONS, |
| | 326 | |
|
| 24 | 327 | | mentionSuggestedProfiles = suggestions.ToDictionary(profile => profile.userId, profile => profile); |
| | 328 | |
|
| 12 | 329 | | if (suggestions.Count == 0) |
| 9 | 330 | | HideMentionSuggestions(); |
| | 331 | | else |
| | 332 | | { |
| 3 | 333 | | view.ShowMentionSuggestions(); |
| 3 | 334 | | dataStore.mentions.isMentionSuggestionVisible.Set(true); |
| | 335 | |
|
| 9 | 336 | | view.SetMentionSuggestions(suggestions.Select(profile => new ChatMentionSuggestionModel |
| | 337 | | { |
| | 338 | | userId = profile.userId, |
| | 339 | | userName = profile.userName, |
| | 340 | | imageUrl = profile.face256SnapshotURL, |
| | 341 | | }) |
| | 342 | | .ToList()); |
| | 343 | | } |
| 12 | 344 | | } |
| 3 | 345 | | catch (Exception e) when (e is not OperationCanceledException) { HideMentionSuggestions(); } |
| 13 | 346 | | } |
| | 347 | |
|
| 17 | 348 | | int lastWrittenCharacterIndex = Math.Max(0, cursorPosition - 1); |
| | 349 | |
|
| 17 | 350 | | if (mentionFromIndex >= message.Length || message[lastWrittenCharacterIndex] == ' ') |
| 2 | 351 | | mentionFromIndex = cursorPosition; |
| | 352 | |
|
| 17 | 353 | | Match match = mentionRegex.Match(message, mentionFromIndex); |
| | 354 | |
|
| 17 | 355 | | if (match.Success) |
| | 356 | | { |
| 13 | 357 | | mentionSuggestionCancellationToken = mentionSuggestionCancellationToken.SafeRestart(); |
| 13 | 358 | | mentionFromIndex = match.Index; |
| 13 | 359 | | mentionLength = match.Length; |
| 13 | 360 | | string name = match.Value[1..]; |
| 13 | 361 | | ShowMentionSuggestionsAsync(name, mentionSuggestionCancellationToken.Token).Forget(); |
| | 362 | | } |
| | 363 | | else |
| | 364 | | { |
| 4 | 365 | | mentionSuggestionCancellationToken.SafeCancelAndDispose(); |
| 4 | 366 | | mentionFromIndex = lastWrittenCharacterIndex; |
| 4 | 367 | | HideMentionSuggestions(); |
| | 368 | | } |
| 4 | 369 | | } |
| | 370 | |
|
| | 371 | | private void HandleSendMessage(ChatMessage message) |
| | 372 | | { |
| 16 | 373 | | mentionFromIndex = 0; |
| 16 | 374 | | HideMentionSuggestions(); |
| | 375 | |
|
| 16 | 376 | | var ownProfile = userProfileBridge.GetOwn(); |
| 16 | 377 | | message.sender = ownProfile.userId; |
| | 378 | |
|
| 16 | 379 | | RegisterMessageHistory(message); |
| 16 | 380 | | currentHistoryIteration = 0; |
| | 381 | |
|
| 16 | 382 | | if (IsSpamming(message.sender) || (IsSpamming(ownProfile.userName) && !string.IsNullOrEmpty(message.body))) |
| | 383 | | { |
| 0 | 384 | | OnMessageSentBlockedBySpam?.Invoke(message); |
| 0 | 385 | | return; |
| | 386 | | } |
| | 387 | |
|
| 32 | 388 | | foreach (string mention in MentionsUtils.GetAllMentions(message.body)) |
| 0 | 389 | | socialAnalytics.SendMessageWithMention(mention); |
| | 390 | |
|
| 16 | 391 | | ApplyWhisperAttributes(message); |
| | 392 | |
|
| 16 | 393 | | if (message.body.ToLower().StartsWith("/join")) |
| | 394 | | { |
| 1 | 395 | | if (!ownProfile.isGuest) |
| 1 | 396 | | dataStore.channels.channelJoinedSource.Set(ChannelJoinedSource.Command); |
| | 397 | | else |
| | 398 | | { |
| 0 | 399 | | dataStore.HUDs.connectWalletModalVisible.Set(true); |
| 0 | 400 | | return; |
| | 401 | | } |
| | 402 | | } |
| | 403 | |
|
| 16 | 404 | | OnSendMessage?.Invoke(message); |
| 8 | 405 | | } |
| | 406 | |
|
| | 407 | | private void RegisterMessageHistory(ChatMessage message) |
| | 408 | | { |
| 16 | 409 | | if (string.IsNullOrEmpty(message.body)) return; |
| | 410 | |
|
| 20 | 411 | | lastMessagesSent.RemoveAll(s => s.Equals(message.body)); |
| 16 | 412 | | lastMessagesSent.Insert(0, message.body); |
| | 413 | |
|
| 16 | 414 | | if (lastMessagesSent.Count > MAX_HISTORY_ITERATION) |
| 0 | 415 | | lastMessagesSent.RemoveAt(lastMessagesSent.Count - 1); |
| 16 | 416 | | } |
| | 417 | |
|
| | 418 | | private void ApplyWhisperAttributes(ChatMessage message) |
| | 419 | | { |
| 18 | 420 | | if (!detectWhisper) return; |
| 14 | 421 | | var body = message.body; |
| 14 | 422 | | if (string.IsNullOrWhiteSpace(body)) return; |
| | 423 | |
|
| 14 | 424 | | var match = whisperRegex.Match(body); |
| 26 | 425 | | if (!match.Success) return; |
| | 426 | |
|
| 2 | 427 | | message.messageType = ChatMessage.Type.PRIVATE; |
| 2 | 428 | | message.recipient = match.Groups[2].Value; |
| 2 | 429 | | message.body = match.Groups[4].Value; |
| 2 | 430 | | } |
| | 431 | |
|
| | 432 | | private void HandleInputFieldSelected() |
| | 433 | | { |
| 0 | 434 | | currentHistoryIteration = 0; |
| 0 | 435 | | OnInputFieldSelected?.Invoke(); |
| 0 | 436 | | } |
| | 437 | |
|
| | 438 | | private void HandleInputFieldDeselected() => |
| 0 | 439 | | currentHistoryIteration = 0; |
| | 440 | |
|
| | 441 | | private bool IsSpamming(string senderName) |
| | 442 | | { |
| 80 | 443 | | if (string.IsNullOrEmpty(senderName)) return false; |
| | 444 | |
|
| 52 | 445 | | var isSpamming = false; |
| | 446 | |
|
| 104 | 447 | | if (!temporarilyMutedSenders.ContainsKey(senderName)) return false; |
| | 448 | |
|
| 0 | 449 | | var muteTimestamp = DateTimeOffset.FromUnixTimeMilliseconds((long)temporarilyMutedSenders[senderName]); |
| | 450 | |
|
| 0 | 451 | | if ((DateTimeOffset.UtcNow - muteTimestamp).Minutes < TEMPORARILY_MUTE_MINUTES) |
| 0 | 452 | | isSpamming = true; |
| | 453 | | else |
| 0 | 454 | | temporarilyMutedSenders.Remove(senderName); |
| | 455 | |
|
| 0 | 456 | | return isSpamming; |
| | 457 | | } |
| | 458 | |
|
| | 459 | | private void UpdateSpam(ChatEntryModel model) |
| | 460 | | { |
| 20 | 461 | | if (spamMessages.Count == 0) |
| 13 | 462 | | spamMessages.Add(model); |
| 7 | 463 | | else if (spamMessages[^1].senderName == model.senderName) |
| | 464 | | { |
| 5 | 465 | | if (MessagesSentTooFast(spamMessages[^1].timestamp, model.timestamp)) |
| | 466 | | { |
| 5 | 467 | | spamMessages.Add(model); |
| | 468 | |
|
| 5 | 469 | | if (spamMessages.Count >= MAX_CONTINUOUS_MESSAGES) |
| | 470 | | { |
| 0 | 471 | | temporarilyMutedSenders.Add(model.senderName, model.timestamp); |
| 0 | 472 | | spamMessages.Clear(); |
| | 473 | | } |
| | 474 | | } |
| | 475 | | else |
| 0 | 476 | | spamMessages.Clear(); |
| | 477 | | } |
| | 478 | | else |
| 2 | 479 | | spamMessages.Clear(); |
| 7 | 480 | | } |
| | 481 | |
|
| | 482 | | private bool MessagesSentTooFast(ulong oldMessageTimeStamp, ulong newMessageTimeStamp) |
| | 483 | | { |
| 5 | 484 | | var oldDateTime = DateTimeOffset.FromUnixTimeMilliseconds((long)oldMessageTimeStamp); |
| 5 | 485 | | var newDateTime = DateTimeOffset.FromUnixTimeMilliseconds((long)newMessageTimeStamp); |
| 5 | 486 | | return (newDateTime - oldDateTime).TotalMilliseconds < MIN_MILLISECONDS_BETWEEN_MESSAGES; |
| | 487 | | } |
| | 488 | |
|
| | 489 | | private void FillInputWithNextMessage() |
| | 490 | | { |
| 5 | 491 | | if (lastMessagesSent.Count == 0) return; |
| 5 | 492 | | view.FocusInputField(); |
| 5 | 493 | | view.SetInputFieldText(lastMessagesSent[currentHistoryIteration]); |
| 5 | 494 | | currentHistoryIteration = (currentHistoryIteration + 1) % lastMessagesSent.Count; |
| 5 | 495 | | } |
| | 496 | |
|
| | 497 | | private void FillInputWithPreviousMessage() |
| | 498 | | { |
| 2 | 499 | | if (lastMessagesSent.Count == 0) |
| | 500 | | { |
| 0 | 501 | | view.ResetInputField(); |
| 0 | 502 | | return; |
| | 503 | | } |
| | 504 | |
|
| 2 | 505 | | currentHistoryIteration--; |
| | 506 | |
|
| 2 | 507 | | if (currentHistoryIteration < 0) |
| 1 | 508 | | currentHistoryIteration = lastMessagesSent.Count - 1; |
| | 509 | |
|
| 2 | 510 | | view.FocusInputField(); |
| 2 | 511 | | view.SetInputFieldText(lastMessagesSent[currentHistoryIteration]); |
| 2 | 512 | | } |
| | 513 | |
|
| | 514 | | private void HandleMentionSuggestionSelected(string userId) |
| | 515 | | { |
| 0 | 516 | | view.AddMentionToInputField(mentionFromIndex, mentionLength, userId, mentionSuggestedProfiles[userId].userNa |
| 0 | 517 | | socialAnalytics.SendMentionCreated(MentionCreationSource.SuggestionList, userId); |
| 0 | 518 | | HideMentionSuggestions(); |
| 0 | 519 | | } |
| | 520 | |
|
| | 521 | | private void HideMentionSuggestions() |
| | 522 | | { |
| 40 | 523 | | view.HideMentionSuggestions(); |
| 40 | 524 | | dataStore.mentions.isMentionSuggestionVisible.Set(false); |
| 40 | 525 | | } |
| | 526 | |
|
| | 527 | | private void HandleCopyMessageToClipboard(ChatEntryModel model) |
| | 528 | | { |
| 1 | 529 | | clipboard.WriteText(ChatUtils.RemoveNoParse(model.bodyText)); |
| 1 | 530 | | copyPasteAnalyticsService.Copy("message"); |
| 1 | 531 | | } |
| | 532 | | } |
| | 533 | | } |