shared address book (#7229)

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2024-03-20 15:05:54 +08:00
committed by GitHub
parent ecb70b43df
commit 41da6d552f
73 changed files with 4714 additions and 866 deletions

View File

@@ -7,7 +7,8 @@ import 'package:collection/collection.dart';
class Peer {
final String id;
String hash;
String hash; // personal ab hash password
String password; // shared ab password
String username; // pc username
String hostname;
String platform;
@@ -18,6 +19,7 @@ class Peer {
String rdpUsername;
bool online = false;
String loginName; //login username
bool? sameServer;
String getId() {
if (alias != '') {
@@ -29,6 +31,7 @@ class Peer {
Peer.fromJson(Map<String, dynamic> json)
: id = json['id'] ?? '',
hash = json['hash'] ?? '',
password = json['password'] ?? '',
username = json['username'] ?? '',
hostname = json['hostname'] ?? '',
platform = json['platform'] ?? '',
@@ -37,12 +40,14 @@ class Peer {
forceAlwaysRelay = json['forceAlwaysRelay'] == 'true',
rdpPort = json['rdpPort'] ?? '',
rdpUsername = json['rdpUsername'] ?? '',
loginName = json['loginName'] ?? '';
loginName = json['loginName'] ?? '',
sameServer = json['same_server'];
Map<String, dynamic> toJson() {
return <String, dynamic>{
"id": id,
"hash": hash,
"password": password,
"username": username,
"hostname": hostname,
"platform": platform,
@@ -52,13 +57,43 @@ class Peer {
"rdpPort": rdpPort,
"rdpUsername": rdpUsername,
'loginName': loginName,
'same_server': sameServer,
};
}
Map<String, dynamic> toAbUploadJson() {
Map<String, dynamic> toPersonalAbUploadJson(bool includingHash) {
var res = <String, dynamic>{
"id": id,
"username": username,
"hostname": hostname,
"platform": platform,
"alias": alias,
"tags": tags,
};
if (includingHash) {
res['hash'] = hash;
}
return res;
}
Map<String, dynamic> toSharedAbUploadJson(bool includingPassword) {
var res = <String, dynamic>{
"id": id,
"username": username,
"hostname": hostname,
"platform": platform,
"alias": alias,
"tags": tags,
};
if (includingPassword) {
res['password'] = password;
}
return res;
}
Map<String, dynamic> toSharedAbCacheJson() {
return <String, dynamic>{
"id": id,
"hash": hash,
"username": username,
"hostname": hostname,
"platform": platform,
@@ -80,6 +115,7 @@ class Peer {
Peer({
required this.id,
required this.hash,
required this.password,
required this.username,
required this.hostname,
required this.platform,
@@ -89,12 +125,14 @@ class Peer {
required this.rdpPort,
required this.rdpUsername,
required this.loginName,
this.sameServer,
});
Peer.loading()
: this(
id: '...',
hash: '',
password: '',
username: '...',
hostname: '...',
platform: '...',
@@ -108,6 +146,7 @@ class Peer {
bool equal(Peer other) {
return id == other.id &&
hash == other.hash &&
password == other.password &&
username == other.username &&
hostname == other.hostname &&
platform == other.platform &&
@@ -121,33 +160,38 @@ class Peer {
Peer.copy(Peer other)
: this(
id: other.id,
hash: other.hash,
username: other.username,
hostname: other.hostname,
platform: other.platform,
alias: other.alias,
tags: other.tags.toList(),
forceAlwaysRelay: other.forceAlwaysRelay,
rdpPort: other.rdpPort,
rdpUsername: other.rdpUsername,
loginName: other.loginName,
);
id: other.id,
hash: other.hash,
password: other.password,
username: other.username,
hostname: other.hostname,
platform: other.platform,
alias: other.alias,
tags: other.tags.toList(),
forceAlwaysRelay: other.forceAlwaysRelay,
rdpPort: other.rdpPort,
rdpUsername: other.rdpUsername,
loginName: other.loginName,
sameServer: other.sameServer);
}
enum UpdateEvent { online, load }
typedef GetInitPeers = RxList<Peer> Function();
class Peers extends ChangeNotifier {
final String name;
final String loadEvent;
List<Peer> peers = List.empty(growable: true);
final RxList<Peer>? initPeers;
final GetInitPeers? getInitPeers;
UpdateEvent event = UpdateEvent.load;
static const _cbQueryOnlines = 'callback_query_onlines';
Peers(
{required this.name, required this.initPeers, required this.loadEvent}) {
peers = initPeers ?? [];
{required this.name,
required this.getInitPeers,
required this.loadEvent}) {
peers = getInitPeers?.call() ?? [];
platformFFI.registerEventHandler(_cbQueryOnlines, name, (evt) async {
_updateOnlineState(evt);
});
@@ -198,8 +242,8 @@ class Peers extends ChangeNotifier {
void _updatePeers(Map<String, dynamic> evt) {
final onlineStates = _getOnlineStates();
if (initPeers != null) {
peers = initPeers!;
if (getInitPeers != null) {
peers = getInitPeers?.call() ?? [];
} else {
peers = _decodePeers(evt['peers']);
}