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

158 lines
4.3 KiB
Dart
Raw Normal View History

2019-05-04 17:35:03 +00:00
import 'package:comunic/helpers/database/friends_database_helper.dart';
import 'package:comunic/lists/friends_list.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/friend.dart';
2019-06-15 14:01:58 +00:00
import 'package:comunic/models/friend_status.dart';
2019-05-04 17:35:03 +00:00
import 'package:meta/meta.dart';
/// Friends helper
///
/// @author Pierre HUBERT
class FriendsHelper {
2019-05-04 17:35:03 +00:00
final FriendsDatabaseHelper _friendsDatabaseHelper = FriendsDatabaseHelper();
/// Get the list of friends of the user from the server
///
/// Returns the list of friends in case of success, or null if an error
/// occurred
2019-05-04 17:35:03 +00:00
Future<FriendsList> _downloadList() async {
final response = await APIRequest(
uri: "friends/getList",
needLogin: true,
args: {
"complete": "true",
},
).exec();
if (response.code != 200) return null;
// Parse and return the list of friends
2020-05-05 16:49:50 +00:00
FriendsList list = FriendsList()
..addAll(response
.getArray()
.cast<Map<String, dynamic>>()
.map(apiToFriend)
.toList());
2019-05-04 17:35:03 +00:00
// Save the list of friends
_friendsDatabaseHelper.clearTable();
_friendsDatabaseHelper.insertAll(list);
return list;
}
2019-05-01 16:01:20 +00:00
2020-05-05 16:49:50 +00:00
/// Turn an API entry into a [Friend] object
static Friend apiToFriend(Map<String, dynamic> row) {
return Friend(
id: row["ID_friend"],
accepted: row["accepted"] == 1,
lastActive: row["time_last_activity"],
following: row["following"] == 1,
canPostTexts: row["canPostTexts"],
);
}
2019-05-04 17:35:03 +00:00
/// Get the list, either from an online or an offline source
Future<FriendsList> getList({@required bool online}) async {
2019-06-15 06:16:47 +00:00
if (online)
2019-05-04 17:35:03 +00:00
return await _downloadList();
else
return FriendsList()..addAll(await _friendsDatabaseHelper.getAll());
}
2019-05-01 16:01:20 +00:00
/// Respond to friendship request
Future<bool> respondRequest(int friendID, bool accept) async {
final response = await APIRequest(
uri: "friends/respondRequest",
needLogin: true,
args: {
"friendID": friendID.toString(),
"accept": accept.toString()
}).exec();
2019-06-15 14:01:58 +00:00
return response.isOK;
2019-05-01 16:01:20 +00:00
}
2019-05-01 17:46:13 +00:00
/// Update following status for a friend
Future<bool> setFollowing(int friendID, bool follow) async {
final response = await APIRequest(
2019-06-15 06:16:47 +00:00
uri: "friends/setFollowing",
needLogin: true,
args: {
"friendID": friendID.toString(),
"follow": follow.toString()
}).exec();
2019-05-01 17:46:13 +00:00
return response.code == 200;
}
/// Remove a friend from the list
Future<bool> removeFriend(int friendID) async {
final response = await APIRequest(
2019-06-15 06:16:47 +00:00
uri: "friends/remove",
needLogin: true,
args: {"friendID": friendID.toString()}).exec();
return response.code == 200;
}
2019-06-15 14:01:58 +00:00
/// Get friendship status. Throw an exception in case of failure
Future<FriendStatus> getFriendshipStatus(int userID) async {
final response = await APIRequest(
uri: "friends/getStatus",
needLogin: true,
args: {"friendID": userID.toString()},
).exec();
if (response.code != 200)
throw Exception("Could not get friendship status!");
final obj = response.getObject();
return FriendStatus(
userID: userID,
areFriend: obj["are_friend"],
sentRequest: obj["sent_request"],
receivedRequest: obj["received_request"],
following: obj["following"],
);
}
2019-06-15 06:16:47 +00:00
/// Get and return the list of friends of an other user
///
/// Throws an Exception if could not get the list of friends
Future<Set<int>> getOtherUserList(int userID) async {
final response = await APIRequest(
uri: "friends/get_user_list",
needLogin: true,
2019-06-15 06:16:47 +00:00
args: {"userID": userID.toString()},
).exec();
2019-06-15 06:16:47 +00:00
if (response.code != 200)
throw new Exception("Could not get the list of friends of this user!");
return Set<int>.from(response.getArray());
}
2019-06-15 14:01:58 +00:00
/// Send a friendship request to a specified user
Future<bool> sendRequest(int userID) async {
return (await APIRequest(
uri: "friends/sendRequest",
needLogin: true,
args: {"friendID": userID.toString()},
).exec())
.isOK;
}
/// Cancel a friendship request
Future<bool> cancelRequest(int userID) async {
return (await APIRequest(
uri: "friends/removeRequest",
needLogin: true,
args: {"friendID": userID.toString()},
).exec())
.isOK;
}
2019-05-01 16:01:20 +00:00
}