1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +00:00

Add support for server conversation message

This commit is contained in:
Pierre HUBERT 2021-03-10 19:08:18 +01:00
parent 08c77340a0
commit 2989e98c50
7 changed files with 131 additions and 28 deletions

View File

@ -101,7 +101,7 @@ class ConversationsHelper {
Future<Conversation> _downloadSingle(int id) async { Future<Conversation> _downloadSingle(int id) async {
try { try {
final response = await APIRequest( final response = await APIRequest(
uri: "conversations/getInfoOne", uri: "conversations/get_single",
needLogin: true, needLogin: true,
args: {"conversationID": id.toString()}).execWithThrow(); args: {"conversationID": id.toString()}).execWithThrow();
@ -417,8 +417,32 @@ class ConversationsHelper {
var serverMessage; var serverMessage;
if (map["server_message"] != null) { if (map["server_message"] != null) {
final srvMessageMap = map["server_message"]; final srvMessageMap = map["server_message"];
var messageType;
switch (srvMessageMap["type"]) {
case "user_created_conv":
messageType = ConversationServerMessageType.USER_CREATED_CONVERSATION;
break;
case "user_added_another":
messageType = ConversationServerMessageType.USER_ADDED_ANOTHER_USER;
break;
case "user_left":
messageType = ConversationServerMessageType.USER_LEFT_CONV;
break;
case "user_removed_another":
messageType = ConversationServerMessageType.USER_REMOVED_ANOTHER_USER;
break;
default:
throw Exception(
"${srvMessageMap["type"]} is an unknown server message type!");
}
serverMessage = ConversationServerMessage( serverMessage = ConversationServerMessage(
type: srvMessageMap["type"], type: messageType,
userID: srvMessageMap["user_id"], userID: srvMessageMap["user_id"],
userWhoAdded: srvMessageMap["user_who_added"], userWhoAdded: srvMessageMap["user_who_added"],
userAdded: srvMessageMap["user_added"], userAdded: srvMessageMap["user_added"],

View File

@ -8,8 +8,7 @@ import 'package:comunic/models/conversation_message.dart';
/// ///
/// @author Pierre Hubert /// @author Pierre Hubert
final HashMap<int, ConversationsMessagesSerializationHelper> _instances = HashMap<int, ConversationsMessagesSerializationHelper> _instances;
HashSet() as HashMap<int, ConversationsMessagesSerializationHelper>;
class ConversationsMessagesSerializationHelper class ConversationsMessagesSerializationHelper
extends BaseSerializationHelper<ConversationMessage> { extends BaseSerializationHelper<ConversationMessage> {
@ -20,6 +19,8 @@ class ConversationsMessagesSerializationHelper
assert(convID != null); assert(convID != null);
factory ConversationsMessagesSerializationHelper(int convID) { factory ConversationsMessagesSerializationHelper(int convID) {
if (_instances == null) _instances = HashMap();
if (!_instances.containsKey(convID)) if (!_instances.containsKey(convID))
_instances[convID] = ConversationsMessagesSerializationHelper._(convID); _instances[convID] = ConversationsMessagesSerializationHelper._(convID);

View File

@ -24,11 +24,10 @@ class ConversationMessagesList extends ListBase<ConversationMessage> {
} }
/// Get the list of the users ID who own a message in this list /// Get the list of the users ID who own a message in this list
List<int> getUsersID() { Set<int> getUsersID() {
final List<int> users = List(); final Set<int> users = Set();
for (ConversationMessage message in this) for (ConversationMessage message in this) users.addAll(message.usersID);
if (!users.contains(message.userID)) users.add(message.userID);
return users; return users;
} }

View File

@ -1,6 +1,8 @@
import 'package:comunic/helpers/serialization/base_serialization_helper.dart'; import 'package:comunic/helpers/serialization/base_serialization_helper.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/displayed_content.dart'; import 'package:comunic/models/displayed_content.dart';
import 'package:comunic/utils/account_utils.dart' as account; import 'package:comunic/utils/account_utils.dart' as account;
import 'package:comunic/utils/intl_utils.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
/// Single conversation message /// Single conversation message
@ -88,6 +90,33 @@ class ConversationServerMessage {
throw Exception("Unsupported server message type!"); throw Exception("Unsupported server message type!");
} }
String getText(UsersList list) {
switch (type) {
case ConversationServerMessageType.USER_CREATED_CONVERSATION:
return tr("%1% created the conversation",
args: {"1": list.getUser(userID).fullName});
case ConversationServerMessageType.USER_ADDED_ANOTHER_USER:
return tr("%1% added %2% to the conversation", args: {
"1": list.getUser(userWhoAdded).fullName,
"2": list.getUser(userAdded).fullName,
});
case ConversationServerMessageType.USER_LEFT_CONV:
return tr("%1% left the conversation", args: {
"1": list.getUser(userID).fullName,
});
case ConversationServerMessageType.USER_REMOVED_ANOTHER_USER:
return tr("%1% removed %2% from the conversation", args: {
"1": list.getUser(userWhoRemoved).fullName,
"2": list.getUser(userRemoved).fullName,
});
}
throw Exception("Unsupported message type!");
}
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
"type": type.toString(), "type": type.toString(),
"userID": userID, "userID": userID,
@ -126,7 +155,7 @@ class ConversationMessage extends SerializableElement<ConversationMessage> {
@required this.serverMessage, @required this.serverMessage,
}) : assert(id != null), }) : assert(id != null),
assert(convID != null), assert(convID != null),
assert(userID != null), assert(userID != null || serverMessage != null),
assert(timeSent != null), assert(timeSent != null),
assert(message != null || file != null || serverMessage != null); assert(message != null || file != null || serverMessage != null);
@ -142,6 +171,8 @@ class ConversationMessage extends SerializableElement<ConversationMessage> {
bool get isOwner => account.userID() == userID; bool get isOwner => account.userID() == userID;
bool get isServerMessage => serverMessage != null;
/// Get the list of the ID of the users implied in this message /// Get the list of the ID of the users implied in this message
Set<int> get usersID { Set<int> get usersID {
if (userID != null) return Set()..add(userID); if (userID != null) return Set()..add(userID);
@ -160,7 +191,7 @@ class ConversationMessage extends SerializableElement<ConversationMessage> {
"convID": convID, "convID": convID,
"userID": userID, "userID": userID,
"timeSent": timeSent, "timeSent": timeSent,
"message": message, "message": message.content,
"file": file?.toJson(), "file": file?.toJson(),
"serverMessage": serverMessage?.toJson(), "serverMessage": serverMessage?.toJson(),
}; };

View File

@ -8,6 +8,7 @@ import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/conversation_message.dart'; import 'package:comunic/models/conversation_message.dart';
import 'package:comunic/models/new_conversation_message.dart'; import 'package:comunic/models/new_conversation_message.dart';
import 'package:comunic/ui/tiles/conversation_message_tile.dart'; import 'package:comunic/ui/tiles/conversation_message_tile.dart';
import 'package:comunic/ui/tiles/server_conversation_message_tile.dart';
import 'package:comunic/ui/widgets/safe_state.dart'; import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/ui/widgets/scroll_watcher.dart'; import 'package:comunic/ui/widgets/scroll_watcher.dart';
import 'package:comunic/utils/files_utils.dart'; import 'package:comunic/utils/files_utils.dart';
@ -134,7 +135,7 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
await _applyNewMessages(messages); await _applyNewMessages(messages);
} catch (e, s) { } catch (e, s) {
print("Failed to load messages! $e => $s"); debugPrint("Failed to load messages! $e => $s", wrapWidth: 4096);
_errorLoading(); _errorLoading();
} }
} }
@ -178,7 +179,7 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
//Then get information about users //Then get information about users
final usersToGet = final usersToGet =
findMissingFromList(_usersInfo.usersID, messages.getUsersID()).toSet(); findMissingFromSet(_usersInfo.usersID.toSet(), messages.getUsersID());
final users = await _usersHelper.getList(usersToGet); final users = await _usersHelper.getList(usersToGet);
@ -314,14 +315,19 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
reverse: true, reverse: true,
itemCount: _messages.length, itemCount: _messages.length,
itemBuilder: (c, i) { itemBuilder: (c, i) {
return ConversationMessageTile( return _messages[i].isServerMessage
message: _messages.elementAt(i), ? ServerConversationMessageTile(
userInfo: _usersInfo.getUser(_messages[i].userID), message: _messages[i].serverMessage,
isLastMessage: _isLastMessage(i), users: _usersInfo,
isFirstMessage: _isFirstMessage(i), )
onRequestMessageUpdate: _updateMessage, : ConversationMessageTile(
onRequestMessageDelete: _deleteMessage, message: _messages.elementAt(i),
); userInfo: _usersInfo.getUser(_messages[i].userID),
isLastMessage: _isLastMessage(i),
isFirstMessage: _isFirstMessage(i),
onRequestMessageUpdate: _updateMessage,
onRequestMessageDelete: _deleteMessage,
);
}), }),
); );
} }

View File

@ -0,0 +1,33 @@
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/conversation_message.dart';
import 'package:flutter/material.dart';
/// Server conversation message list
///
/// @author Pierre hubert
class ServerConversationMessageTile extends StatelessWidget {
final ConversationServerMessage message;
final UsersList users;
const ServerConversationMessageTile({
Key key,
@required this.message,
@required this.users,
}) : assert(message != null),
assert(users != null),
super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Text(
message.getText(users),
style: TextStyle(
fontStyle: FontStyle.italic,
fontSize: 12,
),
),
);
}
}

