From cf03001c83a7f6317513fd283a7761427c050e72 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Mon, 7 Nov 2022 11:52:07 +0900 Subject: [PATCH] Better handling for joining channels with only ID --- .../Online/API/Requests/GetChannelRequest.cs | 19 +++++++++++++ .../Requests/Responses/GetChannelResponse.cs | 19 +++++++++++++ osu.Game/Online/Chat/ChannelManager.cs | 28 +++++-------------- .../Notifications/NotificationsClient.cs | 10 +++---- .../WebSocket/WebSocketNotificationsClient.cs | 3 +- 5 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 osu.Game/Online/API/Requests/GetChannelRequest.cs create mode 100644 osu.Game/Online/API/Requests/Responses/GetChannelResponse.cs diff --git a/osu.Game/Online/API/Requests/GetChannelRequest.cs b/osu.Game/Online/API/Requests/GetChannelRequest.cs new file mode 100644 index 0000000000..5bc9cb519a --- /dev/null +++ b/osu.Game/Online/API/Requests/GetChannelRequest.cs @@ -0,0 +1,19 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Game.Online.API.Requests.Responses; + +namespace osu.Game.Online.API.Requests +{ + public class GetChannelRequest : APIRequest + { + private readonly long channelId; + + public GetChannelRequest(long channelId) + { + this.channelId = channelId; + } + + protected override string Target => $"chat/channels/{channelId}"; + } +} diff --git a/osu.Game/Online/API/Requests/Responses/GetChannelResponse.cs b/osu.Game/Online/API/Requests/Responses/GetChannelResponse.cs new file mode 100644 index 0000000000..24b886e74d --- /dev/null +++ b/osu.Game/Online/API/Requests/Responses/GetChannelResponse.cs @@ -0,0 +1,19 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Generic; +using Newtonsoft.Json; +using osu.Game.Online.Chat; + +namespace osu.Game.Online.API.Requests.Responses +{ + [JsonObject(MemberSerialization.OptIn)] + public class GetChannelResponse + { + [JsonProperty(@"channel")] + public Channel Channel { get; set; } = null!; + + [JsonProperty(@"users")] + public List Users { get; set; } = null!; + } +} diff --git a/osu.Game/Online/Chat/ChannelManager.cs b/osu.Game/Online/Chat/ChannelManager.cs index 93033dffa0..ee3194243b 100644 --- a/osu.Game/Online/Chat/ChannelManager.cs +++ b/osu.Game/Online/Chat/ChannelManager.cs @@ -88,15 +88,14 @@ namespace osu.Game.Online.Chat { connector.ChannelJoined += ch => Schedule(() => { - var localChannel = getChannel(ch); - - if (localChannel != ch) + if (ch.Joined.Value) + JoinChannel(ch); + else { - localChannel.Joined.Value = true; - localChannel.Id = ch.Id; + var req = new GetChannelRequest(ch.Id); + req.Success += response => JoinChannel(response.Channel); + api.Queue(req); } - - joinChannel(localChannel); }); connector.ChannelParted += ch => Schedule(() => LeaveChannel(getChannel(ch))); @@ -421,20 +420,7 @@ namespace osu.Game.Online.Chat { Channel found = null; - bool lookupCondition(Channel ch) - { - // If both channels have an id, use that. - if (lookup.Id > 0 && ch.Id > 0) - return ch.Id == lookup.Id; - - // In the case that the local echo is received in a new channel (i.e. one that does not yet have an ID), - // then we need to check for any existing channel with the message containing the same message matched by UUID. - if (lookup.Messages.Count > 0 && ch.Messages.Any(m => m.Uuid == lookup.Messages.Last().Uuid)) - return true; - - // As a last resort, fallback to matching by name. - return lookup.Name == ch.Name; - } + bool lookupCondition(Channel ch) => lookup.Id > 0 ? ch.Id == lookup.Id : ch.Name == lookup.Name; var available = AvailableChannels.FirstOrDefault(lookupCondition); if (available != null) diff --git a/osu.Game/Online/Notifications/NotificationsClient.cs b/osu.Game/Online/Notifications/NotificationsClient.cs index 5182bfa0e5..c706124351 100644 --- a/osu.Game/Online/Notifications/NotificationsClient.cs +++ b/osu.Game/Online/Notifications/NotificationsClient.cs @@ -46,7 +46,10 @@ namespace osu.Game.Online.Notifications if (updates?.Presence != null) { foreach (var channel in updates.Presence) + { + channel.Joined.Value = true; HandleJoinedChannel(channel); + } //todo: handle left channels @@ -59,12 +62,7 @@ namespace osu.Game.Online.Notifications return fetchReq; } - protected void HandleJoinedChannel(Channel channel) - { - // we received this from the server so should mark the channel already joined. - channel.Joined.Value = true; - ChannelJoined?.Invoke(channel); - } + protected void HandleJoinedChannel(Channel channel) => ChannelJoined?.Invoke(channel); protected void HandleChannelParted(Channel channel) => ChannelParted?.Invoke(channel); diff --git a/osu.Game/Online/Notifications/WebSocket/WebSocketNotificationsClient.cs b/osu.Game/Online/Notifications/WebSocket/WebSocketNotificationsClient.cs index 8f4b3c2f97..71aec942ac 100644 --- a/osu.Game/Online/Notifications/WebSocket/WebSocketNotificationsClient.cs +++ b/osu.Game/Online/Notifications/WebSocket/WebSocketNotificationsClient.cs @@ -119,6 +119,7 @@ namespace osu.Game.Online.Notifications.WebSocket Channel? joinedChannel = JsonConvert.DeserializeObject(message.Data.ToString()); Debug.Assert(joinedChannel != null); + joinedChannel.Joined.Value = true; HandleJoinedChannel(joinedChannel); break; @@ -138,7 +139,7 @@ namespace osu.Game.Online.Notifications.WebSocket Debug.Assert(messageData != null); foreach (var msg in messageData.Messages) - HandleJoinedChannel(new Channel(msg.Sender) { Id = msg.ChannelId, Messages = { msg } }); + HandleJoinedChannel(new Channel { Id = msg.ChannelId }); HandleMessages(messageData.Messages); break;