From 71a082110abe74015dfbaf62b7e6b1dca7b2a726 Mon Sep 17 00:00:00 2001 From: Angela Zhang Date: Thu, 17 Dec 2020 16:56:34 -0600 Subject: [PATCH] Making style changes + supports reopening PM chats --- osu.Game/Online/Chat/ChannelManager.cs | 54 ++++++++++++++------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/osu.Game/Online/Chat/ChannelManager.cs b/osu.Game/Online/Chat/ChannelManager.cs index 51bbe6ca45..54ef3c6c30 100644 --- a/osu.Game/Online/Chat/ChannelManager.cs +++ b/osu.Game/Online/Chat/ChannelManager.cs @@ -33,8 +33,12 @@ namespace osu.Game.Online.Chat private readonly BindableList availableChannels = new BindableList(); private readonly BindableList joinedChannels = new BindableList(); - // Keeps a list of closed channel identifiers - private readonly BindableList closedChannels = new BindableList(); + + /// + /// Keeps a list of closed channel identifiers. Stores the channel ID for normal + /// channels, or the user ID for PM channels + /// + private readonly BindableList closedChannelIds = new BindableList(); // For efficiency purposes, this constant bounds the number of closed channels we store. // This number is somewhat arbitrary; future developers are free to modify it. @@ -418,13 +422,13 @@ namespace osu.Game.Online.Chat // Prevent the closedChannel list from exceeding the max size // by removing the oldest element - if (closedChannels.Count >= closed_channels_max_size) + if (closedChannelIds.Count >= closed_channels_max_size) { - closedChannels.RemoveAt(0); + closedChannelIds.RemoveAt(0); } - // insert at the end of the closedChannels list - closedChannels.Insert(closedChannels.Count, channel.Name); + // For PM channels, we store the user ID; else, we store the channel id + closedChannelIds.Add(channel.Type == ChannelType.PM ? channel.Users.First().Id : channel.Id); if (channel.Joined.Value) { @@ -440,7 +444,7 @@ namespace osu.Game.Online.Chat /// public void JoinLastClosedChannel() { - if (closedChannels.Count <= 0) + if (closedChannelIds.Count <= 0) { return; } @@ -451,32 +455,32 @@ namespace osu.Game.Online.Chat // every time the user joins a channel, which would make joining a channel // slower. I wanted to centralize all major slowdowns so they // can only occur if the user actually decides to use this feature. - for (int i = closedChannels.Count - 1; i >= 0; i--) + while (closedChannelIds.Count != 0) { - string lastClosedChannelName = closedChannels.Last(); - closedChannels.RemoveAt(closedChannels.Count - 1); - bool alreadyJoined = false; + long lastClosedChannelId = closedChannelIds.Last(); + closedChannelIds.RemoveAt(closedChannelIds.Count - 1); - // If the user already joined the channel, do not - // try to join it - for (int j = 0; j < joinedChannels.Count; j++) - { - if (joinedChannels[j].Name == lastClosedChannelName) - { - alreadyJoined = true; - break; - } - } + bool lookupCondition(Channel ch) => + ch.Type == ChannelType.PM ? ch.Users.Any(u => u.Id == lastClosedChannelId) + : ch.Id == lastClosedChannelId; - if (alreadyJoined == false) + // If the user hasn't already joined the channel, try to join it + if (joinedChannels.FirstOrDefault(lookupCondition) == null) { - Channel lastClosedChannel = AvailableChannels.FirstOrDefault(c => c.Name == lastClosedChannelName); + Channel lastClosedChannel = AvailableChannels.FirstOrDefault(lookupCondition); if (lastClosedChannel != null) { - JoinChannel(lastClosedChannel); - return; + CurrentChannel.Value = JoinChannel(lastClosedChannel); } + else + { + // Try to get User to open PM chat + var req = new GetUserRequest(lastClosedChannelId); + req.Success += user => CurrentChannel.Value = JoinChannel(new Channel(user)); + api.Queue(req); + } + return; } } }