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

51 lines
1.6 KiB
Dart

import 'package:comunic/lists/conversations_list.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/conversation.dart';
/// Conversation helper
///
/// @author Pierre HUBERT
class ConversationsHelper {
/// Download the list of conversations from the server
Future<ConversationsList> downloadList() async {
final response =
await APIRequest(uri: "conversations/getList", needLogin: true).exec();
if (response.code != 200) return null;
try {
ConversationsList list = ConversationsList();
response.getArray().forEach((f) => list.add(Conversation(
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;
} on Exception catch (e) {
print(e.toString());
return null;
}
}
/// Get the name of a [conversation]. This requires information about the
/// users of this conversation
static String getConversationName(Conversation conversation, UsersList users) {
if (conversation.has_name) return conversation.name;
// TODO : exclude current user name
String name = "";
for (int i = 0; i < 3 && i < conversation.members.length; i++)
name +=
(i > 0 ? ", " : "") + users.getUser(conversation.members[i]).fullName;
return name;
}
}