2021-03-22 12:09:25 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Models.Client;
|
|
|
|
|
using Data.Models.Server;
|
2020-05-22 21:29:41 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Dtos.Meta.Responses;
|
2020-05-22 21:29:41 -04:00
|
|
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
using Stats.Dtos;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2020-05-22 21:29:41 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
namespace Stats.Helpers
|
2020-05-22 21:29:41 -04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// implementation of IResourceQueryHelper
|
|
|
|
|
/// </summary>
|
2020-08-20 11:38:11 -04:00
|
|
|
|
public class ChatResourceQueryHelper : IResourceQueryHelper<ChatSearchQuery, MessageResponse>
|
2020-05-22 21:29:41 -04:00
|
|
|
|
{
|
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
|
|
|
|
private readonly ILogger _logger;
|
2021-07-09 17:50:33 -04:00
|
|
|
|
private readonly DefaultSettings _defaultSettings;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
private List<EFServer> serverCache;
|
2020-05-22 21:29:41 -04:00
|
|
|
|
|
2021-07-09 17:50:33 -04:00
|
|
|
|
public ChatResourceQueryHelper(ILogger<ChatResourceQueryHelper> logger, IDatabaseContextFactory contextFactory, DefaultSettings defaultSettings)
|
2020-05-22 21:29:41 -04:00
|
|
|
|
{
|
|
|
|
|
_contextFactory = contextFactory;
|
|
|
|
|
_logger = logger;
|
2021-07-09 17:50:33 -04:00
|
|
|
|
_defaultSettings = defaultSettings;
|
2020-05-22 21:29:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2020-08-20 11:38:11 -04:00
|
|
|
|
public async Task<ResourceQueryHelperResult<MessageResponse>> QueryResource(ChatSearchQuery query)
|
2020-05-22 21:29:41 -04:00
|
|
|
|
{
|
|
|
|
|
if (query == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Query must be specified");
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 11:38:11 -04:00
|
|
|
|
var result = new ResourceQueryHelperResult<MessageResponse>();
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(enableTracking: false);
|
2020-08-20 11:38:11 -04:00
|
|
|
|
|
|
|
|
|
if (serverCache == null)
|
|
|
|
|
{
|
|
|
|
|
serverCache = await context.Set<EFServer>().ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(query.ServerId, out int serverId))
|
|
|
|
|
{
|
|
|
|
|
query.ServerId = serverCache.FirstOrDefault(_server => _server.ServerId == serverId)?.EndPoint ?? query.ServerId;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 21:29:41 -04:00
|
|
|
|
var iqMessages = context.Set<EFClientMessage>()
|
|
|
|
|
.Where(_message => _message.TimeSent >= query.SentAfter)
|
2020-08-20 11:38:11 -04:00
|
|
|
|
.Where(_message => _message.TimeSent < query.SentBefore);
|
2020-05-22 21:29:41 -04:00
|
|
|
|
|
|
|
|
|
if (query.ClientId != null)
|
|
|
|
|
{
|
|
|
|
|
iqMessages = iqMessages.Where(_message => _message.ClientId == query.ClientId.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.ServerId != null)
|
|
|
|
|
{
|
|
|
|
|
iqMessages = iqMessages.Where(_message => _message.Server.EndPoint == query.ServerId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(query.MessageContains))
|
|
|
|
|
{
|
2020-08-20 11:38:11 -04:00
|
|
|
|
iqMessages = iqMessages.Where(_message => EF.Functions.Like(_message.Message.ToLower(), $"%{query.MessageContains.ToLower()}%"));
|
2020-05-22 21:29:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var iqResponse = iqMessages
|
2020-08-20 11:38:11 -04:00
|
|
|
|
.Select(_message => new MessageResponse
|
2020-05-22 21:29:41 -04:00
|
|
|
|
{
|
|
|
|
|
ClientId = _message.ClientId,
|
2020-08-20 11:38:11 -04:00
|
|
|
|
ClientName = query.IsProfileMeta ? "" : _message.Client.CurrentAlias.Name,
|
|
|
|
|
ServerId = _message.ServerId,
|
|
|
|
|
When = _message.TimeSent,
|
2020-05-22 21:29:41 -04:00
|
|
|
|
Message = _message.Message,
|
2020-08-20 11:38:11 -04:00
|
|
|
|
ServerName = query.IsProfileMeta ? "" : _message.Server.HostName,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
GameName = _message.Server.GameName == null ? Server.Game.IW4 : (Server.Game)_message.Server.GameName.Value,
|
2020-11-18 10:08:24 -05:00
|
|
|
|
SentIngame = _message.SentIngame
|
2020-05-22 21:29:41 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (query.Direction == SharedLibraryCore.Dtos.SortDirection.Descending)
|
|
|
|
|
{
|
2020-08-20 11:38:11 -04:00
|
|
|
|
iqResponse = iqResponse.OrderByDescending(_message => _message.When);
|
2020-05-22 21:29:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-08-20 11:38:11 -04:00
|
|
|
|
iqResponse = iqResponse.OrderBy(_message => _message.When);
|
2020-05-22 21:29:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var resultList = await iqResponse
|
|
|
|
|
.Skip(query.Offset)
|
|
|
|
|
.Take(query.Count)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
2020-08-20 11:38:11 -04:00
|
|
|
|
foreach (var message in resultList)
|
|
|
|
|
{
|
|
|
|
|
message.IsHidden = serverCache.Any(server => server.ServerId == message.ServerId && server.IsPasswordProtected);
|
|
|
|
|
|
2021-07-09 17:50:33 -04:00
|
|
|
|
if (!message.Message.IsQuickMessage())
|
2020-08-20 11:38:11 -04:00
|
|
|
|
{
|
2021-07-09 17:50:33 -04:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var quickMessages = _defaultSettings
|
|
|
|
|
.QuickMessages
|
|
|
|
|
.First(_qm => _qm.Game == message.GameName);
|
|
|
|
|
message.Message = quickMessages.Messages[message.Message.Substring(1)];
|
|
|
|
|
message.IsQuickMessage = true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
message.Message = message.Message.Substring(1);
|
2020-08-20 11:38:11 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 21:29:41 -04:00
|
|
|
|
result.TotalResultCount = await iqResponse.CountAsync();
|
|
|
|
|
result.Results = resultList;
|
|
|
|
|
result.RetrievedResultCount = resultList.Count;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|