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

147 行
4.6 KiB

using System.Collections.Generic;
using UIWidgetsSample;
using Unity.UIWidgets.foundation;
namespace ChatComponents
{
public enum Role
{
admin,
agent,
moderator,
user
}
/// Extension with one [toShortString] method
/// RoleToShortString
public class RoleToShortString
{
/// Converts enum to the string equal to enum's name
public Role role;
public string toShortString()
{
return ToString().Split('.').last();
}
}
/// A class that represents user.
public class User : Equatable
{
/// Created user timestamp, in ms
public readonly int? createdAt;
/// First name of the user
public readonly string firstName;
/// Unique ID of the user
public readonly string id;
/// Remote image URL representing user's avatar
public readonly string imageUrl;
/// Last name of the user
public readonly string lastName;
/// Timestamp when user was last visible, in ms
public readonly int? lastSeen;
/// Additional custom metadata or attributes related to the user
public readonly Dictionary<string, object> metadata;
/// User [Role]
public Role? role;
/// Creates a user.
public User(
string id,
int? createdAt = null,
string firstName = null,
string imageUrl = null,
string lastName = null,
int? lastSeen = null,
Dictionary<string, object> metadata = null,
Role? role = null
)
{
this.createdAt = createdAt;
this.firstName = firstName;
this.id = id;
this.imageUrl = imageUrl;
this.lastName = lastName;
this.lastSeen = lastSeen;
this.metadata = metadata;
this.role = role;
}
/// Equatable props
public override List<object> props => new List<object>
{createdAt, firstName, id, imageUrl, lastName, lastSeen, metadata, role};
/// Creates user from a map (decoded JSON).
public static User fromJson(Dictionary<string, object> json)
{
return new User(
createdAt: json["createdAt"] as int?,
firstName: json["firstName"] as string,
id: json["id"] as string,
imageUrl: json["imageUrl"] as string,
lastName: json["lastName"] as string,
lastSeen: json["lastSeen"] as int?,
metadata: json["metadata"] as Dictionary<string, object>,
role: ChatRoomUtils.getRoleFromString(json["role"] as string)
);
/// Converts user to the map representation, encodable to JSON.
}
public Dictionary<string, object> toJson()
{
return new Dictionary<string, object>
{
{"createdAt", createdAt},
{"firstName", firstName},
{"id", id},
{"imageUrl", imageUrl},
{"lastName", lastName},
{"lastSeen", lastSeen},
{"metadata", metadata},
{"role", ChatRoomUtils.toShortString(role == null ? null : role)}
};
}
/// Creates a copy of the user with an updated data.
/// [firstName], [imageUrl], [lastName], [lastSeen] and [role] 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.
private User copyWith(
string firstName = null,
string imageUrl = null,
string lastName = null,
int? lastSeen = null,
Dictionary<string, object> metadata = null,
Role? role = 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 User(
firstName: firstName,
id: id,
imageUrl: imageUrl,
lastName: lastName,
lastSeen: lastSeen,
metadata: metadata == null
? null
: result,
role: role
);
}
}
}