您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
131 行
4.4 KiB
131 行
4.4 KiB
using System.Collections.Generic;
|
|
|
|
namespace ChatComponents
|
|
{
|
|
public class TextMessage : Message
|
|
{
|
|
/// See [PreviewData]
|
|
public readonly PreviewData previewData;
|
|
|
|
/// User"s message
|
|
public readonly string text;
|
|
|
|
/// Creates a text message.
|
|
public TextMessage(
|
|
User author,
|
|
string id,
|
|
int? createdAt = null,
|
|
Dictionary<string, object> metadata = null,
|
|
string roomId = null,
|
|
Status? status = default,
|
|
PreviewData previewData = null,
|
|
string text = null
|
|
) : base(
|
|
author,
|
|
createdAt,
|
|
id,
|
|
metadata,
|
|
roomId,
|
|
status,
|
|
MessageType.text
|
|
)
|
|
{
|
|
this.previewData = previewData;
|
|
this.text = text;
|
|
}
|
|
|
|
|
|
public override List<object> props => new List<object>
|
|
{
|
|
author, createdAt, id, metadata, previewData, roomId, status, text
|
|
};
|
|
|
|
/// Creates a full text message from a partial one.
|
|
private TextMessage fromPartial(
|
|
User author,
|
|
string id,
|
|
PartialText partialText,
|
|
int? createdAt = null,
|
|
Dictionary<string, object> metadata = null,
|
|
string roomId = null,
|
|
Status? status = default
|
|
)
|
|
{
|
|
return new TextMessage(
|
|
author,
|
|
createdAt: createdAt,
|
|
id: id,
|
|
metadata: metadata,
|
|
roomId: roomId,
|
|
status: status,
|
|
previewData: null,
|
|
text: partialText.text);
|
|
}
|
|
|
|
/// Creates a text message from a map (decoded JSON).
|
|
private TextMessage fromJson(Dictionary<string, object> json)
|
|
{
|
|
var test = json["size"];
|
|
return new TextMessage(
|
|
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),
|
|
previewData: json["previewData"] == null
|
|
? null
|
|
: PreviewData.fromJson(json["previewData"] as Dictionary<string, object>),
|
|
text: json["text"] as string
|
|
);
|
|
}
|
|
|
|
public override Dictionary<string, object> toJson()
|
|
{
|
|
return new Dictionary<string, object>
|
|
{
|
|
{"author", author.toJson()},
|
|
{"createdAt", createdAt},
|
|
{"id", id},
|
|
{"metadata", metadata},
|
|
{"roomId", roomId},
|
|
{"previewData", previewData?.toJson()},
|
|
{"status", ChatRoomUtils.toShortString(status)},
|
|
{"type", ChatRoomUtils.toShortString(MessageType.file)},
|
|
{"text", text}
|
|
};
|
|
}
|
|
|
|
/// Converts a text message to the map representation, encodable to JSON.
|
|
/// Creates a copy of the text 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.
|
|
/// [status] with null value will be overwritten by the previous status.
|
|
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 TextMessage(
|
|
author,
|
|
createdAt: createdAt,
|
|
id: id,
|
|
metadata: metadata == null
|
|
? null
|
|
: result,
|
|
previewData: previewData,
|
|
roomId: roomId,
|
|
text: text ?? this.text,
|
|
status: status ?? this.status
|
|
);
|
|
}
|
|
}
|
|
}
|