using LeanCloud;
using LeanCloud.Storage.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeanCloud.Realtime
{
///
/// 对话操作的签名类型,比如讲一个 client id 加入到对话中
///
///
public enum ConversationSignatureAction
{
///
/// add 加入对话和邀请对方加入对话
///
Add,
///
/// remove 当前 client Id 离开对话和将其他人踢出对话
///
Remove
}
///
///
///
public interface ISignatureFactory
{
///
/// 构建登录签名
///
/// 需要登录到云端服务器的 client Id
///
Task CreateConnectSignature(string clientId);
///
///
///
///
///
///
Task CreateStartConversationSignature(string clientId, IEnumerable targetIds);
///
///
///
///
///
///
/// 需要签名的操作
///
Task CreateConversationSignature(string conversationId, string clientId, IEnumerable targetIds, ConversationSignatureAction action);
}
internal class DefaulSiganatureFactory : ISignatureFactory
{
Task ISignatureFactory.CreateConnectSignature(string clientId)
{
return Task.FromResult(null);
}
Task ISignatureFactory.CreateConversationSignature(string conversationId, string clientId, IEnumerable targetIds, ConversationSignatureAction action)
{
return Task.FromResult(null);
}
Task ISignatureFactory.CreateStartConversationSignature(string clientId, IEnumerable targetIds)
{
return Task.FromResult(null);
}
}
public class LeanEngineSignatureFactory : ISignatureFactory
{
public Task CreateConnectSignature(string clientId)
{
var data = new Dictionary();
data.Add("client_id", clientId);
return AVCloud.CallFunctionAsync>("connect", data).OnSuccess(_ =>
{
var jsonData = _.Result;
var s = jsonData["signature"].ToString();
var n = jsonData["nonce"].ToString();
var t = long.Parse(jsonData["timestamp"].ToString());
var signature = new AVIMSignature(s, t, n);
return signature;
});
}
public Task CreateStartConversationSignature(string clientId, IEnumerable targetIds)
{
var data = new Dictionary();
data.Add("client_id", clientId);
data.Add("members", targetIds.ToList());
return AVCloud.CallFunctionAsync>("startConversation", data).OnSuccess(_ =>
{
var jsonData = _.Result;
var s = jsonData["signature"].ToString();
var n = jsonData["nonce"].ToString();
var t = long.Parse(jsonData["timestamp"].ToString());
var signature = new AVIMSignature(s, t, n);
return signature;
});
}
public Task CreateConversationSignature(string conversationId, string clientId, IEnumerable targetIds, ConversationSignatureAction action)
{
var actionList = new string[] { "invite", "kick" };
var data = new Dictionary();
data.Add("client_id", clientId);
data.Add("conv_id", conversationId);
data.Add("members", targetIds.ToList());
data.Add("action", actionList[(int)action]);
return AVCloud.CallFunctionAsync>("oprateConversation", data).OnSuccess(_ =>
{
var jsonData = _.Result;
var s = jsonData["signature"].ToString();
var n = jsonData["nonce"].ToString();
var t = long.Parse(jsonData["timestamp"].ToString());
var signature = new AVIMSignature(s, t, n);
return signature;
});
}
}
}