diff --git a/Moonlight/App/Services/Community/PostService.cs b/Moonlight/App/Services/Community/PostService.cs index aed5986f..24378ff4 100644 --- a/Moonlight/App/Services/Community/PostService.cs +++ b/Moonlight/App/Services/Community/PostService.cs @@ -45,6 +45,7 @@ public class PostService { post.Title = title; post.Content = content; + post.UpdatedAt = DateTime.UtcNow; PostRepository.Update(post); @@ -53,6 +54,30 @@ public class PostService public async Task Delete(Post post) { + var postWithData = PostRepository + .Get() + .Include(x => x.Comments) + .Include(x => x.Likes) + .First(x => x.Id == post.Id); + + // Cache relational data to delete later on + var likes = postWithData.Likes.ToArray(); + var comments = postWithData.Comments.ToArray(); + + // Clear relations + postWithData.Comments.Clear(); + postWithData.Likes.Clear(); + + PostRepository.Update(postWithData); + + // Delete relational data + foreach (var like in likes) + PostLikeRepository.Delete(like); + + foreach (var comment in comments) + PostCommentRepository.Delete(comment); + + // Now delete the post itself PostRepository.Delete(post); await Events.OnPostDeleted.InvokeAsync(post); } @@ -67,7 +92,7 @@ public class PostService if (content.Length > 1024) throw new DisplayException("Comment content cannot be longer than 1024 characters"); - if (!Regex.IsMatch(content, "^[a-zA-Z0-9äöüßÄÖÜẞ,.;_\\n\\t-]+$")) + if (!Regex.IsMatch(content, "^[ a-zA-Z0-9äöüßÄÖÜẞ,.;_\\n\\t-]+$")) throw new DisplayException("Illegal characters in comment content"); //TODO: Swear word filter diff --git a/Moonlight/Shared/Components/Community/PostView.razor b/Moonlight/Shared/Components/Community/PostView.razor index b428f2b3..c0043432 100644 --- a/Moonlight/Shared/Components/Community/PostView.razor +++ b/Moonlight/Shared/Components/Community/PostView.razor @@ -1,6 +1,7 @@ @using Moonlight.App.Database.Entities.Community @using Ganss.Xss @using Microsoft.EntityFrameworkCore +@using Moonlight.App.Models.Enums @using Moonlight.App.Repositories @using Moonlight.App.Services @using Moonlight.App.Services.Community @@ -8,6 +9,7 @@ @inject Repository PostRepository @inject IdentityService IdentityService @inject PostService PostService +@inject ToastService ToastService
@@ -16,19 +18,29 @@
- @(Post.Author.Username) + @(Post.Author.Username) @(Formatter.FormatAgoFromDateTime(Post.CreatedAt))
-
-
+ @if (Post.Author.Id == IdentityService.CurrentUser.Id || IdentityService.Permissions[Permission.AdminCommunity]) + { +
+ Remove post +
+ }
- @{ + @if (IsEditing) + { + + } + else + { var sanitizer = new HtmlSanitizer(); var content = sanitizer.Sanitize(Post.Content); + @((MarkupString)content) }
@@ -49,6 +61,25 @@ @(LikesCount) Like(s) + @if (Post.Author.Id == IdentityService.CurrentUser.Id || IdentityService.Permissions[Permission.AdminCommunity]) + { + + }
@if (ShowComments) @@ -66,6 +97,11 @@
@(comment.Author.Username) @(Formatter.FormatAgoFromDateTime(comment.CreatedAt)) + + @if (comment.Author.Id == IdentityService.CurrentUser.Id || IdentityService.Permissions[Permission.AdminCommunity]) + { + Remove comment + }
@(Formatter.FormatLineBreaks(comment.Content)) @@ -90,8 +126,8 @@
- - + +
@@ -103,6 +139,9 @@ [Parameter] public Post Post { get; set; } + [Parameter] + public Func? OnUpdate { get; set; } + private int CommentsCount = -1; private int LikesCount = -1; private bool HasLiked = false; @@ -111,6 +150,10 @@ private PostComment[] Comments = Array.Empty(); private string Comment = ""; + private bool IsEditing = false; + private string EditTitle = ""; + private string EditContent = ""; + protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) @@ -118,7 +161,7 @@ await UpdateCounts(); } } - + private Task LoadComments(LazyLoader _) { Comments = PostRepository @@ -161,6 +204,7 @@ Comment = ""; ShowComments = true; + await LoadComments(null!); await InvokeAsync(StateHasChanged); await UpdateCounts(); } @@ -168,9 +212,12 @@ private async Task DeleteComment(PostComment comment) { await PostService.DeleteComment(Post, comment); - + + await LoadComments(null!); await InvokeAsync(StateHasChanged); await UpdateCounts(); + + await ToastService.Success("Successfully deleted comment"); } private async Task ToggleComments() @@ -187,4 +234,31 @@ await PostService.ToggleLike(Post, IdentityService.CurrentUser); await UpdateCounts(); } + + private async Task ToggleEdit(bool preventSaving = false) + { + IsEditing = !IsEditing; + + if (IsEditing) + { + EditTitle = Post.Title; + EditContent = Post.Content; + } + else if (!preventSaving) + { + await PostService.Update(Post, EditTitle, EditContent); + await ToastService.Success("Successfully saved post"); + } + + await InvokeAsync(StateHasChanged); + } + + private async Task DeletePost() + { + await PostService.Delete(Post); + await ToastService.Success("Successfully deleted post"); + + if (OnUpdate != null) + await OnUpdate.Invoke(); + } } \ No newline at end of file diff --git a/Moonlight/Shared/Views/Community/Index.razor b/Moonlight/Shared/Views/Community/Index.razor index a577f0db..65ee3da7 100644 --- a/Moonlight/Shared/Views/Community/Index.razor +++ b/Moonlight/Shared/Views/Community/Index.razor @@ -8,10 +8,10 @@ @inject Repository PostRepository
- + @foreach (var post in Posts) { - +
}
@@ -19,6 +19,7 @@ @code { + private LazyLoader LazyLoader; private Post[] Posts; private Task Load(LazyLoader _)