Skip to content

Commit

Permalink
chore: move message processing onto a bg thread
Browse files Browse the repository at this point in the history
  • Loading branch information
not-nullptr committed Sep 29, 2024
1 parent 1c9e7ea commit 4d71778
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions Aerochat/Windows/Chat.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 4d71778

Please sign in to comment.