From 3800fc49c800189477950d174ab8377e3b599d5b Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Wed, 26 Feb 2025 14:00:27 -0800 Subject: [PATCH] implement ``ModerationService`` add type ``NotBanned`` to ``UserBanType`` enum --- src/Schema/UserBanType.cs | 1 + src/Services/ModerationService.cs | 84 +++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/Services/ModerationService.cs diff --git a/src/Schema/UserBanType.cs b/src/Schema/UserBanType.cs index 9a3a892..29c99a3 100644 --- a/src/Schema/UserBanType.cs +++ b/src/Schema/UserBanType.cs @@ -2,6 +2,7 @@ namespace sodoff.Schema; public enum UserBanType { + NotBanned = 0, IndefiniteOpenChatBan = 1, TemporaryOpenChatBan = 2, IndefiniteAccountBan = 3, diff --git a/src/Services/ModerationService.cs b/src/Services/ModerationService.cs new file mode 100644 index 0000000..e1bb61b --- /dev/null +++ b/src/Services/ModerationService.cs @@ -0,0 +1,84 @@ +using System; +using sodoff.Model; +using sodoff.Schema; + +namespace sodoff.Services; + +public class ModerationService +{ + public readonly DBContext ctx; + + public ModerationService(DBContext ctx) + { + this.ctx = ctx; + } + + public UserBan AddBanToViking(Viking viking, UserBanType userBanType, DateTime expirationDate = new DateTime()) + { + // get UTC time stamp of function execution + DateTime timestamp = DateTime.UtcNow; + + // construct user ban + UserBan userBan = new UserBan + { + UserBanType = userBanType, + ExpiresOn = expirationDate, + CreatedAt = timestamp + }; + + // add to viking userban list + viking.UserBans.Add(userBan); + ctx.SaveChanges(); + + // return ban + return userBan; + } + + public bool RemoveBanById(int id) + { + // find ban + UserBan? ban = ctx.Bans.FirstOrDefault(e => e.Id == id); + + // remove it + if (ban != null) { ctx.Bans.Remove(ban); ctx.SaveChanges(); return true; } + else return false; + } + + public bool RemoveBansFromVikingByType(Viking viking, UserBanType userBanType) + { + // get all bans of type + List userBans = viking.UserBans.Where(e => e.UserBanType == userBanType).ToList(); + + if (userBans.Count == 0) return false; + + // delete all + foreach(var ban in userBans) + viking.UserBans.Remove(ban); + + ctx.SaveChanges(); + + return true; + } + + public UserBanType IsVikingBanned(Viking viking) + { + // get UTC time stamp of function execution + DateTime timestamp = DateTime.UtcNow; + + // sort viking ban list by latest first + List bans = viking.UserBans.OrderByDescending(e => e.CreatedAt).ToList(); + + if (bans.Count == 0) return UserBanType.NotBanned; // no bans in list means viking is not banned + + if (bans.First().UserBanType == UserBanType.IndefiniteAccountBan) return UserBanType.IndefiniteAccountBan; + else if (bans.First().UserBanType == UserBanType.IndefiniteOpenChatBan) return UserBanType.IndefiniteOpenChatBan; + + if (DateTime.Compare(bans.First().ExpiresOn ?? new DateTime(9999, 99, 99), timestamp) > 0) return bans.First().UserBanType; + else + { + // ban should be removed + RemoveBanById(bans.First().Id); + return UserBanType.NotBanned; + } + } +}