Don't create activities for story levels

Also no longer shows you activities from incompatible levels (someone plays an LBP3 level but you won't be shown it from LBP2)

Also gets rid of versus scores
This commit is contained in:
Slendy 2023-08-31 18:20:53 -05:00
commit 24fa301182
No known key found for this signature in database
GPG key ID: 7288D68361B91428
4 changed files with 96 additions and 47 deletions

View file

@ -43,6 +43,10 @@ public class ActivityController : ControllerBase
bool excludeMyPlaylists = true bool excludeMyPlaylists = true
) )
{ {
dtoQuery = token.GameVersion == GameVersion.LittleBigPlanetVita
? dtoQuery.Where(dto => dto.TargetSlotGameVersion == null || dto.TargetSlotGameVersion == token.GameVersion)
: dtoQuery.Where(dto => dto.TargetSlotGameVersion == null || dto.TargetSlotGameVersion <= token.GameVersion);
Expression<Func<ActivityDto, bool>> predicate = PredicateExtensions.False<ActivityDto>(); Expression<Func<ActivityDto, bool>> predicate = PredicateExtensions.False<ActivityDto>();
List<int> favouriteUsers = await this.database.HeartedProfiles.Where(hp => hp.UserId == token.UserId) List<int> favouriteUsers = await this.database.HeartedProfiles.Where(hp => hp.UserId == token.UserId)

View file

@ -71,6 +71,7 @@ public static class ActivityQueryExtensions
// to build a pattern matching switch statement with expression trees. so the only other option // to build a pattern matching switch statement with expression trees. so the only other option
// is to basically rewrite this nested ternary mess with expression trees which isn't much better // is to basically rewrite this nested ternary mess with expression trees which isn't much better
// The resulting SQL generated by EntityFramework uses a CASE statement which is probably fine // The resulting SQL generated by EntityFramework uses a CASE statement which is probably fine
// TOTAL HOURS WASTED: 3
public static IQueryable<ActivityDto> ToActivityDto public static IQueryable<ActivityDto> ToActivityDto
(this IQueryable<ActivityEntity> activityQuery, bool includeSlotCreator = false, bool includeTeamPick = false) (this IQueryable<ActivityEntity> activityQuery, bool includeSlotCreator = false, bool includeTeamPick = false)
{ {
@ -79,7 +80,7 @@ public static class ActivityQueryExtensions
Activity = a, Activity = a,
TargetSlotId = a is LevelActivityEntity TargetSlotId = a is LevelActivityEntity
? ((LevelActivityEntity)a).SlotId ? ((LevelActivityEntity)a).SlotId
: a is PhotoActivityEntity && ((PhotoActivityEntity)a).Photo.PhotoId != 0 : a is PhotoActivityEntity && ((PhotoActivityEntity)a).Photo.SlotId != 0
? ((PhotoActivityEntity)a).Photo.SlotId ? ((PhotoActivityEntity)a).Photo.SlotId
: a is CommentActivityEntity && ((CommentActivityEntity)a).Comment.Type == CommentType.Level : a is CommentActivityEntity && ((CommentActivityEntity)a).Comment.Type == CommentType.Level
? ((CommentActivityEntity)a).Comment.TargetSlotId ? ((CommentActivityEntity)a).Comment.TargetSlotId
@ -87,22 +88,18 @@ public static class ActivityQueryExtensions
? ((ScoreActivityEntity)a).Score.SlotId ? ((ScoreActivityEntity)a).Score.SlotId
: a is ReviewActivityEntity : a is ReviewActivityEntity
? ((ReviewActivityEntity)a).Review.SlotId ? ((ReviewActivityEntity)a).Review.SlotId
: 0, : null,
TargetSlotGameVersion = a is LevelActivityEntity
TargetUserId = a is UserActivityEntity ? ((LevelActivityEntity)a).Slot.GameVersion
? ((UserActivityEntity)a).TargetUserId : a is PhotoActivityEntity && ((PhotoActivityEntity)a).Photo.SlotId != 0
: a is CommentActivityEntity && ((CommentActivityEntity)a).Comment.Type == CommentType.Profile ? ((PhotoActivityEntity)a).Photo.Slot.GameVersion
? ((CommentActivityEntity)a).Comment.TargetUserId : a is CommentActivityEntity && ((CommentActivityEntity)a).Comment.Type == CommentType.Level
: a is PhotoActivityEntity && ((PhotoActivityEntity)a).Photo.SlotId != 0 ? ((CommentActivityEntity)a).Comment.TargetSlot.GameVersion
? ((PhotoActivityEntity)a).Photo.CreatorId : a is ScoreActivityEntity
: 0, ? ((ScoreActivityEntity)a).Score.Slot.GameVersion
TargetPlaylistId = a is PlaylistActivityEntity || a is PlaylistWithSlotActivityEntity : a is ReviewActivityEntity
? ((PlaylistActivityEntity)a).PlaylistId ? ((ReviewActivityEntity)a).Review.Slot.GameVersion
: 0, : null,
TargetNewsId = a is NewsActivityEntity ? ((NewsActivityEntity)a).NewsId : 0,
TargetTeamPickId = includeTeamPick
? a.Type == EventType.MMPickLevel && a is LevelActivityEntity ? ((LevelActivityEntity)a).SlotId : 0
: 0,
TargetSlotCreatorId = includeSlotCreator TargetSlotCreatorId = includeSlotCreator
? a is LevelActivityEntity ? a is LevelActivityEntity
? ((LevelActivityEntity)a).Slot.CreatorId ? ((LevelActivityEntity)a).Slot.CreatorId
@ -114,8 +111,22 @@ public static class ActivityQueryExtensions
? ((ScoreActivityEntity)a).Score.Slot.CreatorId ? ((ScoreActivityEntity)a).Score.Slot.CreatorId
: a is ReviewActivityEntity : a is ReviewActivityEntity
? ((ReviewActivityEntity)a).Review.Slot!.CreatorId ? ((ReviewActivityEntity)a).Review.Slot!.CreatorId
: 0 : null
: 0, : null,
});
TargetUserId = a is UserActivityEntity
? ((UserActivityEntity)a).TargetUserId
: a is CommentActivityEntity && ((CommentActivityEntity)a).Comment.Type == CommentType.Profile
? ((CommentActivityEntity)a).Comment.TargetUserId
: a is PhotoActivityEntity && ((PhotoActivityEntity)a).Photo.SlotId != 0
? ((PhotoActivityEntity)a).Photo.CreatorId
: null,
TargetPlaylistId = a is PlaylistActivityEntity || a is PlaylistWithSlotActivityEntity
? ((PlaylistActivityEntity)a).PlaylistId
: null,
TargetNewsId = a is NewsActivityEntity ? ((NewsActivityEntity)a).NewsId : null,
TargetTeamPickId = includeTeamPick
? a.Type == EventType.MMPickLevel && a is LevelActivityEntity ? ((LevelActivityEntity)a).SlotId : null
: null, });
} }
} }

