Skip to content

Commit

Permalink
Hotfix: optimize handling of CHANNEL_UNREAD_UPDATE (#105)
Browse files Browse the repository at this point in the history
* Fix a bunch of bugs

#96, #100, #101, #102

Open in New Window menu item not working for channels
Generally improve consistency

* Hotfix: optimise CHANNEL_UNREAD_UPDATE handling

* Update DSharpPlus

* Update MarkdownTextBlock.Methods.cs
  • Loading branch information
WamWooWam authored Aug 31, 2024
1 parent 2eddc22 commit 2590401
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Libraries/DSharpPlus
2 changes: 1 addition & 1 deletion Unicord.Universal.Package/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
IgnorableNamespaces="uap mp uap2 uap3 uap4 uap5 rescap">
<Identity Name="24101WamWooWamRD.Unicord" Publisher="CN=0F22111D-EDF0-42F0-B58D-26C4C5C5054B" Version="2.0.3.0" />
<Identity Name="24101WamWooWamRD.Unicord" Publisher="CN=0F22111D-EDF0-42F0-B58D-26C4C5C5054B" Version="2.0.4.0" />
<mp:PhoneIdentity PhoneProductId="5783aabf-3049-421f-ae1d-e88bd89018f2" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>ms-resource:AppDisplayName</DisplayName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private bool IsHuge(MarkdownDocument markdown)
if (inline.Type == MarkdownInlineType.TextRun)
{
var text = ((TextRunInline)inline).Text;
// TODO: add a limit to this
if (!string.IsNullOrWhiteSpace(text) && !NeoSmart.Unicode.Emoji.IsEmoji(text))
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion Unicord.Universal/Models/Channels/ChannelPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ public bool ShowSlowMode
public string SlowModeText
=> string.Format(_strings.GetString(ImmuneToSlowMode ? "ImmuneSlowModeFormat" : "SlowModeFormat"), TimeSpan.FromSeconds(Channel.PerUserRateLimit ?? 0).ToNaturalString());

private bool ImmuneToSlowMode => Permissions.HasPermission(Permissions.ManageMessages) && Permissions.HasPermission(Permissions.ManageChannels);
private bool ImmuneToSlowMode
=> Permissions.HasPermission(Permissions.ManageMessages) && Permissions.HasPermission(Permissions.ManageChannels);

public bool ShowTypingUsers
=> TypingUsers?.Any() == true;
Expand Down
12 changes: 11 additions & 1 deletion Unicord.Universal/Models/Channels/ReadStateViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using Microsoft.Toolkit.Mvvm.Messaging;
using Unicord.Universal.Models;

namespace Unicord.Universal.Models.Channels
{
Expand All @@ -26,9 +25,20 @@ public ReadStateViewModel(ulong channelId, ChannelViewModel channelViewModel = n
if (!App.Discord.ReadStates.TryGetValue(channelId, out readState))
readState = App.Discord.DefaultReadState;

WeakReferenceMessenger.Default.Register<ReadStateViewModel, ChannelUnreadUpdateEventArgs>(this, (r, m) => r.OnChannelUnreadUpdate(m.Event));
WeakReferenceMessenger.Default.Register<ReadStateViewModel, ReadStateUpdatedEventArgs>(this, (r, m) => r.OnReadStateUpdated(m.Event));
}

private void OnChannelUnreadUpdate(ChannelUnreadUpdateEventArgs e)
{
if (!e.ReadStates.ContainsKey(channelId))
return;

InvokePropertyChanged(nameof(Unread));
InvokePropertyChanged(nameof(MentionCount));
InvokePropertyChanged(nameof(LastMessageId));
}

private void OnReadStateUpdated(ReadStateUpdatedEventArgs e)
{
if (e.ReadState.Id != readState.Id)
Expand Down
16 changes: 12 additions & 4 deletions Unicord.Universal/Models/Guild/GuildViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ public GuildViewModel(ulong guildId, ViewModelBase parent = null)
: base(parent)
{
_guildId = guildId;
//_accessibleChannels = new ConcurrentDictionary<ulong, ChannelViewModel>();

//PopulateAccessibleChannels();

WeakReferenceMessenger.Default.Register<GuildViewModel, ChannelUnreadUpdateEventArgs>(this, (r, m) => r.OnChannelUnreadUpdate(m.Event));
WeakReferenceMessenger.Default.Register<GuildViewModel, ReadStateUpdatedEventArgs>(this, (r, m) => r.OnReadStateUpdated(m.Event));
}

Expand Down Expand Up @@ -71,6 +69,16 @@ private Task OnReadStateUpdated(ReadStateUpdatedEventArgs e)
return Task.CompletedTask;
}

private Task OnChannelUnreadUpdate(ChannelUnreadUpdateEventArgs e)
{
if (e.GuildId == Id)
{
InvokePropertyChanged(nameof(Unread));
}

return Task.CompletedTask;
}

// TODO: update this cache when the user's roles change, and when the guild gets updated, and when a new channel gets created :D
private void PopulateAccessibleChannels()
{
Expand All @@ -87,7 +95,7 @@ private void PopulateAccessibleChannels()
{
foreach (var (key, value) in Guild.Channels)
{
if (!value.PermissionsFor(Guild.CurrentMember).HasFlag(Permissions.AccessChannels))
if (!value.PermissionsFor(Guild.CurrentMember).HasFlag(Permissions.AccessChannels))
continue;

if (value.Parent != null && !value.Parent.PermissionsFor(Guild.CurrentMember).HasFlag(Permissions.AccessChannels))
Expand Down
7 changes: 7 additions & 0 deletions Unicord.Universal/Models/Messaging/DiscordClientMessenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static void Register(DiscordClient client)
client.ChannelCreated += OnChannelCreated;
client.ChannelDeleted += OnChannelDeleted;
client.ChannelUpdated += OnChannelUpdated;
client.ChannelUnreadUpdated += OnChannelUnreadUpdated;
client.TypingStarted += OnTypingStarted;
client.GuildCreated += OnGuildCreated;
client.GuildDeleted += OnGuildDeleted;
Expand Down Expand Up @@ -58,6 +59,7 @@ public static void Unregister(DiscordClient client)
client.ChannelCreated -= OnChannelCreated;
client.ChannelDeleted -= OnChannelDeleted;
client.ChannelUpdated -= OnChannelUpdated;
client.ChannelUnreadUpdated -= OnChannelUnreadUpdated;
client.TypingStarted -= OnTypingStarted;
client.GuildCreated -= OnGuildCreated;
client.GuildDeleted -= OnGuildDeleted;
Expand Down Expand Up @@ -206,5 +208,10 @@ private static Task OnGuildMembersChunked(GuildMembersChunkEventArgs e)
{
return Task.WhenAll(WeakReferenceMessenger.Default.Send(e));
}

private static Task OnChannelUnreadUpdated(ChannelUnreadUpdateEventArgs e)
{
return Task.WhenAll(WeakReferenceMessenger.Default.Send(e));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public override async Task<WindowHandle> OpenChannelWindowAsync(DiscordChannel c
var handle = (AppWindowHandle)CreateOrUpdateHandle(window);

var frame = new Frame();
//frame.Background =
frame.Navigate(typeof(MainPage), new MainPageArgs() { ChannelId = channel.Id, FullFrame = true });

ElementCompositionPreview.SetAppWindowContent(window, frame);
Expand Down
4 changes: 3 additions & 1 deletion Unicord.Universal/Unicord.Universal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,9 @@
<PRIResource Include="Resources\en-GB\DMChannelsPage.resw" />
<PRIResource Include="Resources\en-GB\Dialogs.resw" />
<PRIResource Include="Resources\en-GB\Converters.resw" />
<PRIResource Include="Resources\en-GB\Controls.resw" />
<PRIResource Include="Resources\en-GB\Controls.resw">
<SubType>Designer</SubType>
</PRIResource>
<PRIResource Include="Resources\en-GB\ChannelPage.resw" />
<PRIResource Include="Resources\en-GB\AgeGatePage.resw" />
<PRIResource Include="Resources\en-GB\AccountsSettingsPage.resw" />
Expand Down

0 comments on commit 2590401

Please sign in to comment.