refactoring: voipService
This commit is contained in:
parent
420f34c3ee
commit
e42e0aa4e5
@ -28,23 +28,22 @@ namespace ASC.VoipService.Dao
|
||||
{
|
||||
public class AbstractDao
|
||||
{
|
||||
private readonly string dbid = "default";
|
||||
|
||||
private Lazy<VoipDbContext> LazyVoipDbContext { get; }
|
||||
protected VoipDbContext VoipDbContext { get => LazyVoipDbContext.Value; }
|
||||
|
||||
protected AbstractDao(DbContextManager<VoipDbContext> dbOptions, TenantManager tenantManager)
|
||||
{
|
||||
LazyVoipDbContext = new Lazy<VoipDbContext>(() => dbOptions.Get(dbid));
|
||||
TenantID = tenantManager.GetCurrentTenant().TenantId;
|
||||
}
|
||||
private readonly string _dbid = "default";
|
||||
private Lazy<VoipDbContext> _lazyVoipDbContext;
|
||||
|
||||
protected VoipDbContext VoipDbContext { get => _lazyVoipDbContext.Value; }
|
||||
protected int TenantID
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
protected AbstractDao(DbContextManager<VoipDbContext> dbOptions, TenantManager tenantManager)
|
||||
{
|
||||
_lazyVoipDbContext = new Lazy<VoipDbContext>(() => dbOptions.Get(_dbid));
|
||||
TenantID = tenantManager.GetCurrentTenant().TenantId;
|
||||
}
|
||||
|
||||
protected string GetTenantColumnName(string table)
|
||||
{
|
||||
const string tenant = "tenant_id";
|
||||
|
@ -23,34 +23,34 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace ASC.VoipService.Dao
|
||||
{
|
||||
namespace ASC.VoipService.Dao;
|
||||
|
||||
[Singletone]
|
||||
public class VoipDaoCache
|
||||
{
|
||||
internal ICache Cache { get; }
|
||||
private ICacheNotify<CachedVoipItem> Notify { get; }
|
||||
internal readonly ICache _cache;
|
||||
private readonly ICacheNotify<CachedVoipItem> _notify;
|
||||
|
||||
public VoipDaoCache(ICacheNotify<CachedVoipItem> notify, ICache cache)
|
||||
{
|
||||
Cache = cache;
|
||||
Notify = notify;
|
||||
Notify.Subscribe((c) => Cache.Remove(CachedVoipDao.GetCacheKey(c.Tenant)), Common.Caching.CacheNotifyAction.Any);
|
||||
_cache = cache;
|
||||
_notify = notify;
|
||||
_notify.Subscribe((c) => _cache.Remove(CachedVoipDao.GetCacheKey(c.Tenant)), Common.Caching.CacheNotifyAction.Any);
|
||||
}
|
||||
|
||||
public void ResetCache(int tenant)
|
||||
{
|
||||
Notify.Publish(new CachedVoipItem { Tenant = tenant }, Common.Caching.CacheNotifyAction.Any);
|
||||
_notify.Publish(new CachedVoipItem { Tenant = tenant }, Common.Caching.CacheNotifyAction.Any);
|
||||
}
|
||||
}
|
||||
|
||||
[Scope]
|
||||
public class CachedVoipDao : VoipDao
|
||||
{
|
||||
private readonly ICache cache;
|
||||
private static readonly TimeSpan timeout = TimeSpan.FromDays(1);
|
||||
|
||||
private VoipDaoCache VoipDaoCache { get; }
|
||||
private readonly ICache _cache;
|
||||
private readonly VoipDaoCache _voipDaoCache;
|
||||
|
||||
public CachedVoipDao(
|
||||
TenantManager tenantManager,
|
||||
@ -63,30 +63,30 @@ namespace ASC.VoipService.Dao
|
||||
VoipDaoCache voipDaoCache)
|
||||
: base(tenantManager, dbOptions, authContext, tenantUtil, securityContext, baseCommonLinkUtility, consumerFactory)
|
||||
{
|
||||
cache = voipDaoCache.Cache;
|
||||
VoipDaoCache = voipDaoCache;
|
||||
_cache = voipDaoCache._cache;
|
||||
_voipDaoCache = voipDaoCache;
|
||||
}
|
||||
|
||||
public override VoipPhone SaveOrUpdateNumber(VoipPhone phone)
|
||||
{
|
||||
var result = base.SaveOrUpdateNumber(phone);
|
||||
VoipDaoCache.ResetCache(TenantID);
|
||||
_voipDaoCache.ResetCache(TenantID);
|
||||
return result;
|
||||
}
|
||||
|
||||
public override void DeleteNumber(string phoneId = "")
|
||||
{
|
||||
base.DeleteNumber(phoneId);
|
||||
VoipDaoCache.ResetCache(TenantID);
|
||||
_voipDaoCache.ResetCache(TenantID);
|
||||
}
|
||||
|
||||
public override IEnumerable<VoipPhone> GetNumbers(params string[] ids)
|
||||
{
|
||||
var numbers = cache.Get<List<VoipPhone>>(GetCacheKey(TenantID));
|
||||
var numbers = _cache.Get<List<VoipPhone>>(GetCacheKey(TenantID));
|
||||
if (numbers == null)
|
||||
{
|
||||
numbers = new List<VoipPhone>(base.GetAllNumbers());
|
||||
cache.Insert(GetCacheKey(TenantID), numbers, DateTime.UtcNow.Add(timeout));
|
||||
_cache.Insert(GetCacheKey(TenantID), numbers, DateTime.UtcNow.Add(timeout));
|
||||
}
|
||||
|
||||
return ids.Length > 0 ? numbers.Where(r => ids.Contains(r.Id) || ids.Contains(r.Number)).ToList() : numbers;
|
||||
@ -97,4 +97,3 @@ namespace ASC.VoipService.Dao
|
||||
return "voip" + tenant.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
}
|
@ -23,8 +23,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace ASC.VoipService.Dao
|
||||
{
|
||||
namespace ASC.VoipService.Dao;
|
||||
|
||||
public class VoipCallFilter
|
||||
{
|
||||
public string Type { get; set; }
|
||||
@ -111,4 +111,3 @@ namespace ASC.VoipService.Dao
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -23,11 +23,17 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace ASC.VoipService.Dao
|
||||
{
|
||||
namespace ASC.VoipService.Dao;
|
||||
|
||||
[Scope(typeof(CachedVoipDao))]
|
||||
public class VoipDao : AbstractDao
|
||||
{
|
||||
private readonly AuthContext _authContext;
|
||||
private readonly TenantUtil _tenantUtil;
|
||||
private readonly SecurityContext _securityContext;
|
||||
private readonly BaseCommonLinkUtility _baseCommonLinkUtility;
|
||||
private readonly ConsumerFactory _consumerFactory;
|
||||
|
||||
public VoipDao(
|
||||
TenantManager tenantManager,
|
||||
DbContextManager<VoipDbContext> dbOptions,
|
||||
@ -38,11 +44,11 @@ namespace ASC.VoipService.Dao
|
||||
ConsumerFactory consumerFactory)
|
||||
: base(dbOptions, tenantManager)
|
||||
{
|
||||
AuthContext = authContext;
|
||||
TenantUtil = tenantUtil;
|
||||
SecurityContext = securityContext;
|
||||
BaseCommonLinkUtility = baseCommonLinkUtility;
|
||||
ConsumerFactory = consumerFactory;
|
||||
_authContext = authContext;
|
||||
_tenantUtil = tenantUtil;
|
||||
_securityContext = securityContext;
|
||||
_baseCommonLinkUtility = baseCommonLinkUtility;
|
||||
_consumerFactory = consumerFactory;
|
||||
}
|
||||
|
||||
public virtual VoipPhone SaveOrUpdateNumber(VoipPhone phone)
|
||||
@ -136,7 +142,7 @@ namespace ASC.VoipService.Dao
|
||||
call.DialDate = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
voipCall.DialDate = TenantUtil.DateTimeToUtc(call.DialDate);
|
||||
voipCall.DialDate = _tenantUtil.DateTimeToUtc(call.DialDate);
|
||||
|
||||
if (call.DialDuration > 0)
|
||||
{
|
||||
@ -216,7 +222,7 @@ namespace ASC.VoipService.Dao
|
||||
|
||||
if (from.HasValue)
|
||||
{
|
||||
query = query.Where(r => r.DbVoipCall.DialDate >= TenantUtil.DateTimeFromUtc(from.Value));
|
||||
query = query.Where(r => r.DbVoipCall.DialDate >= _tenantUtil.DateTimeFromUtc(from.Value));
|
||||
}
|
||||
|
||||
if (count != 0)
|
||||
@ -293,7 +299,6 @@ namespace ASC.VoipService.Dao
|
||||
public CrmContact CrmContact { get; set; }
|
||||
}
|
||||
|
||||
#region Converters
|
||||
|
||||
private VoipPhone ToPhone(VoipNumber r)
|
||||
{
|
||||
@ -309,7 +314,7 @@ namespace ASC.VoipService.Dao
|
||||
From = dbVoipCall.DbVoipCall.NumberFrom,
|
||||
To = dbVoipCall.DbVoipCall.NumberTo,
|
||||
AnsweredBy = dbVoipCall.DbVoipCall.AnsweredBy,
|
||||
DialDate = TenantUtil.DateTimeFromUtc(dbVoipCall.DbVoipCall.DialDate),
|
||||
DialDate = _tenantUtil.DateTimeFromUtc(dbVoipCall.DbVoipCall.DialDate),
|
||||
DialDuration = dbVoipCall.DbVoipCall.DialDuration,
|
||||
Price = dbVoipCall.DbVoipCall.Price,
|
||||
Status = (VoipCallStatus)dbVoipCall.DbVoipCall.Status,
|
||||
@ -336,12 +341,12 @@ namespace ASC.VoipService.Dao
|
||||
|
||||
public Consumer Consumer
|
||||
{
|
||||
get { return ConsumerFactory.GetByKey("twilio"); }
|
||||
get { return _consumerFactory.GetByKey("twilio"); }
|
||||
}
|
||||
|
||||
public TwilioProvider GetProvider()
|
||||
{
|
||||
return new TwilioProvider(Consumer["twilioAccountSid"], Consumer["twilioAuthToken"], AuthContext, TenantUtil, SecurityContext, BaseCommonLinkUtility);
|
||||
return new TwilioProvider(Consumer["twilioAccountSid"], Consumer["twilioAuthToken"], _authContext, _tenantUtil, _securityContext, _baseCommonLinkUtility);
|
||||
}
|
||||
|
||||
public bool ConfigSettingsExist
|
||||
@ -352,13 +357,4 @@ namespace ASC.VoipService.Dao
|
||||
!string.IsNullOrEmpty(Consumer["twilioAuthToken"]);
|
||||
}
|
||||
}
|
||||
|
||||
private AuthContext AuthContext { get; }
|
||||
private TenantUtil TenantUtil { get; }
|
||||
private SecurityContext SecurityContext { get; }
|
||||
private BaseCommonLinkUtility BaseCommonLinkUtility { get; }
|
||||
private ConsumerFactory ConsumerFactory { get; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -23,8 +23,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace ASC.VoipService
|
||||
{
|
||||
namespace ASC.VoipService;
|
||||
|
||||
public interface IVoipProvider
|
||||
{
|
||||
IEnumerable<VoipPhone> GetExistingPhoneNumbers();
|
||||
@ -51,4 +51,3 @@ namespace ASC.VoipService
|
||||
|
||||
void DisablePhone(VoipPhone phone);
|
||||
}
|
||||
}
|
||||
|
@ -27,8 +27,7 @@ namespace ASC.VoipService.Twilio
|
||||
{
|
||||
public class TwilioPhone : VoipPhone
|
||||
{
|
||||
private readonly TwilioRestClient twilio;
|
||||
|
||||
private readonly TwilioRestClient _twilio;
|
||||
public TwilioPhone(
|
||||
TwilioRestClient twilio,
|
||||
AuthContext authContext,
|
||||
@ -37,7 +36,7 @@ namespace ASC.VoipService.Twilio
|
||||
BaseCommonLinkUtility baseCommonLinkUtility) :
|
||||
base(authContext, tenantUtil, securityContext, baseCommonLinkUtility)
|
||||
{
|
||||
this.twilio = twilio;
|
||||
_twilio = twilio;
|
||||
Settings = new TwilioVoipSettings(authContext, tenantUtil, securityContext, baseCommonLinkUtility);
|
||||
}
|
||||
|
||||
@ -52,7 +51,7 @@ namespace ASC.VoipService.Twilio
|
||||
SendDigits = number.Length > 1 ? number[1] + "#" : string.Empty,
|
||||
Record = Settings.Caller.Record,
|
||||
Url = new System.Uri(Settings.Connect(contactId: contactId))
|
||||
}, twilio);
|
||||
}, _twilio);
|
||||
|
||||
return new VoipCall { Id = call.Sid, From = call.From, To = call.To };
|
||||
}
|
||||
@ -64,7 +63,7 @@ namespace ASC.VoipService.Twilio
|
||||
|
||||
public override VoipCall RedirectCall(string callId, string to)
|
||||
{
|
||||
var call = CallResource.Update(callId, url: new System.Uri(Settings.Redirect(to)), method: HttpMethod.Post, client: twilio);
|
||||
var call = CallResource.Update(callId, url: new System.Uri(Settings.Redirect(to)), method: HttpMethod.Post, client: _twilio);
|
||||
return new VoipCall { Id = call.Sid, To = to };
|
||||
}
|
||||
|
||||
@ -79,24 +78,24 @@ namespace ASC.VoipService.Twilio
|
||||
|
||||
public Queue CreateQueue(string name, int size, string waitUrl, int waitTime)
|
||||
{
|
||||
var queues = QueueResource.Read(new ReadQueueOptions(), twilio);
|
||||
var queues = QueueResource.Read(new ReadQueueOptions(), _twilio);
|
||||
var queue = queues.FirstOrDefault(r => r.FriendlyName == name);
|
||||
if (queue == null)
|
||||
{
|
||||
queue = QueueResource.Create(name, client: twilio);
|
||||
queue = QueueResource.Create(name, client: _twilio);
|
||||
}
|
||||
return new Queue(queue.Sid, name, size, waitUrl, waitTime);
|
||||
}
|
||||
|
||||
public string GetQueue(string name)
|
||||
{
|
||||
var queues = QueueResource.Read(new ReadQueueOptions(), twilio);
|
||||
var queues = QueueResource.Read(new ReadQueueOptions(), _twilio);
|
||||
return queues.First(r => r.FriendlyName == name).Sid;
|
||||
}
|
||||
|
||||
public IEnumerable<string> QueueCalls(string id)
|
||||
{
|
||||
var calls = MemberResource.Read(id, client: twilio);
|
||||
var calls = MemberResource.Read(id, client: _twilio);
|
||||
return calls.Select(r => r.CallSid);
|
||||
}
|
||||
|
||||
@ -106,7 +105,7 @@ namespace ASC.VoipService.Twilio
|
||||
if (calls.Contains(callId))
|
||||
{
|
||||
MemberResource.Update(queueId, callId, new System.Uri(Settings.Dequeue(reject)), HttpMethod.Post,
|
||||
client: twilio);
|
||||
client: _twilio);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,28 +31,27 @@ namespace ASC.VoipService.Twilio
|
||||
{
|
||||
public class TwilioProvider : IVoipProvider
|
||||
{
|
||||
private readonly string accountSid;
|
||||
private readonly string authToken;
|
||||
private readonly TwilioRestClient client;
|
||||
|
||||
private AuthContext AuthContext { get; }
|
||||
private TenantUtil TenantUtil { get; }
|
||||
private SecurityContext SecurityContext { get; }
|
||||
private BaseCommonLinkUtility BaseCommonLinkUtility { get; }
|
||||
private readonly string _accountSid;
|
||||
private readonly string _authToken;
|
||||
private readonly TwilioRestClient _client;
|
||||
private readonly AuthContext _authContext;
|
||||
private readonly TenantUtil _tenantUtil;
|
||||
private readonly SecurityContext _securityContext;
|
||||
private readonly BaseCommonLinkUtility _baseCommonLinkUtility;
|
||||
|
||||
public TwilioProvider(string accountSid, string authToken, AuthContext authContext, TenantUtil tenantUtil, SecurityContext securityContext, BaseCommonLinkUtility baseCommonLinkUtility)
|
||||
{
|
||||
if (string.IsNullOrEmpty(accountSid)) throw new ArgumentNullException(nameof(accountSid));
|
||||
if (string.IsNullOrEmpty(authToken)) throw new ArgumentNullException(nameof(authToken));
|
||||
|
||||
this.authToken = authToken;
|
||||
AuthContext = authContext;
|
||||
TenantUtil = tenantUtil;
|
||||
SecurityContext = securityContext;
|
||||
BaseCommonLinkUtility = baseCommonLinkUtility;
|
||||
this.accountSid = accountSid;
|
||||
_authToken = authToken;
|
||||
_authContext = authContext;
|
||||
_tenantUtil = tenantUtil;
|
||||
_securityContext = securityContext;
|
||||
_baseCommonLinkUtility = baseCommonLinkUtility;
|
||||
_accountSid = accountSid;
|
||||
|
||||
client = new TwilioRestClient(accountSid, authToken);
|
||||
_client = new TwilioRestClient(accountSid, authToken);
|
||||
}
|
||||
|
||||
#region Call
|
||||
@ -66,7 +65,7 @@ namespace ASC.VoipService.Twilio
|
||||
{
|
||||
try
|
||||
{
|
||||
var record = RecordingResource.Fetch(callId, recordSid, client: client);
|
||||
var record = RecordingResource.Fetch(callId, recordSid, client: _client);
|
||||
|
||||
if (!record.Price.HasValue)
|
||||
{
|
||||
@ -108,31 +107,31 @@ namespace ASC.VoipService.Twilio
|
||||
var newNumber = IncomingPhoneNumberResource.Create(
|
||||
new CreateIncomingPhoneNumberOptions
|
||||
{
|
||||
PathAccountSid = accountSid,
|
||||
PathAccountSid = _accountSid,
|
||||
PhoneNumber = new PhoneNumber(phoneNumber)
|
||||
}, client);
|
||||
}, _client);
|
||||
|
||||
return new TwilioPhone(client, AuthContext, TenantUtil, SecurityContext, BaseCommonLinkUtility) { Id = newNumber.Sid, Number = phoneNumber.Substring(1) };
|
||||
return new TwilioPhone(_client, _authContext, _tenantUtil, _securityContext, _baseCommonLinkUtility) { Id = newNumber.Sid, Number = phoneNumber.Substring(1) };
|
||||
}
|
||||
|
||||
public VoipPhone DeleteNumber(VoipPhone phone)
|
||||
{
|
||||
IncomingPhoneNumberResource.Delete(phone.Id, client: client);
|
||||
IncomingPhoneNumberResource.Delete(phone.Id, client: _client);
|
||||
return phone;
|
||||
}
|
||||
|
||||
public IEnumerable<VoipPhone> GetExistingPhoneNumbers()
|
||||
{
|
||||
var result = IncomingPhoneNumberResource.Read(client: client);
|
||||
return result.Select(r => new TwilioPhone(client, AuthContext, TenantUtil, SecurityContext, BaseCommonLinkUtility) { Id = r.Sid, Number = r.PhoneNumber.ToString() });
|
||||
var result = IncomingPhoneNumberResource.Read(client: _client);
|
||||
return result.Select(r => new TwilioPhone(_client, _authContext, _tenantUtil, _securityContext, _baseCommonLinkUtility) { Id = r.Sid, Number = r.PhoneNumber.ToString() });
|
||||
}
|
||||
|
||||
public IEnumerable<VoipPhone> GetAvailablePhoneNumbers(PhoneNumberType phoneNumberType, string isoCountryCode)
|
||||
{
|
||||
return phoneNumberType switch
|
||||
{
|
||||
PhoneNumberType.Local => LocalResource.Read(isoCountryCode, voiceEnabled: true, client: client).Select(r => new TwilioPhone(client, AuthContext, TenantUtil, SecurityContext, BaseCommonLinkUtility) { Number = r.PhoneNumber.ToString() }),
|
||||
PhoneNumberType.TollFree => TollFreeResource.Read(isoCountryCode, voiceEnabled: true, client: client).Select(r => new TwilioPhone(client, AuthContext, TenantUtil, SecurityContext, BaseCommonLinkUtility) { Number = r.PhoneNumber.ToString() }),
|
||||
PhoneNumberType.Local => LocalResource.Read(isoCountryCode, voiceEnabled: true, client: _client).Select(r => new TwilioPhone(_client, _authContext, _tenantUtil, _securityContext, _baseCommonLinkUtility) { Number = r.PhoneNumber.ToString() }),
|
||||
PhoneNumberType.TollFree => TollFreeResource.Read(isoCountryCode, voiceEnabled: true, client: _client).Select(r => new TwilioPhone(_client, _authContext, _tenantUtil, _securityContext, _baseCommonLinkUtility) { Number = r.PhoneNumber.ToString() }),
|
||||
|
||||
_ => new List<VoipPhone>(),
|
||||
};
|
||||
@ -140,13 +139,13 @@ namespace ASC.VoipService.Twilio
|
||||
|
||||
public VoipPhone GetPhone(string phoneSid)
|
||||
{
|
||||
var phone = IncomingPhoneNumberResource.Fetch(phoneSid, client: client);
|
||||
var phone = IncomingPhoneNumberResource.Fetch(phoneSid, client: _client);
|
||||
|
||||
var result = new TwilioPhone(client, AuthContext, TenantUtil, SecurityContext, BaseCommonLinkUtility)
|
||||
var result = new TwilioPhone(_client, _authContext, _tenantUtil, _securityContext, _baseCommonLinkUtility)
|
||||
{
|
||||
Id = phone.Sid,
|
||||
Number = phone.PhoneNumber.ToString(),
|
||||
Settings = new TwilioVoipSettings(AuthContext, TenantUtil, SecurityContext, BaseCommonLinkUtility)
|
||||
Settings = new TwilioVoipSettings(_authContext, _tenantUtil, _securityContext, _baseCommonLinkUtility)
|
||||
};
|
||||
|
||||
if (phone.VoiceUrl == null)
|
||||
@ -159,12 +158,12 @@ namespace ASC.VoipService.Twilio
|
||||
|
||||
public VoipPhone GetPhone(VoipNumber data)
|
||||
{
|
||||
return new TwilioPhone(client, AuthContext, TenantUtil, SecurityContext, BaseCommonLinkUtility)
|
||||
return new TwilioPhone(_client, _authContext, _tenantUtil, _securityContext, _baseCommonLinkUtility)
|
||||
{
|
||||
Id = data.Id,
|
||||
Number = data.Number,
|
||||
Alias = data.Alias,
|
||||
Settings = new TwilioVoipSettings(data.Settings, AuthContext)
|
||||
Settings = new TwilioVoipSettings(data.Settings, _authContext)
|
||||
};
|
||||
}
|
||||
|
||||
@ -177,7 +176,7 @@ namespace ASC.VoipService.Twilio
|
||||
{
|
||||
try
|
||||
{
|
||||
var call = CallResource.Fetch(result.Id, client: client);
|
||||
var call = CallResource.Fetch(result.Id, client: _client);
|
||||
if (!call.Price.HasValue || string.IsNullOrEmpty(call.Duration))
|
||||
{
|
||||
count--;
|
||||
@ -205,19 +204,19 @@ namespace ASC.VoipService.Twilio
|
||||
{
|
||||
new IncomingClientScope(agent.ClientID)
|
||||
};
|
||||
var capability = new ClientCapability(accountSid, authToken, scopes: scopes);
|
||||
var capability = new ClientCapability(_accountSid, _authToken, scopes: scopes);
|
||||
|
||||
return capability.ToJwt();
|
||||
}
|
||||
|
||||
public void UpdateSettings(VoipPhone phone)
|
||||
{
|
||||
IncomingPhoneNumberResource.Update(phone.Id, voiceUrl: new Uri(phone.Settings.Connect(false)), client: client);
|
||||
IncomingPhoneNumberResource.Update(phone.Id, voiceUrl: new Uri(phone.Settings.Connect(false)), client: _client);
|
||||
}
|
||||
|
||||
public void DisablePhone(VoipPhone phone)
|
||||
{
|
||||
IncomingPhoneNumberResource.Update(phone.Id, voiceUrl: new Uri("https://demo.twilio.com/welcome/voice/"), client: client);
|
||||
IncomingPhoneNumberResource.Update(phone.Id, voiceUrl: new Uri("https://demo.twilio.com/welcome/voice/"), client: _client);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
@ -23,16 +23,15 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace ASC.VoipService.Twilio
|
||||
{
|
||||
namespace ASC.VoipService.Twilio;
|
||||
|
||||
public class TwilioResponseHelper
|
||||
{
|
||||
private readonly VoipSettings settings;
|
||||
private readonly string baseUrl;
|
||||
|
||||
private AuthContext AuthContext { get; }
|
||||
private TenantUtil TenantUtil { get; }
|
||||
private SecurityContext SecurityContext { get; }
|
||||
private readonly VoipSettings _settings;
|
||||
private readonly string _baseUrl;
|
||||
private readonly AuthContext _authContext;
|
||||
private readonly TenantUtil _tenantUtil;
|
||||
private readonly SecurityContext _securityContext;
|
||||
|
||||
public TwilioResponseHelper(
|
||||
VoipSettings settings,
|
||||
@ -41,11 +40,11 @@ namespace ASC.VoipService.Twilio
|
||||
TenantUtil tenantUtil,
|
||||
SecurityContext securityContext)
|
||||
{
|
||||
this.settings = settings;
|
||||
AuthContext = authContext;
|
||||
TenantUtil = tenantUtil;
|
||||
SecurityContext = securityContext;
|
||||
this.baseUrl = baseUrl.TrimEnd('/') + "/twilio/";
|
||||
_settings = settings;
|
||||
_authContext = authContext;
|
||||
_tenantUtil = tenantUtil;
|
||||
_securityContext = securityContext;
|
||||
_baseUrl = baseUrl.TrimEnd('/') + "/twilio/";
|
||||
}
|
||||
|
||||
public VoiceResponse Inbound(Tuple<Agent, bool> agentTuple)
|
||||
@ -54,10 +53,10 @@ namespace ASC.VoipService.Twilio
|
||||
var anyOnline = agentTuple != null && agentTuple.Item2;
|
||||
var response = new VoiceResponse();
|
||||
|
||||
if (settings.WorkingHours != null && settings.WorkingHours.Enabled)
|
||||
if (_settings.WorkingHours != null && _settings.WorkingHours.Enabled)
|
||||
{
|
||||
var now = TenantUtil.DateTimeFromUtc(DateTime.UtcNow);
|
||||
if (!(settings.WorkingHours.From <= now.TimeOfDay && settings.WorkingHours.To >= now.TimeOfDay))
|
||||
var now = _tenantUtil.DateTimeFromUtc(DateTime.UtcNow);
|
||||
if (!(_settings.WorkingHours.From <= now.TimeOfDay && _settings.WorkingHours.To >= now.TimeOfDay))
|
||||
{
|
||||
return AddVoiceMail(response);
|
||||
}
|
||||
@ -65,12 +64,12 @@ namespace ASC.VoipService.Twilio
|
||||
|
||||
if (anyOnline)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(settings.GreetingAudio))
|
||||
if (!string.IsNullOrEmpty(_settings.GreetingAudio))
|
||||
{
|
||||
response.Play(Uri.EscapeDataString(settings.GreetingAudio));
|
||||
response.Play(Uri.EscapeDataString(_settings.GreetingAudio));
|
||||
}
|
||||
|
||||
response.Enqueue(settings.Queue.Name, GetEcho("Enqueue", agent != null), "POST",
|
||||
response.Enqueue(_settings.Queue.Name, GetEcho("Enqueue", agent != null), "POST",
|
||||
GetEcho("Wait", agent != null), "POST");
|
||||
}
|
||||
|
||||
@ -79,9 +78,9 @@ namespace ASC.VoipService.Twilio
|
||||
|
||||
public VoiceResponse Outbound()
|
||||
{
|
||||
return !settings.Caller.AllowOutgoingCalls
|
||||
return !_settings.Caller.AllowOutgoingCalls
|
||||
? new VoiceResponse()
|
||||
: AddToResponse(new VoiceResponse(), settings.Caller);
|
||||
: AddToResponse(new VoiceResponse(), _settings.Caller);
|
||||
}
|
||||
|
||||
public VoiceResponse Dial()
|
||||
@ -101,7 +100,7 @@ namespace ASC.VoipService.Twilio
|
||||
|
||||
public VoiceResponse Dequeue()
|
||||
{
|
||||
return AddToResponse(new VoiceResponse(), settings.Caller);
|
||||
return AddToResponse(new VoiceResponse(), _settings.Caller);
|
||||
}
|
||||
|
||||
public VoiceResponse Leave()
|
||||
@ -112,7 +111,7 @@ namespace ASC.VoipService.Twilio
|
||||
public VoiceResponse Wait(string queueTime, string queueSize)
|
||||
{
|
||||
var response = new VoiceResponse();
|
||||
var queue = settings.Queue;
|
||||
var queue = _settings.Queue;
|
||||
|
||||
if (Convert.ToInt32(queueTime) > queue.WaitTime || Convert.ToInt32(queueSize) > queue.Size) return response.Leave();
|
||||
|
||||
@ -136,8 +135,8 @@ namespace ASC.VoipService.Twilio
|
||||
|
||||
if (digits == "#") return AddVoiceMail(response);
|
||||
|
||||
var oper = settings.Operators.Find(r => r.PostFix == digits && availableOperators.Contains(r)) ??
|
||||
settings.Operators.FirstOrDefault(r => availableOperators.Contains(r));
|
||||
var oper = _settings.Operators.Find(r => r.PostFix == digits && availableOperators.Contains(r)) ??
|
||||
_settings.Operators.FirstOrDefault(r => availableOperators.Contains(r));
|
||||
|
||||
return oper != null ? AddToResponse(response, oper) : response;
|
||||
}
|
||||
@ -146,16 +145,16 @@ namespace ASC.VoipService.Twilio
|
||||
{
|
||||
if (to == "hold")
|
||||
{
|
||||
return new VoiceResponse().Play(Uri.EscapeDataString(settings.HoldAudio), 0);
|
||||
return new VoiceResponse().Play(Uri.EscapeDataString(_settings.HoldAudio), 0);
|
||||
}
|
||||
|
||||
|
||||
if (Guid.TryParse(to, out var newCallerId))
|
||||
{
|
||||
SecurityContext.AuthenticateMeWithoutCookie(newCallerId);
|
||||
_securityContext.AuthenticateMeWithoutCookie(newCallerId);
|
||||
}
|
||||
|
||||
return new VoiceResponse().Enqueue(settings.Queue.Name, GetEcho("enqueue"), "POST",
|
||||
return new VoiceResponse().Enqueue(_settings.Queue.Name, GetEcho("enqueue"), "POST",
|
||||
GetEcho("wait") + "&RedirectTo=" + to, "POST");
|
||||
}
|
||||
|
||||
@ -187,14 +186,14 @@ namespace ASC.VoipService.Twilio
|
||||
|
||||
private VoiceResponse AddVoiceMail(VoiceResponse response)
|
||||
{
|
||||
return string.IsNullOrEmpty(settings.VoiceMail)
|
||||
return string.IsNullOrEmpty(_settings.VoiceMail)
|
||||
? response.Say("")
|
||||
: response.Play(Uri.EscapeDataString(settings.VoiceMail)).Record(method: "POST", action: GetEcho("voiceMail"), maxLength: 30);
|
||||
: response.Play(Uri.EscapeDataString(_settings.VoiceMail)).Record(method: "POST", action: GetEcho("voiceMail"), maxLength: 30);
|
||||
}
|
||||
|
||||
public string GetEcho(string action, bool user = true)
|
||||
{
|
||||
var result = baseUrl.TrimEnd('/');
|
||||
var result = _baseUrl.TrimEnd('/');
|
||||
|
||||
if (!string.IsNullOrEmpty(action))
|
||||
{
|
||||
@ -202,10 +201,9 @@ namespace ASC.VoipService.Twilio
|
||||
}
|
||||
if (user)
|
||||
{
|
||||
result += "?CallerId=" + AuthContext.CurrentAccount.ID;
|
||||
result += "?CallerId=" + _authContext.CurrentAccount.ID;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,8 +26,8 @@
|
||||
|
||||
using Uri = System.Uri;
|
||||
|
||||
namespace ASC.VoipService.Twilio
|
||||
{
|
||||
namespace ASC.VoipService.Twilio;
|
||||
|
||||
public class TwilioVoipSettings : VoipSettings
|
||||
{
|
||||
public TwilioVoipSettings(
|
||||
@ -80,4 +80,3 @@ namespace ASC.VoipService.Twilio
|
||||
return new TwilioResponseHelper(this, BaseCommonLinkUtility.GetFullAbsolutePath(""), AuthContext, TenantUtil, SecurityContext).GetEcho(method, user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,8 +23,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace ASC.VoipService
|
||||
{
|
||||
namespace ASC.VoipService;
|
||||
|
||||
public class VoipCall
|
||||
{
|
||||
public string Id { get; set; }
|
||||
@ -73,4 +73,3 @@ namespace ASC.VoipService
|
||||
Answered,
|
||||
Missed
|
||||
}
|
||||
}
|
@ -23,8 +23,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace ASC.VoipService
|
||||
{
|
||||
namespace ASC.VoipService;
|
||||
|
||||
public class Agent
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
@ -129,8 +129,6 @@ namespace ASC.VoipService
|
||||
public bool IsDefault { get; set; }
|
||||
}
|
||||
|
||||
#region Enum
|
||||
|
||||
public enum AnswerType
|
||||
{
|
||||
Number,
|
||||
@ -159,6 +157,3 @@ namespace ASC.VoipService
|
||||
VoiceMail,
|
||||
Queue
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
@ -23,8 +23,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace ASC.VoipService
|
||||
{
|
||||
namespace ASC.VoipService;
|
||||
|
||||
public class VoipPhone
|
||||
{
|
||||
public string Id { get; set; }
|
||||
@ -82,4 +82,3 @@ namespace ASC.VoipService
|
||||
|
||||
public decimal Price { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -23,8 +23,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace ASC.VoipService
|
||||
{
|
||||
namespace ASC.VoipService;
|
||||
|
||||
public class VoipSettings
|
||||
{
|
||||
public string VoiceUrl { get; set; }
|
||||
@ -184,4 +184,3 @@ namespace ASC.VoipService
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user