Skip to content

Commit 656b61e

Browse files
authored
Merge pull request #261 from smoogipoo/local-user-status-update
Always broadcast local user's state back to themselves
2 parents 2d51d49 + 2a9de42 commit 656b61e

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

osu.Server.Spectator/Hubs/Metadata/MetadataClientState.cs

+19-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,25 @@ public MetadataClientState(in string connectionId, in int userId, in string? ver
1919
VersionHash = versionHash;
2020
}
2121

22-
public UserPresence ToUserPresence() => new UserPresence
22+
/// <summary>
23+
/// Creates a <see cref="UserPresence"/> which represents this user's state as it should be broadcast to other users.
24+
/// </summary>
25+
/// <returns>The representative user presence, or <c>null</c> if the user should appear offline.</returns>
26+
public UserPresence? ToUserPresence()
2327
{
24-
Activity = UserActivity,
25-
Status = UserStatus,
26-
};
28+
switch (UserStatus)
29+
{
30+
case null:
31+
case Game.Users.UserStatus.Offline:
32+
return null;
33+
34+
default:
35+
return new UserPresence
36+
{
37+
Activity = UserActivity,
38+
Status = UserStatus,
39+
};
40+
}
41+
}
2742
}
2843
}

osu.Server.Spectator/Hubs/Metadata/MetadataHub.cs

+20-13
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,13 @@ public async Task UpdateActivity(UserActivity? activity)
136136

137137
usage.Item.UserActivity = activity;
138138

139-
if (shouldBroadcastPresenceToOtherUsers(usage.Item))
140-
await broadcastUserPresenceUpdate(usage.Item.UserId, usage.Item.ToUserPresence());
139+
await Task.WhenAll
140+
(
141+
shouldBroadcastPresenceToOtherUsers(usage.Item)
142+
? broadcastUserPresenceUpdate(usage.Item.UserId, usage.Item.ToUserPresence())
143+
: Task.CompletedTask,
144+
Clients.Caller.UserPresenceUpdated(usage.Item.UserId, usage.Item.ToUserPresence())
145+
);
141146
}
142147
}
143148

@@ -147,18 +152,20 @@ public async Task UpdateStatus(UserStatus? status)
147152
{
148153
Debug.Assert(usage.Item != null);
149154

150-
if (usage.Item.UserStatus != status)
151-
{
152-
usage.Item.UserStatus = status;
155+
if (usage.Item.UserStatus == status)
156+
return;
153157

154-
if (status == UserStatus.Offline)
155-
{
156-
// special case of users that already broadcast that they are online switching to "appear offline".
157-
await broadcastUserPresenceUpdate(usage.Item.UserId, null);
158-
}
159-
else
160-
await broadcastUserPresenceUpdate(usage.Item.UserId, usage.Item.ToUserPresence());
161-
}
158+
usage.Item.UserStatus = status;
159+
160+
await Task.WhenAll
161+
(
162+
// Of note, we always send status updates to other users.
163+
//
164+
// This is a single special case where we don't check against `shouldBroadcastPresentToOtherUsers` because
165+
// it is required to tell other clients that "we went offline" in the "appears offline" scenario.
166+
broadcastUserPresenceUpdate(usage.Item.UserId, usage.Item.ToUserPresence()),
167+
Clients.Caller.UserPresenceUpdated(usage.Item.UserId, usage.Item.ToUserPresence())
168+
);
162169
}
163170
}
164171

0 commit comments

Comments
 (0)