2022-01-26 11:32:16 -05:00
|
|
|
|
using System;
|
2018-12-01 13:17:53 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Models;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-08-21 11:40:03 -04:00
|
|
|
|
using SharedLibraryCore.Configuration;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore.Services
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public class PenaltyService : IEntityService<EFPenalty>
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2021-08-21 11:40:03 -04:00
|
|
|
|
private readonly ApplicationConfiguration _appConfig;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
|
|
|
|
|
2021-08-21 11:40:03 -04:00
|
|
|
|
public PenaltyService(IDatabaseContextFactory contextFactory, ApplicationConfiguration appConfig)
|
2020-11-27 22:52:52 -05:00
|
|
|
|
{
|
|
|
|
|
_contextFactory = contextFactory;
|
2021-08-21 11:40:03 -04:00
|
|
|
|
_appConfig = appConfig;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2020-04-21 18:34:00 -04:00
|
|
|
|
public virtual async Task<EFPenalty> Create(EFPenalty newEntity)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext();
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var penalty = new EFPenalty
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
Active = true,
|
|
|
|
|
OffenderId = newEntity.Offender.ClientId,
|
|
|
|
|
PunisherId = newEntity.Punisher.ClientId,
|
2022-02-22 18:09:50 -05:00
|
|
|
|
LinkId = newEntity.Link?.AliasLinkId,
|
2020-11-27 22:52:52 -05:00
|
|
|
|
Type = newEntity.Type,
|
|
|
|
|
Expires = newEntity.Expires,
|
|
|
|
|
Offense = newEntity.Offense,
|
|
|
|
|
When = DateTime.UtcNow,
|
2022-01-26 11:32:16 -05:00
|
|
|
|
AutomatedOffense = newEntity.AutomatedOffense ??
|
|
|
|
|
newEntity.Punisher.AdministeredPenalties?.FirstOrDefault()?.AutomatedOffense,
|
2020-11-27 22:52:52 -05:00
|
|
|
|
IsEvadedOffense = newEntity.IsEvadedOffense
|
|
|
|
|
};
|
2022-02-22 18:09:50 -05:00
|
|
|
|
|
|
|
|
|
if (LinkedPenalties.Contains(newEntity.Type))
|
|
|
|
|
{
|
|
|
|
|
var penaltyIdentifiers = new EFPenaltyIdentifier
|
|
|
|
|
{
|
|
|
|
|
Penalty = penalty,
|
|
|
|
|
NetworkId = newEntity.Offender.NetworkId,
|
|
|
|
|
IPv4Address = newEntity.Offender.CurrentAlias.IPAddress
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.PenaltyIdentifiers.Add(penaltyIdentifiers);
|
|
|
|
|
}
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
|
|
|
|
context.Penalties.Add(penalty);
|
|
|
|
|
await context.SaveChangesAsync();
|
2019-04-02 21:20:37 -04:00
|
|
|
|
|
|
|
|
|
return newEntity;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 12:28:15 -04:00
|
|
|
|
public async Task CreatePenaltyIdentifier(int penaltyId, long networkId, int ipv4Address)
|
|
|
|
|
{
|
|
|
|
|
await using var context = _contextFactory.CreateContext();
|
|
|
|
|
var penaltyIdentifiers = new EFPenaltyIdentifier
|
|
|
|
|
{
|
|
|
|
|
PenaltyId = penaltyId,
|
|
|
|
|
NetworkId = networkId,
|
|
|
|
|
IPv4Address = ipv4Address
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.PenaltyIdentifiers.Add(penaltyIdentifiers);
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
public Task<EFPenalty> Delete(EFPenalty entity)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IList<EFPenalty>> Find(Func<EFPenalty, bool> expression)
|
|
|
|
|
{
|
2018-04-05 00:38:45 -04:00
|
|
|
|
throw await Task.FromResult(new Exception());
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFPenalty> Get(int entityID)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
public Task<EFPenalty> GetUnique(long entityProperty, object altKey)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFPenalty> Update(EFPenalty entity)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public async Task<IList<PenaltyInfo>> GetRecentPenalties(int count, int offset,
|
|
|
|
|
EFPenalty.PenaltyType showOnly = EFPenalty.PenaltyType.Any, bool ignoreAutomated = true)
|
2019-03-29 22:56:56 -04:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
|
|
|
|
var iqPenalties = context.Penalties
|
2022-01-26 11:32:16 -05:00
|
|
|
|
.Where(p => showOnly == EFPenalty.PenaltyType.Any
|
|
|
|
|
? p.Type != EFPenalty.PenaltyType.Any
|
|
|
|
|
: p.Type == showOnly)
|
2021-08-21 11:40:03 -04:00
|
|
|
|
.Where(_penalty => !ignoreAutomated || _penalty.PunisherId != 1)
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.OrderByDescending(p => p.When)
|
|
|
|
|
.Skip(offset)
|
|
|
|
|
.Take(count)
|
2022-01-26 11:32:16 -05:00
|
|
|
|
.Select(_penalty => new PenaltyInfo
|
|
|
|
|
{
|
|
|
|
|
Id = _penalty.PenaltyId,
|
|
|
|
|
Offense = _penalty.Offense,
|
|
|
|
|
AutomatedOffense = _penalty.AutomatedOffense,
|
|
|
|
|
OffenderId = _penalty.OffenderId,
|
|
|
|
|
OffenderName = _penalty.Offender.CurrentAlias.Name,
|
|
|
|
|
PunisherId = _penalty.PunisherId,
|
|
|
|
|
PunisherName = _penalty.Punisher.CurrentAlias.Name,
|
|
|
|
|
PunisherLevel = _penalty.Punisher.Level,
|
|
|
|
|
PenaltyType = _penalty.Type,
|
|
|
|
|
Expires = _penalty.Expires,
|
|
|
|
|
TimePunished = _penalty.When,
|
|
|
|
|
IsEvade = _penalty.IsEvadedOffense
|
|
|
|
|
});
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
|
|
|
|
return await iqPenalties.ToListAsync();
|
2019-03-29 22:56:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-22 18:09:50 -05:00
|
|
|
|
private static readonly EFPenalty.PenaltyType[] LinkedPenalties =
|
2022-02-25 09:22:40 -05:00
|
|
|
|
{ EFPenalty.PenaltyType.Ban, EFPenalty.PenaltyType.Flag, EFPenalty.PenaltyType.TempBan };
|
2019-06-28 17:57:01 -04:00
|
|
|
|
|
2022-02-22 18:09:50 -05:00
|
|
|
|
private static readonly Expression<Func<EFPenalty, bool>> Filter = p =>
|
|
|
|
|
LinkedPenalties.Contains(p.Type) && p.Active && (p.Expires == null || p.Expires > DateTime.UtcNow);
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
2022-02-22 18:09:50 -05:00
|
|
|
|
private static readonly Expression<Func<EFPenaltyIdentifier, bool>> FilterById = pi =>
|
|
|
|
|
LinkedPenalties.Contains(pi.Penalty.Type) && pi.Penalty.Active &&
|
|
|
|
|
(pi.Penalty.Expires == null || pi.Penalty.Expires > DateTime.UtcNow);
|
2018-02-14 14:01:26 -05:00
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
public async Task<List<EFPenalty>> GetActivePenaltiesAsync(int linkId, int currentAliasId, long networkId, Reference.Game game,
|
2022-02-22 18:09:50 -05:00
|
|
|
|
int? ip = null)
|
2018-02-15 23:01:28 -05:00
|
|
|
|
{
|
2022-06-15 20:37:34 -04:00
|
|
|
|
var penaltiesByIdentifier = await GetActivePenaltiesByIdentifier(ip, networkId, game);
|
2018-09-02 17:59:27 -04:00
|
|
|
|
|
2022-02-22 18:09:50 -05:00
|
|
|
|
if (penaltiesByIdentifier.Any())
|
|
|
|
|
{
|
|
|
|
|
return penaltiesByIdentifier;
|
|
|
|
|
}
|
2018-12-01 13:17:53 -05:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
|
|
|
|
|
2021-10-20 12:16:35 -04:00
|
|
|
|
IQueryable<EFPenalty> iqIpPenalties;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2021-10-20 12:16:35 -04:00
|
|
|
|
if (_appConfig.EnableImplicitAccountLinking)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
iqIpPenalties = context.Aliases
|
2021-08-21 11:40:03 -04:00
|
|
|
|
.Where(a => a.IPAddress != null && a.IPAddress == ip)
|
|
|
|
|
.SelectMany(a => a.Link.ReceivedPenalties)
|
2022-02-22 18:09:50 -05:00
|
|
|
|
.Where(Filter);
|
2021-10-20 12:16:35 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-28 16:33:21 -05:00
|
|
|
|
var usedIps = await context.Aliases.AsNoTracking()
|
2022-02-22 18:09:50 -05:00
|
|
|
|
.Where(alias =>
|
|
|
|
|
(alias.LinkId == linkId || alias.AliasId == currentAliasId) && alias.IPAddress != null)
|
2022-01-28 16:33:21 -05:00
|
|
|
|
.Select(alias => alias.IPAddress).ToListAsync();
|
|
|
|
|
|
|
|
|
|
var aliasedIds = await context.Aliases.AsNoTracking().Where(alias => usedIps.Contains(alias.IPAddress))
|
|
|
|
|
.Select(alias => alias.LinkId)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
iqIpPenalties = context.Penalties.AsNoTracking()
|
2022-02-22 18:09:50 -05:00
|
|
|
|
.Where(penalty => aliasedIds.Contains(penalty.LinkId ?? -1) || penalty.LinkId == linkId)
|
|
|
|
|
.Where(Filter);
|
2021-10-20 12:16:35 -04:00
|
|
|
|
}
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
2022-01-28 15:33:08 -05:00
|
|
|
|
var activeIpPenalties = await iqIpPenalties.ToListAsync();
|
2022-02-01 19:20:29 -05:00
|
|
|
|
var activePenalties = activeIpPenalties.Distinct();
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
|
|
|
|
// this is a bit more performant in memory (ordering)
|
|
|
|
|
return activePenalties.OrderByDescending(p => p.When).ToList();
|
2018-02-15 23:01:28 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
public async Task<List<EFPenalty>> GetActivePenaltiesByIdentifier(int? ip, long networkId, Reference.Game game)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2022-02-22 18:09:50 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
2022-03-25 12:28:15 -04:00
|
|
|
|
|
2022-02-22 18:09:50 -05:00
|
|
|
|
var activePenaltiesIds = context.PenaltyIdentifiers.Where(identifier =>
|
2022-06-15 20:37:34 -04:00
|
|
|
|
identifier.IPv4Address != null && identifier.IPv4Address == ip || identifier.NetworkId == networkId && identifier.Penalty.Offender.GameName == game)
|
2022-02-22 18:09:50 -05:00
|
|
|
|
.Where(FilterById);
|
|
|
|
|
return await activePenaltiesIds.Select(ids => ids.Penalty).ToListAsync();
|
|
|
|
|
}
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
2022-03-25 12:28:15 -04:00
|
|
|
|
public async Task<List<EFPenalty>> ActivePenaltiesByRecentIdentifiers(int linkId)
|
|
|
|
|
{
|
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
|
|
|
|
|
|
|
|
|
var recentlyUsedIps = await context.Aliases.Where(alias => alias.LinkId == linkId)
|
|
|
|
|
.Where(alias => alias.IPAddress != null)
|
|
|
|
|
.Where(alias => alias.DateAdded >= DateTime.UtcNow - _appConfig.RecentAliasIpLinkTimeLimit)
|
|
|
|
|
.Select(alias => alias.IPAddress).ToListAsync();
|
|
|
|
|
|
|
|
|
|
if (!recentlyUsedIps.Any())
|
|
|
|
|
{
|
|
|
|
|
return new List<EFPenalty>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var activePenaltiesIds = context.PenaltyIdentifiers
|
|
|
|
|
.Where(identifier => recentlyUsedIps.Contains(identifier.IPv4Address))
|
|
|
|
|
.Where(FilterById);
|
|
|
|
|
|
|
|
|
|
return await activePenaltiesIds.Select(ids => ids.Penalty).ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
public virtual async Task RemoveActivePenalties(int aliasLinkId, long networkId, Reference.Game game, int? ipAddress = null)
|
2022-02-22 18:09:50 -05:00
|
|
|
|
{
|
|
|
|
|
await using var context = _contextFactory.CreateContext();
|
2020-11-27 22:52:52 -05:00
|
|
|
|
var now = DateTime.UtcNow;
|
2022-02-22 18:09:50 -05:00
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
var activePenalties = await GetActivePenaltiesByIdentifier(ipAddress, networkId, game);
|
2022-02-22 18:09:50 -05:00
|
|
|
|
|
|
|
|
|
if (activePenalties.Any())
|
|
|
|
|
{
|
|
|
|
|
var ids = activePenalties.Select(penalty => penalty.PenaltyId);
|
|
|
|
|
await context.Penalties.Where(penalty => ids.Contains(penalty.PenaltyId))
|
|
|
|
|
.ForEachAsync(penalty =>
|
|
|
|
|
{
|
|
|
|
|
penalty.Active = false;
|
|
|
|
|
penalty.Expires = now;
|
|
|
|
|
});
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-21 11:40:03 -04:00
|
|
|
|
var penaltiesByLink = context.Penalties
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.Where(p => p.LinkId == aliasLinkId)
|
2021-08-21 11:40:03 -04:00
|
|
|
|
.Where(p => p.Expires > now || p.Expires == null);
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
2021-08-21 11:40:03 -04:00
|
|
|
|
var penaltiesByIp = context.Penalties
|
|
|
|
|
.Where(p => p.Offender.CurrentAlias.IPAddress != null && p.Offender.CurrentAlias.IPAddress == null)
|
|
|
|
|
.Where(p => p.Expires > now || p.Expires == null);
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2021-08-21 11:40:03 -04:00
|
|
|
|
await penaltiesByLink.Union(penaltiesByIp).Distinct().ForEachAsync(p =>
|
|
|
|
|
{
|
|
|
|
|
p.Active = false;
|
|
|
|
|
p.Expires = now;
|
|
|
|
|
});
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await context.SaveChangesAsync();
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-28 15:33:08 -05:00
|
|
|
|
}
|