From 4d717787f5126022fad67fcff180a7a8b6577b52 Mon Sep 17 00:00:00 2001 From: not-nullptr Date: Sun, 29 Sep 2024 01:21:39 +0100 Subject: [PATCH] chore: move message processing onto a bg thread --- Aerochat/Windows/Chat.xaml.cs | 51 ++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/Aerochat/Windows/Chat.xaml.cs b/Aerochat/Windows/Chat.xaml.cs index 4182ca78..a4fc9ac5 100644 --- a/Aerochat/Windows/Chat.xaml.cs +++ b/Aerochat/Windows/Chat.xaml.cs @@ -494,54 +494,57 @@ protected override void OnClosing(CancelEventArgs e) private async Task OnMessageCreation(DSharpPlus.DiscordClient sender, DSharpPlus.EventArgs.MessageCreateEventArgs args) { + if (args.Channel.Id != ChannelId) return; args.Channel.GetType().GetProperty("LastMessageId")?.SetValue(args.Channel, args.Message.Id); Dispatcher.Invoke(UpdateChannelListerReadReciepts); if (args.Channel.Id != ChannelId) return; bool isNudge = args.Message.Content == "[nudge]"; - if (!Discord.Client.TryGetCachedUser(args.Author.Id, out DiscordUser user)) - { - if (args.Author == null) user = await Discord.Client.GetUserAsync(args.Author.Id); - else user = args.Author; - }; + DiscordUser user = args.Author; + if (user is null) return; - Application.Current.Dispatcher.Invoke(delegate - { + var member = args.Guild?.Members.FirstOrDefault(x => x.Key == args.Author.Id).Value; + + MessageViewModel message = MessageViewModel.FromMessage(args.Message, member); - var member = args.Guild?.Members.FirstOrDefault(x => x.Key == args.Author.Id).Value; + MessageViewModel? eph = ViewModel.Messages.FirstOrDefault(x => x.Ephemeral && x.Message == message.Message); - MessageViewModel message = MessageViewModel.FromMessage(args.Message, member); + int messageIndex = -1; - MessageViewModel? eph = ViewModel.Messages.FirstOrDefault(x => x.Ephemeral && x.Message == message.Message); - if (eph != null) + if (eph != null) + { + messageIndex = ViewModel.Messages.IndexOf(eph); + } + + Dispatcher.Invoke(() => + { + if (messageIndex != -1) { - int messageIndex = ViewModel.Messages.IndexOf(eph); - if (messageIndex != -1) - { - ViewModel.Messages.RemoveAt(messageIndex); - } + ViewModel.Messages.RemoveAt(messageIndex); } ViewModel.LastReceivedMessage = message; - ViewModel.Messages.Add(message); + }); - // if the user is in the typing users list, remove them - if (TypingUsers.Contains(args.Author)) - { - TypingUsers.Remove(args.Author); - } + // if the user is in the typing users list, remove them + if (TypingUsers.Contains(args.Author)) + { + TypingUsers.Remove(args.Author); + } + Dispatcher.Invoke(() => + { if (isNudge) { chatSoundPlayer.Open(new Uri("Resources/Sounds/nudge.wav", UriKind.Relative)); } else { - if (IsActive && message.Author.Id != Discord.Client.CurrentUser.Id) chatSoundPlayer.Open(new Uri("Resources/Sounds/type.wav", UriKind.Relative)); + if (IsActive && message.Author?.Id != Discord.Client.CurrentUser.Id) chatSoundPlayer.Open(new Uri("Resources/Sounds/type.wav", UriKind.Relative)); } - ProcessLastRead(); }); + Dispatcher.Invoke(ProcessLastRead); if (isNudge) {