Skip to content

Commit

Permalink
Add metadata to message and user
Browse files Browse the repository at this point in the history
  • Loading branch information
demchenkoalex committed Apr 24, 2021
1 parent fa81d19 commit 58a2229
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.9

- Add custom metadata to message and user classes

## 2.0.8

- Add custom metadata to the room class. Thanks @alihen for the PR!
Expand Down
29 changes: 23 additions & 6 deletions lib/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ abstract class Message {
const Message(
this.authorId,
this.id,
this.metadata,
this.status,
this.timestamp,
this.type,
Expand Down Expand Up @@ -46,6 +47,9 @@ abstract class Message {
/// Unique ID of the message
final String id;

/// Additional custom metadata or attributes related to the message
final Map<String, dynamic>? metadata;

/// Message [Status]
final Status? status;

Expand Down Expand Up @@ -106,25 +110,27 @@ class FileMessage extends Message {
required String authorId,
required this.fileName,
required String id,
Map<String, dynamic>? metadata,
this.mimeType,
required this.size,
Status? status,
int? timestamp,
required this.uri,
}) : super(authorId, id, status, timestamp, MessageType.file);
}) : super(authorId, id, metadata, status, timestamp, MessageType.file);

/// Creates a full file message from a partial one.
FileMessage.fromPartial({
required String authorId,
required String id,
Map<String, dynamic>? metadata,
required PartialFile partialFile,
Status? status,
int? timestamp,
}) : fileName = partialFile.fileName,
mimeType = partialFile.mimeType,
size = partialFile.size,
uri = partialFile.uri,
super(authorId, id, status, timestamp, MessageType.file);
super(authorId, id, metadata, status, timestamp, MessageType.file);

/// Creates a file message from a map (decoded JSON).
FileMessage.fromJson(Map<String, dynamic> json)
Expand All @@ -135,6 +141,7 @@ class FileMessage extends Message {
super(
json['authorId'] as String,
json['id'] as String,
json['metadata'] as Map<String, dynamic>?,
getStatusFromString(json['status'] as String?),
json['timestamp'] as int?,
MessageType.file,
Expand All @@ -146,6 +153,7 @@ class FileMessage extends Message {
'authorId': authorId,
'fileName': fileName,
'id': id,
'metadata': metadata,
'mimeType': mimeType,
'size': size,
'status': status,
Expand Down Expand Up @@ -224,17 +232,19 @@ class ImageMessage extends Message {
this.height,
required String id,
required this.imageName,
Map<String, dynamic>? metadata,
required this.size,
Status? status,
int? timestamp,
required this.uri,
this.width,
}) : super(authorId, id, status, timestamp, MessageType.image);
}) : super(authorId, id, metadata, status, timestamp, MessageType.image);

/// Creates a full image message from a partial one.
ImageMessage.fromPartial({
required String authorId,
required String id,
Map<String, dynamic>? metadata,
required PartialImage partialImage,
Status? status,
int? timestamp,
Expand All @@ -243,7 +253,7 @@ class ImageMessage extends Message {
size = partialImage.size,
uri = partialImage.uri,
width = partialImage.width,
super(authorId, id, status, timestamp, MessageType.image);
super(authorId, id, metadata, status, timestamp, MessageType.image);

/// Creates an image message from a map (decoded JSON).
ImageMessage.fromJson(Map<String, dynamic> json)
Expand All @@ -255,6 +265,7 @@ class ImageMessage extends Message {
super(
json['authorId'] as String,
json['id'] as String,
json['metadata'] as Map<String, dynamic>?,
getStatusFromString(json['status'] as String?),
json['timestamp'] as int?,
MessageType.image,
Expand All @@ -267,6 +278,7 @@ class ImageMessage extends Message {
'height': height,
'id': id,
'imageName': imageName,
'metadata': metadata,
'size': size,
'status': status,
'timestamp': timestamp,
Expand Down Expand Up @@ -322,22 +334,24 @@ class TextMessage extends Message {
const TextMessage({
required String authorId,
required String id,
Map<String, dynamic>? metadata,
this.previewData,
Status? status,
required this.text,
int? timestamp,
}) : super(authorId, id, status, timestamp, MessageType.text);
}) : super(authorId, id, metadata, status, timestamp, MessageType.text);

/// Creates a full text message from a partial one.
TextMessage.fromPartial({
required String authorId,
required String id,
Map<String, dynamic>? metadata,
required PartialText partialText,
Status? status,
int? timestamp,
}) : previewData = null,
text = partialText.text,
super(authorId, id, status, timestamp, MessageType.text);
super(authorId, id, metadata, status, timestamp, MessageType.text);

/// Creates a text message from a map (decoded JSON).
TextMessage.fromJson(Map<String, dynamic> json)
Expand All @@ -348,6 +362,7 @@ class TextMessage extends Message {
super(
json['authorId'] as String,
json['id'] as String,
json['metadata'] as Map<String, dynamic>?,
getStatusFromString(json['status'] as String?),
json['timestamp'] as int?,
MessageType.text,
Expand All @@ -358,6 +373,7 @@ class TextMessage extends Message {
Map<String, dynamic> toJson() => {
'authorId': authorId,
'id': id,
'metadata': metadata,
'previewData': previewData?.toJson(),
'status': status,
'text': text,
Expand All @@ -370,6 +386,7 @@ class TextMessage extends Message {
return TextMessage(
authorId: authorId,
id: id,
metadata: metadata,
previewData: previewData,
status: status,
text: text,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class User {
this.firstName,
required this.id,
this.lastName,
this.metadata,
});

/// Remote image URL representing user's avatar
Expand All @@ -22,4 +23,7 @@ class User {

/// Last name of the user
final String? lastName;

/// Additional custom metadata or attributes related to the user
final Map<String, dynamic>? metadata;
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_chat_types
description: >
Utility library for the flutter_chat_ui and flutter_firebase_chat_core libraries
which contains shared type declarations.
version: 2.0.8
version: 2.0.9
homepage: https://flyer.chat

environment:
Expand Down

0 comments on commit 58a2229

Please sign in to comment.