Skip to content

Commit 022e11b

Browse files
committed
Add methods for the custom client property commands to the API
1 parent b37f4d0 commit 022e11b

4 files changed

Lines changed: 443 additions & 7 deletions

File tree

src/main/java/com/github/theholywaffle/teamspeak3/TS3Api.java

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,23 @@ public void deleteComplaint(int targetClientDBId, int fromClientDBId) {
960960
asyncApi.deleteComplaint(targetClientDBId, fromClientDBId).getUninterruptibly();
961961
}
962962

963+
/**
964+
* Removes the {@code key} custom client property from a client.
965+
*
966+
* @param clientDBId
967+
* the database ID of the target client
968+
* @param key
969+
* the key of the custom property to delete, cannot be {@code null}
970+
*
971+
* @throws TS3CommandFailedException
972+
* if the execution of a command fails
973+
* @querycommands 1
974+
* @see Client#getDatabaseId()
975+
*/
976+
public void deleteCustomClientProperty(int clientDBId, String key) {
977+
asyncApi.deleteCustomClientProperty(clientDBId, key).getUninterruptibly();
978+
}
979+
963980
/**
964981
* Removes all stored database information about the specified client.
965982
* Please note that this data is also automatically removed after a configured time (usually 90 days).
@@ -1969,6 +1986,26 @@ public ConnectionInfo getConnectionInfo() {
19691986
return asyncApi.getConnectionInfo().getUninterruptibly();
19701987
}
19711988

1989+
/**
1990+
* Gets a map of all custom client properties and their values
1991+
* assigned to the client with database ID {@code clientDBId}.
1992+
*
1993+
* @param clientDBId
1994+
* the database ID of the target client
1995+
*
1996+
* @return a map of the client's custom client property assignments
1997+
*
1998+
* @throws TS3CommandFailedException
1999+
* if the execution of a command fails
2000+
* @querycommands 1
2001+
* @see Client#getDatabaseId()
2002+
* @see #searchCustomClientProperty(String)
2003+
* @see #searchCustomClientProperty(String, String)
2004+
*/
2005+
public Map<String, String> getCustomClientProperties(int clientDBId) {
2006+
return asyncApi.getCustomClientProperties(clientDBId).getUninterruptibly();
2007+
}
2008+
19722009
/**
19732010
* Gets all clients in the database whose last nickname matches the specified name <b>exactly</b>.
19742011
*
@@ -3607,6 +3644,53 @@ public String resetPermissions() {
36073644
return asyncApi.resetPermissions().getUninterruptibly();
36083645
}
36093646

3647+
/**
3648+
* Finds all clients that have any value associated with the {@code key} custom client property,
3649+
* and returns the client's database ID and the key and value of the matching custom property.
3650+
*
3651+
* @param key
3652+
* the key to search for, cannot be {@code null}
3653+
*
3654+
* @return a list of client database IDs and their matching custom client properties
3655+
*
3656+
* @throws TS3CommandFailedException
3657+
* if the execution of a command fails
3658+
* @querycommands 1
3659+
* @see Client#getDatabaseId()
3660+
* @see #searchCustomClientProperty(String, String)
3661+
* @see #getCustomClientProperties(int)
3662+
*/
3663+
public List<CustomPropertyAssignment> searchCustomClientProperty(String key) {
3664+
return asyncApi.searchCustomClientProperty(key).getUninterruptibly();
3665+
}
3666+
3667+
/**
3668+
* Finds all clients whose value associated with the {@code key} custom client property matches the
3669+
* SQL-like pattern {@code valuePattern}, and returns the client's database ID and the key and value
3670+
* of the matching custom property.
3671+
* <p>
3672+
* Patterns are case insensitive. They support the wildcard characters {@code %}, which matches any sequence of
3673+
* zero or more characters, and {@code _}, which matches exactly one arbitrary character.
3674+
* </p>
3675+
*
3676+
* @param key
3677+
* the key to search for, cannot be {@code null}
3678+
* @param valuePattern
3679+
* the pattern that values need to match to be included
3680+
*
3681+
* @return a list of client database IDs and their matching custom client properties
3682+
*
3683+
* @throws TS3CommandFailedException
3684+
* if the execution of a command fails
3685+
* @querycommands 1
3686+
* @see Client#getDatabaseId()
3687+
* @see #searchCustomClientProperty(String)
3688+
* @see #getCustomClientProperties(int)
3689+
*/
3690+
public List<CustomPropertyAssignment> searchCustomClientProperty(String key, String valuePattern) {
3691+
return asyncApi.searchCustomClientProperty(key, valuePattern).getUninterruptibly();
3692+
}
3693+
36103694
/**
36113695
* Moves the server query into the virtual server with the specified ID.
36123696
*
@@ -3820,6 +3904,58 @@ public void setClientChannelGroup(int groupId, int channelId, int clientDBId) {
38203904
asyncApi.setClientChannelGroup(groupId, channelId, clientDBId).getUninterruptibly();
38213905
}
38223906

3907+
/**
3908+
* Sets the value of the multiple custom client properties for a client.
3909+
* <p>
3910+
* If any key present in the map already has a value assigned for this client,
3911+
* the existing value will be overwritten.
3912+
* This method does not delete keys not present in the map.
3913+
* </p><p>
3914+
* If {@code properties} contains an entry with {@code null} as its key,
3915+
* that entry will be ignored and no exception will be thrown.
3916+
* </p>
3917+
*
3918+
* @param clientDBId
3919+
* the database ID of the target client
3920+
* @param properties
3921+
* the map of properties to set, cannot be {@code null}
3922+
*
3923+
* @throws TS3CommandFailedException
3924+
* if the execution of a command fails
3925+
* @querycommands properties.size()
3926+
* @see Client#getDatabaseId()
3927+
* @see #setCustomClientProperty(int, String, String)
3928+
* @see #deleteCustomClientProperty(int, String)
3929+
*/
3930+
public void setCustomClientProperties(int clientDBId, Map<String, String> properties) {
3931+
asyncApi.setCustomClientProperties(clientDBId, properties).getUninterruptibly();
3932+
}
3933+
3934+
/**
3935+
* Sets the value of the {@code key} custom client property for a client.
3936+
* <p>
3937+
* If there is already an assignment of the {@code key} custom client property
3938+
* for this client, the existing value will be overwritten.
3939+
* </p>
3940+
*
3941+
* @param clientDBId
3942+
* the database ID of the target client
3943+
* @param key
3944+
* the key of the custom property to set, cannot be {@code null}
3945+
* @param value
3946+
* the (new) value of the custom property to set
3947+
*
3948+
* @throws TS3CommandFailedException
3949+
* if the execution of a command fails
3950+
* @querycommands 1
3951+
* @see Client#getDatabaseId()
3952+
* @see #setCustomClientProperties(int, Map)
3953+
* @see #deleteCustomClientProperty(int, String)
3954+
*/
3955+
public void setCustomClientProperty(int clientDBId, String key, String value) {
3956+
asyncApi.setCustomClientProperty(clientDBId, key, value).getUninterruptibly();
3957+
}
3958+
38233959
/**
38243960
* Sets the read flag to {@code true} for a given message. This will not delete the message.
38253961
*

src/main/java/com/github/theholywaffle/teamspeak3/TS3ApiAsync.java

Lines changed: 177 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@
4040
import java.io.IOException;
4141
import java.io.InputStream;
4242
import java.io.OutputStream;
43-
import java.util.ArrayList;
44-
import java.util.Arrays;
45-
import java.util.Collection;
46-
import java.util.Collections;
47-
import java.util.List;
48-
import java.util.Locale;
49-
import java.util.Map;
43+
import java.util.*;
5044
import java.util.concurrent.TimeUnit;
5145
import java.util.function.Function;
5246
import java.util.regex.Pattern;
@@ -1073,6 +1067,28 @@ public CommandFuture<Void> deleteComplaint(int targetClientDBId, int fromClientD
10731067
return executeAndReturnError(cmd);
10741068
}
10751069

1070+
/**
1071+
* Removes the {@code key} custom client property from a client.
1072+
*
1073+
* @param clientDBId
1074+
* the database ID of the target client
1075+
* @param key
1076+
* the key of the custom property to delete, cannot be {@code null}
1077+
*
1078+
* @return a future to track the progress of this command
1079+
*
1080+
* @throws TS3CommandFailedException
1081+
* if the execution of a command fails
1082+
* @querycommands 1
1083+
* @see Client#getDatabaseId()
1084+
*/
1085+
public CommandFuture<Void> deleteCustomClientProperty(int clientDBId, String key) {
1086+
if (key == null) throw new IllegalArgumentException("Key cannot be null");
1087+
1088+
Command cmd = CustomPropertyCommands.customDelete(clientDBId, key);
1089+
return executeAndReturnError(cmd);
1090+
}
1091+
10761092
/**
10771093
* Removes all stored database information about the specified client.
10781094
* Please note that this data is also automatically removed after a configured time (usually 90 days).
@@ -2251,6 +2267,39 @@ public CommandFuture<ConnectionInfo> getConnectionInfo() {
22512267
return executeAndTransformFirst(cmd, ConnectionInfo::new);
22522268
}
22532269

2270+
/**
2271+
* Gets a map of all custom client properties and their values
2272+
* assigned to the client with database ID {@code clientDBId}.
2273+
*
2274+
* @param clientDBId
2275+
* the database ID of the target client
2276+
*
2277+
* @return a map of the client's custom client property assignments
2278+
*
2279+
* @throws TS3CommandFailedException
2280+
* if the execution of a command fails
2281+
* @querycommands 1
2282+
* @see Client#getDatabaseId()
2283+
* @see #searchCustomClientProperty(String)
2284+
* @see #searchCustomClientProperty(String, String)
2285+
*/
2286+
public CommandFuture<Map<String, String>> getCustomClientProperties(int clientDBId) {
2287+
Command cmd = CustomPropertyCommands.customInfo(clientDBId);
2288+
CommandFuture<Map<String, String>> future = cmd.getFuture()
2289+
.map(result -> {
2290+
List<Wrapper> response = result.getResponses();
2291+
Map<String, String> properties = new HashMap<>(response.size());
2292+
for (Wrapper wrapper : response) {
2293+
properties.put(wrapper.get("ident"), wrapper.get("value"));
2294+
}
2295+
2296+
return properties;
2297+
});
2298+
2299+
query.doCommandAsync(cmd);
2300+
return future;
2301+
}
2302+
22542303
/**
22552304
* Gets all clients in the database whose last nickname matches the specified name <b>exactly</b>.
22562305
*
@@ -4152,6 +4201,56 @@ public CommandFuture<String> resetPermissions() {
41524201
return executeAndReturnStringProperty(cmd, "token");
41534202
}
41544203

4204+
/**
4205+
* Finds all clients that have any value associated with the {@code key} custom client property,
4206+
* and returns the client's database ID and the key and value of the matching custom property.
4207+
*
4208+
* @param key
4209+
* the key to search for, cannot be {@code null}
4210+
*
4211+
* @return a list of client database IDs and their matching custom client properties
4212+
*
4213+
* @throws TS3CommandFailedException
4214+
* if the execution of a command fails
4215+
* @querycommands 1
4216+
* @see Client#getDatabaseId()
4217+
* @see #searchCustomClientProperty(String, String)
4218+
* @see #getCustomClientProperties(int)
4219+
*/
4220+
public CommandFuture<List<CustomPropertyAssignment>> searchCustomClientProperty(String key) {
4221+
return searchCustomClientProperty(key, "%");
4222+
}
4223+
4224+
/**
4225+
* Finds all clients whose value associated with the {@code key} custom client property matches the
4226+
* SQL-like pattern {@code valuePattern}, and returns the client's database ID and the key and value
4227+
* of the matching custom property.
4228+
* <p>
4229+
* Patterns are case insensitive. They support the wildcard characters {@code %}, which matches any sequence of
4230+
* zero or more characters, and {@code _}, which matches exactly one arbitrary character.
4231+
* </p>
4232+
*
4233+
* @param key
4234+
* the key to search for, cannot be {@code null}
4235+
* @param valuePattern
4236+
* the pattern that values need to match to be included
4237+
*
4238+
* @return a list of client database IDs and their matching custom client properties
4239+
*
4240+
* @throws TS3CommandFailedException
4241+
* if the execution of a command fails
4242+
* @querycommands 1
4243+
* @see Client#getDatabaseId()
4244+
* @see #searchCustomClientProperty(String)
4245+
* @see #getCustomClientProperties(int)
4246+
*/
4247+
public CommandFuture<List<CustomPropertyAssignment>> searchCustomClientProperty(String key, String valuePattern) {
4248+
if (key == null) throw new IllegalArgumentException("Key cannot be null");
4249+
4250+
Command cmd = CustomPropertyCommands.customSearch(key, valuePattern);
4251+
return executeAndTransform(cmd, CustomPropertyAssignment::new);
4252+
}
4253+
41554254
/**
41564255
* Moves the server query into the virtual server with the specified ID.
41574256
*
@@ -4394,6 +4493,77 @@ public CommandFuture<Void> setClientChannelGroup(int groupId, int channelId, int
43944493
return executeAndReturnError(cmd);
43954494
}
43964495

4496+
/**
4497+
* Sets the value of the multiple custom client properties for a client.
4498+
* <p>
4499+
* If any key present in the map already has a value assigned for this client,
4500+
* the existing value will be overwritten.
4501+
* This method does not delete keys not present in the map.
4502+
* </p><p>
4503+
* If {@code properties} contains an entry with {@code null} as its key,
4504+
* that entry will be ignored and no exception will be thrown.
4505+
* </p>
4506+
*
4507+
* @param clientDBId
4508+
* the database ID of the target client
4509+
* @param properties
4510+
* the map of properties to set, cannot be {@code null}
4511+
*
4512+
* @return a future to track the progress of this command
4513+
*
4514+
* @throws TS3CommandFailedException
4515+
* if the execution of a command fails
4516+
* @querycommands properties.size()
4517+
* @see Client#getDatabaseId()
4518+
* @see #setCustomClientProperty(int, String, String)
4519+
* @see #deleteCustomClientProperty(int, String)
4520+
*/
4521+
public CommandFuture<Void> setCustomClientProperties(int clientDBId, Map<String, String> properties) {
4522+
Collection<CommandFuture<Void>> futures = new ArrayList<>(properties.size());
4523+
4524+
for (Map.Entry<String, String> entry : properties.entrySet()) {
4525+
String key = entry.getKey();
4526+
String value = entry.getValue();
4527+
4528+
if (key != null) {
4529+
futures.add(setCustomClientProperty(clientDBId, key, value));
4530+
}
4531+
}
4532+
4533+
return CommandFuture.ofAll(futures)
4534+
.map(__ -> null); // Return success as Void, not List<Void>
4535+
}
4536+
4537+
/**
4538+
* Sets the value of the {@code key} custom client property for a client.
4539+
* <p>
4540+
* If there is already an assignment of the {@code key} custom client property
4541+
* for this client, the existing value will be overwritten.
4542+
* </p>
4543+
*
4544+
* @param clientDBId
4545+
* the database ID of the target client
4546+
* @param key
4547+
* the key of the custom property to set, cannot be {@code null}
4548+
* @param value
4549+
* the (new) value of the custom property to set
4550+
*
4551+
* @return a future to track the progress of this command
4552+
*
4553+
* @throws TS3CommandFailedException
4554+
* if the execution of a command fails
4555+
* @querycommands 1
4556+
* @see Client#getDatabaseId()
4557+
* @see #setCustomClientProperties(int, Map)
4558+
* @see #deleteCustomClientProperty(int, String)
4559+
*/
4560+
public CommandFuture<Void> setCustomClientProperty(int clientDBId, String key, String value) {
4561+
if (key == null) throw new IllegalArgumentException("Key cannot be null");
4562+
4563+
Command cmd = CustomPropertyCommands.customSet(clientDBId, key, value);
4564+
return executeAndReturnError(cmd);
4565+
}
4566+
43974567
/**
43984568
* Sets the read flag to true for a given message. This will not delete the message.
43994569
*

0 commit comments

Comments
 (0)