Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure user state is never conveyed for "appear offline" users #257

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions osu.Server.Spectator.Tests/MetadataHubTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ public async Task UserStatusIsTrackedAndCleanedUp()
using (var usage = await userStates.GetForUse(user_id))
Assert.NotNull(usage.Item);

mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Once);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Never);

await hub.UpdateStatus(UserStatus.Online);
await hub.UpdateActivity(new UserActivity.ChoosingBeatmap());

using (var usage = await userStates.GetForUse(user_id))
Expand All @@ -105,34 +106,72 @@ public async Task UserStatusIsTrackedAndCleanedUp()

using (var usage = await userStates.GetForUse(user_id, true))
Assert.Null(usage.Item);

mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, null), Times.Once);
}

[Fact]
public async Task OfflineUserUpdatesAreNotBroadcast()
public async Task UserSwitchingToAppearOfflineHandledCorrectly()
{
await hub.OnConnectedAsync();
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, null), Times.Never);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Never);

await hub.UpdateStatus(UserStatus.Online);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, null), Times.Never);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Once);

await hub.UpdateStatus(UserStatus.Offline);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, null), Times.Once);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Once);

using (var usage = await userStates.GetForUse(user_id))
{
Assert.NotNull(usage.Item!.UserStatus);
Assert.Equal(UserStatus.Offline, usage.Item!.UserStatus);
}

await hub.UpdateActivity(new UserActivity.ChoosingBeatmap());
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, null), Times.Once);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Once);

using (var usage = await userStates.GetForUse(user_id))
{
Assert.NotNull(usage.Item!.UserActivity);
Assert.IsType<UserActivity.ChoosingBeatmap>(usage.Item!.UserActivity);
}

await hub.OnDisconnectedAsync(null);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, null), Times.Once);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Once);
}

[Fact]
public async Task AppearOfflineUserUpdatesAreNeverBroadcast()
{
await hub.OnConnectedAsync();
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Never);

await hub.UpdateStatus(UserStatus.Offline);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Never);

using (var usage = await userStates.GetForUse(user_id))
{
Assert.NotNull(usage.Item!.UserStatus);
Assert.Equal(UserStatus.Offline, usage.Item!.UserStatus);
}

await hub.UpdateActivity(new UserActivity.ChoosingBeatmap());
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Never);

using (var usage = await userStates.GetForUse(user_id))
{
Assert.NotNull(usage.Item!.UserActivity);
Assert.IsType<UserActivity.ChoosingBeatmap>(usage.Item!.UserActivity);
}

mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, null), Times.Exactly(2));
await hub.OnDisconnectedAsync(null);
mockWatchersGroup.Verify(client => client.UserPresenceUpdated(user_id, It.IsAny<UserPresence>()), Times.Never);
}

[Fact]
Expand Down
39 changes: 33 additions & 6 deletions osu.Server.Spectator/Hubs/Metadata/MetadataHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public override async Task OnConnectedAsync()
usage.Item = new MetadataClientState(Context.ConnectionId, Context.GetUserId(), versionHash);

await logLogin(usage);
await broadcastUserPresenceUpdate(usage.Item.UserId, usage.Item.ToUserPresence());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm intent, this is deleted because it's basically conveying nothing, correct? I'm not really sure why I put that there in the first place but I guess that makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should never fire anything.

await Clients.Caller.DailyChallengeUpdated(dailyChallengeUpdater.Current);
}
}
Expand Down Expand Up @@ -116,7 +115,8 @@ public async Task UpdateActivity(UserActivity? activity)

usage.Item.UserActivity = activity;

await broadcastUserPresenceUpdate(usage.Item.UserId, usage.Item.ToUserPresence());
if (shouldBroadcastPresenceToOtherUsers(usage.Item))
await broadcastUserPresenceUpdate(usage.Item.UserId, usage.Item.ToUserPresence());
}
}

Expand All @@ -129,7 +129,14 @@ public async Task UpdateStatus(UserStatus? status)
if (usage.Item.UserStatus != status)
{
usage.Item.UserStatus = status;
await broadcastUserPresenceUpdate(usage.Item.UserId, usage.Item.ToUserPresence());

if (status == UserStatus.Offline)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why this is a local check that isn't using the helper method? (shouldBroadcastPresentToOtherUsers() I mean)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually a different condition – this case we explicitly want to sent the null status just once when a user transitions to "appear offline". See comment just below, hopefully.

{
// special case of users that already broadcast that they are online switching to "appear offline".
await broadcastUserPresenceUpdate(usage.Item.UserId, null);
}
else
await broadcastUserPresenceUpdate(usage.Item.UserId, usage.Item.ToUserPresence());
}
}
}
Expand Down Expand Up @@ -205,16 +212,36 @@ public async Task EndWatchingMultiplayerRoom(long id)
protected override async Task CleanUpState(MetadataClientState state)
{
await base.CleanUpState(state);
await broadcastUserPresenceUpdate(state.UserId, null);
if (shouldBroadcastPresenceToOtherUsers(state))
await broadcastUserPresenceUpdate(state.UserId, null);
await scoreProcessedSubscriber.UnregisterFromAllMultiplayerRoomsAsync(state.UserId);
}

private Task broadcastUserPresenceUpdate(int userId, UserPresence? userPresence)
{
if (userPresence?.Status == UserStatus.Offline)
userPresence = null;
// we never want appearing offline users to have their status broadcast to other clients.
Debug.Assert(userPresence?.Status != UserStatus.Offline);

return Clients.Group(ONLINE_PRESENCE_WATCHERS_GROUP).UserPresenceUpdated(userId, userPresence);
}

private bool shouldBroadcastPresenceToOtherUsers(MetadataClientState state)
{
if (state.UserStatus == null)
return false;

switch (state.UserStatus.Value)
{
case UserStatus.Offline:
return false;

case UserStatus.DoNotDisturb:
case UserStatus.Online:
return true;

default:
throw new ArgumentOutOfRangeException();
}
}
}
}