2022-02-15 11:52:43 +00:00
|
|
|
namespace ASC.Core.Notify.Signalr;
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
public class ConfigureSignalrServiceClient : IConfigureNamedOptions<SignalrServiceClient>
|
2020-10-28 12:26:27 +00:00
|
|
|
{
|
2022-02-15 12:30:54 +00:00
|
|
|
internal readonly TenantManager TenantManager;
|
|
|
|
internal readonly CoreSettings CoreSettings;
|
|
|
|
internal readonly MachinePseudoKeys MachinePseudoKeys;
|
|
|
|
internal readonly IConfiguration Configuration;
|
|
|
|
internal readonly IOptionsMonitor<ILog> Options;
|
|
|
|
internal readonly IHttpClientFactory ClientFactory;
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public ConfigureSignalrServiceClient(
|
|
|
|
TenantManager tenantManager,
|
|
|
|
CoreSettings coreSettings,
|
|
|
|
MachinePseudoKeys machinePseudoKeys,
|
|
|
|
IConfiguration configuration,
|
|
|
|
IOptionsMonitor<ILog> options,
|
|
|
|
IHttpClientFactory clientFactory)
|
|
|
|
{
|
|
|
|
TenantManager = tenantManager;
|
|
|
|
CoreSettings = coreSettings;
|
|
|
|
MachinePseudoKeys = machinePseudoKeys;
|
|
|
|
Configuration = configuration;
|
|
|
|
Options = options;
|
|
|
|
ClientFactory = clientFactory;
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void Configure(string name, SignalrServiceClient options)
|
|
|
|
{
|
2022-02-15 12:30:54 +00:00
|
|
|
options.Logger = Options.CurrentValue;
|
2022-02-15 11:52:43 +00:00
|
|
|
options.Hub = name.Trim('/');
|
|
|
|
options.TenantManager = TenantManager;
|
|
|
|
options.CoreSettings = CoreSettings;
|
|
|
|
options.ClientFactory = ClientFactory;
|
|
|
|
options.SKey = MachinePseudoKeys.GetMachineConstant();
|
|
|
|
options.Url = Configuration["web:hub:internal"];
|
|
|
|
options.EnableSignalr = !string.IsNullOrEmpty(options.Url);
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var replaceSetting = Configuration["jabber:replace-domain"];
|
|
|
|
if (!string.IsNullOrEmpty(replaceSetting))
|
2022-02-14 21:02:57 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
options.JabberReplaceDomain = true;
|
|
|
|
var q =
|
|
|
|
replaceSetting.Split(new[] { "->" }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
.Select(s => s.Trim().ToLowerInvariant())
|
|
|
|
.ToList();
|
|
|
|
options.JabberReplaceFromDomain = q.ElementAt(0);
|
|
|
|
options.JabberReplaceToDomain = q.ElementAt(1);
|
2022-02-14 21:02:57 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
catch (Exception) { }
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void Configure(SignalrServiceClient options)
|
|
|
|
{
|
|
|
|
Configure("default", options);
|
|
|
|
}
|
|
|
|
}
|
2021-10-12 10:14:33 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
[Scope(typeof(ConfigureSignalrServiceClient))]
|
|
|
|
public class SignalrServiceClient
|
|
|
|
{
|
|
|
|
private static readonly TimeSpan _timeout = TimeSpan.FromSeconds(1);
|
2022-02-15 12:30:54 +00:00
|
|
|
internal ILog Logger;
|
2022-02-15 11:52:43 +00:00
|
|
|
private static DateTime _lastErrorTime;
|
|
|
|
public bool EnableSignalr { get; set; }
|
|
|
|
internal byte[] SKey;
|
|
|
|
internal string Url;
|
|
|
|
internal bool JabberReplaceDomain;
|
|
|
|
internal string JabberReplaceFromDomain;
|
|
|
|
internal string JabberReplaceToDomain;
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
internal string Hub;
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 12:30:54 +00:00
|
|
|
internal TenantManager TenantManager;
|
|
|
|
internal CoreSettings CoreSettings;
|
|
|
|
internal IHttpClientFactory ClientFactory;
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public SignalrServiceClient() { }
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void SendMessage(string callerUserName, string calleeUserName, string messageText, int tenantId,
|
|
|
|
string domain)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
domain = ReplaceDomain(domain);
|
|
|
|
var tenant = tenantId == -1
|
|
|
|
? TenantManager.GetTenant(domain)
|
|
|
|
: TenantManager.GetTenant(tenantId);
|
|
|
|
var isTenantUser = callerUserName.Length == 0;
|
|
|
|
var message = new MessageClass
|
2021-10-12 10:14:33 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
UserName = isTenantUser ? tenant.GetTenantDomain(CoreSettings) : callerUserName,
|
|
|
|
Text = messageText
|
|
|
|
};
|
2020-02-17 08:58:14 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
MakeRequest("send", new { tenantId = tenant.Id, callerUserName, calleeUserName, message, isTenantUser });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void SendInvite(string chatRoomName, string calleeUserName, string domain)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
domain = ReplaceDomain(domain);
|
2020-02-17 08:58:14 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
var tenant = TenantManager.GetTenant(domain);
|
2020-02-13 15:12:27 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
var message = new MessageClass
|
|
|
|
{
|
|
|
|
UserName = tenant.GetTenantDomain(CoreSettings),
|
|
|
|
Text = chatRoomName
|
|
|
|
};
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
MakeRequest("sendInvite", new { tenantId = tenant.Id, calleeUserName, message });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void SendState(string from, byte state, int tenantId, string domain)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
domain = ReplaceDomain(domain);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (tenantId == -1)
|
|
|
|
{
|
|
|
|
tenantId = TenantManager.GetTenant(domain).Id;
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
MakeRequest("setState", new { tenantId, from, state });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void SendOfflineMessages(string callerUserName, List<string> users, int tenantId)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("sendOfflineMessages", new { tenantId, callerUserName, users });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void SendUnreadCounts(Dictionary<string, int> unreadCounts, string domain)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
domain = ReplaceDomain(domain);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
var tenant = TenantManager.GetTenant(domain);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
MakeRequest("sendUnreadCounts", new { tenantId = tenant.Id, unreadCounts });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void SendUnreadUsers(Dictionary<int, Dictionary<Guid, int>> unreadUsers)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("sendUnreadUsers", unreadUsers);
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void SendUnreadUser(int tenant, string userId, int count)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("updateFolders", new { tenant, userId, count });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void SendMailNotification(int tenant, string userId, int state)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("sendMailNotification", new { tenant, userId, state });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void EnqueueCall(string numberId, string callId, string agent)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("enqueue", new { numberId, callId, agent });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void IncomingCall(string callId, string agent)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("incoming", new { callId, agent });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void MissCall(string numberId, string callId, string agent)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("miss", new { numberId, callId, agent });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public void Reload(string numberId, string agentId = null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var numberRoom = TenantManager.GetCurrentTenant().Id + numberId;
|
|
|
|
MakeRequest("reload", new { numberRoom, agentId });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
|
|
|
public void StartEdit<T>(T fileId, string room)
|
|
|
|
{
|
|
|
|
try
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
MakeRequest("start-edit", new { room, fileId });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
catch (Exception error)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
ProcessError(error);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-25 17:16:00 +00:00
|
|
|
public void StopEdit<T>(T fileId, string room, string data)
|
|
|
|
{
|
|
|
|
try
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
MakeRequest("stop-edit", new { room, fileId, data });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
catch (Exception error)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
ProcessError(error);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
2022-02-25 17:16:00 +00:00
|
|
|
public void CreateFile<T>(T fileId, string room, string data)
|
|
|
|
{
|
|
|
|
try
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
MakeRequest("create-file", new { room, fileId, data });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
catch (Exception error)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
ProcessError(error);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 17:16:00 +00:00
|
|
|
public void DeleteFile<T>(T fileId, string room)
|
|
|
|
{
|
|
|
|
try
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
MakeRequest("delete-file", new { room, fileId });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
catch (Exception error)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
ProcessError(error);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public T GetAgent<T>(string numberId, List<Guid> contactsResponsibles)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return MakeRequest<T>("GetAgent", new { numberId, contactsResponsibles });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
return default;
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private string ReplaceDomain(string domain)
|
|
|
|
{
|
|
|
|
if (JabberReplaceDomain && domain.EndsWith(JabberReplaceFromDomain))
|
|
|
|
{
|
|
|
|
var place = domain.LastIndexOf(JabberReplaceFromDomain);
|
|
|
|
if (place >= 0)
|
|
|
|
{
|
|
|
|
return domain.Remove(place, JabberReplaceFromDomain.Length).Insert(place, JabberReplaceToDomain);
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
return domain;
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
private void ProcessError(Exception e)
|
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
Logger.ErrorFormat("Service Error: {0}, {1}, {2}", e.Message, e.StackTrace,
|
2019-05-15 14:56:09 +00:00
|
|
|
(e.InnerException != null) ? e.InnerException.Message : string.Empty);
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-23 14:08:00 +00:00
|
|
|
if (e is HttpRequestException)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-02-25 17:16:00 +00:00
|
|
|
_lastErrorTime = DateTime.Now;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private string MakeRequest(string method, object data)
|
|
|
|
{
|
|
|
|
if (!IsAvailable())
|
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = new HttpRequestMessage();
|
|
|
|
request.Headers.Add("Authorization", CreateAuthToken());
|
|
|
|
request.Method = HttpMethod.Post;
|
|
|
|
request.RequestUri = new Uri(GetMethod(method));
|
|
|
|
|
|
|
|
var jsonData = JsonConvert.SerializeObject(data);
|
2022-02-15 12:30:54 +00:00
|
|
|
Logger.DebugFormat("Method:{0}, Data:{1}", method, jsonData);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
|
|
|
|
|
|
|
|
var httpClient = ClientFactory.CreateClient();
|
|
|
|
|
|
|
|
using (var response = httpClient.Send(request))
|
|
|
|
using (var stream = response.Content.ReadAsStream())
|
|
|
|
using (var streamReader = new StreamReader(stream))
|
|
|
|
{
|
|
|
|
return streamReader.ReadToEnd();
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private T MakeRequest<T>(string method, object data)
|
|
|
|
{
|
|
|
|
var resultMakeRequest = MakeRequest(method, data);
|
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(resultMakeRequest);
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private bool IsAvailable()
|
|
|
|
{
|
|
|
|
return EnableSignalr && _lastErrorTime + _timeout < DateTime.Now;
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
private string GetMethod(string method)
|
|
|
|
{
|
|
|
|
return $"{Url.TrimEnd('/')}/controller/{Hub}/{method}";
|
|
|
|
}
|
2022-02-25 17:16:00 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public string CreateAuthToken(string pkey = "socketio")
|
|
|
|
{
|
|
|
|
using var hasher = new HMACSHA1(SKey);
|
|
|
|
var now = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
|
|
|
|
var hash = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(string.Join("\n", now, pkey))));
|
|
|
|
|
|
|
|
return $"ASC {pkey}:{now}:{hash}";
|
|
|
|
}
|
|
|
|
}
|