1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 14:29:22 +00:00
comunicmobile/lib/helpers/groups_helper.dart

98 lines
2.8 KiB
Dart

import 'package:comunic/lists/groups_list.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/group.dart';
/// Groups helper
///
/// @author Pierre HUBERT
const _APIGroupsMembershipLevelsMap = {
"administrator": GroupMembershipLevel.ADMINISTRATOR,
"moderator": GroupMembershipLevel.MODERATOR,
"member": GroupMembershipLevel.MEMBER,
"invited": GroupMembershipLevel.INVITED,
"pending": GroupMembershipLevel.PENDING,
"visitor": GroupMembershipLevel.VISITOR
};
const _APIGroupsVisibilityLevelsMap = {
"open": GroupVisibilityLevel.OPEN,
"private": GroupVisibilityLevel.PRIVATE,
"secrete": GroupVisibilityLevel.SECRETE
};
const _APIGroupsRegistrationLevelsMap = {
"open": GroupRegistrationLevel.OPEN,
"moderated": GroupRegistrationLevel.MODERATED,
"closed": GroupRegistrationLevel.CLOSED
};
const _APIGroupsPostsCreationLevelsMap = {
"moderators": GroupPostCreationLevel.MODERATORS,
"members": GroupPostCreationLevel.MEMBERS
};
final _groupsListCache = GroupsList();
class GroupsHelper {
/// Download a list of groups information from the server
Future<GroupsList> _downloadList(Set<int> groups) async {
final response = await APIRequest(
uri: "groups/get_multiple_info",
needLogin: true,
args: {"list": groups.join(",")},
).exec();
if (response.code != 200) return null;
final list = GroupsList();
response
.getObject()
.forEach((k, d) => list[int.parse(k)] = _getGroupFromAPI(d));
return list;
}
/// Get a list of groups from the server
Future<GroupsList> getList(Set<int> groups, {bool force = false}) async {
final list = GroupsList();
// Check which groups information to download
final toDownload = Set<int>();
groups.forEach((groupID) {
if (_groupsListCache.containsKey(groupID))
list[groupID] = _groupsListCache[groupID];
else
toDownload.add(groupID);
});
// Download required groups information
if (toDownload.length > 0) {
final downloaded = await _downloadList(toDownload);
if (downloaded == null) return null;
list.addAll(downloaded);
_groupsListCache.addAll(downloaded);
}
return list;
}
/// Turn an API entry into a group object
Group _getGroupFromAPI(Map<String, dynamic> map) {
return Group(
id: map["id"],
name: map["name"],
numberMembers: map["number_members"],
membershipLevel: _APIGroupsMembershipLevelsMap[map["membership"]],
visibilityLevel: _APIGroupsVisibilityLevelsMap[map["visibility"]],
registrationLevel:
_APIGroupsRegistrationLevelsMap[map["registration_level"]],
postCreationLevel: _APIGroupsPostsCreationLevelsMap[map["posts_level"]],
virtualDirectory: map["virtual_directory"],
following: map["following"]);
}
}