From 180cac5aa9dd8de8b36b79223f9c66d6aa131e36 Mon Sep 17 00:00:00 2001 From: Slendy Date: Mon, 13 May 2024 18:46:49 -0500 Subject: [PATCH] Remove debug prints and prevent activities from being registered in read only mode --- .../Controllers/ActivityController.cs | 7 ----- .../Database/ActivityInterceptor.cs | 21 ++++++++++---- .../Activity/ActivityEntityEventHandler.cs | 29 +++---------------- 3 files changed, 20 insertions(+), 37 deletions(-) diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/ActivityController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/ActivityController.cs index fb511289..69f38827 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/ActivityController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/ActivityController.cs @@ -30,9 +30,6 @@ public class ActivityController : ControllerBase this.database = database; } - /// - /// This method is only used for LBP2 so we exclude playlists - /// private async Task> GetFilters ( IQueryable dtoQuery, @@ -123,8 +120,6 @@ public class ActivityController : ControllerBase dto.Activity.Type != EventType.AddLevelToPlaylist); } - Logger.Debug(predicate.ToString(), LogArea.Activity); - dtoQuery = dtoQuery.Where(predicate); return dtoQuery; @@ -344,8 +339,6 @@ public class ActivityController : ControllerBase List outerGroups = groups.ToOuterActivityGroups(); - PrintOuterGroups(outerGroups); - long oldestTimestamp = GetOldestTime(groups, times.Start).ToUnixTimeMilliseconds(); await this.CacheEntities(outerGroups); diff --git a/ProjectLighthouse/Database/ActivityInterceptor.cs b/ProjectLighthouse/Database/ActivityInterceptor.cs index fb364d95..7ded9cd5 100644 --- a/ProjectLighthouse/Database/ActivityInterceptor.cs +++ b/ProjectLighthouse/Database/ActivityInterceptor.cs @@ -22,13 +22,20 @@ public class ActivityInterceptor : SaveChangesInterceptor public required object OldEntity { get; init; } } - private readonly ConcurrentDictionary<(Type Type, int HashCode), CustomTrackedEntity> unsavedEntities; + private struct TrackedEntityKey + { + public Type Type { get; set; } + public int HashCode { get; set; } + public Guid ContextId { get; set; } + } + + private readonly ConcurrentDictionary unsavedEntities; private readonly IEntityEventHandler eventHandler; public ActivityInterceptor(IEntityEventHandler eventHandler) { this.eventHandler = eventHandler; - this.unsavedEntities = new ConcurrentDictionary<(Type Type, int HashCode), CustomTrackedEntity>(); + this.unsavedEntities = new ConcurrentDictionary(); } #region Hooking stuff @@ -78,8 +85,12 @@ public class ActivityInterceptor : SaveChangesInterceptor if (entry.Metadata.Name.Contains("Token")) continue; if (entry.State is not (EntityState.Added or EntityState.Deleted or EntityState.Modified)) continue; - - this.unsavedEntities.TryAdd((entry.Entity.GetType(), entry.Entity.GetHashCode()), + this.unsavedEntities.TryAdd(new TrackedEntityKey + { + ContextId = context.ContextId.InstanceId, + Type = entry.Entity.GetType(), + HashCode = entry.Entity.GetHashCode(), + }, new CustomTrackedEntity { State = entry.State, @@ -97,7 +108,7 @@ public class ActivityInterceptor : SaveChangesInterceptor List entries = context.ChangeTracker.Entries().ToList(); - foreach (KeyValuePair<(Type Type, int HashCode), CustomTrackedEntity> kvp in this.unsavedEntities) + foreach (KeyValuePair kvp in this.unsavedEntities) { EntityEntry entry = entries.FirstOrDefault(e => e.Metadata.ClrType == kvp.Key.Type && e.Entity.GetHashCode() == kvp.Key.HashCode); diff --git a/ProjectLighthouse/Types/Activity/ActivityEntityEventHandler.cs b/ProjectLighthouse/Types/Activity/ActivityEntityEventHandler.cs index a2b520e6..91e3e605 100644 --- a/ProjectLighthouse/Types/Activity/ActivityEntityEventHandler.cs +++ b/ProjectLighthouse/Types/Activity/ActivityEntityEventHandler.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using System.Linq.Expressions; +using LBPUnion.ProjectLighthouse.Configuration; using LBPUnion.ProjectLighthouse.Database; using LBPUnion.ProjectLighthouse.Extensions; using LBPUnion.ProjectLighthouse.Logging; @@ -13,10 +14,6 @@ using LBPUnion.ProjectLighthouse.Types.Entities.Website; using LBPUnion.ProjectLighthouse.Types.Levels; using LBPUnion.ProjectLighthouse.Types.Logging; using Microsoft.EntityFrameworkCore; -#if DEBUG -using System.ComponentModel.DataAnnotations.Schema; -using System.Reflection; -#endif namespace LBPUnion.ProjectLighthouse.Types.Activity; @@ -24,7 +21,6 @@ public class ActivityEntityEventHandler : IEntityEventHandler { public void OnEntityInserted(DatabaseContext database, T entity) where T : class { - Logger.Debug($@"OnEntityInserted: {entity.GetType().Name}", LogArea.Activity); ActivityEntity? activity = entity switch { SlotEntity slot => slot.Type switch @@ -50,7 +46,7 @@ public class ActivityEntityEventHandler : IEntityEventHandler }, _ => null, }, - CommentType.Profile => new UserCommentActivityEntity() + CommentType.Profile => new UserCommentActivityEntity { Type = EventType.CommentOnUser, CommentId = comment.CommentId, @@ -199,6 +195,8 @@ public class ActivityEntityEventHandler : IEntityEventHandler { if (activity == null) return; + if (ServerConfiguration.Instance.UserGeneratedContentLimits.ReadOnlyMode) return; + Logger.Debug("Inserting activity: " + activity.GetType().Name, LogArea.Activity); RemoveDuplicateEvents(database, activity); @@ -210,24 +208,6 @@ public class ActivityEntityEventHandler : IEntityEventHandler public void OnEntityChanged(DatabaseContext database, T origEntity, T currentEntity) where T : class { - #if DEBUG - foreach (PropertyInfo propInfo in currentEntity.GetType().GetProperties()) - { - if (!propInfo.CanRead || !propInfo.CanWrite) continue; - - if (propInfo.CustomAttributes.Any(c => c.AttributeType == typeof(NotMappedAttribute))) continue; - - object? origVal = propInfo.GetValue(origEntity); - object? newVal = propInfo.GetValue(currentEntity); - if ((origVal == null && newVal == null) || (origVal != null && newVal != null && origVal.Equals(newVal))) continue; - - Logger.Debug($@"Value for {propInfo.Name} changed", LogArea.Activity); - Logger.Debug($@"Orig val: {origVal?.ToString() ?? "null"}", LogArea.Activity); - Logger.Debug($@"New val: {newVal?.ToString() ?? "null"}", LogArea.Activity); - } - Logger.Debug($@"OnEntityChanged: {currentEntity.GetType().Name}", LogArea.Activity); - #endif - ActivityEntity? activity = null; switch (currentEntity) { @@ -354,7 +334,6 @@ public class ActivityEntityEventHandler : IEntityEventHandler public void OnEntityDeleted(DatabaseContext database, T entity) where T : class { - Logger.Debug($@"OnEntityDeleted: {entity.GetType().Name}", LogArea.Activity); ActivityEntity? activity = entity switch { HeartedLevelEntity heartedLevel => heartedLevel.Slot.Type switch