1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-24 22:09:21 +00:00
comunicmobile/lib/helpers/users_helper.dart

198 lines
6.2 KiB
Dart
Raw Permalink Normal View History

import 'package:comunic/enums/user_page_visibility.dart';
2021-03-14 17:15:38 +00:00
import 'package:comunic/helpers/serialization/user_list_serialization_helper.dart';
2020-04-28 16:47:47 +00:00
import 'package:comunic/lists/custom_emojies_list.dart';
import 'package:comunic/lists/users_list.dart';
2019-06-10 12:24:34 +00:00
import 'package:comunic/models/advanced_user_info.dart';
import 'package:comunic/models/api_request.dart';
2020-04-28 16:47:47 +00:00
import 'package:comunic/models/custom_emoji.dart';
import 'package:comunic/models/user.dart';
/// User helper
///
/// Helper used to get information about the users of Comunic
///
/// @author Pierre HUBERT
2019-06-10 12:24:34 +00:00
/// Handle advanced information error
2019-06-15 14:01:58 +00:00
enum GetUserAdvancedInformationErrorCause {
NOT_FOUND,
NETWORK_ERROR,
NOT_AUTHORIZED
}
2019-06-10 12:24:34 +00:00
class GetUserAdvancedUserError extends Error {
final GetUserAdvancedInformationErrorCause cause;
2022-03-10 19:36:55 +00:00
GetUserAdvancedUserError(this.cause);
2019-06-10 12:24:34 +00:00
}
class UsersHelper {
/// Download information about some given users ID
///
/// Return the list of users information in case of success, null in case of
/// failure
Future<UsersList?> _downloadInfo(List<int?> users) async {
// Execute the request
final response = await APIRequest(
uri: "user/getInfoMultiple",
needLogin: true,
args: {"usersID": users.join(",")}).exec();
// Check if the request did not execute correctly
if (response.code != 200) return null;
final list = UsersList();
2022-03-10 19:36:55 +00:00
response.getObject().forEach(
(k, v) => list.add(
2020-04-28 16:47:47 +00:00
User(
id: v["userID"],
firstName: v["firstName"],
lastName: v["lastName"],
pageVisibility: v["publicPage"] == "false"
? UserPageVisibility.PRIVATE
: (v["openPage"] == "false"
? UserPageVisibility.PRIVATE
2020-04-28 16:47:47 +00:00
: UserPageVisibility.OPEN),
virtualDirectory:
v["virtualDirectory"] == "" ? null : v["virtualDirectory"],
accountImageURL: v["accountImage"],
customEmojies: _parseCustomEmojies(v["customEmojis"]),
),
),
);
2019-04-24 09:24:05 +00:00
// Save the list
2021-03-14 17:15:38 +00:00
await UsersListSerialisationHelper().insertOrReplaceElements(list);
2019-04-24 09:24:05 +00:00
return list;
}
2019-04-24 09:33:58 +00:00
/// Get users information from a given [Set]. Throws an exception in case
/// of failure
Future<UsersList> getListWithThrow(Set<int?> users,
{bool forceDownload = false}) async {
2020-04-29 11:42:01 +00:00
final list =
await getUsersInfo(users.toList(), forceDownload: forceDownload);
if (list == null)
throw Exception(
"Could not get information about the given list of users!");
return list;
}
2019-06-15 14:01:58 +00:00
/// Get information about a single user. Throws in case of failure
Future<User> getSingleWithThrow(int? user,
2020-04-28 17:03:23 +00:00
{bool forceDownload = false}) async {
return (await getListWithThrow(Set<int?>()..add(user),
2020-04-28 17:03:23 +00:00
forceDownload: forceDownload))[0];
2019-06-15 14:01:58 +00:00
}
2019-05-10 17:15:11 +00:00
/// Get users information from a given [Set]
2021-03-10 16:54:41 +00:00
///
/// Throws in case of failure
Future<UsersList> getList(Set<int?> users,
2019-05-10 17:15:11 +00:00
{bool forceDownload = false}) async {
2021-03-10 16:54:41 +00:00
final list = await getUsersInfo(users.toList());
if (list == null) throw Exception("Failed to get the list of users!");
2021-03-10 16:54:41 +00:00
return list;
2019-05-10 17:15:11 +00:00
}
2019-04-24 09:33:58 +00:00
/// Get users information
///
/// If [forceDownload] is set to true, the data will always be retrieved from
/// the server, otherwise cached data will be used if available
Future<UsersList?> getUsersInfo(List<int?> users,
2019-04-24 09:33:58 +00:00
{bool forceDownload = false}) async {
List<int?> toDownload = [];
2019-04-24 09:33:58 +00:00
UsersList list = UsersList();
// Check cache
for (int? userID in users) {
if (!forceDownload &&
await UsersListSerialisationHelper().any((u) => u.id == userID))
list.add(
await UsersListSerialisationHelper().first((u) => u.id == userID));
2019-04-24 09:33:58 +00:00
else
toDownload.add(userID);
}
// Process download if required
if (toDownload.length > 0) {
2019-04-24 09:33:58 +00:00
final downloadedList = await _downloadInfo(toDownload);
if (downloadedList == null) return null;
2019-04-24 09:33:58 +00:00
list.addAll(downloadedList);
}
return list;
}
2019-06-10 12:24:34 +00:00
/// Try to fetch advanced information about a user
Future<AdvancedUserInfo> getAdvancedInfo(int id) async {
final response = await APIRequest(
uri: "user/getAdvancedUserInfo",
needLogin: true,
args: {"userID": id.toString()}).exec();
// Handle exceptions
if (response.code != 200) {
var cause = GetUserAdvancedInformationErrorCause.NETWORK_ERROR;
if (response.code == 404)
cause = GetUserAdvancedInformationErrorCause.NOT_FOUND;
2019-06-15 14:01:58 +00:00
if (response.code == 401)
cause = GetUserAdvancedInformationErrorCause.NOT_AUTHORIZED;
2019-06-10 12:24:34 +00:00
throw new GetUserAdvancedUserError(cause);
}
2022-03-10 19:36:55 +00:00
return apiToAdvancedUserInfo(response.getObject());
2019-06-10 12:24:34 +00:00
}
2020-04-28 16:47:47 +00:00
/// Parse the list of custom emojies
2021-04-26 07:33:54 +00:00
static CustomEmojiesList _parseCustomEmojies(List<dynamic> list) {
2020-04-28 16:47:47 +00:00
final l = list.cast<Map<String, dynamic>>();
return CustomEmojiesList()
..addAll(l
.map((f) => CustomEmoji(
id: f["id"],
userID: f["userID"],
2020-04-29 15:03:30 +00:00
shortcut: f["shortcut"],
2020-04-28 16:47:47 +00:00
url: f["url"],
))
.toList());
}
2021-04-26 07:33:54 +00:00
static AdvancedUserInfo apiToAdvancedUserInfo(Map<String, dynamic> data) =>
AdvancedUserInfo(
id: data["userID"],
firstName: data["firstName"],
lastName: data["lastName"],
pageVisibility: data["publicPage"] == "false"
? UserPageVisibility.PRIVATE
: (data["openPage"] == "false"
? UserPageVisibility.PRIVATE
: UserPageVisibility.OPEN),
virtualDirectory:
data["virtualDirectory"] == "" ? null : data["virtualDirectory"],
accountImageURL: data["accountImage"],
emailAddress: data["email_address"],
customEmojies: _parseCustomEmojies(data["customEmojis"]),
publicNote: data["publicNote"],
canPostTexts: data["can_post_texts"],
isFriendsListPublic: data["friend_list_public"],
numberFriends: data["number_friends"],
accountCreationTime: data["account_creation_time"],
personalWebsite: data["personnalWebsite"],
location: data["location"],
likes: data["pageLikes"],
userLike: data["user_like_page"],
);
}