using System; using System.Collections.Generic; namespace ChatComponents { public class ImageMessage : Message { /// Creates a custom message. public readonly string name; public readonly int size; public readonly string uri; public readonly float? width; public readonly float? height; public ImageMessage( User author, string id, string name, int size, string uri, int? createdAt = null, Dictionary metadata = null, string roomId = null, Status? status = default, float? height = null, float? width = null ) : base( author, createdAt, id, metadata, roomId, status, MessageType.image ) { this.name = name; this.size = size; this.uri = uri; this.height = height; this.width = width; } public override List props => new List { author, createdAt, id, metadata, height, width, name, roomId, size, status, uri }; public ImageMessage fromPartial( User author, string id, PartialImage partialImage, int? createdAt = null, Dictionary metadata = null, string roomId = null, Status? status = default ) { return new ImageMessage( author, createdAt: createdAt, id: id, metadata: metadata, roomId: roomId, status: status, name: partialImage.name, size: partialImage.size, uri: partialImage.uri, height: partialImage.height, width: partialImage.width ); } /// Creates a custom message from a map (decoded JSON). public static ImageMessage fromJson(Dictionary json) { var test = json["size"]; return new ImageMessage( User.fromJson(json["author"] as Dictionary), createdAt: json["createdAt"] as int?, id: json["id"] as string, metadata: json["metadata"] as Dictionary, roomId: json["roomId"] as string, status: ChatRoomUtils.getStatusFromString(json["status"] as string), height: Convert.ToDouble(json["height"]) is float ? (float) Convert.ToDouble(json["height"]) : 0f, width: Convert.ToDouble(json["width"]) is float ? (float) Convert.ToDouble(json["width"]) : 0f, name: json["name"] as string, size: Convert.ToInt32(json["size"]), uri: json["uri"] as string ); } /// Converts a custom message to the map representation, /// encodable to JSON. public override Dictionary toJson() { return new Dictionary { {"author", author.toJson()}, {"createdAt", createdAt}, {"id", id}, {"metadata", metadata}, {"name", name}, {"roomId", roomId}, {"size", size}, {"status", ChatRoomUtils.toShortString(status)}, {"type", ChatRoomUtils.toShortString(MessageType.file)}, {"uri", uri}, {"height", height}, {"width", width} }; } /// 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 metadata = null, PreviewData previewData = null, Status? status = null, string text = null ) { var result = new Dictionary(); 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 ImageMessage( author, createdAt: createdAt, id: id, metadata: metadata == null ? null : result, name: name, roomId: roomId, size: size, status: status ?? this.status, uri: uri, height: height, width: width ); } /// Equatable props } }