2019-04-23 15:29:58 +00:00
|
|
|
import 'package:comunic/lists/conversations_list.dart';
|
|
|
|
import 'package:comunic/lists/users_list.dart';
|
2019-04-23 12:35:41 +00:00
|
|
|
import 'package:comunic/models/api_request.dart';
|
|
|
|
import 'package:comunic/models/conversation.dart';
|
2019-04-23 16:15:29 +00:00
|
|
|
import 'package:comunic/utils/account_utils.dart';
|
2019-04-23 12:35:41 +00:00
|
|
|
|
|
|
|
/// Conversation helper
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
class ConversationsHelper {
|
|
|
|
/// Download the list of conversations from the server
|
2019-04-23 15:29:58 +00:00
|
|
|
Future<ConversationsList> downloadList() async {
|
2019-04-23 12:35:41 +00:00
|
|
|
final response =
|
|
|
|
await APIRequest(uri: "conversations/getList", needLogin: true).exec();
|
|
|
|
|
|
|
|
if (response.code != 200) return null;
|
|
|
|
|
|
|
|
try {
|
2019-04-23 15:29:58 +00:00
|
|
|
ConversationsList list = ConversationsList();
|
|
|
|
response.getArray().forEach((f) => list.add(Conversation(
|
2019-04-23 12:35:41 +00:00
|
|
|
id: f["ID"],
|
|
|
|
ownerID: f["ID_owner"],
|
|
|
|
lastActive: f["last_active"],
|
|
|
|
name: f["name"] == false ? null : f["name"],
|
|
|
|
following: f["following"] == 1,
|
|
|
|
sawLastMessage: f["saw_last_message"] == 1,
|
|
|
|
members: f["members"].map<int>((f) => int.parse(f)).toList(),
|
|
|
|
)));
|
|
|
|
|
|
|
|
return list;
|
2019-04-23 15:29:58 +00:00
|
|
|
} on Exception catch (e) {
|
2019-04-23 12:35:41 +00:00
|
|
|
print(e.toString());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 15:29:58 +00:00
|
|
|
|
|
|
|
/// Get the name of a [conversation]. This requires information about the
|
|
|
|
/// users of this conversation
|
2019-04-23 16:15:29 +00:00
|
|
|
static String getConversationName(
|
|
|
|
Conversation conversation, UsersList users) {
|
2019-04-23 15:29:58 +00:00
|
|
|
if (conversation.has_name) return conversation.name;
|
|
|
|
|
|
|
|
String name = "";
|
2019-04-23 16:15:29 +00:00
|
|
|
int count = 0;
|
2019-04-23 15:29:58 +00:00
|
|
|
for (int i = 0; i < 3 && i < conversation.members.length; i++)
|
2019-04-23 16:15:29 +00:00
|
|
|
if (conversation.members[i] != userID()) {
|
|
|
|
name += (count > 0 ? ", " : "") +
|
|
|
|
users
|
|
|
|
.getUser(conversation.members[i])
|
|
|
|
.fullName;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(conversation.members.length > 3)
|
|
|
|
name += ", ...";
|
2019-04-23 15:29:58 +00:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
2019-04-23 12:35:41 +00:00
|
|
|
}
|