shiyun wen
4 年前
当前提交
76f5f997
共有 1 个文件被更改,包括 97 次插入 和 0 次删除
|
|||
using System.Collections.Generic; |
|||
|
|||
namespace ChatComponents |
|||
{ |
|||
public class CustomMessage : Message |
|||
{ |
|||
/// Creates a custom message.
|
|||
public CustomMessage( |
|||
User author, |
|||
string id, |
|||
int? createdAt = null, |
|||
Dictionary<string, object> metadata = null, |
|||
string roomId = null, |
|||
Status? status = default |
|||
) : base( |
|||
author, |
|||
createdAt, |
|||
id, |
|||
metadata, |
|||
roomId, |
|||
status, |
|||
MessageType.custom |
|||
) |
|||
{ |
|||
} |
|||
|
|||
public override List<object> props => new List<object> |
|||
{ |
|||
author, |
|||
createdAt, |
|||
id, |
|||
metadata, |
|||
roomId, |
|||
status |
|||
}; |
|||
|
|||
/// Creates a custom message from a map (decoded JSON).
|
|||
public static CustomMessage fromJson(Dictionary<string, object> json) |
|||
{ |
|||
return new CustomMessage( |
|||
User.fromJson(json["author"] as Dictionary<string, object>), |
|||
createdAt: json["createdAt"] as int?, |
|||
id: json["id"] as string, |
|||
metadata: json["metadata"] as Dictionary<string, object>, |
|||
roomId: json["roomId"] as string, |
|||
status: ChatRoomUtils.getStatusFromString(json["status"] as string)); |
|||
} |
|||
|
|||
/// Converts a custom message to the map representation,
|
|||
/// encodable to JSON.
|
|||
public override Dictionary<string, object> toJson() |
|||
{ |
|||
return new Dictionary<string, object> |
|||
{ |
|||
{"author", author.toJson()}, |
|||
{"createdAt", createdAt}, |
|||
{"id", id}, |
|||
{"metadata", metadata}, |
|||
{"roomId", roomId}, |
|||
{"status", ChatRoomUtils.toShortString(status)}, |
|||
{"type", ChatRoomUtils.toShortString(type)} |
|||
}; |
|||
} |
|||
|
|||
/// Creates a copy of the custom message with an updated data.
|
|||
/// [metadata] with null value will nullify existing metadata, otherwise
|
|||
/// both metadatas will be merged into one Map, where keys from a passed
|
|||
/// metadata will overwite keys from the previous one.
|
|||
/// [previewData] is ignored for this message type.
|
|||
/// [status] with null value will be overwritten by the previous status.
|
|||
/// [text] is ignored for this message type.
|
|||
public override Message copyWith( |
|||
Dictionary<string, object> metadata = null, |
|||
PreviewData previewData = null, |
|||
Status? status = null, |
|||
string text = null |
|||
) |
|||
{ |
|||
var result = new Dictionary<string, object>(); |
|||
if (this.metadata != null) |
|||
foreach (var metaItem in this.metadata) |
|||
result.Add(metaItem.Key, metaItem.Value); |
|||
foreach (var metaItem in metadata) result.Add(metaItem.Key, metaItem.Value); |
|||
return new CustomMessage( |
|||
author, |
|||
createdAt: createdAt, |
|||
id: id, |
|||
metadata: metadata == null |
|||
? null |
|||
: result, |
|||
roomId: roomId, |
|||
status: status ?? this.status |
|||
); |
|||
} |
|||
/// Equatable props
|
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue