2018-12-01 13:17:53 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Dtos;
|
2018-04-26 20:19:42 -04:00
|
|
|
|
using SharedLibraryCore.Objects;
|
2018-12-01 13:17:53 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Threading.Tasks;
|
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
|
|
|
|
{
|
|
|
|
|
public class PenaltyService : Interfaces.IEntityService<EFPenalty>
|
|
|
|
|
{
|
2018-04-09 15:17:10 -04:00
|
|
|
|
public async Task<EFPenalty> Create(EFPenalty newEntity)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2019-04-02 21:20:37 -04:00
|
|
|
|
var penalty = new EFPenalty()
|
2017-11-29 19:35:50 -05:00
|
|
|
|
{
|
2019-04-02 21:20:37 -04:00
|
|
|
|
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,
|
|
|
|
|
AutomatedOffense = newEntity.AutomatedOffense,
|
|
|
|
|
IsEvadedOffense = newEntity.IsEvadedOffense
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
newEntity.Offender.ReceivedPenalties?.Add(penalty);
|
|
|
|
|
context.Penalties.Add(penalty);
|
2017-11-25 20:29:58 -05:00
|
|
|
|
await context.SaveChangesAsync();
|
2019-04-05 14:34:03 -04:00
|
|
|
|
|
|
|
|
|
// certain penalties we want to save across all profiles
|
|
|
|
|
if (penalty.Type.ShouldPenaltyApplyToAllProfiles())
|
|
|
|
|
{
|
|
|
|
|
var iqLinkedProfiles = context.Clients
|
|
|
|
|
.Where(_client => _client.AliasLinkId == newEntity.Link.AliasLinkId)
|
|
|
|
|
// prevent adding the penalty twice to the same profile
|
|
|
|
|
.Where(_client => _client.ClientId != penalty.OffenderId);
|
|
|
|
|
|
|
|
|
|
await iqLinkedProfiles.ForEachAsync(_client =>
|
|
|
|
|
{
|
|
|
|
|
var linkedPenalty = new EFPenalty()
|
|
|
|
|
{
|
|
|
|
|
OffenderId = _client.ClientId,
|
|
|
|
|
PunisherId = newEntity.Punisher.ClientId,
|
|
|
|
|
LinkId = newEntity.Link.AliasLinkId,
|
|
|
|
|
Type = newEntity.Type,
|
|
|
|
|
Expires = newEntity.Expires,
|
|
|
|
|
Offense = newEntity.Offense,
|
|
|
|
|
When = DateTime.UtcNow,
|
|
|
|
|
AutomatedOffense = newEntity.AutomatedOffense,
|
|
|
|
|
IsEvadedOffense = newEntity.IsEvadedOffense
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.Penalties.Add(linkedPenalty);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
2019-04-02 21:20:37 -04:00
|
|
|
|
|
|
|
|
|
return newEntity;
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 23:33:42 -05:00
|
|
|
|
public Task<EFPenalty> GetUnique(long entityProperty)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFPenalty> Update(EFPenalty entity)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 20:56:31 -04:00
|
|
|
|
public async Task<IList<PenaltyInfo>> GetRecentPenalties(int count, int offset, Penalty.PenaltyType showOnly = Penalty.PenaltyType.Any)
|
2019-03-29 22:56:56 -04:00
|
|
|
|
{
|
2019-03-31 20:56:31 -04:00
|
|
|
|
using (var context = new DatabaseContext(true))
|
2019-03-29 22:56:56 -04:00
|
|
|
|
{
|
2019-03-31 20:56:31 -04:00
|
|
|
|
var iqPenalties = context.Penalties
|
|
|
|
|
.Where(p => showOnly == Penalty.PenaltyType.Any ? p.Type != Penalty.PenaltyType.Any : p.Type == showOnly)
|
|
|
|
|
.OrderByDescending(p => p.When)
|
2019-03-29 22:56:56 -04:00
|
|
|
|
.Skip(offset)
|
2019-03-31 20:56:31 -04:00
|
|
|
|
.Take(count)
|
|
|
|
|
.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
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
#if DEBUG == true
|
|
|
|
|
var querySql = iqPenalties.ToSql();
|
|
|
|
|
#endif
|
2019-03-29 22:56:56 -04:00
|
|
|
|
return await iqPenalties.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-14 14:01:26 -05:00
|
|
|
|
/// <summary>
|
2019-03-31 20:56:31 -04:00
|
|
|
|
/// retrieves penalty information for meta service
|
2018-02-14 14:01:26 -05:00
|
|
|
|
/// </summary>
|
2019-03-31 20:56:31 -04:00
|
|
|
|
/// <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>
|
2018-02-14 14:01:26 -05:00
|
|
|
|
/// <returns></returns>
|
2019-03-31 20:56:31 -04:00
|
|
|
|
public async Task<IList<PenaltyInfo>> GetClientPenaltyForMetaAsync(int clientId, int count, int offset, DateTime? startAt)
|
2018-02-14 14:01:26 -05:00
|
|
|
|
{
|
2019-03-31 20:56:31 -04:00
|
|
|
|
using (var ctx = new DatabaseContext(true))
|
2018-02-14 14:01:26 -05:00
|
|
|
|
{
|
2019-03-31 20:56:31 -04:00
|
|
|
|
var iqPenalties = ctx.Penalties.AsNoTracking()
|
2019-04-05 14:34:03 -04:00
|
|
|
|
//.Where(_penalty => _penalty.Active)
|
2019-03-31 20:56:31 -04:00
|
|
|
|
.Where(_penalty => _penalty.OffenderId == clientId || _penalty.PunisherId == clientId)
|
|
|
|
|
.Where(_penalty => _penalty.When < startAt)
|
|
|
|
|
.OrderByDescending(_penalty => _penalty.When)
|
|
|
|
|
.Skip(offset)
|
|
|
|
|
.Take(count)
|
|
|
|
|
.Select(_penalty => new PenaltyInfo()
|
2018-04-13 02:32:30 -04:00
|
|
|
|
{
|
2019-03-31 20:56:31 -04:00
|
|
|
|
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
|
2018-04-28 17:39:45 -04:00
|
|
|
|
});
|
2018-04-26 20:19:42 -04:00
|
|
|
|
|
2019-03-31 20:56:31 -04:00
|
|
|
|
#if DEBUG == true
|
|
|
|
|
var querySql = iqPenalties.ToSql();
|
|
|
|
|
#endif
|
2018-04-26 20:19:42 -04:00
|
|
|
|
|
2019-03-31 20:56:31 -04:00
|
|
|
|
return await iqPenalties.ToListAsync();
|
2018-02-14 14:01:26 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-05 14:34:03 -04:00
|
|
|
|
public async Task<List<EFPenalty>> GetActivePenaltiesAsync(int linkId, int? ip = null, bool includePunisherName = false)
|
2018-02-15 23:01:28 -05:00
|
|
|
|
{
|
2018-09-02 17:59:27 -04:00
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
2018-12-01 13:17:53 -05:00
|
|
|
|
Expression<Func<EFPenalty, bool>> filter = (p) => new Penalty.PenaltyType[]
|
|
|
|
|
{
|
|
|
|
|
Penalty.PenaltyType.TempBan,
|
2018-12-29 13:43:40 -05:00
|
|
|
|
Penalty.PenaltyType.Ban,
|
|
|
|
|
Penalty.PenaltyType.Flag
|
2018-12-01 13:17:53 -05:00
|
|
|
|
}.Contains(p.Type) &&
|
|
|
|
|
p.Active &&
|
|
|
|
|
(p.Expires == null || p.Expires > now);
|
|
|
|
|
|
2018-09-12 20:53:11 -04:00
|
|
|
|
using (var context = new DatabaseContext(true))
|
2018-02-15 23:01:28 -05:00
|
|
|
|
{
|
2018-12-01 13:17:53 -05:00
|
|
|
|
var iqLinkPenalties = context.Penalties
|
|
|
|
|
.Where(p => p.LinkId == linkId)
|
|
|
|
|
.Where(filter);
|
|
|
|
|
|
|
|
|
|
var iqIPPenalties = context.Aliases
|
2018-12-29 13:43:40 -05:00
|
|
|
|
.Where(a => a.IPAddress != null & a.IPAddress == ip)
|
2018-12-01 13:17:53 -05:00
|
|
|
|
.SelectMany(a => a.Link.ReceivedPenalties)
|
|
|
|
|
.Where(filter);
|
|
|
|
|
|
2018-09-12 20:53:11 -04:00
|
|
|
|
#if DEBUG == true
|
2018-12-01 13:17:53 -05:00
|
|
|
|
var penaltiesSql = iqLinkPenalties.ToSql();
|
|
|
|
|
var ipPenaltiesSql = iqIPPenalties.ToSql();
|
2018-09-12 20:53:11 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
2018-12-29 13:43:40 -05:00
|
|
|
|
var activePenalties = (await iqLinkPenalties.ToListAsync())
|
|
|
|
|
.Union(await iqIPPenalties.ToListAsync())
|
|
|
|
|
.Distinct();
|
|
|
|
|
|
2018-09-12 20:53:11 -04:00
|
|
|
|
// this is a bit more performant in memory (ordering)
|
2018-12-01 13:17:53 -05:00
|
|
|
|
return activePenalties.OrderByDescending(p => p.When).ToList();
|
2018-02-15 23:01:28 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 21:20:37 -04:00
|
|
|
|
public async Task RemoveActivePenalties(int aliasLinkId, EFClient origin)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
2019-04-02 21:20:37 -04:00
|
|
|
|
var penalties = context.Penalties
|
2017-11-25 20:29:58 -05:00
|
|
|
|
.Include(p => p.Link.Children)
|
|
|
|
|
.Where(p => p.LinkId == aliasLinkId)
|
2019-04-02 21:20:37 -04:00
|
|
|
|
.Where(p => p.Expires > now || p.Expires == null);
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
2019-04-02 21:20:37 -04:00
|
|
|
|
await penalties.ForEachAsync(p =>
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
p.Active = false;
|
2019-04-05 14:34:03 -04:00
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|