Initial commit carried over from private repo. This is V2.
This commit is contained in:
40
code/MessengerApi/Handlers/Endpoint/AckEndpointHandler.cs
Normal file
40
code/MessengerApi/Handlers/Endpoint/AckEndpointHandler.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using MessengerApi.Contracts.Models.Scoped;
|
||||
using MessengerApi.Models.Scoped;
|
||||
|
||||
namespace MessengerApi.Handlers.Endpoint
|
||||
{
|
||||
public class AckEndpointHandler
|
||||
{
|
||||
private readonly ILogger logger;
|
||||
private readonly IUnitOfWork unitOfWork;
|
||||
private readonly Identity identity;
|
||||
|
||||
public AckEndpointHandler(
|
||||
ILogger logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
Identity identity)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.unitOfWork = unitOfWork;
|
||||
this.identity = identity;
|
||||
}
|
||||
|
||||
public async Task AckMessage(Guid messageId)
|
||||
{
|
||||
var message = unitOfWork.Messages.GetById(messageId);
|
||||
|
||||
// Authorize.
|
||||
if (message.ToId != this.identity.User.Id)
|
||||
{
|
||||
throw new InvalidOperationException("It's not your message to ack.");
|
||||
}
|
||||
else if(!message.IsDelivered)
|
||||
{
|
||||
throw new InvalidOperationException("Can't ack undelivered message.");
|
||||
}
|
||||
|
||||
// Act.
|
||||
message.IsAcknowledged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
35
code/MessengerApi/Handlers/Endpoint/PeekEndpointHandler.cs
Normal file
35
code/MessengerApi/Handlers/Endpoint/PeekEndpointHandler.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using MessengerApi.Contracts.Models.Scoped;
|
||||
using MessengerApi.Models.Scoped;
|
||||
|
||||
namespace MessengerApi.Handlers.Endpoint
|
||||
{
|
||||
public class PeekEndpointHandler
|
||||
{
|
||||
private readonly ILogger logger;
|
||||
|
||||
private readonly Timing timing;
|
||||
private readonly Identity identity;
|
||||
private readonly IUnitOfWork unitOfWork;
|
||||
|
||||
public PeekEndpointHandler(
|
||||
ILogger logger,
|
||||
Timing timing,
|
||||
Identity identity,
|
||||
IUnitOfWork unitOfWork)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.timing = timing;
|
||||
this.identity = identity;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public Task<int> Peek()
|
||||
{
|
||||
var pendingMessages = this.unitOfWork.Messages.GetPendingMessages(this.identity.User);
|
||||
|
||||
this.logger.Debug($"[{this.timing.Timestamp:s}] User {this.identity.User.Name} is receiving {pendingMessages.Count()}.");
|
||||
|
||||
return Task.FromResult(pendingMessages.Count());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
using MessengerApi.Contracts.Models.Scoped;
|
||||
using MessengerApi.Db.Entities;
|
||||
using MessengerApi.Models.Scoped;
|
||||
|
||||
namespace MessengerApi.Handlers.Endpoint
|
||||
{
|
||||
public class ReceiveEndpointHandler
|
||||
{
|
||||
private readonly ILogger logger;
|
||||
|
||||
private readonly Timing timing;
|
||||
private readonly Identity identity;
|
||||
private readonly IUnitOfWork unitOfWork;
|
||||
|
||||
public ReceiveEndpointHandler(
|
||||
ILogger logger,
|
||||
Timing timing,
|
||||
Identity identity,
|
||||
IUnitOfWork unitOfWork)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.timing = timing;
|
||||
this.identity = identity;
|
||||
this.unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public Task<Message[]> ReceiveMessages()
|
||||
{
|
||||
var pendingMessages = this.unitOfWork.Messages.GetPendingMessages(this.identity.User);
|
||||
|
||||
this.logger.Debug($"[{this.timing.Timestamp:s}] User {this.identity.User.Name} is receiving {pendingMessages.Count()}.");
|
||||
|
||||
if (!pendingMessages.Any())
|
||||
{
|
||||
return Task.FromResult(new Message[0]);
|
||||
}
|
||||
|
||||
var messages = pendingMessages.ToList();
|
||||
messages.ForEach(x => x.IsDelivered = true);
|
||||
return Task.FromResult(messages.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
60
code/MessengerApi/Handlers/Endpoint/SendEndpointHandler.cs
Normal file
60
code/MessengerApi/Handlers/Endpoint/SendEndpointHandler.cs
Normal file
@ -0,0 +1,60 @@
|
||||
using MessengerApi.Configuration.Model;
|
||||
using MessengerApi.Contracts.Models.Scoped;
|
||||
using MessengerApi.Db.Entities;
|
||||
using MessengerApi.Models.Scoped;
|
||||
|
||||
namespace MessengerApi.Handlers.Endpoint
|
||||
{
|
||||
public class SendEndpointHandler
|
||||
{
|
||||
private readonly MessengerConfiguration configuration;
|
||||
private readonly ILogger logger;
|
||||
|
||||
private readonly Timing timing;
|
||||
private readonly Identity identity;
|
||||
private readonly IUnitOfWork unitOfWork;
|
||||
|
||||
public SendEndpointHandler(
|
||||
MessengerConfiguration configuration,
|
||||
ILogger logger,
|
||||
IUnitOfWork unitOfWork,
|
||||
Timing timing,
|
||||
Identity identity)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.logger = logger;
|
||||
this.unitOfWork = unitOfWork;
|
||||
this.timing = timing;
|
||||
this.identity = identity;
|
||||
}
|
||||
|
||||
public Task<Message> SendMessage(
|
||||
Guid? toUserId,
|
||||
string payload,
|
||||
string payloadType,
|
||||
int? payloadLifespanInSeconds)
|
||||
{
|
||||
// Authorize.
|
||||
var targetRecipientId = toUserId.HasValue
|
||||
? this.identity.UserRoutes.Single(x => x.From.Id == this.identity.User.Id && x.To.Id == toUserId.Value).To.Id
|
||||
: this.identity.UserRoutes.Single().To.Id;
|
||||
|
||||
this.logger.Debug($"[{this.timing.Timestamp:s}] User {this.identity.User.Name} is authorized to send message to {targetRecipientId}.");
|
||||
|
||||
// Act.
|
||||
var message = new Message
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
CreatedUtc = this.timing.Timestamp,
|
||||
FromId = this.identity.User.Id,
|
||||
ToId = targetRecipientId,
|
||||
Payload = payload,
|
||||
PayloadType = payloadType,
|
||||
PayloadLifespanInSeconds = payloadLifespanInSeconds ?? (this.configuration.DefaultMessageLifetimeInMinutes * 60)
|
||||
};
|
||||
|
||||
this.unitOfWork.Messages.Add(message);
|
||||
return Task.FromResult(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user