View File

@ -3,10 +3,10 @@
/// @author Pierre HUBERT /// @author Pierre HUBERT
/// Transform a list of dynamic thins into something a list of ints /// Transform a list of dynamic thins into something a list of ints
List<int> listToIntList(List<dynamic> srcList){ List<int> listToIntList(List<dynamic> srcList) {
List<int> list = List(); List<int> list = List();
srcList.forEach((e){ srcList.forEach((e) {
list.add(int.parse(e)); list.add(int.parse(e));
}); });
@ -14,13 +14,22 @@ List<int> listToIntList(List<dynamic> srcList){
} }
/// Find the list of missing elements of a [testList] from a [srcList] /// Find the list of missing elements of a [testList] from a [srcList]
List<T> findMissingFromList<T>(List<T> srcList, List<T>testList) { List<T> findMissingFromList<T>(List<T> srcList, List<T> testList) {
List<T> dest = List(); List<T> dest = List();
testList.forEach((f){ testList.forEach((f) {
if(!srcList.contains(f) && !dest.contains(f)) if (!srcList.contains(f) && !dest.contains(f)) dest.add(f);
dest.add(f); });
return dest;
}
/// Find the list of missing elements of a [testList] from a [srcList]
Set<T> findMissingFromSet<T>(Set<T> srcList, Set<T> testList) {
Set<T> dest = Set();
testList.forEach((f) {
if (!srcList.contains(f)) dest.add(f);
}); });
return dest; return dest;