Skip to content

Commit 3df2830

Browse files
authored
Add system message type (#26)
* feat: add system message type * refactor: do not require author
1 parent 78edf92 commit 3df2830

File tree

8 files changed

+212
-7
lines changed

8 files changed

+212
-7
lines changed

lib/src/message.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import 'package:meta/meta.dart';
44
import 'messages/custom_message.dart';
55
import 'messages/file_message.dart';
66
import 'messages/image_message.dart';
7+
import 'messages/system_message.dart';
78
import 'messages/text_message.dart';
89
import 'messages/unsupported_message.dart';
910
import 'user.dart' show User;
1011

1112
/// All possible message types.
12-
enum MessageType { custom, file, image, text, unsupported }
13+
enum MessageType { custom, file, image, text, system, unsupported }
1314

1415
/// All possible statuses message can have.
1516
enum Status { delivered, error, seen, sending, sent }
@@ -35,18 +36,23 @@ abstract class Message extends Equatable {
3536
/// Creates a particular message from a map (decoded JSON).
3637
/// Type is determined by the `type` field.
3738
factory Message.fromJson(Map<String, dynamic> json) {
38-
final type = json['type'] as String;
39+
final type = MessageType.values.firstWhere(
40+
(e) => e.name == json['type'],
41+
orElse: () => MessageType.unsupported,
42+
);
3943

4044
switch (type) {
41-
case 'custom':
45+
case MessageType.custom:
4246
return CustomMessage.fromJson(json);
43-
case 'file':
47+
case MessageType.file:
4448
return FileMessage.fromJson(json);
45-
case 'image':
49+
case MessageType.image:
4650
return ImageMessage.fromJson(json);
47-
case 'text':
51+
case MessageType.system:
52+
return SystemMessage.fromJson(json);
53+
case MessageType.text:
4854
return TextMessage.fromJson(json);
49-
default:
55+
case MessageType.unsupported:
5056
return UnsupportedMessage.fromJson(json);
5157
}
5258
}

lib/src/messages/custom_message.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/messages/file_message.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/messages/image_message.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/messages/system_message.dart

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
import 'package:meta/meta.dart';
3+
4+
import '../message.dart';
5+
import '../user.dart' show User;
6+
7+
part 'system_message.g.dart';
8+
9+
/// A class that represents a system message (anything around chat management). Use [metadata] to store anything
10+
/// you want.
11+
@JsonSerializable()
12+
@immutable
13+
abstract class SystemMessage extends Message {
14+
/// Creates a custom message.
15+
const SystemMessage._({
16+
super.author = const User(id: 'system'),
17+
super.createdAt,
18+
required super.id,
19+
super.metadata,
20+
super.remoteId,
21+
super.repliedMessage,
22+
super.roomId,
23+
super.showStatus,
24+
super.status,
25+
MessageType? type,
26+
super.updatedAt,
27+
}) : super(type: type ?? MessageType.system);
28+
29+
const factory SystemMessage({
30+
User author,
31+
int? createdAt,
32+
required String id,
33+
Map<String, dynamic>? metadata,
34+
String? remoteId,
35+
Message? repliedMessage,
36+
String? roomId,
37+
bool? showStatus,
38+
Status? status,
39+
MessageType? type,
40+
int? updatedAt,
41+
}) = _SystemMessage;
42+
43+
/// Creates a custom message from a map (decoded JSON).
44+
factory SystemMessage.fromJson(Map<String, dynamic> json) =>
45+
_$SystemMessageFromJson(json);
46+
47+
/// Equatable props.
48+
@override
49+
List<Object?> get props => [
50+
author,
51+
createdAt,
52+
id,
53+
metadata,
54+
remoteId,
55+
repliedMessage,
56+
roomId,
57+
status,
58+
updatedAt,
59+
];
60+
61+
@override
62+
Message copyWith({
63+
User? author,
64+
int? createdAt,
65+
String? id,
66+
Map<String, dynamic>? metadata,
67+
String? remoteId,
68+
Message? repliedMessage,
69+
String? roomId,
70+
bool? showStatus,
71+
Status? status,
72+
int? updatedAt,
73+
});
74+
75+
/// Converts a custom message to the map representation,
76+
/// encodable to JSON.
77+
@override
78+
Map<String, dynamic> toJson() => _$SystemMessageToJson(this);
79+
}
80+
81+
/// A utility class to enable better copyWith.
82+
class _SystemMessage extends SystemMessage {
83+
const _SystemMessage({
84+
super.author,
85+
super.createdAt,
86+
required super.id,
87+
super.metadata,
88+
super.remoteId,
89+
super.repliedMessage,
90+
super.roomId,
91+
super.showStatus,
92+
super.status,
93+
super.type,
94+
super.updatedAt,
95+
}) : super._();
96+
97+
@override
98+
Message copyWith({
99+
User? author,
100+
dynamic createdAt = _Unset,
101+
String? id,
102+
dynamic metadata = _Unset,
103+
dynamic remoteId = _Unset,
104+
dynamic repliedMessage = _Unset,
105+
dynamic roomId,
106+
dynamic showStatus = _Unset,
107+
dynamic status = _Unset,
108+
dynamic updatedAt = _Unset,
109+
}) =>
110+
_SystemMessage(
111+
author: author ?? this.author,
112+
createdAt: createdAt == _Unset ? this.createdAt : createdAt as int?,
113+
id: id ?? this.id,
114+
metadata: metadata == _Unset
115+
? this.metadata
116+
: metadata as Map<String, dynamic>?,
117+
remoteId: remoteId == _Unset ? this.remoteId : remoteId as String?,
118+
repliedMessage: repliedMessage == _Unset
119+
? this.repliedMessage
120+
: repliedMessage as Message?,
121+
roomId: roomId == _Unset ? this.roomId : roomId as String?,
122+
showStatus:
123+
showStatus == _Unset ? this.showStatus : showStatus as bool?,
124+
status: status == _Unset ? this.status : status as Status?,
125+
updatedAt: updatedAt == _Unset ? this.updatedAt : updatedAt as int?,
126+
);
127+
}
128+
129+
class _Unset {}

lib/src/messages/system_message.g.dart

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/messages/text_message.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/messages/unsupported_message.g.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)