-Implement Schema And Model For Bans And Implement `ModerationService`

-Modify ``User`` To Have Ban Collection
-Add ``Bans`` Table To ``DBContext``
This commit is contained in:
Alan Moon 2024-12-12 14:12:44 -08:00
parent 3ddbb93b10
commit 0e90d5a111
6 changed files with 105 additions and 0 deletions

View File

@ -25,6 +25,7 @@ public class DBContext : DbContext {
public DbSet<Party> Parties { get; set; } = null!; public DbSet<Party> Parties { get; set; } = null!;
public DbSet<Neighborhood> Neighborhoods { get; set; } = null!; public DbSet<Neighborhood> Neighborhoods { get; set; } = null!;
// we had a brief debate on whether it's neighborhoods or neighborheed // we had a brief debate on whether it's neighborhoods or neighborheed
public DbSet<UserBan> UserBans { get; set; } = null!;
private readonly IOptions<ApiServerConfig> config; private readonly IOptions<ApiServerConfig> config;
@ -252,5 +253,9 @@ public class DBContext : DbContext {
builder.Entity<Neighborhood>().HasOne(r => r.Viking) builder.Entity<Neighborhood>().HasOne(r => r.Viking)
.WithOne(e => e.Neighborhood) .WithOne(e => e.Neighborhood)
.HasForeignKey<Neighborhood>(e => e.VikingId); .HasForeignKey<Neighborhood>(e => e.VikingId);
builder.Entity<UserBan>().HasOne(r => r.User)
.WithMany(e => e.Bans)
.HasForeignKey(e => e.UserId);
} }
} }

View File

@ -17,4 +17,5 @@ public class User {
public virtual ICollection<Session> Sessions { get; set; } = null!; public virtual ICollection<Session> Sessions { get; set; } = null!;
public virtual ICollection<Viking> Vikings { get; set; } = null!; public virtual ICollection<Viking> Vikings { get; set; } = null!;
public virtual ICollection<PairData> PairData { get; set; } = null!; public virtual ICollection<PairData> PairData { get; set; } = null!;
public virtual ICollection<UserBan> Bans { get; set; } = null!;
} }

20
src/Model/UserBan.cs Normal file
View File

@ -0,0 +1,20 @@
using System;
using System.ComponentModel.DataAnnotations;
using sodoff.Schema;
namespace sodoff.Model;
public class UserBan
{
[Key]
public int Id { get; set; }
[Required]
public Guid? UserId { get; set; } = null!;
public UserBanType BanType { get; set; } = UserBanType.TemporarySuspension;
public DateTime? CreatedAt { get; set; } = new DateTime();
public DateTime? EndsAt { get; set; } = new DateTime();
public virtual User User { get; set; } = null!;
}

View File

@ -40,6 +40,7 @@ builder.Services.AddScoped<GameDataService>();
builder.Services.AddScoped<ProfileService>(); builder.Services.AddScoped<ProfileService>();
builder.Services.AddScoped<NeighborhoodService>(); builder.Services.AddScoped<NeighborhoodService>();
builder.Services.AddScoped<MMOCommunicationService>(); builder.Services.AddScoped<MMOCommunicationService>();
builder.Services.AddScoped<ModerationService>();
bool assetServer = builder.Configuration.GetSection("AssetServer").GetValue<bool>("Enabled"); bool assetServer = builder.Configuration.GetSection("AssetServer").GetValue<bool>("Enabled");
string assetIP = builder.Configuration.GetSection("AssetServer").GetValue<string>("ListenIP"); string assetIP = builder.Configuration.GetSection("AssetServer").GetValue<string>("ListenIP");

11
src/Schema/UserBanType.cs Normal file
View File

@ -0,0 +1,11 @@
namespace sodoff.Schema;
public enum UserBanType
{
IndefiniteSuspension = 1,
IndefiniteMPSuspension = 2,
TemporarySuspension = 3,
TemporaryMPSuspension = 4,
IndefiniteTCSuspension = 5,
TemporaryTCSuspension = 6
}

View File

@ -0,0 +1,67 @@
using System;
using sodoff.Model;
using sodoff.Schema;
namespace sodoff.Services;
public class ModerationService
{
private readonly DBContext ctx;
public ModerationService(DBContext ctx)
{
this.ctx = ctx;
}
public UserBan AddBanToUser(User user, UserBanType banType, DateTime dateEnd = new DateTime())
{
// create a ban in relation to the specified user
UserBan userBan = new UserBan
{
BanType = banType,
CreatedAt = DateTime.UtcNow
};
// if suspension is indefinite, set end date to infinity
if(banType == UserBanType.IndefiniteSuspension || banType == UserBanType.IndefiniteMPSuspension || banType == UserBanType.IndefiniteTCSuspension) userBan.EndsAt = new DateTime(9999, 01, 01);
else userBan.EndsAt = dateEnd;
// add ban to user ban list
user.Bans.Add(userBan);
// update database
ctx.SaveChanges();
// return ban
return userBan;
}
public bool RemoveBanFromUser(User user, UserBan ban)
{
// remove ban from database if it exists on the user
if(user.Bans.FirstOrDefault(e => e == ban) != null) { user.Bans.Remove(ban); ctx.SaveChanges(); return true; }
else return false;
}
public bool RemoveAllBansFromUser(User user)
{
// remove all bans from user if user has any
if(user.Bans.Count >= 0) { foreach(var ban in user.Bans) user.Bans.Remove(ban); ctx.SaveChanges(); return true; }
else return false;
}
public UserBan GetLatestBanFromUser(User user)
{
// retreive the most recently created ban from user
UserBan? userBan = user.Bans.OrderByDescending(e => e.CreatedAt).FirstOrDefault();
if(userBan != null) return userBan;
else return null!; // return null if the user has no bans on record
}
public ICollection<UserBan> GetAllBansFromUser(User user, bool descendingOrder = false)
{
if(descendingOrder) return user.Bans.OrderByDescending(e => e.CreatedAt).ToList();
else return user.Bans.OrderBy(e => e.CreatedAt).ToList(); // return sorted list by created date
}
}