ab share rules, use user/group name rather than level (#7443)

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2024-03-21 07:23:08 +08:00 committed by GitHub
parent 0540e2cf6c
commit 4e060203ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 48 additions and 44 deletions

View File

@ -238,17 +238,6 @@ enum ShareRule {
}
}
enum ShareLevel {
user(1),
group(2),
team(3);
const ShareLevel(this.value);
final int value;
static String teamName = translate('Everyone');
}
class AbProfile {
String guid;
String name;
@ -279,17 +268,39 @@ class AbTag {
class AbRulePayload {
String guid;
int level;
String name;
int rule;
String? user;
String? group;
int rule;
AbRulePayload(this.guid, this.level, this.name, this.rule, {this.group});
AbRulePayload(
this.guid,
this.user,
this.group,
this.rule,
);
AbRulePayload.fromJson(Map<String, dynamic> json)
: guid = json['guid'] ?? '',
level = json['level'] ?? 0,
name = json['name'] ?? '',
rule = json['rule'] ?? 0,
group = json['group'];
user = json['user'],
group = json['group'],
rule = json['rule'] ?? 0;
static String buildName(String? user, String? group) {
if (user != null && group != null) {
return '-';
}
if (user != null) {
return user;
}
if (group != null) {
return group;
}
return teamName;
}
String getName() {
return buildName(user, group);
}
static String teamName = translate('Everyone');
}

View File

@ -1029,13 +1029,12 @@ class __RuleTreeState extends State<_RuleTree> {
if (!match(user)) {
continue;
}
final userRuleIndex = rules.indexWhere(
(e) => e.level == ShareLevel.user.value && e.name == user);
final userRuleIndex = rules.indexWhere((e) => e.user == user);
if (userRuleIndex < 0) {
if (!onlyShowExisting) {
userNodes.add(TreeNode(
content: _buildEmptyNodeContent(
ShareLevel.user, user, totalWidth, indent * 2),
content:
_buildEmptyNodeContent(user, null, totalWidth, indent * 2),
key: ValueKey(keyIndex++),
children: []));
}
@ -1052,8 +1051,7 @@ class __RuleTreeState extends State<_RuleTree> {
List<TreeNode> groupNodes = [];
map.forEach((group, users) {
final groupRuleIndex = rules.indexWhere(
(e) => e.level == ShareLevel.group.value && e.name == group);
final groupRuleIndex = rules.indexWhere((e) => e.group == group);
final children = buildUserNodes(users);
if (!match(group) && children.isEmpty) {
return;
@ -1061,8 +1059,7 @@ class __RuleTreeState extends State<_RuleTree> {
if (groupRuleIndex < 0) {
if (!onlyShowExisting || children.isNotEmpty) {
groupNodes.add(TreeNode(
content: _buildEmptyNodeContent(
ShareLevel.group, group, totalWidth, indent),
content: _buildEmptyNodeContent(null, group, totalWidth, indent),
key: ValueKey(keyIndex++),
children: children));
}
@ -1077,15 +1074,14 @@ class __RuleTreeState extends State<_RuleTree> {
List<TreeNode> totalNodes = [];
final teamRuleIndex =
rules.indexWhere((e) => e.level == ShareLevel.team.value);
if (!match(ShareLevel.teamName) && groupNodes.isEmpty) {
rules.indexWhere((e) => e.user == null && e.group == null);
if (!match(AbRulePayload.teamName) && groupNodes.isEmpty) {
return [];
}
if (teamRuleIndex < 0) {
if (!onlyShowExisting || groupNodes.isNotEmpty) {
totalNodes.add(TreeNode(
content: _buildEmptyNodeContent(
ShareLevel.team, ShareLevel.teamName, totalWidth, 0),
content: _buildEmptyNodeContent(null, null, totalWidth, 0),
key: ValueKey(keyIndex++),
children: groupNodes));
}
@ -1093,10 +1089,7 @@ class __RuleTreeState extends State<_RuleTree> {
final rule = rules[teamRuleIndex];
totalNodes.add(TreeNode(
content: _buildRuleNodeContent(
AbRulePayload(
rule.guid, rule.level, ShareLevel.teamName, rule.rule),
totalWidth,
0),
AbRulePayload(rule.guid, null, null, rule.rule), totalWidth, 0),
key: ValueKey(keyIndex++),
children: groupNodes));
}
@ -1170,7 +1163,8 @@ class __RuleTreeState extends State<_RuleTree> {
}
Widget _buildEmptyNodeContent(
ShareLevel level, String name, double totalWidth, double indent) {
String? user, String? group, double totalWidth, double indent) {
String name = AbRulePayload.buildName(user, group);
return SizedBox(
width: totalWidth - indent,
child: Row(
@ -1191,8 +1185,7 @@ class __RuleTreeState extends State<_RuleTree> {
setState(() {
isInProgress = true;
});
final errMsg =
await gFFI.abModel.addRule(name, level.value, rule);
final errMsg = await gFFI.abModel.addRule(user, group, rule);
setState(() {
isInProgress = false;
});
@ -1217,7 +1210,7 @@ class __RuleTreeState extends State<_RuleTree> {
width: totalWidth - indent,
child: Row(
children: [
SizedBox(width: col1Width - indent, child: _text(rule.name)),
SizedBox(width: col1Width - indent, child: _text(rule.getName())),
SizedBox(
width: col2Width, child: _text(ShareRule.shortDesc(rule.rule))),
const Spacer(),
@ -1248,7 +1241,7 @@ class __RuleTreeState extends State<_RuleTree> {
text: "Invalid rule: ${rule.rule}");
return;
}
_addOrUpdateRuleDialog(onSubmit, rule.rule, rule.name);
_addOrUpdateRuleDialog(onSubmit, rule.rule, rule.getName());
},
),
_iconButton(

View File

@ -402,7 +402,7 @@ class AbModel {
return [];
}
Future<String?> addRule(String name, int level, int rule) async {
Future<String?> addRule(String? user, String? group, int rule) async {
try {
final abGuid = current.sharedProfile()?.guid;
if (abGuid == null) {
@ -412,9 +412,9 @@ class AbModel {
var headers = getHttpHeaders();
headers['Content-Type'] = "application/json";
final body = jsonEncode({
'ab': abGuid,
'name': name,
'level': level,
'guid': abGuid,
'user': user,
'group': group,
'rule': rule,
});
final resp =