From 5f140821a83f7e8f5daeeae6de23bc9dacc2f02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Tue, 21 Feb 2023 16:59:20 +0100 Subject: [PATCH] Implemented RichObjectString in text messages --- .../talk/chat/getChatResponse.dart | 39 ++++++++++- .../talk/chat/getChatResponse.g.dart | 29 ++++++++ .../talk/chat/richObjectStringProcessor.dart | 13 ++++ .../talk/room/getRoomResponse.dart | 66 ++++++++++--------- .../talk/room/getRoomResponse.g.dart | 52 +-------------- lib/screen/pages/talk/chatList.dart | 3 +- lib/screen/pages/talk/chatView.dart | 3 +- 7 files changed, 119 insertions(+), 86 deletions(-) create mode 100644 lib/api/marianumcloud/talk/chat/richObjectStringProcessor.dart diff --git a/lib/api/marianumcloud/talk/chat/getChatResponse.dart b/lib/api/marianumcloud/talk/chat/getChatResponse.dart index 6d9c3f4..4564d0a 100644 --- a/lib/api/marianumcloud/talk/chat/getChatResponse.dart +++ b/lib/api/marianumcloud/talk/chat/getChatResponse.dart @@ -33,6 +33,7 @@ class GetChatResponseObject { bool isReplyable; String referenceId; String message; + @JsonKey(fromJson: _fromJson) Map? messageParameters; GetChatResponseObject( this.id, @@ -45,9 +46,45 @@ class GetChatResponseObject { this.messageType, this.isReplyable, this.referenceId, - this.message); + this.message, + this.messageParameters); factory GetChatResponseObject.fromJson(Map json) => _$GetChatResponseObjectFromJson(json); Map toJson() => _$GetChatResponseObjectToJson(this); + } +Map? _fromJson(json) { + if(json is Map) { + Map data = {}; + for (var element in json.keys) { + data.putIfAbsent(element, () => RichObjectString.fromJson(json[element])); + } + return data; + } + + return null; +} + +@JsonSerializable(explicitToJson: true) +class RichObjectString { + RichObjectStringObjectType type; + String id; + String name; + + String? path; + String? link; + + RichObjectString(this.type, this.id, this.name, this.path, this.link); + + factory RichObjectString.fromJson(Map json) => _$RichObjectStringFromJson(json); + Map toJson() => _$RichObjectStringToJson(this); +} + +enum RichObjectStringObjectType { + @JsonValue("user") user, + @JsonValue("group") group, + @JsonValue("file") file, + @JsonValue("guest") guest, + @JsonValue("highlight") highlight, +} \ No newline at end of file diff --git a/lib/api/marianumcloud/talk/chat/getChatResponse.g.dart b/lib/api/marianumcloud/talk/chat/getChatResponse.g.dart index 7e70ddc..23fbcb7 100644 --- a/lib/api/marianumcloud/talk/chat/getChatResponse.g.dart +++ b/lib/api/marianumcloud/talk/chat/getChatResponse.g.dart @@ -34,6 +34,7 @@ GetChatResponseObject _$GetChatResponseObjectFromJson( json['isReplyable'] as bool, json['referenceId'] as String, json['message'] as String, + _fromJson(json['messageParameters']), ); Map _$GetChatResponseObjectToJson( @@ -52,6 +53,8 @@ Map _$GetChatResponseObjectToJson( 'isReplyable': instance.isReplyable, 'referenceId': instance.referenceId, 'message': instance.message, + 'messageParameters': + instance.messageParameters?.map((k, e) => MapEntry(k, e.toJson())), }; const _$GetRoomResponseObjectMessageActorTypeEnumMap = { @@ -67,3 +70,29 @@ const _$GetRoomResponseObjectMessageTypeEnumMap = { GetRoomResponseObjectMessageType.system: 'system', GetRoomResponseObjectMessageType.command: 'command', }; + +RichObjectString _$RichObjectStringFromJson(Map json) => + RichObjectString( + $enumDecode(_$RichObjectStringObjectTypeEnumMap, json['type']), + json['id'] as String, + json['name'] as String, + json['path'] as String?, + json['link'] as String?, + ); + +Map _$RichObjectStringToJson(RichObjectString instance) => + { + 'type': _$RichObjectStringObjectTypeEnumMap[instance.type]!, + 'id': instance.id, + 'name': instance.name, + 'path': instance.path, + 'link': instance.link, + }; + +const _$RichObjectStringObjectTypeEnumMap = { + RichObjectStringObjectType.user: 'user', + RichObjectStringObjectType.group: 'group', + RichObjectStringObjectType.file: 'file', + RichObjectStringObjectType.guest: 'guest', + RichObjectStringObjectType.highlight: 'highlight', +}; diff --git a/lib/api/marianumcloud/talk/chat/richObjectStringProcessor.dart b/lib/api/marianumcloud/talk/chat/richObjectStringProcessor.dart new file mode 100644 index 0000000..4bef792 --- /dev/null +++ b/lib/api/marianumcloud/talk/chat/richObjectStringProcessor.dart @@ -0,0 +1,13 @@ +import 'package:marianum_mobile/api/marianumcloud/talk/chat/getChatResponse.dart'; + +class RichObjectStringProcessor { + static String parse(String message, Map? data) { + if(data == null) return message; + + data.forEach((key, value) { + message = message.replaceAll(RegExp("{$key}"), value.name); + }); + + return message; + } +} \ No newline at end of file diff --git a/lib/api/marianumcloud/talk/room/getRoomResponse.dart b/lib/api/marianumcloud/talk/room/getRoomResponse.dart index ee95fb9..88293c3 100644 --- a/lib/api/marianumcloud/talk/room/getRoomResponse.dart +++ b/lib/api/marianumcloud/talk/room/getRoomResponse.dart @@ -1,6 +1,8 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:marianum_mobile/api/apiResponse.dart'; +import '../chat/getChatResponse.dart'; + part 'getRoomResponse.g.dart'; @JsonSerializable(explicitToJson: true) @@ -48,7 +50,7 @@ class GetRoomResponseObject { bool unreadMentionDirect; int lastReadMessage; int lastCommonReadMessage; - GetRoomResponseObjectMessage lastMessage; + GetChatResponseObject lastMessage; String? status; String? statusIcon; String? statusMessage; @@ -104,37 +106,37 @@ enum GetRoomResponseObjectParticipantNotificationLevel { @JsonValue(3) neverNotify, } -@JsonSerializable(explicitToJson: true) -class GetRoomResponseObjectMessage { - int id; - String token; - GetRoomResponseObjectMessageActorType actorType; - String actorId; - String actorDisplayName; - int timestamp; - String message; - String systemMessage; - GetRoomResponseObjectMessageType messageType; - bool isReplyable; - String referenceId; - - - GetRoomResponseObjectMessage( - this.id, - this.token, - this.actorType, - this.actorId, - this.actorDisplayName, - this.timestamp, - this.message, - this.systemMessage, - this.messageType, - this.isReplyable, - this.referenceId); - - factory GetRoomResponseObjectMessage.fromJson(Map json) => _$GetRoomResponseObjectMessageFromJson(json); - Map toJson() => _$GetRoomResponseObjectMessageToJson(this); -} +// @JsonSerializable(explicitToJson: true) +// class GetRoomResponseObjectMessage { +// int id; +// String token; +// GetRoomResponseObjectMessageActorType actorType; +// String actorId; +// String actorDisplayName; +// int timestamp; +// String message; +// String systemMessage; +// GetRoomResponseObjectMessageType messageType; +// bool isReplyable; +// String referenceId; +// +// +// GetRoomResponseObjectMessage( +// this.id, +// this.token, +// this.actorType, +// this.actorId, +// this.actorDisplayName, +// this.timestamp, +// this.message, +// this.systemMessage, +// this.messageType, +// this.isReplyable, +// this.referenceId); +// +// factory GetRoomResponseObjectMessage.fromJson(Map json) => _$GetRoomResponseObjectMessageFromJson(json); +// Map toJson() => _$GetRoomResponseObjectMessageToJson(this); +// } enum GetRoomResponseObjectMessageActorType { @JsonValue("users") user, diff --git a/lib/api/marianumcloud/talk/room/getRoomResponse.g.dart b/lib/api/marianumcloud/talk/room/getRoomResponse.g.dart index 5400980..cc44798 100644 --- a/lib/api/marianumcloud/talk/room/getRoomResponse.g.dart +++ b/lib/api/marianumcloud/talk/room/getRoomResponse.g.dart @@ -48,7 +48,7 @@ GetRoomResponseObject _$GetRoomResponseObjectFromJson( json['unreadMentionDirect'] as bool, json['lastReadMessage'] as int, json['lastCommonReadMessage'] as int, - GetRoomResponseObjectMessage.fromJson( + GetChatResponseObject.fromJson( json['lastMessage'] as Map), json['status'] as String?, json['statusIcon'] as String?, @@ -106,53 +106,3 @@ const _$GetRoomResponseObjectParticipantNotificationLevelEnumMap = { GetRoomResponseObjectParticipantNotificationLevel.notifyOnMention: 2, GetRoomResponseObjectParticipantNotificationLevel.neverNotify: 3, }; - -GetRoomResponseObjectMessage _$GetRoomResponseObjectMessageFromJson( - Map json) => - GetRoomResponseObjectMessage( - json['id'] as int, - json['token'] as String, - $enumDecode( - _$GetRoomResponseObjectMessageActorTypeEnumMap, json['actorType']), - json['actorId'] as String, - json['actorDisplayName'] as String, - json['timestamp'] as int, - json['message'] as String, - json['systemMessage'] as String, - $enumDecode( - _$GetRoomResponseObjectMessageTypeEnumMap, json['messageType']), - json['isReplyable'] as bool, - json['referenceId'] as String, - ); - -Map _$GetRoomResponseObjectMessageToJson( - GetRoomResponseObjectMessage instance) => - { - 'id': instance.id, - 'token': instance.token, - 'actorType': - _$GetRoomResponseObjectMessageActorTypeEnumMap[instance.actorType]!, - 'actorId': instance.actorId, - 'actorDisplayName': instance.actorDisplayName, - 'timestamp': instance.timestamp, - 'message': instance.message, - 'systemMessage': instance.systemMessage, - 'messageType': - _$GetRoomResponseObjectMessageTypeEnumMap[instance.messageType]!, - 'isReplyable': instance.isReplyable, - 'referenceId': instance.referenceId, - }; - -const _$GetRoomResponseObjectMessageActorTypeEnumMap = { - GetRoomResponseObjectMessageActorType.user: 'users', - GetRoomResponseObjectMessageActorType.guest: 'guests', - GetRoomResponseObjectMessageActorType.bot: 'bots', - GetRoomResponseObjectMessageActorType.bridge: 'bridged', -}; - -const _$GetRoomResponseObjectMessageTypeEnumMap = { - GetRoomResponseObjectMessageType.comment: 'comment', - GetRoomResponseObjectMessageType.deletedComment: 'comment_deleted', - GetRoomResponseObjectMessageType.system: 'system', - GetRoomResponseObjectMessageType.command: 'command', -}; diff --git a/lib/screen/pages/talk/chatList.dart b/lib/screen/pages/talk/chatList.dart index 3bb2997..da11c98 100644 --- a/lib/screen/pages/talk/chatList.dart +++ b/lib/screen/pages/talk/chatList.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:jiffy/jiffy.dart'; +import 'package:marianum_mobile/api/marianumcloud/talk/chat/richObjectStringProcessor.dart'; import 'package:marianum_mobile/api/marianumcloud/talk/room/getRoomResponse.dart'; import 'package:marianum_mobile/data/chatList/chatListProps.dart'; import 'package:provider/provider.dart'; @@ -62,7 +63,7 @@ class _ChatListState extends State { chats.add(ListTile( title: Text(chatRoom.displayName), - subtitle: Text("${Jiffy.unixFromSecondsSinceEpoch(chatRoom.lastMessage.timestamp).fromNow()}: ${chatRoom.lastMessage.message.replaceAll("\n", " ")}", overflow: TextOverflow.ellipsis), + subtitle: Text("${Jiffy.unixFromSecondsSinceEpoch(chatRoom.lastMessage.timestamp).fromNow()}: ${RichObjectStringProcessor.parse(chatRoom.lastMessage.message.replaceAll("\n", " "), chatRoom.lastMessage.messageParameters)}", overflow: TextOverflow.ellipsis), trailing: Visibility( visible: chatRoom.unreadMessages > 0, child: Container( diff --git a/lib/screen/pages/talk/chatView.dart b/lib/screen/pages/talk/chatView.dart index 8f2ceda..b3ba238 100644 --- a/lib/screen/pages/talk/chatView.dart +++ b/lib/screen/pages/talk/chatView.dart @@ -2,6 +2,7 @@ import 'package:bubble/bubble.dart'; import 'package:flutter/material.dart'; import 'package:jiffy/jiffy.dart'; +import 'package:marianum_mobile/api/marianumcloud/talk/chat/richObjectStringProcessor.dart'; import 'package:marianum_mobile/api/marianumcloud/talk/room/getRoomResponse.dart'; import 'package:marianum_mobile/data/chatList/chatProps.dart'; import 'package:provider/provider.dart'; @@ -96,7 +97,7 @@ class _ChatViewState extends State { ), Padding( padding: EdgeInsets.symmetric(vertical: showMetadata ? 18 : 0), - child: Text(element.message), + child: Text(RichObjectStringProcessor.parse(element.message, element.messageParameters)), ), Visibility( visible: showMetadata,