using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Newtonsoft.Json;
using LeanCloud.Realtime.Internal.Protocol;
using LeanCloud.Storage.Internal;
using LeanCloud.Storage.Internal.Codec;
using LeanCloud.Common;
namespace LeanCloud.Realtime.Internal.Controller {
internal class LCIMConversationController : LCIMController {
internal LCIMConversationController(LCIMClient client) : base(client) {
}
#region 内部接口
///
/// 创建对话
///
///
///
///
///
///
///
///
///
internal async Task CreateConv(
IEnumerable members = null,
string name = null,
bool transient = false,
bool unique = true,
bool temporary = false,
int temporaryTtl = 86400,
Dictionary properties = null) {
GenericCommand request = NewCommand(CommandType.Conv, OpType.Start);
ConvCommand conv = new ConvCommand {
Transient = transient,
Unique = unique,
};
if (members != null) {
conv.M.AddRange(members);
}
if (!string.IsNullOrEmpty(name)) {
conv.N = name;
}
if (temporary) {
conv.TempConv = temporary;
conv.TempConvTTL = temporaryTtl;
}
if (properties != null) {
conv.Attr = new JsonObjectMessage {
Data = JsonConvert.SerializeObject(LCEncoder.Encode(properties))
};
}
if (Client.SignatureFactory != null) {
LCIMSignature signature = await Client.SignatureFactory.CreateStartConversationSignature(Client.Id, members);
conv.S = signature.Signature;
conv.T = signature.Timestamp;
conv.N = signature.Nonce;
}
request.ConvMessage = conv;
GenericCommand response = await Connection.SendRequest(request);
string convId = response.ConvMessage.Cid;
if (!Client.ConversationDict.TryGetValue(convId, out LCIMConversation conversation)) {
if (transient) {
conversation = new LCIMChatRoom(Client);
} else if (temporary) {
conversation = new LCIMTemporaryConversation(Client);
} else if (properties != null && properties.ContainsKey("system")) {
conversation = new LCIMServiceConversation(Client);
} else {
conversation = new LCIMConversation(Client);
}
Client.ConversationDict[convId] = conversation;
}
// 合并请求数据
conversation.Id = convId;
conversation.Unique = unique;
conversation.UniqueId = response.ConvMessage.UniqueId;
conversation.Name = name;
conversation.CreatorId = Client.Id;
conversation.ids = members != null ?
new HashSet(members) : new HashSet();
conversation.CreatedAt = DateTime.Parse(response.ConvMessage.Cdate);
conversation.UpdatedAt = conversation.CreatedAt;
return conversation;
}
///
/// 查询成员数量
///
///
///
internal async Task GetMembersCount(string convId) {
ConvCommand conv = new ConvCommand {
Cid = convId,
};
GenericCommand command = NewCommand(CommandType.Conv, OpType.Count);
command.ConvMessage = conv;
GenericCommand response = await Connection.SendRequest(command);
return response.ConvMessage.Count;
}
///
/// 更新对话属性
///
///
///
///
internal async Task> UpdateInfo(string convId,
Dictionary attributes) {
ConvCommand conv = new ConvCommand {
Cid = convId,
};
conv.Attr = new JsonObjectMessage {
Data = JsonConvert.SerializeObject(attributes)
};
GenericCommand request = NewCommand(CommandType.Conv, OpType.Update);
request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request);
JsonObjectMessage attr = response.ConvMessage.AttrModified;
// 更新自定义属性
if (attr != null) {
Dictionary updatedAttr = JsonConvert.DeserializeObject>(attr.Data);
return updatedAttr;
}
return null;
}
///
/// 增加成员
///
///
///
///
internal async Task AddMembers(string convId,
IEnumerable clientIds) {
ConvCommand conv = new ConvCommand {
Cid = convId,
};
conv.M.AddRange(clientIds);
// 签名参数
if (Client.SignatureFactory != null) {
LCIMSignature signature = await Client.SignatureFactory.CreateConversationSignature(convId,
Client.Id,
clientIds,
LCIMSignatureAction.Invite);
conv.S = signature.Signature;
conv.T = signature.Timestamp;
conv.N = signature.Nonce;
}
GenericCommand request = NewCommand(CommandType.Conv, OpType.Add);
request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request);
List allowedIds = response.ConvMessage.AllowedPids.ToList();
List errors = response.ConvMessage.FailedPids.ToList();
return NewPartiallySuccessResult(allowedIds, errors);
}
///
/// 移除成员
///
///
///
///
internal async Task RemoveMembers(string convId,
IEnumerable removeIds) {
ConvCommand conv = new ConvCommand {
Cid = convId,
};
conv.M.AddRange(removeIds);
// 签名参数
if (Client.SignatureFactory != null) {
LCIMSignature signature = await Client.SignatureFactory.CreateConversationSignature(convId,
Client.Id,
removeIds,
LCIMSignatureAction.Kick);
conv.S = signature.Signature;
conv.T = signature.Timestamp;
conv.N = signature.Nonce;
}
GenericCommand request = NewCommand(CommandType.Conv, OpType.Remove);
request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request);
List allowedIds = response.ConvMessage.AllowedPids.ToList();
List errors = response.ConvMessage.FailedPids.ToList();
return NewPartiallySuccessResult(allowedIds, errors);
}
///
/// 静音
///
///
///
internal async Task Mute(string convId) {
ConvCommand conv = new ConvCommand {
Cid = convId
};
GenericCommand request = NewCommand(CommandType.Conv, OpType.Mute);
request.ConvMessage = conv;
await Client.Connection.SendRequest(request);
}
///
/// 解除静音
///
///
///
internal async Task Unmute(string convId) {
ConvCommand conv = new ConvCommand {
Cid = convId
};
GenericCommand request = NewCommand(CommandType.Conv, OpType.Unmute);
request.ConvMessage = conv;
await Client.Connection.SendRequest(request);
}
///
/// 禁言用户
///
///
///
///
internal async Task MuteMembers(string convId,
IEnumerable clientIds) {
if (clientIds == null || clientIds.Count() == 0) {
throw new ArgumentNullException(nameof(clientIds));
}
ConvCommand conv = new ConvCommand {
Cid = convId
};
conv.M.AddRange(clientIds);
GenericCommand request = NewCommand(CommandType.Conv, OpType.AddShutup);
request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request);
return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids);
}
///
/// 解除用户禁言
///
///
///
///
internal async Task UnmuteMembers(string convId,
IEnumerable clientIds) {
ConvCommand conv = new ConvCommand {
Cid = convId
};
conv.M.AddRange(clientIds);
GenericCommand request = NewCommand(CommandType.Conv, OpType.RemoveShutup);
request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request);
return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids);
}
///
/// 拉黑成员
///
///
///
///
internal async Task BlockMembers(string convId,
IEnumerable clientIds) {
BlacklistCommand blacklist = new BlacklistCommand {
SrcCid = convId,
};
blacklist.ToPids.AddRange(clientIds);
if (Client.SignatureFactory != null) {
LCIMSignature signature = await Client.SignatureFactory.CreateBlacklistSignature(convId,
Client.Id,
clientIds,
LCIMSignatureAction.ConversationBlockClients);
blacklist.S = signature.Signature;
blacklist.T = signature.Timestamp;
blacklist.N = signature.Nonce;
}
GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Block);
request.BlacklistMessage = blacklist;
GenericCommand response = await Client.Connection.SendRequest(request);
return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids);
}
///
/// 移除成员黑名单
///
///
///
///
internal async Task UnblockMembers(string convId,
IEnumerable clientIds) {
BlacklistCommand blacklist = new BlacklistCommand {
SrcCid = convId,
};
blacklist.ToPids.AddRange(clientIds);
if (Client.SignatureFactory != null) {
LCIMSignature signature = await Client.SignatureFactory.CreateBlacklistSignature(convId,
Client.Id,
clientIds,
LCIMSignatureAction.ConversationUnblockClients);
blacklist.S = signature.Signature;
blacklist.T = signature.Timestamp;
blacklist.N = signature.Nonce;
}
GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Unblock);
request.BlacklistMessage = blacklist;
GenericCommand response = await Client.Connection.SendRequest(request);
return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids);
}
///
/// 修改成员角色
///
///
///
///
///
internal async Task UpdateMemberRole(string convId,
string memberId,
string role) {
ConvCommand conv = new ConvCommand {
Cid = convId,
TargetClientId = memberId,
Info = new ConvMemberInfo {
Pid = memberId,
Role = role
}
};
GenericCommand request = NewCommand(CommandType.Conv, OpType.MemberInfoUpdate);
request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request);
}
///
/// 获取所有成员角色
///
///
///
internal async Task> GetAllMemberInfo(string convId) {
string path = "classes/_ConversationMemberInfo";
string token = await Client.SessionController.GetToken();
Dictionary headers = new Dictionary {
{ "X-LC-IM-Session-Token", token }
};
Dictionary queryParams = new Dictionary {
{ "client_id", Client.Id },
{ "cid", convId }
};
Dictionary response = await LCApplication.HttpClient.Get>(path,
headers: headers, queryParams: queryParams);
List