您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

138 行
4.5 KiB

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<string, object> 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<User> users;
/// Creates a [Room]
public Room(
RoomType type,
List<User> users,
string id,
int? createdAt = null,
string imageUrl = null,
Dictionary<string, object> 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<object> props => new List<object>
{createdAt, id, imageUrl, metadata, name, type, users};
/// Creates room from a map (decoded JSON).
public Room fromJson(Dictionary<string, object> json)
{
var users = (json["users"] as List<Dictionary<string, object>>);
var usersList = new List<User>();
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<string, object>,
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<string, object> toJson()
{
var users = new List<User>();
return new Dictionary<string, object>
{
{"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<string, object> metadata = null,
string name = null,
RoomType? type = null,
List<User> users = 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 Room(
id: id,
imageUrl: imageUrl,
metadata: metadata == null
? null
: result,
name: name,
type: type ?? this.type,
users: users ?? this.users
);
}
}
}