Parse a client's properties once, for joins and updates alike
onClientJoin set every field and onClientChanged set the same ones again behind presence checks, so the two drifted: a client entering as channel commander showed as none until they toggled it, and the description they entered with was dropped. applyClientProperties now maps each field once, only when the event carries it, as applyChannelProperties does for channels; the update keeps its side effects (info refresh, contacts, unread count, announcements). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+42
-50
@@ -7,6 +7,7 @@ import eu.lixko.ts3j.client.sound.SoundEvent;
|
||||
import eu.lixko.ts3j.client.sound.SoundNotifier;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* The {@link TS3Listener} side of a connection: keeps {@link ServerModel} in sync with
|
||||
@@ -36,25 +37,7 @@ final class ConnectionEventHandler implements TS3Listener {
|
||||
@Override
|
||||
public void onClientJoin(ClientJoinEvent e) {
|
||||
ClientEntry c = conn.getModel().putClient(e.getClientId(), e.getClientNickname(), e.getClientTargetId());
|
||||
c.type = safeInt(e, "client_type");
|
||||
c.talkPower = e.getClientTalkPower();
|
||||
applyTalkRequest(c, e);
|
||||
c.talker = e.getBoolean("client_is_talker");
|
||||
c.phoneticNickname = TeamspeakConnection.orEmpty(e.get("client_nickname_phonetic"));
|
||||
c.inputMuted = e.isClientInputMuted();
|
||||
c.outputMuted = e.isClientOutputMuted();
|
||||
c.inputHardware = e.isClientUsingHardwareInput();
|
||||
c.outputHardware = e.isClientUsingHardwareOutput();
|
||||
c.away = e.isClientAway();
|
||||
c.awayMessage = TeamspeakConnection.orEmpty(e.get("client_away_message"));
|
||||
c.uniqueId = TeamspeakConnection.orEmpty(e.getUniqueClientIdentifier());
|
||||
c.myTeamSpeakId = TeamspeakConnection.orEmpty(e.get("client_myteamspeak_id"));
|
||||
c.avatarFlag = TeamspeakConnection.orEmpty(e.get("client_flag_avatar"));
|
||||
c.country = TeamspeakConnection.orEmpty(e.get("client_country"));
|
||||
c.databaseId = e.getClientDatabaseId();
|
||||
c.serverGroupIds = parseIntList(e.getClientServerGroups());
|
||||
c.channelGroupId = e.getClientChannelGroupId();
|
||||
c.unreadMessages = safeInt(e, "client_unread_messages");
|
||||
applyClientProperties(c, e);
|
||||
c.self = (e.getClientId() == conn.getSelfClientId());
|
||||
// Query clients stay out of sight, as they do in the tree and in TS3's log.
|
||||
if (!c.self && !c.isQuery()) {
|
||||
@@ -315,9 +298,37 @@ final class ConnectionEventHandler implements TS3Listener {
|
||||
public void onClientChanged(ClientUpdatedEvent e) {
|
||||
ClientEntry c = conn.getModel().getClient(e.getClientId());
|
||||
if (c == null) return;
|
||||
boolean renamed = has(e, "client_nickname") && !e.get("client_nickname").equals(c.nickname);
|
||||
String oldName = c.nickname;
|
||||
if (renamed) c.nickname = e.get("client_nickname");
|
||||
int unreadBefore = c.unreadMessages;
|
||||
applyClientProperties(c, e);
|
||||
boolean renamed = !Objects.equals(c.nickname, oldName);
|
||||
// The answers to clientgetvariables and to a description request.
|
||||
if (reported(e, "client_description") || has(e, "client_version") || has(e, "client_platform")) {
|
||||
conn.ui.onInfoUpdated();
|
||||
}
|
||||
if (reported(e, "client_myteamspeak_id") && !c.self && !c.isQuery()) conn.applyContact(c);
|
||||
if (has(e, "client_unread_messages") && c.id == conn.getSelfClientId()) conn.unreadMessagesChanged(unreadBefore);
|
||||
announceClientUpdate(e, c, renamed, oldName);
|
||||
conn.ui.onModelChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* The properties a client event carries that the model keeps; each only when present, so
|
||||
* the full set a client enters with and a partial update share one mapping.
|
||||
*/
|
||||
private static void applyClientProperties(ClientEntry c, BaseEvent e) {
|
||||
if (has(e, "client_nickname")) c.nickname = e.get("client_nickname");
|
||||
if (reported(e, "client_nickname_phonetic")) {
|
||||
c.phoneticNickname = TeamspeakConnection.orEmpty(e.get("client_nickname_phonetic"));
|
||||
}
|
||||
if (reported(e, "client_unique_identifier")) {
|
||||
c.uniqueId = TeamspeakConnection.orEmpty(e.get("client_unique_identifier"));
|
||||
}
|
||||
if (reported(e, "client_myteamspeak_id")) c.myTeamSpeakId = TeamspeakConnection.orEmpty(e.get("client_myteamspeak_id"));
|
||||
if (has(e, "client_database_id")) c.databaseId = safeInt(e, "client_database_id");
|
||||
if (has(e, "client_type")) c.type = safeInt(e, "client_type");
|
||||
if (has(e, "client_servergroups")) c.serverGroupIds = parseIntList(e.get("client_servergroups"));
|
||||
if (has(e, "client_channel_group_id")) c.channelGroupId = safeInt(e, "client_channel_group_id");
|
||||
if (has(e, "client_input_muted")) c.inputMuted = e.getBoolean("client_input_muted");
|
||||
if (has(e, "client_output_muted")) c.outputMuted = e.getBoolean("client_output_muted");
|
||||
if (has(e, "client_input_hardware")) c.inputHardware = e.getBoolean("client_input_hardware");
|
||||
@@ -330,41 +341,22 @@ final class ConnectionEventHandler implements TS3Listener {
|
||||
} else if (reported(e, "client_away_message")) {
|
||||
c.awayMessage = TeamspeakConnection.orEmpty(e.get("client_away_message"));
|
||||
}
|
||||
if (has(e, "client_talk_power")) c.talkPower = e.getInt("client_talk_power");
|
||||
if (has(e, "client_talk_power")) c.talkPower = safeInt(e, "client_talk_power");
|
||||
if (has(e, "client_talk_request")) applyTalkRequest(c, e);
|
||||
if (has(e, "client_is_talker")) c.talker = e.getBoolean("client_is_talker");
|
||||
if (reported(e, "client_nickname_phonetic")) {
|
||||
c.phoneticNickname = TeamspeakConnection.orEmpty(e.get("client_nickname_phonetic"));
|
||||
}
|
||||
if (reported(e, "client_description")) {
|
||||
c.description = TeamspeakConnection.orEmpty(e.get("client_description"));
|
||||
conn.ui.onInfoUpdated();
|
||||
}
|
||||
// The answer to clientgetvariables.
|
||||
if (has(e, "client_version") || has(e, "client_platform")) {
|
||||
if (has(e, "client_version")) c.version = e.get("client_version");
|
||||
if (has(e, "client_platform")) c.platform = e.get("client_platform");
|
||||
conn.ui.onInfoUpdated();
|
||||
}
|
||||
if (has(e, "client_month_bytes_downloaded"))
|
||||
c.monthBytesDownloaded = TeamspeakConnection.safeLong(e, "client_month_bytes_downloaded");
|
||||
if (has(e, "client_month_bytes_uploaded"))
|
||||
c.monthBytesUploaded = TeamspeakConnection.safeLong(e, "client_month_bytes_uploaded");
|
||||
if (has(e, "client_is_channel_commander"))
|
||||
c.channelCommander = e.getBoolean("client_is_channel_commander");
|
||||
if (has(e, "client_is_channel_commander")) c.channelCommander = e.getBoolean("client_is_channel_commander");
|
||||
if (reported(e, "client_description")) c.description = TeamspeakConnection.orEmpty(e.get("client_description"));
|
||||
if (has(e, "client_version")) c.version = e.get("client_version");
|
||||
if (has(e, "client_platform")) c.platform = e.get("client_platform");
|
||||
if (reported(e, "client_flag_avatar")) c.avatarFlag = TeamspeakConnection.orEmpty(e.get("client_flag_avatar"));
|
||||
if (reported(e, "client_country")) c.country = TeamspeakConnection.orEmpty(e.get("client_country"));
|
||||
if (reported(e, "client_myteamspeak_id")) {
|
||||
c.myTeamSpeakId = TeamspeakConnection.orEmpty(e.get("client_myteamspeak_id"));
|
||||
if (!c.self && !c.isQuery()) conn.applyContact(c);
|
||||
if (has(e, "client_month_bytes_downloaded")) {
|
||||
c.monthBytesDownloaded = TeamspeakConnection.safeLong(e, "client_month_bytes_downloaded");
|
||||
}
|
||||
if (has(e, "client_unread_messages")) {
|
||||
int before = c.unreadMessages;
|
||||
c.unreadMessages = safeInt(e, "client_unread_messages");
|
||||
if (c.id == conn.getSelfClientId()) conn.unreadMessagesChanged(before);
|
||||
if (has(e, "client_month_bytes_uploaded")) {
|
||||
c.monthBytesUploaded = TeamspeakConnection.safeLong(e, "client_month_bytes_uploaded");
|
||||
}
|
||||
announceClientUpdate(e, c, renamed, oldName);
|
||||
conn.ui.onModelChanged();
|
||||
if (has(e, "client_unread_messages")) c.unreadMessages = safeInt(e, "client_unread_messages");
|
||||
}
|
||||
|
||||
/** Renames, talk-power changes and recording flags all arrive as a client update. */
|
||||
|
||||
+12
@@ -115,6 +115,18 @@ class ConnectionEventHandlerTest {
|
||||
assertEquals(1, modelChanges);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aJoiningCommanderIsShownAsOne() {
|
||||
model.putChannel(1, "Lobby", 0, 0);
|
||||
events.onClientJoin(new ClientJoinEvent(fields("notifycliententerview cfid=0 ctid=1 reasonid=0 clid=6"
|
||||
+ " client_unique_identifier=uid6== client_nickname=bob client_is_channel_commander=1"
|
||||
+ " client_description=hello")));
|
||||
|
||||
ClientEntry bob = model.getClient(6);
|
||||
assertTrue(bob.channelCommander);
|
||||
assertEquals("hello", bob.description);
|
||||
}
|
||||
|
||||
@Test
|
||||
void movesAndLeaves() {
|
||||
model.putChannel(1, "Lobby", 0, 0);
|
||||
|
||||
Reference in New Issue
Block a user