getGroup function
Implementation
Future<Group> getGroup(
String groupId,
String baseUrl,
String appToken,
User user, [
bool parent = false,
String? groupAsMember,
bool rek = false,
]) async {
final storage = Sentc.getStorage();
String userId;
if (groupAsMember == null || groupAsMember == "") {
userId = user.userId;
} else {
userId = groupAsMember;
}
final groupKey = "group_data_user_${userId}_id_$groupId";
final groupJson = await storage.getItem(groupKey);
final jwt = await user.getJwt();
if (groupJson != null) {
final group = Group.fromJson(jsonDecode(groupJson), baseUrl, appToken, user);
if (group.lastCheckTime + 60000 * 5 < DateTime.now().millisecondsSinceEpoch) {
//load the group from json data and just look for group updates
final update = await Sentc.getApi().groupGetGroupUpdates(
baseUrl: baseUrl,
authToken: appToken,
jwt: jwt,
id: groupId,
groupAsMember: groupAsMember,
);
group.rank = update;
group.lastCheckTime = DateTime.now().millisecondsSinceEpoch;
//update the group data in the storage
await storage.set(groupKey, jsonEncode(group));
}
return group;
}
//group data was not in the cache
final out = await Sentc.getApi().groupGetGroupData(
baseUrl: baseUrl,
authToken: appToken,
jwt: jwt,
id: groupId,
groupAsMember: groupAsMember,
);
final accessByGroupAsMember = out.accessByGroupAsMember;
if (accessByGroupAsMember != null && accessByGroupAsMember != "" && !rek) {
//only load the group once even for rek. calls.
//if group as member set. load this group first to get the keys
//no group as member flag
await getGroup(accessByGroupAsMember, baseUrl, appToken, user);
}
if (out.accessByParentGroup != null) {
parent = true;
//check if the parent group is fetched
//rec here because the user might be in a parent of the parent group or so
//check the tree until we found the group where the user access by user
await getGroup(out.parentGroupId!, baseUrl, appToken, user, false, groupAsMember, true);
}
final groupObj = Group._(
baseUrl,
appToken,
user,
groupId,
out.parentGroupId,
out.rank,
out.createdTime,
out.joinedTime,
out.accessByParentGroup,
accessByGroupAsMember,
DateTime.now().millisecondsSinceEpoch,
);
await storage.set(groupKey, jsonEncode(groupObj));
return groupObj;
}