View file

@ -1,4 +1,5 @@
using LBPUnion.ProjectLighthouse.Types.Entities.Activity; using LBPUnion.ProjectLighthouse.Types.Entities.Activity;
using LBPUnion.ProjectLighthouse.Types.Users;
namespace LBPUnion.ProjectLighthouse.Types.Activity; namespace LBPUnion.ProjectLighthouse.Types.Activity;
@ -7,6 +8,7 @@ public class ActivityDto
public required ActivityEntity Activity { get; set; } public required ActivityEntity Activity { get; set; }
public int? TargetSlotId { get; set; } public int? TargetSlotId { get; set; }
public int? TargetSlotCreatorId { get; set; } public int? TargetSlotCreatorId { get; set; }
public GameVersion? TargetSlotGameVersion { get; set; }
public int? TargetUserId { get; set; } public int? TargetUserId { get; set; }
public int? TargetPlaylistId { get; set; } public int? TargetPlaylistId { get; set; }
public int? TargetNewsId { get; set; } public int? TargetNewsId { get; set; }

View file

@ -16,7 +16,6 @@ using System.Reflection;
namespace LBPUnion.ProjectLighthouse.Types.Activity; namespace LBPUnion.ProjectLighthouse.Types.Activity;
//TODO implement missing event triggers
public class ActivityEntityEventHandler : IEntityEventHandler public class ActivityEntityEventHandler : IEntityEventHandler
{ {
public void OnEntityInserted<T>(DatabaseContext database, T entity) where T : class public void OnEntityInserted<T>(DatabaseContext database, T entity) where T : class
@ -24,35 +23,56 @@ public class ActivityEntityEventHandler : IEntityEventHandler
Console.WriteLine($@"OnEntityInserted: {entity.GetType().Name}"); Console.WriteLine($@"OnEntityInserted: {entity.GetType().Name}");
ActivityEntity? activity = entity switch ActivityEntity? activity = entity switch
{ {
SlotEntity slot => new LevelActivityEntity SlotEntity slot => slot.Type switch
{ {
Type = EventType.PublishLevel, SlotType.User => new LevelActivityEntity
SlotId = slot.SlotId, {
UserId = slot.CreatorId, Type = EventType.PublishLevel,
SlotId = slot.SlotId,
UserId = slot.CreatorId,
},
_ => null,
}, },
CommentEntity comment => new CommentActivityEntity CommentEntity comment => comment.TargetSlot?.Type switch
{ {
Type = comment.Type == CommentType.Level ? EventType.CommentOnLevel : EventType.CommentOnUser, SlotType.User => new CommentActivityEntity
CommentId = comment.CommentId, {
UserId = comment.PosterUserId, Type = comment.Type == CommentType.Level ? EventType.CommentOnLevel : EventType.CommentOnUser,
CommentId = comment.CommentId,
UserId = comment.PosterUserId,
},
_ => null,
}, },
PhotoEntity photo => new PhotoActivityEntity PhotoEntity photo => photo.Slot?.Type switch
{ {
Type = EventType.UploadPhoto, SlotType.User => new PhotoActivityEntity
PhotoId = photo.PhotoId, {
UserId = photo.CreatorId, Type = EventType.UploadPhoto,
PhotoId = photo.PhotoId,
UserId = photo.CreatorId,
},
_ => null,
}, },
ScoreEntity score => new ScoreActivityEntity ScoreEntity score => score.Slot.Type switch
{ {
Type = EventType.Score, // Don't add story scores or versus scores
ScoreId = score.ScoreId, SlotType.User when score.Type != 7 => new ScoreActivityEntity
UserId = score.UserId, {
Type = EventType.Score,
ScoreId = score.ScoreId,
UserId = score.UserId,
},
_ => null,
}, },
HeartedLevelEntity heartedLevel => new LevelActivityEntity HeartedLevelEntity heartedLevel => heartedLevel.Slot.Type switch
{ {
Type = EventType.HeartLevel, SlotType.User => new LevelActivityEntity
SlotId = heartedLevel.SlotId, {
UserId = heartedLevel.UserId, Type = EventType.HeartLevel,
SlotId = heartedLevel.SlotId,
UserId = heartedLevel.UserId,
},
_ => null,
}, },
HeartedProfileEntity heartedProfile => new UserActivityEntity HeartedProfileEntity heartedProfile => new UserActivityEntity
{ {
@ -123,8 +143,7 @@ public class ActivityEntityEventHandler : IEntityEventHandler
object? origVal = propInfo.GetValue(origEntity); object? origVal = propInfo.GetValue(origEntity);
object? newVal = propInfo.GetValue(currentEntity); object? newVal = propInfo.GetValue(currentEntity);
if ((origVal == null && newVal == null) || (origVal != null && newVal != null && origVal.Equals(newVal))) if ((origVal == null && newVal == null) || (origVal != null && newVal != null && origVal.Equals(newVal))) continue;
continue;
Console.WriteLine($@"Value for {propInfo.Name} changed"); Console.WriteLine($@"Value for {propInfo.Name} changed");
Console.WriteLine($@"Orig val: {origVal?.ToString() ?? "null"}"); Console.WriteLine($@"Orig val: {origVal?.ToString() ?? "null"}");
@ -197,6 +216,14 @@ public class ActivityEntityEventHandler : IEntityEventHandler
{ {
if (origEntity is not CommentEntity oldComment) break; if (origEntity is not CommentEntity oldComment) break;
if (comment.TargetSlotId != null)
{
SlotType slotType = database.Slots.Where(s => s.SlotId == comment.TargetSlotId)
.Select(s => s.Type)
.FirstOrDefault();
if (slotType != SlotType.User) break;
}
if (oldComment.Deleted || !comment.Deleted) break; if (oldComment.Deleted || !comment.Deleted) break;
if (comment.Type != CommentType.Level) break; if (comment.Type != CommentType.Level) break;
@ -238,6 +265,7 @@ public class ActivityEntityEventHandler : IEntityEventHandler
}; };
InsertActivity(database, entity); InsertActivity(database, entity);
} }
break; break;
} }
} }
@ -250,11 +278,15 @@ public class ActivityEntityEventHandler : IEntityEventHandler
Console.WriteLine($@"OnEntityDeleted: {entity.GetType().Name}"); Console.WriteLine($@"OnEntityDeleted: {entity.GetType().Name}");
ActivityEntity? activity = entity switch ActivityEntity? activity = entity switch
{ {
HeartedLevelEntity heartedLevel => new LevelActivityEntity HeartedLevelEntity heartedLevel => heartedLevel.Slot.Type switch
{ {
Type = EventType.UnheartLevel, SlotType.User => new LevelActivityEntity
SlotId = heartedLevel.SlotId, {
UserId = heartedLevel.UserId, Type = EventType.UnheartLevel,
SlotId = heartedLevel.SlotId,
UserId = heartedLevel.UserId,
},
_ => null,
}, },
HeartedProfileEntity heartedProfile => new UserActivityEntity HeartedProfileEntity heartedProfile => new UserActivityEntity
{ {