From 7ca3a1895a412092da45c7db10c3c7babec59b40 Mon Sep 17 00:00:00 2001 From: Zihad Date: Thu, 27 Mar 2025 23:10:11 +0600 Subject: [PATCH] Hide blocked users from currently online --- .../Dashboard/CurrentlyOnlineDisplay.cs | 64 +++++++++++++++++-- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/osu.Game/Overlays/Dashboard/CurrentlyOnlineDisplay.cs b/osu.Game/Overlays/Dashboard/CurrentlyOnlineDisplay.cs index 39df3ba22c..bda23078d9 100644 --- a/osu.Game/Overlays/Dashboard/CurrentlyOnlineDisplay.cs +++ b/osu.Game/Overlays/Dashboard/CurrentlyOnlineDisplay.cs @@ -2,7 +2,9 @@ // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; +using System.Collections.Specialized; using System.Diagnostics; +using System.Linq; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Extensions; @@ -14,6 +16,7 @@ using osu.Framework.Localisation; using osu.Framework.Screens; using osu.Game.Database; using osu.Game.Graphics.UserInterface; +using osu.Game.Online.API; using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.Metadata; using osu.Game.Resources.Localisation.Web; @@ -31,6 +34,7 @@ namespace osu.Game.Overlays.Dashboard private const float padding = 10; private readonly IBindableDictionary onlineUserPresences = new BindableDictionary(); + private readonly IBindableList blockedUsers = new BindableList(); private readonly Dictionary userPanels = new Dictionary(); private SearchContainer userFlow = null!; @@ -42,6 +46,9 @@ namespace osu.Game.Overlays.Dashboard [Resolved] private UserLookupCache users { get; set; } = null!; + [Resolved] + private IAPIProvider api { get; set; } = null!; + [BackgroundDependencyLoader] private void load(OverlayColourProvider colourProvider) { @@ -95,6 +102,8 @@ namespace osu.Game.Overlays.Dashboard onlineUserPresences.BindTo(metadataClient.UserPresences); onlineUserPresences.BindCollectionChanged(onUserPresenceUpdated, true); + blockedUsers.BindTo(api.Blocks); + blockedUsers.BindCollectionChanged(onBlocksUpdated); } protected override void OnFocus(FocusEvent e) @@ -104,6 +113,36 @@ namespace osu.Game.Overlays.Dashboard searchTextBox.TakeFocus(); } + private void onBlocksUpdated(object? sender, NotifyCollectionChangedEventArgs e) + { + switch (e.Action) + { + case NotifyCollectionChangedAction.Add: + Debug.Assert(e.NewItems != null); + + foreach (APIRelation block in e.NewItems.Cast()) + { + int userId = block.TargetID; + removeUserPanel(userId); + } + + break; + + case NotifyCollectionChangedAction.Remove: + Debug.Assert(e.OldItems != null); + + foreach (APIRelation block in e.OldItems) + { + int userId = block.TargetID; + if (!onlineUserPresences.ContainsKey(userId)) continue; + + addUserPanel(userId); + } + + break; + } + } + private void onUserPresenceUpdated(object? sender, NotifyDictionaryChangedEventArgs e) => Schedule(() => { switch (e.Action) @@ -114,12 +153,9 @@ namespace osu.Game.Overlays.Dashboard foreach (var kvp in e.NewItems) { int userId = kvp.Key; + if (blockedUsers.Any(b => b.TargetID == userId)) continue; - users.GetUserAsync(userId).ContinueWith(task => - { - if (task.GetResultSafely() is APIUser user) - Schedule(() => userFlow.Add(userPanels[userId] = createUserPanel(user))); - }); + addUserPanel(userId); } break; @@ -130,14 +166,28 @@ namespace osu.Game.Overlays.Dashboard foreach (var kvp in e.OldItems) { int userId = kvp.Key; - if (userPanels.Remove(userId, out var userPanel)) - userPanel.Expire(); + removeUserPanel(userId); } break; } }); + private void addUserPanel(int userId) + { + users.GetUserAsync(userId).ContinueWith(task => + { + if (task.GetResultSafely() is APIUser user) + Schedule(() => userFlow.Add(userPanels[userId] = createUserPanel(user))); + }); + } + + private void removeUserPanel(int userId) + { + if (userPanels.Remove(userId, out var userPanel)) + userPanel.Expire(); + } + private OnlineUserPanel createUserPanel(APIUser user) => new OnlineUserPanel(user).With(panel => {