From dcc7634f5e5128b11a39b55a401b62799892f1a3 Mon Sep 17 00:00:00 2001 From: limited_dev Date: Mon, 13 Feb 2023 18:45:14 +0100 Subject: [PATCH] feat: The posts are now sorted fix: modmenu now checks for the permission, you now have to be 18yo to access the boards, images now get deleted when deleting threads / posts, fixed grammar error in the register page, other fixed which i forget --- .../Data/Repository/UsersRepository.cs | 3 + ImageBoardServerApp/Data/TheManager.cs | 83 +++++++++++++++++++ .../Pages/Accounts/Register.razor | 2 +- .../Pages/Accounts/UserPage.razor | 1 + .../Pages/Moderation/ModMenu.razor | 2 +- .../Pages/Moderation/ReportsPage.razor | 4 +- .../Pages/Moderation/UsersPage.razor | 2 +- .../Shared/Components/Board.razor | 66 ++++++++------- .../Shared/Components/Board.razor.css | 5 +- .../Shared/Components/Comment.razor | 10 ++- .../Shared/Components/Post.razor | 12 ++- .../Shared/Components/Report.razor | 47 ----------- .../Shared/Components/ReportEntry.razor | 70 ++++++++++++++++ 13 files changed, 219 insertions(+), 88 deletions(-) create mode 100644 ImageBoardServerApp/Data/TheManager.cs delete mode 100644 ImageBoardServerApp/Shared/Components/Report.razor create mode 100644 ImageBoardServerApp/Shared/Components/ReportEntry.razor diff --git a/ImageBoardServerApp/Data/Repository/UsersRepository.cs b/ImageBoardServerApp/Data/Repository/UsersRepository.cs index 89fb13b..77750b1 100644 --- a/ImageBoardServerApp/Data/Repository/UsersRepository.cs +++ b/ImageBoardServerApp/Data/Repository/UsersRepository.cs @@ -27,6 +27,9 @@ public static class UsersRepository return await db.Users .Where(user => user.Email == email) .Include(user => user.SubmittedReports) + .Include(user => user.Posts) + .Include(user => user.Comments) + .Include(user => user.RecivedReports) .FirstOrDefaultAsync(); } diff --git a/ImageBoardServerApp/Data/TheManager.cs b/ImageBoardServerApp/Data/TheManager.cs new file mode 100644 index 0000000..b901ce5 --- /dev/null +++ b/ImageBoardServerApp/Data/TheManager.cs @@ -0,0 +1,83 @@ +using ImageBoardServerApp.Data.Repository; + +namespace ImageBoardServerApp.Data; + +public class TheManager +{ + + private static long getDiff(PostData post) + { + return (DateTimeOffset.Now.ToUnixTimeMilliseconds() - post.CreatedAt); + } + + private static long getValue(long num) + { + return num < 0 ? num * -1 : num; + } + + public static long getBumpValue(PostData post) + { + return getValue( 10 * 60000 - getDiff(post)) + getDiff(post) / post.Comments.Count; + } + + public static async Task> getPostList(string boardTag) + { + List threads = await PostsRepository.getPostsByBoardAsync(boardTag); + return threads.OrderBy(getBumpValue).ToList(); + } + + public static async Task bumpThreads(BoardData board) + { + List sortedThreads = await getPostList(board.Tag); + if (sortedThreads.Count > board.maxThreads + 1) + { + for (int i = board.maxThreads + 1; i >= sortedThreads.Count; ++i) + { + await deleteThread(sortedThreads[i]); + } + } + } + + public static async Task deleteThread(PostData post) + { + foreach(var c in post.Comments) + { + if (c.Image != null) + { + deleteImage(c.Image); + } + } + + deleteImage(post.Image); + + await PostsRepository.deletePostAsync(post.PostID); + } + + public static async Task deleteComment(CommentData comment) + { + if (comment.Image != null) + { + deleteImage(comment.Image); + } + + await CommentsRepository.deleteCommentAsync(comment.CommentID); + } + + public static void deleteImage(ImageData imageData) + { + string path = $"./wwwroot{imageData.ImageLocation}"; + + try + { + File.Delete(path); + } + catch (FileNotFoundException) + { + Console.WriteLine("The file was not found."); + } + catch (Exception e) + { + Console.WriteLine("An error occurred: " + e.Message); + } + } +} \ No newline at end of file diff --git a/ImageBoardServerApp/Pages/Accounts/Register.razor b/ImageBoardServerApp/Pages/Accounts/Register.razor index b84c7a5..a5b1fd6 100644 --- a/ImageBoardServerApp/Pages/Accounts/Register.razor +++ b/ImageBoardServerApp/Pages/Accounts/Register.razor @@ -18,8 +18,8 @@ [Register] - +By registering you confirm that you are atleast 18 years of age. @code { private string Email { get; set; } private string Password { get; set; } diff --git a/ImageBoardServerApp/Pages/Accounts/UserPage.razor b/ImageBoardServerApp/Pages/Accounts/UserPage.razor index 3fc3eea..1763235 100644 --- a/ImageBoardServerApp/Pages/Accounts/UserPage.razor +++ b/ImageBoardServerApp/Pages/Accounts/UserPage.razor @@ -25,6 +25,7 @@ Please login first. + @code { private string mail { get; set; } = ""; diff --git a/ImageBoardServerApp/Pages/Moderation/ModMenu.razor b/ImageBoardServerApp/Pages/Moderation/ModMenu.razor index ad3edd2..4c61898 100644 --- a/ImageBoardServerApp/Pages/Moderation/ModMenu.razor +++ b/ImageBoardServerApp/Pages/Moderation/ModMenu.razor @@ -3,7 +3,7 @@ @inject AuthenticationStateProvider authStateProvider @inject NavigationManager navManager - +

ModMenu

Welcome @mail to the mod menu diff --git a/ImageBoardServerApp/Pages/Moderation/ReportsPage.razor b/ImageBoardServerApp/Pages/Moderation/ReportsPage.razor index 7c68791..67a05e5 100644 --- a/ImageBoardServerApp/Pages/Moderation/ReportsPage.razor +++ b/ImageBoardServerApp/Pages/Moderation/ReportsPage.razor @@ -1,11 +1,11 @@ @page "/sys/reports" @using ImageBoardServerApp.Data.Repository - +

Reports

@foreach (var r in reports) { - +
}
diff --git a/ImageBoardServerApp/Pages/Moderation/UsersPage.razor b/ImageBoardServerApp/Pages/Moderation/UsersPage.razor index afdad0d..de04c13 100644 --- a/ImageBoardServerApp/Pages/Moderation/UsersPage.razor +++ b/ImageBoardServerApp/Pages/Moderation/UsersPage.razor @@ -1,6 +1,6 @@ @page "/sys/users" @using ImageBoardServerApp.Data.Repository - +

Users

@foreach (var u in users) diff --git a/ImageBoardServerApp/Shared/Components/Board.razor b/ImageBoardServerApp/Shared/Components/Board.razor index 3da6734..0074473 100644 --- a/ImageBoardServerApp/Shared/Components/Board.razor +++ b/ImageBoardServerApp/Shared/Components/Board.razor @@ -4,35 +4,45 @@ /@board.Tag/ - @board.Topic - BulletBoard -
-

@board.Topic Board

- - -
- -
-
- - -

Threads

-
-@if (posts != null) -{ - @if (posts.Any()) - { - @foreach(var post in posts) + + +
+

@board.Topic Board

+ + +
+ +
+
+ + +

Threads

+
+ @if (posts != null) { - -
+ @if (posts.Any()) + { + @foreach(var post in posts) + { + +
+ } + } } - } -} -else -{ -

- Loading Posts... -

-} + else + { +

+ Loading Posts... +

+ } +
+ +
+ You are not logged in. + Please login first. +
+
+
@code { @@ -40,7 +50,7 @@ else protected override async Task OnInitializedAsync() { - posts = await PostsRepository.getPostsByBoardAsync(board.Tag); + posts = await TheManager.getPostList(board.Tag); } [Parameter] diff --git a/ImageBoardServerApp/Shared/Components/Board.razor.css b/ImageBoardServerApp/Shared/Components/Board.razor.css index 62dbafd..d0432ac 100644 --- a/ImageBoardServerApp/Shared/Components/Board.razor.css +++ b/ImageBoardServerApp/Shared/Components/Board.razor.css @@ -1,4 +1,7 @@ .boardHeader{ text-align: center; - +} + +.notLoggedIn{ + text-align: center; } \ No newline at end of file diff --git a/ImageBoardServerApp/Shared/Components/Comment.razor b/ImageBoardServerApp/Shared/Components/Comment.razor index 34b85cf..1c64be9 100644 --- a/ImageBoardServerApp/Shared/Components/Comment.razor +++ b/ImageBoardServerApp/Shared/Components/Comment.razor @@ -9,9 +9,9 @@ @toggleText ] @comment.Username - @if (@comment.User.Role != "User") + @if (@role != "User") { - ##@comment.User.Role + ##@role } @getTimeFromUnix(comment.CreatedAt) No.@comment.CommentID @@ -61,7 +61,7 @@ UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name); if (foundusr.PermissionInteger >= 50 || comment.UserID == foundusr.UserID) { - await CommentsRepository.deleteCommentAsync(comment.CommentID); + await TheManager.deleteComment(comment); } } @@ -74,6 +74,7 @@ } private ImageData image; + private string role; protected override async Task OnInitializedAsync() { @@ -90,6 +91,9 @@ { image = await ImagesRepository.getImageByIdAsync(i); } + + var cmt = await CommentsRepository.getCommentByIdAsync(comment.CommentID); + role = cmt.User.Role; } private bool opened = true; diff --git a/ImageBoardServerApp/Shared/Components/Post.razor b/ImageBoardServerApp/Shared/Components/Post.razor index cfe73fe..7d1613e 100644 --- a/ImageBoardServerApp/Shared/Components/Post.razor +++ b/ImageBoardServerApp/Shared/Components/Post.razor @@ -3,6 +3,7 @@ @using ImageBoardServerApp.Data @using ImageBoardServerApp.Data.Repository @inject AuthenticationStateProvider authStateProvider +@inject NavigationManager navigationManager;
@if (showOpenThread) @@ -12,12 +13,13 @@ ] } @post.Title - @post.Username + @post.Username @if (post.User.Role != "User") { - ##@post.User.Role + ##@post.User.Role } - @getTimeFromUnix(post.CreatedAt) + + @getTimeFromUnix(post.CreatedAt) No.@post.PostID
@if (opened) @@ -81,13 +83,15 @@ private async Task deletePost() { + string boardTag = post.Board; var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider; var user = await cauthStateProvder.GetAuthenticationStateAsync(); var usr = user.User; UserData foundusr = await UsersRepository.getUserByEmailAsync(usr.Identity.Name); if (foundusr.PermissionInteger >= 50 || post.UserID == foundusr.UserID) { - await PostsRepository.deletePostAsync(post.PostID); + await TheManager.deleteThread(post); + navigationManager.NavigateTo($"/{boardTag}"); } } diff --git a/ImageBoardServerApp/Shared/Components/Report.razor b/ImageBoardServerApp/Shared/Components/Report.razor deleted file mode 100644 index 8c2dbd4..0000000 --- a/ImageBoardServerApp/Shared/Components/Report.razor +++ /dev/null @@ -1,47 +0,0 @@ -@using System.ComponentModel.DataAnnotations -
- @if (report.Type == "op") - { - /@report.ReportedPost.Board/ - } - else - { - /@report.ReportedComment.Board/ - } - #@report.ReportID - @report.Type -
- Reason: @report.ReportReason -
- Explaination: @report.ReportExlaination -
- Ban Reported User -
- Ban Reporter -
- @if (report.Type == "op") - { - - } - else - { - - } -
- -@code { - [Parameter] - [Required] - public ReportData report { get; set; } - - - private async Task banTarget() - { - - } - - private async Task banReporter() - { - - } -} \ No newline at end of file diff --git a/ImageBoardServerApp/Shared/Components/ReportEntry.razor b/ImageBoardServerApp/Shared/Components/ReportEntry.razor new file mode 100644 index 0000000..57d1d53 --- /dev/null +++ b/ImageBoardServerApp/Shared/Components/ReportEntry.razor @@ -0,0 +1,70 @@ +@using System.ComponentModel.DataAnnotations +@using ImageBoardServerApp.Data.Repository +
+ @if (report.Type == "op") + { + /@report.ReportedPost.Board/ + } + else + { + /@report.ReportedComment.Board/ + } + #@report.ReportID + @report.Type +
+ Reason: @report.ReportReason +
+ Explaination: @report.ReportExlaination +
+ Ban User: + 1 Tag + + 7 Tage + + 31 Tage + + 365 Tage + + 99M Tage +
+ Ban Reporter: + 1 Tag + + 7 Tage + + 31 Tage + + 365 Tage + + 99M Tage +
+ @if (report.Type == "op") + { + + } + else + { + + } +
+ +@code { + [Parameter] + [Required] + public ReportData report { get; set; } + + + private async Task banTarget(int days) + { + UserData user = report.UserReported; + user.TimeBanned = DateTimeOffset.Now.AddDays(days).ToUnixTimeMilliseconds(); + await UsersRepository.updateUserAsync(user); + } + + private async Task banReporter(int days) + { + UserData user = report.UserReporter; + user.TimeBanned = DateTimeOffset.Now.AddDays(days).ToUnixTimeMilliseconds(); + await UsersRepository.updateUserAsync(user); + } +} \ No newline at end of file