using System.Collections.Generic; using System.Linq; using UIWidgetsSample; namespace ChatComponents { public enum RoomType { channel, direct, group, unsupported } /// A class that represents a room where 2 or more participants can chat public class Room : Equatable { /// Created room timestamp, in ms public readonly int? createdAt; /// Room"s unique ID public readonly string id; /// Room"s image. In case of the [RoomType.direct] - avatar of the second person, /// otherwise a custom image [RoomType.group]. public readonly string imageUrl; /// Additional custom metadata or attributes related to the room public readonly Dictionary metadata; /// Room"s name. In case of the [RoomType.direct] - name of the second person, /// otherwise a custom name [RoomType.group]. public readonly string name; /// [RoomType] public readonly RoomType type; /// List of users which are in the room public readonly List users; /// Creates a [Room] public Room( RoomType type, List users, string id, int? createdAt = null, string imageUrl = null, Dictionary metadata = null, string name = null ) { this.type = type; this.users = users; this.id = id; this.createdAt = createdAt; this.imageUrl = imageUrl; this.metadata = metadata; this.name = name; } /// Equatable props public override List props => new List {createdAt, id, imageUrl, metadata, name, type, users}; /// Creates room from a map (decoded JSON). public Room fromJson(Dictionary json) { var users = (json["users"] as List>); var usersList = new List(); foreach (var user in users){ if (User.fromJson(user) != null) { usersList.Add(User.fromJson(user)); } } return new Room( createdAt: json["createdAt"] as int?, id: json["id"] as string, imageUrl: json["imageUrl"] as string, metadata: json["metadata"] as Dictionary, name: json["name"] as string, type: ChatRoomUtils.getRoomTypeFromString(json["type"] as string), users: usersList); } /// Converts room to the map representation, encodable to JSON. public Dictionary toJson() { var users = new List(); return new Dictionary { {"createdAt", createdAt}, {"id", id}, {"imageUrl", imageUrl}, {"metadata", metadata}, {"name", name}, {"type", ChatRoomUtils.toShortString(type)}, {"users", users.Where(e => e.toJson() != null).ToList()} }; } /// Creates a copy of the room with an updated data. /// [imageUrl] and [name] with null values will nullify existing values /// [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. /// [type] and [users] with null values will be overwritten by previous values. private Room copyWith( string imageUrl = null, Dictionary metadata = null, string name = null, RoomType? type = null, List users = 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 Room( id: id, imageUrl: imageUrl, metadata: metadata == null ? null : result, name: name, type: type ?? this.type, users: users ?? this.users ); } } }