Compare commits

..

No commits in common. "78bab88165af20f6c7aa17321913a80c3d322b62" and "65522567a5d86af905c0fa687f079f1b62327026" have entirely different histories.

7 changed files with 83 additions and 19 deletions

9
Requests/IRequest.cs Normal file
View File

@ -0,0 +1,9 @@
namespace HermesSocketLibrary.Requests
{
public interface IRequest
{
string Name { get; }
Task<RequestResult> Grant(string sender, IDictionary<string, object>? data);
}
}

View File

@ -0,0 +1,9 @@
using HermesSocketLibrary.Socket.Data;
namespace HermesSocketLibrary.Requests
{
public interface IRequestManager
{
Task<RequestResult> Grant(string sender, RequestMessage? message);
}
}

View File

@ -0,0 +1,41 @@
using HermesSocketLibrary.Socket.Data;
using Serilog;
namespace HermesSocketLibrary.Requests
{
public class RequestManager : IRequestManager
{
private readonly IDictionary<string, IRequest> _requests;
private readonly ILogger _logger;
public RequestManager(IEnumerable<IRequest> requests, ILogger logger)
{
_logger = logger;
_requests = requests.ToDictionary(r => r.Name, r => r);
}
public async Task<RequestResult> Grant(string sender, RequestMessage? message)
{
if (message == null || message.Type == null)
return new RequestResult(false, null);
if (!_requests.TryGetValue(message.Type, out IRequest? request) || request == null)
{
_logger.Warning($"Did not find request type '{message.Type}'.");
return new RequestResult(false, null);
}
try
{
return await request.Grant(sender, message.Data);
}
catch (Exception e)
{
_logger.Error(e, $"Failed to grant a request of type '{message.Type}'.");
}
return new RequestResult(false, null);
}
}
}

16
Requests/RequestResult.cs Normal file
View File

@ -0,0 +1,16 @@
namespace HermesSocketLibrary.Requests
{
public class RequestResult
{
public bool Success;
public object? Result;
public bool NotifyClientsOnAccount;
public RequestResult(bool success, object? result, bool notifyClientsOnAccount = true)
{
Success = success;
Result = result;
NotifyClientsOnAccount = notifyClientsOnAccount;
}
}
}

View File

@ -1,3 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace HermesSocketLibrary.Socket.Data namespace HermesSocketLibrary.Socket.Data
{ {
public class Connection public class Connection
@ -8,7 +13,7 @@ namespace HermesSocketLibrary.Socket.Data
public string AccessToken { get; set; } public string AccessToken { get; set; }
public string GrantType { get; set; } public string GrantType { get; set; }
public string Scope { get; set; } public string Scope { get; set; }
public DateTime ExpiresAt { get; set; } public DateTime ExpiresIn { get; set; }
public bool Default { get; set; } public bool Default { get; set; }
} }
} }

View File

@ -1,5 +1,3 @@
using HermesSocketLibrary.Requests.Messages;
namespace HermesSocketLibrary.Socket.Data namespace HermesSocketLibrary.Socket.Data
{ {
public class LoginAckMessage public class LoginAckMessage
@ -11,11 +9,9 @@ namespace HermesSocketLibrary.Socket.Data
public bool WebLogin { get; set; } public bool WebLogin { get; set; }
public string DefaultTTSVoice { get; set; } public string DefaultTTSVoice { get; set; }
public IList<string> EnabledTTSVoices { get; set; } public IEnumerable<string> EnabledTTSVoices { get; set; }
public IDictionary<string, string> TTSVoicesAvailable { get; set; } public IDictionary<string, string> TTSVoicesAvailable { get; set; }
public IList<TTSWordFilter> WordFilters { get; set; } public Connection[] Connections { get; set; }
public IList<Connection> Connections { get; set; }
} }
} }

View File

@ -1,12 +0,0 @@
namespace HermesSocketServer.Models
{
public class PolicyMessage
{
public Guid Id { get; set; }
public string UserId { get; set; }
public Guid GroupId { get; set; }
public string Path { get; set; }
public int Usage { get; set; }
public int Span { get; set; }
}
}