From bd21b8f9ed4f78c6200df0743f0d979bb6457429 Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 23 Dec 2021 02:06:23 -0500 Subject: [PATCH 01/10] Dont allow duplicate photo subjects on photo, dont allow more than 4 photo subjects on photo --- .../Controllers/PhotosController.cs | 16 ++++++++++++ .../CleanupBrokenPhotosMaintenanceJob.cs | 25 +++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/ProjectLighthouse/Controllers/PhotosController.cs b/ProjectLighthouse/Controllers/PhotosController.cs index 59c5df91..5e3de091 100644 --- a/ProjectLighthouse/Controllers/PhotosController.cs +++ b/ProjectLighthouse/Controllers/PhotosController.cs @@ -53,6 +53,11 @@ namespace LBPUnion.ProjectLighthouse.Controllers photo.CreatorId = user.UserId; photo.Creator = user; + if (photo.Subjects.Count > 4) + { + return this.BadRequest(); + } + foreach (PhotoSubject subject in photo.Subjects) { subject.User = await this.database.Users.FirstOrDefaultAsync(u => u.Username == subject.Username); @@ -67,6 +72,17 @@ namespace LBPUnion.ProjectLighthouse.Controllers await this.database.SaveChangesAsync(); + // Check for duplicate photo subjects + List subjectUserIds = new(4); + foreach (PhotoSubject subject in photo.Subjects) + { + if (subjectUserIds.Contains(subject.UserId)) + { + return this.BadRequest(); + } + subjectUserIds.Add(subject.UserId); + } + photo.PhotoSubjectIds = photo.Subjects.Select(subject => subject.PhotoSubjectId.ToString()).ToArray(); // photo.Slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == photo.SlotId); diff --git a/ProjectLighthouse/Maintenance/MaintenanceJobs/CleanupBrokenPhotosMaintenanceJob.cs b/ProjectLighthouse/Maintenance/MaintenanceJobs/CleanupBrokenPhotosMaintenanceJob.cs index 78d45fde..252ee93f 100644 --- a/ProjectLighthouse/Maintenance/MaintenanceJobs/CleanupBrokenPhotosMaintenanceJob.cs +++ b/ProjectLighthouse/Maintenance/MaintenanceJobs/CleanupBrokenPhotosMaintenanceJob.cs @@ -13,7 +13,7 @@ namespace LBPUnion.ProjectLighthouse.Maintenance.MaintenanceJobs { private readonly Database database = new(); public string Name() => "Cleanup Broken Photos"; - public string Description() => "Deletes all photos that have missing assets."; + public string Description() => "Deletes all photos that have missing assets or invalid photo subjects."; [SuppressMessage("ReSharper", "LoopCanBePartlyConvertedToQuery")] public async Task Run() @@ -23,6 +23,8 @@ namespace LBPUnion.ProjectLighthouse.Maintenance.MaintenanceJobs bool hashNullOrEmpty = false; bool noHashesExist = false; bool largeHashIsInvalidFile = false; + bool tooManyPhotoSubjects = false; + bool duplicatePhotoSubjects = false; hashNullOrEmpty = string.IsNullOrEmpty (photo.LargeHash) || @@ -50,6 +52,23 @@ namespace LBPUnion.ProjectLighthouse.Maintenance.MaintenanceJobs goto removePhoto; } + if (photo.Subjects.Count > 4) + { + tooManyPhotoSubjects = true; + goto removePhoto; + } + + List subjectUserIds = new(4); + foreach (PhotoSubject subject in photo.Subjects) + { + if (subjectUserIds.Contains(subject.UserId)) + { + duplicatePhotoSubjects = true; + goto removePhoto; + } + subjectUserIds.Add(subject.UserId); + } + continue; removePhoto: @@ -59,7 +78,9 @@ namespace LBPUnion.ProjectLighthouse.Maintenance.MaintenanceJobs $"Removing photo (id: {photo.PhotoId}): " + $"{nameof(hashNullOrEmpty)}: {hashNullOrEmpty}, " + $"{nameof(noHashesExist)}: {noHashesExist}, " + - $"{nameof(largeHashIsInvalidFile)}: {largeHashIsInvalidFile}" + $"{nameof(largeHashIsInvalidFile)}: {largeHashIsInvalidFile}, " + + $"{nameof(tooManyPhotoSubjects)}: {tooManyPhotoSubjects}" + + $"{nameof(duplicatePhotoSubjects)}: {duplicatePhotoSubjects}" ); this.database.Photos.Remove(photo); From d3c1f2f0357d6e7f2f824f82908d03fbb7511653 Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 23 Dec 2021 19:25:11 -0500 Subject: [PATCH 02/10] Add meta tags to BaseLayout --- ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml | 14 +++++++++++--- .../Pages/Layouts/BaseLayout.cshtml.cs | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml index d4e14ade..5395e53f 100644 --- a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml +++ b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml @@ -48,10 +48,18 @@ - - - + + + @* Embed Stuff *@ + + + @if (!string.IsNullOrEmpty(Model.Description)) + { + + } + + @* Google Analytics *@ @if (ServerSettings.Instance.GoogleAnalyticsEnabled) { diff --git a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml.cs b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml.cs index f7f14670..9f921154 100644 --- a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml.cs +++ b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml.cs @@ -21,6 +21,7 @@ namespace LBPUnion.ProjectLighthouse.Pages.Layouts public bool ShowTitleInPage = true; public string Title = string.Empty; + public string Description = string.Empty; private User? user; From 6817c3089eb1736f292257bfb674e762822f3162 Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 23 Dec 2021 19:28:07 -0500 Subject: [PATCH 03/10] Add descriptions to slot and user pages --- ProjectLighthouse/Pages/SlotPage.cshtml | 4 +++- ProjectLighthouse/Pages/UserPage.cshtml | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ProjectLighthouse/Pages/SlotPage.cshtml b/ProjectLighthouse/Pages/SlotPage.cshtml index 221d99f2..f9de969d 100644 --- a/ProjectLighthouse/Pages/SlotPage.cshtml +++ b/ProjectLighthouse/Pages/SlotPage.cshtml @@ -3,8 +3,10 @@ @{ Layout = "Layouts/BaseLayout"; - Model.Title = Model.Slot.Name; Model.ShowTitleInPage = false; + + Model.Title = Model.Slot.Name; + Model.Description = Model.Slot.Description; } @await Html.PartialAsync("Partials/SlotCardPartial", Model.Slot, new ViewDataDictionary(ViewData) diff --git a/ProjectLighthouse/Pages/UserPage.cshtml b/ProjectLighthouse/Pages/UserPage.cshtml index 2ad6916e..6f89c86c 100644 --- a/ProjectLighthouse/Pages/UserPage.cshtml +++ b/ProjectLighthouse/Pages/UserPage.cshtml @@ -8,8 +8,10 @@ @{ Layout = "Layouts/BaseLayout"; - Model.Title = Model.ProfileUser!.Username + "'s user page"; Model.ShowTitleInPage = false; + + Model.Title = Model.ProfileUser!.Username + "'s user page"; + Model.Description = Model.ProfileUser!.Biography; }
From 024d5eff9045384aaf8f0d553f084bfbd1813d6b Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 23 Dec 2021 20:57:30 -0500 Subject: [PATCH 04/10] Add ability to determine if a request originated from a mobile device --- .../Helpers/Extensions/RequestExtensions.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 ProjectLighthouse/Helpers/Extensions/RequestExtensions.cs diff --git a/ProjectLighthouse/Helpers/Extensions/RequestExtensions.cs b/ProjectLighthouse/Helpers/Extensions/RequestExtensions.cs new file mode 100644 index 00000000..a31b359a --- /dev/null +++ b/ProjectLighthouse/Helpers/Extensions/RequestExtensions.cs @@ -0,0 +1,15 @@ +using System.Text.RegularExpressions; +using Microsoft.AspNetCore.Http; +using Microsoft.Net.Http.Headers; + +namespace LBPUnion.ProjectLighthouse.Helpers.Extensions +{ + // yoinked and adapted from https://stackoverflow.com/a/68641796 + public static class RequestExtensions + { + private static readonly Regex mobileCheck = new + ("Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled); + + public static bool IsMobile(this HttpRequest request) => mobileCheck.IsMatch(request.Headers[HeaderNames.UserAgent].ToString()); + } +} \ No newline at end of file From 2cdd094e009b0573752b568da59b7ff4b52c1c9c Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 23 Dec 2021 20:57:49 -0500 Subject: [PATCH 05/10] Add debug info section to webpage --- ProjectLighthouse/Helpers/TimestampHelper.cs | 2 + .../Pages/Layouts/BaseLayout.cshtml | 42 +++++++++++++++++++ .../Pages/Layouts/BaseLayout.cshtml.cs | 12 +++--- .../Types/Settings/ServerStatics.cs | 6 +++ 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/ProjectLighthouse/Helpers/TimestampHelper.cs b/ProjectLighthouse/Helpers/TimestampHelper.cs index 3a3e9d48..456f33ab 100644 --- a/ProjectLighthouse/Helpers/TimestampHelper.cs +++ b/ProjectLighthouse/Helpers/TimestampHelper.cs @@ -5,5 +5,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers public static class TimestampHelper { public static long Timestamp => (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; + + public static long TimestampMillis => (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds; } } \ No newline at end of file diff --git a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml index 5395e53f..d1bd9f16 100644 --- a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml +++ b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml @@ -1,4 +1,5 @@ @using LBPUnion.ProjectLighthouse.Helpers +@using LBPUnion.ProjectLighthouse.Helpers.Extensions @using LBPUnion.ProjectLighthouse.Types @using LBPUnion.ProjectLighthouse.Types.Settings @model LBPUnion.ProjectLighthouse.Pages.Layouts.BaseLayout @@ -26,6 +27,9 @@ } Model.NavigationItemsRight.Add(new PageNavigationItem("Log out", "/logout", "user alternate slash")); // should always be last } + + Model.IsMobile = Model.Request.IsMobile(); + long timeStarted = TimestampHelper.TimestampMillis; } @@ -151,6 +155,44 @@ }
+ @if (ServerStatics.IsDebug) + { +
+
+ + +
+

Model.IsMobile: @Model.IsMobile

+

Model.Title: @Model.Title

+

Model.Description: @Model.Description

+

Render time: ~@(TimestampHelper.TimestampMillis - timeStarted)ms

+
+
+
+ + + } diff --git a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml.cs b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml.cs index 9f921154..f71c2bb3 100644 --- a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml.cs +++ b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml.cs @@ -7,6 +7,13 @@ namespace LBPUnion.ProjectLighthouse.Pages.Layouts { public class BaseLayout : PageModel { + public BaseLayout(Database database) + { + this.Database = database; + } + + public bool IsMobile; + public readonly Database Database; public readonly List NavigationItems = new() @@ -25,11 +32,6 @@ namespace LBPUnion.ProjectLighthouse.Pages.Layouts private User? user; - public BaseLayout(Database database) - { - this.Database = database; - } - public new User? User { get { if (this.user != null) return this.user; diff --git a/ProjectLighthouse/Types/Settings/ServerStatics.cs b/ProjectLighthouse/Types/Settings/ServerStatics.cs index cea385e4..12af421e 100644 --- a/ProjectLighthouse/Types/Settings/ServerStatics.cs +++ b/ProjectLighthouse/Types/Settings/ServerStatics.cs @@ -27,5 +27,11 @@ namespace LBPUnion.ProjectLighthouse.Types.Settings } public static bool IsUnitTesting => AppDomain.CurrentDomain.GetAssemblies().Any(assembly => assembly.FullName.StartsWith("xunit")); + + #if DEBUG + public static readonly bool IsDebug = true; + #else + public static readonly bool IsDebug = false; + #endif } } \ No newline at end of file From cb2f5cf1fc66eeecc3a9fd85c44e3af60d180326 Mon Sep 17 00:00:00 2001 From: jvyden Date: Thu, 23 Dec 2021 21:12:28 -0500 Subject: [PATCH 06/10] Improve mobile support --- .../Pages/Layouts/BaseLayout.cshtml | 25 +++++++++++++++---- ProjectLighthouse/StaticFiles/css/styles.css | 3 +++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml index d1bd9f16..ee35c549 100644 --- a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml +++ b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml @@ -63,6 +63,8 @@ } + + @* Google Analytics *@ @if (ServerSettings.Instance.GoogleAnalyticsEnabled) { @@ -93,14 +95,21 @@