优化
This commit is contained in:
181
client/flutter/lib/models/group.dart
Normal file
181
client/flutter/lib/models/group.dart
Normal file
@@ -0,0 +1,181 @@
|
||||
class Group {
|
||||
final String id;
|
||||
final String name;
|
||||
final String? avatar;
|
||||
final String? description;
|
||||
final String? owner;
|
||||
final List<GroupMember> members;
|
||||
final List<GroupPanel> panels;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
Group({
|
||||
required this.id,
|
||||
required this.name,
|
||||
this.avatar,
|
||||
this.description,
|
||||
this.owner,
|
||||
this.members = const [],
|
||||
this.panels = const [],
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
factory Group.fromJson(Map<String, dynamic> json) {
|
||||
return Group(
|
||||
id: (json['_id'] ?? json['id'] ?? '').toString(),
|
||||
name: json['name'] ?? '',
|
||||
avatar: json['avatar'],
|
||||
description: json['description'],
|
||||
owner: json['owner']?.toString(),
|
||||
members: (json['members'] as List?)
|
||||
?.map((e) => GroupMember.fromJson(e))
|
||||
.toList() ??
|
||||
[],
|
||||
panels: (json['panels'] as List?)
|
||||
?.map((e) => GroupPanel.fromJson(e))
|
||||
.toList() ??
|
||||
[],
|
||||
createdAt: json['createdAt'] != null
|
||||
? DateTime.parse(json['createdAt'].toString())
|
||||
: null,
|
||||
updatedAt: json['updatedAt'] != null
|
||||
? DateTime.parse(json['updatedAt'].toString())
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'_id': id,
|
||||
'name': name,
|
||||
'avatar': avatar,
|
||||
'description': description,
|
||||
'owner': owner,
|
||||
'members': members.map((e) => e.toJson()).toList(),
|
||||
'panels': panels.map((e) => e.toJson()).toList(),
|
||||
'createdAt': createdAt?.toIso8601String(),
|
||||
'updatedAt': updatedAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
/// 群组成员数量
|
||||
int get memberCount => members.length;
|
||||
|
||||
/// 获取第一个文本面板(频道)用于聊天
|
||||
GroupPanel? get firstTextPanel {
|
||||
try {
|
||||
return panels.firstWhere((p) => p.type == 0);
|
||||
} catch (_) {
|
||||
return panels.isNotEmpty ? panels.first : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GroupMember {
|
||||
final String? userId;
|
||||
final List<String>? roles;
|
||||
final DateTime? muteUntil;
|
||||
|
||||
GroupMember({
|
||||
this.userId,
|
||||
this.roles,
|
||||
this.muteUntil,
|
||||
});
|
||||
|
||||
factory GroupMember.fromJson(Map<String, dynamic> json) {
|
||||
return GroupMember(
|
||||
userId: json['userId']?.toString(),
|
||||
roles: (json['roles'] as List?)?.map((e) => e.toString()).toList(),
|
||||
muteUntil: json['muteUntil'] != null
|
||||
? DateTime.parse(json['muteUntil'].toString())
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'userId': userId,
|
||||
'roles': roles,
|
||||
'muteUntil': muteUntil?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// 群组面板类型:
|
||||
/// 0 = 文本频道
|
||||
/// 1 = 面板组(文件夹)
|
||||
/// 2 = 插件面板
|
||||
class GroupPanel {
|
||||
final String id;
|
||||
final String name;
|
||||
final String? parentId;
|
||||
final int type;
|
||||
final String? provider;
|
||||
final String? pluginPanelName;
|
||||
final Map<String, dynamic>? meta;
|
||||
|
||||
GroupPanel({
|
||||
required this.id,
|
||||
required this.name,
|
||||
this.parentId,
|
||||
this.type = 0,
|
||||
this.provider,
|
||||
this.pluginPanelName,
|
||||
this.meta,
|
||||
});
|
||||
|
||||
factory GroupPanel.fromJson(Map<String, dynamic> json) {
|
||||
return GroupPanel(
|
||||
id: (json['id'] ?? '').toString(),
|
||||
name: json['name'] ?? '',
|
||||
parentId: json['parentId']?.toString(),
|
||||
type: json['type'] ?? 0,
|
||||
provider: json['provider'],
|
||||
pluginPanelName: json['pluginPanelName'],
|
||||
meta: json['meta'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'parentId': parentId,
|
||||
'type': type,
|
||||
'provider': provider,
|
||||
'pluginPanelName': pluginPanelName,
|
||||
'meta': meta,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// getGroupBasicInfo 返回的基本群组信息
|
||||
class GroupBasicInfo {
|
||||
final String groupId;
|
||||
final String name;
|
||||
final String? avatar;
|
||||
final String? description;
|
||||
final int memberCount;
|
||||
final String? backgroundImage;
|
||||
|
||||
GroupBasicInfo({
|
||||
required this.groupId,
|
||||
required this.name,
|
||||
this.avatar,
|
||||
this.description,
|
||||
this.memberCount = 0,
|
||||
this.backgroundImage,
|
||||
});
|
||||
|
||||
factory GroupBasicInfo.fromJson(Map<String, dynamic> json) {
|
||||
return GroupBasicInfo(
|
||||
groupId: (json['groupId'] ?? '').toString(),
|
||||
name: json['name'] ?? '',
|
||||
avatar: json['avatar'],
|
||||
description: json['description'],
|
||||
memberCount: json['memberCount'] ?? 0,
|
||||
backgroundImage: json['backgroundImage'],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user