mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 12:59:21 +00:00
Add support for server conversation message
This commit is contained in:
parent
08c77340a0
commit
2989e98c50
@ -101,7 +101,7 @@ class ConversationsHelper {
|
||||
Future<Conversation> _downloadSingle(int id) async {
|
||||
try {
|
||||
final response = await APIRequest(
|
||||
uri: "conversations/getInfoOne",
|
||||
uri: "conversations/get_single",
|
||||
needLogin: true,
|
||||
args: {"conversationID": id.toString()}).execWithThrow();
|
||||
|
||||
@ -417,8 +417,32 @@ class ConversationsHelper {
|
||||
var serverMessage;
|
||||
if (map["server_message"] != null) {
|
||||
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(
|
||||
type: srvMessageMap["type"],
|
||||
type: messageType,
|
||||
userID: srvMessageMap["user_id"],
|
||||
userWhoAdded: srvMessageMap["user_who_added"],
|
||||
userAdded: srvMessageMap["user_added"],
|
||||
|
@ -8,8 +8,7 @@ import 'package:comunic/models/conversation_message.dart';
|
||||
///
|
||||
/// @author Pierre Hubert
|
||||
|
||||
final HashMap<int, ConversationsMessagesSerializationHelper> _instances =
|
||||
HashSet() as HashMap<int, ConversationsMessagesSerializationHelper>;
|
||||
HashMap<int, ConversationsMessagesSerializationHelper> _instances;
|
||||
|
||||
class ConversationsMessagesSerializationHelper
|
||||
extends BaseSerializationHelper<ConversationMessage> {
|
||||
@ -20,6 +19,8 @@ class ConversationsMessagesSerializationHelper
|
||||
assert(convID != null);
|
||||
|
||||
factory ConversationsMessagesSerializationHelper(int convID) {
|
||||
if (_instances == null) _instances = HashMap();
|
||||
|
||||
if (!_instances.containsKey(convID))
|
||||
_instances[convID] = ConversationsMessagesSerializationHelper._(convID);
|
||||
|
||||
|
@ -24,11 +24,10 @@ class ConversationMessagesList extends ListBase<ConversationMessage> {
|
||||
}
|
||||
|
||||
/// Get the list of the users ID who own a message in this list
|
||||
List<int> getUsersID() {
|
||||
final List<int> users = List();
|
||||
Set<int> getUsersID() {
|
||||
final Set<int> users = Set();
|
||||
|
||||
for (ConversationMessage message in this)
|
||||
if (!users.contains(message.userID)) users.add(message.userID);
|
||||
for (ConversationMessage message in this) users.addAll(message.usersID);
|
||||
|
||||
return users;
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
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/utils/account_utils.dart' as account;
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Single conversation message
|
||||
@ -88,6 +90,33 @@ class ConversationServerMessage {
|
||||
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() => {
|
||||
"type": type.toString(),
|
||||
"userID": userID,
|
||||
@ -126,7 +155,7 @@ class ConversationMessage extends SerializableElement<ConversationMessage> {
|
||||
@required this.serverMessage,
|
||||
}) : assert(id != null),
|
||||
assert(convID != null),
|
||||
assert(userID != null),
|
||||
assert(userID != null || serverMessage != null),
|
||||
assert(timeSent != 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 isServerMessage => serverMessage != null;
|
||||
|
||||
/// Get the list of the ID of the users implied in this message
|
||||
Set<int> get usersID {
|
||||
if (userID != null) return Set()..add(userID);
|
||||
@ -160,7 +191,7 @@ class ConversationMessage extends SerializableElement<ConversationMessage> {
|
||||
"convID": convID,
|
||||
"userID": userID,
|
||||
"timeSent": timeSent,
|
||||
"message": message,
|
||||
"message": message.content,
|
||||
"file": file?.toJson(),
|
||||
"serverMessage": serverMessage?.toJson(),
|
||||
};
|
||||
|
@ -8,6 +8,7 @@ import 'package:comunic/lists/users_list.dart';
|
||||
import 'package:comunic/models/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/server_conversation_message_tile.dart';
|
||||
import 'package:comunic/ui/widgets/safe_state.dart';
|
||||
import 'package:comunic/ui/widgets/scroll_watcher.dart';
|
||||
import 'package:comunic/utils/files_utils.dart';
|
||||
@ -134,7 +135,7 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
|
||||
|
||||
await _applyNewMessages(messages);
|
||||
} catch (e, s) {
|
||||
print("Failed to load messages! $e => $s");
|
||||
debugPrint("Failed to load messages! $e => $s", wrapWidth: 4096);
|
||||
_errorLoading();
|
||||
}
|
||||
}
|
||||
@ -178,7 +179,7 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
|
||||
|
||||
//Then get information about users
|
||||
final usersToGet =
|
||||
findMissingFromList(_usersInfo.usersID, messages.getUsersID()).toSet();
|
||||
findMissingFromSet(_usersInfo.usersID.toSet(), messages.getUsersID());
|
||||
|
||||
final users = await _usersHelper.getList(usersToGet);
|
||||
|
||||
@ -314,7 +315,12 @@ class _ConversationScreenState extends SafeState<ConversationScreen> {
|
||||
reverse: true,
|
||||
itemCount: _messages.length,
|
||||
itemBuilder: (c, i) {
|
||||
return ConversationMessageTile(
|
||||
return _messages[i].isServerMessage
|
||||
? ServerConversationMessageTile(
|
||||
message: _messages[i].serverMessage,
|
||||
users: _usersInfo,
|
||||
)
|
||||
: ConversationMessageTile(
|
||||
message: _messages.elementAt(i),
|
||||
userInfo: _usersInfo.getUser(_messages[i].userID),
|
||||
isLastMessage: _isLastMessage(i),
|
||||
|
33
lib/ui/tiles/server_conversation_message_tile.dart
Normal file
33
lib/ui/tiles/server_conversation_message_tile.dart
Normal 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,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -3,10 +3,10 @@
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
/// 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();
|
||||
|
||||
srcList.forEach((e){
|
||||
srcList.forEach((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]
|
||||
List<T> findMissingFromList<T>(List<T> srcList, List<T>testList) {
|
||||
|
||||
List<T> findMissingFromList<T>(List<T> srcList, List<T> testList) {
|
||||
List<T> dest = List();
|
||||
|
||||
testList.forEach((f){
|
||||
if(!srcList.contains(f) && !dest.contains(f))
|
||||
dest.add(f);
|
||||
testList.forEach((f) {
|
||||
if (!srcList.contains(f) && !dest.contains(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;
|
||||
|
Loading…
Reference in New Issue
Block a user