2021-08-21 11:40:03 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Models;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
using SharedLibraryCore;
|
2021-08-21 11:40:03 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
using SharedLibraryCore.Dtos.Meta.Responses;
|
|
|
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.QueryHelper;
|
2022-03-25 12:28:15 -04:00
|
|
|
|
using SharedLibraryCore.Services;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Meta
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// implementation of IResourceQueryHelper
|
|
|
|
|
/// used to pull in penalties applied to a given client id
|
|
|
|
|
/// </summary>
|
2022-03-25 12:28:15 -04:00
|
|
|
|
public class
|
|
|
|
|
ReceivedPenaltyResourceQueryHelper : IResourceQueryHelper<ClientPaginationRequest, ReceivedPenaltyResponse>
|
2020-08-17 22:21:11 -04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
2021-08-21 11:40:03 -04:00
|
|
|
|
private readonly ApplicationConfiguration _appConfig;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
2022-03-25 12:28:15 -04:00
|
|
|
|
public ReceivedPenaltyResourceQueryHelper(ILogger<ReceivedPenaltyResourceQueryHelper> logger,
|
2021-08-21 11:40:03 -04:00
|
|
|
|
IDatabaseContextFactory contextFactory, ApplicationConfiguration appConfig)
|
2020-08-17 22:21:11 -04:00
|
|
|
|
{
|
|
|
|
|
_contextFactory = contextFactory;
|
|
|
|
|
_logger = logger;
|
2021-08-21 11:40:03 -04:00
|
|
|
|
_appConfig = appConfig;
|
2020-08-17 22:21:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 12:28:15 -04:00
|
|
|
|
public async Task<ResourceQueryHelperResult<ReceivedPenaltyResponse>> QueryResource(
|
|
|
|
|
ClientPaginationRequest query)
|
2020-08-17 22:21:11 -04:00
|
|
|
|
{
|
|
|
|
|
var linkedPenaltyType = Utilities.LinkedPenaltyTypes();
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var ctx = _contextFactory.CreateContext(enableTracking: false);
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
|
|
|
|
var linkId = await ctx.Clients.AsNoTracking()
|
2022-03-25 12:28:15 -04:00
|
|
|
|
.Where(_client => _client.ClientId == query.ClientId)
|
|
|
|
|
.Select(_client => new { _client.AliasLinkId, _client.CurrentAliasId })
|
|
|
|
|
.FirstOrDefaultAsync();
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
|
|
|
|
var iqPenalties = ctx.Penalties.AsNoTracking()
|
2021-08-21 11:40:03 -04:00
|
|
|
|
.Where(_penalty => _penalty.OffenderId == query.ClientId ||
|
2021-09-05 11:43:48 -04:00
|
|
|
|
linkedPenaltyType.Contains(_penalty.Type) && _penalty.LinkId == linkId.AliasLinkId);
|
2021-08-21 11:40:03 -04:00
|
|
|
|
|
2021-09-18 23:31:56 -04:00
|
|
|
|
IQueryable<EFPenalty> iqIpLinkedPenalties = null;
|
2022-03-25 12:28:15 -04:00
|
|
|
|
IQueryable<EFPenalty> identifierPenalties = null;
|
|
|
|
|
|
2021-08-21 11:40:03 -04:00
|
|
|
|
if (!_appConfig.EnableImplicitAccountLinking)
|
|
|
|
|
{
|
|
|
|
|
var usedIps = await ctx.Aliases.AsNoTracking()
|
2022-03-25 12:28:15 -04:00
|
|
|
|
.Where(alias =>
|
|
|
|
|
(alias.LinkId == linkId.AliasLinkId || alias.AliasId == linkId.CurrentAliasId) &&
|
|
|
|
|
alias.IPAddress != null)
|
2021-08-21 11:40:03 -04:00
|
|
|
|
.Select(alias => alias.IPAddress).ToListAsync();
|
2020-08-17 22:21:11 -04:00
|
|
|
|
|
2022-03-25 12:28:15 -04:00
|
|
|
|
identifierPenalties = ctx.PenaltyIdentifiers.AsNoTracking().Where(identifier =>
|
|
|
|
|
identifier.IPv4Address != null && usedIps.Contains(identifier.IPv4Address))
|
|
|
|
|
.Select(id => id.Penalty);
|
|
|
|
|
|
2021-08-21 11:40:03 -04:00
|
|
|
|
var aliasedIds = await ctx.Aliases.AsNoTracking().Where(alias => usedIps.Contains(alias.IPAddress))
|
|
|
|
|
.Select(alias => alias.LinkId)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
iqIpLinkedPenalties = ctx.Penalties.AsNoTracking()
|
|
|
|
|
.Where(penalty =>
|
2022-02-22 18:09:50 -05:00
|
|
|
|
linkedPenaltyType.Contains(penalty.Type) && aliasedIds.Contains(penalty.LinkId ?? -1));
|
2021-08-21 11:40:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 23:31:56 -04:00
|
|
|
|
var iqAllPenalties = iqPenalties;
|
2022-03-25 12:28:15 -04:00
|
|
|
|
|
2021-09-18 23:31:56 -04:00
|
|
|
|
if (iqIpLinkedPenalties != null)
|
|
|
|
|
{
|
|
|
|
|
iqAllPenalties = iqPenalties.Union(iqIpLinkedPenalties);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 12:28:15 -04:00
|
|
|
|
if (identifierPenalties != null)
|
|
|
|
|
{
|
|
|
|
|
iqAllPenalties = iqPenalties.Union(identifierPenalties);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var penalties = await iqAllPenalties
|
2021-08-21 11:40:03 -04:00
|
|
|
|
.Where(_penalty => _penalty.When < query.Before)
|
|
|
|
|
.OrderByDescending(_penalty => _penalty.When)
|
2020-08-17 22:21:11 -04:00
|
|
|
|
.Take(query.Count)
|
|
|
|
|
.Select(_penalty => new ReceivedPenaltyResponse()
|
|
|
|
|
{
|
|
|
|
|
PenaltyId = _penalty.PenaltyId,
|
|
|
|
|
ClientId = query.ClientId,
|
|
|
|
|
Offense = _penalty.Offense,
|
|
|
|
|
AutomatedOffense = _penalty.AutomatedOffense,
|
|
|
|
|
OffenderClientId = _penalty.OffenderId,
|
|
|
|
|
OffenderName = _penalty.Offender.CurrentAlias.Name,
|
|
|
|
|
PunisherClientId = _penalty.PunisherId,
|
|
|
|
|
PunisherName = _penalty.Punisher.CurrentAlias.Name,
|
|
|
|
|
PenaltyType = _penalty.Type,
|
|
|
|
|
When = _penalty.When,
|
|
|
|
|
ExpirationDate = _penalty.Expires,
|
|
|
|
|
IsLinked = _penalty.OffenderId != query.ClientId,
|
|
|
|
|
IsSensitive = _penalty.Type == EFPenalty.PenaltyType.Flag
|
|
|
|
|
})
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
return new ResourceQueryHelperResult<ReceivedPenaltyResponse>
|
|
|
|
|
{
|
|
|
|
|
// todo: maybe actually count
|
|
|
|
|
RetrievedResultCount = penalties.Count,
|
2022-03-25 12:28:15 -04:00
|
|
|
|
Results = penalties.Distinct()
|
2020-08-17 22:21:11 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|