IW4M-Admin/SharedLibraryCore/Services/PenaltyService.cs

221 lines
9.1 KiB
C#
Raw Normal View History

2022-01-26 11:32:16 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
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;
using SharedLibraryCore.Interfaces;
2018-04-08 02:44:42 -04:00
namespace SharedLibraryCore.Services
{
public class PenaltyService : IEntityService<EFPenalty>
{
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)
{
_contextFactory = contextFactory;
2021-08-21 11:40:03 -04:00
_appConfig = appConfig;
}
2022-01-26 11:32:16 -05:00
public virtual async Task<EFPenalty> Create(EFPenalty newEntity)
{
await using var context = _contextFactory.CreateContext();
2022-01-26 11:32:16 -05:00
var penalty = new EFPenalty
{
Active = true,
OffenderId = newEntity.Offender.ClientId,
PunisherId = newEntity.Punisher.ClientId,
LinkId = newEntity.Link.AliasLinkId,
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,
IsEvadedOffense = newEntity.IsEvadedOffense
};
context.Penalties.Add(penalty);
await context.SaveChangesAsync();
return newEntity;
}
public Task<EFPenalty> Delete(EFPenalty entity)
{
throw new NotImplementedException();
}
public async Task<IList<EFPenalty>> Find(Func<EFPenalty, bool> expression)
{
throw await Task.FromResult(new Exception());
}
public Task<EFPenalty> Get(int entityID)
{
throw new NotImplementedException();
}
public Task<EFPenalty> GetUnique(long entityProperty)
{
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)
{
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)
.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
});
return await iqPenalties.ToListAsync();
}
/// <summary>
2022-01-26 11:32:16 -05:00
/// retrieves penalty information for meta service
/// </summary>
/// <param name="clientId">database id of the client</param>
/// <param name="count">how many items to retrieve</param>
/// <param name="offset">not used</param>
/// <param name="startAt">retreive penalties older than this</param>
/// <returns></returns>
2022-01-26 11:32:16 -05:00
public async Task<IList<PenaltyInfo>> GetClientPenaltyForMetaAsync(int clientId, int count, int offset,
DateTime? startAt)
{
var linkedPenaltyType = Utilities.LinkedPenaltyTypes();
await using var context = _contextFactory.CreateContext(false);
var linkId = await context.Clients.AsNoTracking()
.Where(_penalty => _penalty.ClientId == clientId)
.Select(_penalty => _penalty.AliasLinkId)
.FirstOrDefaultAsync();
var iqPenalties = context.Penalties.AsNoTracking()
2022-01-26 11:32:16 -05:00
.Where(_penalty => _penalty.OffenderId == clientId || _penalty.PunisherId == clientId ||
linkedPenaltyType.Contains(_penalty.Type) && _penalty.LinkId == linkId)
.Where(_penalty => _penalty.When <= startAt)
.OrderByDescending(_penalty => _penalty.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
});
return await iqPenalties.Distinct().ToListAsync();
}
public async Task<List<EFPenalty>> GetActivePenaltiesAsync(int linkId, int currentAliasId, int? ip = null,
2022-01-26 11:32:16 -05:00
bool includePunisherName = false)
2018-02-15 23:01:28 -05:00
{
var now = DateTime.UtcNow;
2022-01-26 11:32:16 -05:00
Expression<Func<EFPenalty, bool>> filter = p => new[]
{
EFPenalty.PenaltyType.TempBan,
EFPenalty.PenaltyType.Ban,
EFPenalty.PenaltyType.Flag
}.Contains(p.Type) &&
p.Active &&
(p.Expires == null || p.Expires > now);
await using var context = _contextFactory.CreateContext(false);
IQueryable<EFPenalty> iqIpPenalties;
2022-01-26 11:32:16 -05: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)
.Where(filter);
}
else
{
var usedIps = await context.Aliases.AsNoTracking()
.Where(alias => (alias.LinkId == linkId || alias.AliasId == currentAliasId) && alias.IPAddress != null)
.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-01 19:20:29 -05:00
.Where(penalty => aliasedIds.Contains(penalty.LinkId) || penalty.LinkId == linkId)
.Where(filter);
}
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();
// this is a bit more performant in memory (ordering)
return activePenalties.OrderByDescending(p => p.When).ToList();
2018-02-15 23:01:28 -05:00
}
2021-08-21 11:40:03 -04:00
public virtual async Task RemoveActivePenalties(int aliasLinkId, int? ipAddress = null)
{
await using var context = _contextFactory.CreateContext();
var now = DateTime.UtcNow;
2021-08-21 11:40:03 -04:00
var penaltiesByLink = context.Penalties
.Where(p => p.LinkId == aliasLinkId)
2021-08-21 11:40:03 -04:00
.Where(p => p.Expires > now || p.Expires == null);
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
await context.SaveChangesAsync();
}
}
2022-01-28 15:33:08 -05:00
}