mirror of
https://github.com/rustdesk/rustdesk.git
synced 2025-12-12 19:17:58 +00:00
device group (#10781)
1. Rename `Group` tab to `Accessible devices` 2. Add accessible device groups at the top of search list 3. option `preset-device-group-name` and command line `--assign --device_group_name` Signed-off-by: 21pages <sunboeasy@gmail.com>
This commit is contained in:
@@ -12,16 +12,18 @@ import '../utils/http_service.dart' as http;
|
||||
class GroupModel {
|
||||
final RxBool groupLoading = false.obs;
|
||||
final RxString groupLoadError = "".obs;
|
||||
final RxList<DeviceGroupPayload> deviceGroups = RxList.empty(growable: true);
|
||||
final RxList<UserPayload> users = RxList.empty(growable: true);
|
||||
final RxList<Peer> peers = RxList.empty(growable: true);
|
||||
final RxString selectedUser = ''.obs;
|
||||
final RxString searchUserText = ''.obs;
|
||||
final RxBool isSelectedDeviceGroup = false.obs;
|
||||
final RxString selectedAccessibleItemName = ''.obs;
|
||||
final RxString searchAccessibleItemNameText = ''.obs;
|
||||
WeakReference<FFI> parent;
|
||||
var initialized = false;
|
||||
var _cacheLoadOnceFlag = false;
|
||||
var _statusCode = 200;
|
||||
|
||||
bool get emtpy => users.isEmpty && peers.isEmpty;
|
||||
bool get emtpy => deviceGroups.isEmpty && users.isEmpty && peers.isEmpty;
|
||||
|
||||
late final Peers peersModel;
|
||||
|
||||
@@ -55,6 +57,12 @@ class GroupModel {
|
||||
}
|
||||
|
||||
Future<void> _pull() async {
|
||||
List<DeviceGroupPayload> tmpDeviceGroups = List.empty(growable: true);
|
||||
if (!await _getDeviceGroups(tmpDeviceGroups)) {
|
||||
// old hbbs doesn't support this api
|
||||
// return;
|
||||
}
|
||||
tmpDeviceGroups.sort((a, b) => a.name.compareTo(b.name));
|
||||
List<UserPayload> tmpUsers = List.empty(growable: true);
|
||||
if (!await _getUsers(tmpUsers)) {
|
||||
return;
|
||||
@@ -63,6 +71,7 @@ class GroupModel {
|
||||
if (!await _getPeers(tmpPeers)) {
|
||||
return;
|
||||
}
|
||||
deviceGroups.value = tmpDeviceGroups;
|
||||
// me first
|
||||
var index = tmpUsers
|
||||
.indexWhere((user) => user.name == gFFI.userModel.userName.value);
|
||||
@@ -71,8 +80,9 @@ class GroupModel {
|
||||
tmpUsers.insert(0, user);
|
||||
}
|
||||
users.value = tmpUsers;
|
||||
if (!users.any((u) => u.name == selectedUser.value)) {
|
||||
selectedUser.value = '';
|
||||
if (!users.any((u) => u.name == selectedAccessibleItemName.value) &&
|
||||
!deviceGroups.any((d) => d.name == selectedAccessibleItemName.value)) {
|
||||
selectedAccessibleItemName.value = '';
|
||||
}
|
||||
// recover online
|
||||
final oldOnlineIDs = peers.where((e) => e.online).map((e) => e.id).toList();
|
||||
@@ -84,6 +94,63 @@ class GroupModel {
|
||||
groupLoadError.value = '';
|
||||
}
|
||||
|
||||
Future<bool> _getDeviceGroups(
|
||||
List<DeviceGroupPayload> tmpDeviceGroups) async {
|
||||
final api = "${await bind.mainGetApiServer()}/api/device-group/accessible";
|
||||
try {
|
||||
var uri0 = Uri.parse(api);
|
||||
final pageSize = 100;
|
||||
var total = 0;
|
||||
int current = 0;
|
||||
do {
|
||||
current += 1;
|
||||
var uri = Uri(
|
||||
scheme: uri0.scheme,
|
||||
host: uri0.host,
|
||||
path: uri0.path,
|
||||
port: uri0.port,
|
||||
queryParameters: {
|
||||
'current': current.toString(),
|
||||
'pageSize': pageSize.toString(),
|
||||
});
|
||||
final resp = await http.get(uri, headers: getHttpHeaders());
|
||||
_statusCode = resp.statusCode;
|
||||
Map<String, dynamic> json =
|
||||
_jsonDecodeResp(utf8.decode(resp.bodyBytes), resp.statusCode);
|
||||
if (json.containsKey('error')) {
|
||||
throw json['error'];
|
||||
}
|
||||
if (resp.statusCode != 200) {
|
||||
throw 'HTTP ${resp.statusCode}';
|
||||
}
|
||||
if (json.containsKey('total')) {
|
||||
if (total == 0) total = json['total'];
|
||||
if (json.containsKey('data')) {
|
||||
final data = json['data'];
|
||||
if (data is List) {
|
||||
for (final user in data) {
|
||||
final u = DeviceGroupPayload.fromJson(user);
|
||||
int index = tmpDeviceGroups.indexWhere((e) => e.name == u.name);
|
||||
if (index < 0) {
|
||||
tmpDeviceGroups.add(u);
|
||||
} else {
|
||||
tmpDeviceGroups[index] = u;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (current * pageSize < total);
|
||||
return true;
|
||||
} catch (err) {
|
||||
debugPrint('get accessible device groups: $err');
|
||||
// old hbbs doesn't support this api
|
||||
// groupLoadError.value =
|
||||
// '${translate('pull_group_failed_tip')}: ${translate(err.toString())}';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<bool> _getUsers(List<UserPayload> tmpUsers) async {
|
||||
final api = "${await bind.mainGetApiServer()}/api/users";
|
||||
try {
|
||||
@@ -225,6 +292,7 @@ class GroupModel {
|
||||
try {
|
||||
final map = (<String, dynamic>{
|
||||
"access_token": bind.mainGetLocalOption(key: 'access_token'),
|
||||
"device_groups": deviceGroups.map((e) => e.toGroupCacheJson()).toList(),
|
||||
"users": users.map((e) => e.toGroupCacheJson()).toList(),
|
||||
'peers': peers.map((e) => e.toGroupCacheJson()).toList()
|
||||
});
|
||||
@@ -244,8 +312,14 @@ class GroupModel {
|
||||
if (groupLoading.value) return;
|
||||
final data = jsonDecode(cache);
|
||||
if (data == null || data['access_token'] != access_token) return;
|
||||
deviceGroups.clear();
|
||||
users.clear();
|
||||
peers.clear();
|
||||
if (data['device_groups'] is List) {
|
||||
for (var u in data['device_groups']) {
|
||||
deviceGroups.add(DeviceGroupPayload.fromJson(u));
|
||||
}
|
||||
}
|
||||
if (data['users'] is List) {
|
||||
for (var u in data['users']) {
|
||||
users.add(UserPayload.fromJson(u));
|
||||
@@ -263,9 +337,10 @@ class GroupModel {
|
||||
|
||||
reset() async {
|
||||
groupLoadError.value = '';
|
||||
deviceGroups.clear();
|
||||
users.clear();
|
||||
peers.clear();
|
||||
selectedUser.value = '';
|
||||
selectedAccessibleItemName.value = '';
|
||||
await bind.mainClearGroup();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ class Peer {
|
||||
String rdpUsername;
|
||||
bool online = false;
|
||||
String loginName; //login username
|
||||
String device_group_name;
|
||||
bool? sameServer;
|
||||
|
||||
String getId() {
|
||||
@@ -41,6 +42,7 @@ class Peer {
|
||||
rdpPort = json['rdpPort'] ?? '',
|
||||
rdpUsername = json['rdpUsername'] ?? '',
|
||||
loginName = json['loginName'] ?? '',
|
||||
device_group_name = json['device_group_name'] ?? '',
|
||||
sameServer = json['same_server'];
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
@@ -57,6 +59,7 @@ class Peer {
|
||||
"rdpPort": rdpPort,
|
||||
"rdpUsername": rdpUsername,
|
||||
'loginName': loginName,
|
||||
'device_group_name': device_group_name,
|
||||
'same_server': sameServer,
|
||||
};
|
||||
}
|
||||
@@ -83,6 +86,7 @@ class Peer {
|
||||
"hostname": hostname,
|
||||
"platform": platform,
|
||||
"login_name": loginName,
|
||||
"device_group_name": device_group_name,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -99,6 +103,7 @@ class Peer {
|
||||
required this.rdpPort,
|
||||
required this.rdpUsername,
|
||||
required this.loginName,
|
||||
required this.device_group_name,
|
||||
this.sameServer,
|
||||
});
|
||||
|
||||
@@ -116,6 +121,7 @@ class Peer {
|
||||
rdpPort: '',
|
||||
rdpUsername: '',
|
||||
loginName: '',
|
||||
device_group_name: '',
|
||||
);
|
||||
bool equal(Peer other) {
|
||||
return id == other.id &&
|
||||
@@ -129,6 +135,7 @@ class Peer {
|
||||
forceAlwaysRelay == other.forceAlwaysRelay &&
|
||||
rdpPort == other.rdpPort &&
|
||||
rdpUsername == other.rdpUsername &&
|
||||
device_group_name == other.device_group_name &&
|
||||
loginName == other.loginName;
|
||||
}
|
||||
|
||||
@@ -146,6 +153,7 @@ class Peer {
|
||||
rdpPort: other.rdpPort,
|
||||
rdpUsername: other.rdpUsername,
|
||||
loginName: other.loginName,
|
||||
device_group_name: other.device_group_name,
|
||||
sameServer: other.sameServer);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,14 +28,14 @@ class PeerTabModel with ChangeNotifier {
|
||||
'Favorites',
|
||||
'Discovered',
|
||||
'Address book',
|
||||
'Group',
|
||||
'Accessible devices',
|
||||
];
|
||||
static const List<IconData> icons = [
|
||||
Icons.access_time_filled,
|
||||
Icons.star,
|
||||
Icons.explore,
|
||||
IconFont.addressBook,
|
||||
Icons.group,
|
||||
IconFont.deviceGroupFill,
|
||||
];
|
||||
List<bool> isEnabled = List.from([
|
||||
true,
|
||||
|
||||
Reference in New Issue
Block a user