bulletboards/ImageBoardServerApp/Shared/Components/Forms/CommentForm.razor
2023-06-13 08:51:53 +02:00

209 lines
No EOL
6.9 KiB
Text

@using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Auth
@using ImageBoardServerApp.Data.Repository
@inject NavigationManager navigationManager
@inject IWebHostEnvironment env
@inject AuthenticationStateProvider authStateProvider
<div class="toggler">
<span>[</span>
<button onclick="@ToggleOpened">@toggleText</button>
<span>]</span>
</div>
@if (opened)
{
<div class="pd centered">
<span>Comment on @post.Title in /@post.Board/</span>
<div class="centered formContent">
<div>
<div class="pd centered marg">
<RadzenTextBox Placeholder="Username (Anonymous)" MaxLength="15" @bind-Value="@postUsername" Class="w-100"/>
</div>
<div class="pd centered marg">
<RadzenTextArea Placeholder="Comment..." @bind-Value="@postContent" Cols="30" Rows="6" Class="w-100"/>
</div>
<AuthorizeView Roles="Admin,Mod">
<Authorized>
<RadzenCheckBox @bind-Value=@postAnon Name="postAsAnon"/>
<RadzenLabel Text="Do not show role." Component="postAsAnon"/>
</Authorized>
</AuthorizeView>
</div>
</div>
@if (hasErr)
{
<span class="postError">@postErr</span>
}
<div class="pd centered marg">
<FormInfo/>
<InputFile OnChange="@SingleUpload" type="file" accept="image/*"/>
<RadzenButton class="pd" Click="@onPostClick" Text="Post!"></RadzenButton>
</div>
</div>
}
@code {
private bool opened = false;
private string toggleText = "Open Comment Formula";
private void ToggleOpened()
{
opened = !opened;
toggleText = opened ? "Close Comment Formula" : "Open Comment Formula";
}
[Parameter]
[Required]
public PostData post { get; set; }
string postUsername { get; set; }
string postContent { get; set; } = "";
bool postAnon { get; set; } = false;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailRawAsync(usr.Identity.Name);
if (foundusr == null)
{
hasErr = true;
postErr = "You are not logged in.";
return;
}
postUsername = foundusr.LastUsedName;
await base.OnAfterRenderAsync(firstRender);
}
private IBrowserFile selectedFile;
private async Task SingleUpload(InputFileChangeEventArgs e)
{
selectedFile = e.GetMultipleFiles()[0];
this.StateHasChanged();
}
string postErr { get; set; }
bool hasErr { get; set; } = false;
private async Task onPostClick()
{
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
var usr = user.User;
UserData foundusr = await UsersRepository.getUserByEmailRawAsync(usr.Identity.Name);
if (foundusr == null)
{
hasErr = true;
postErr = "You are not logged in.";
return;
}
int userID = foundusr.UserID;
if (DateTimeOffset.Now.ToUnixTimeMilliseconds() - foundusr.TimeBanned < 0)
{
foundusr.TimeBanned = -1;
}
if (foundusr.TimeBanned != -1)
{
hasErr = true;
postErr = "You are banned and may not comment.";
//Maybe redirect to /banned?
return;
}
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
if (postUsername == null || postUsername == "")
{
postUsername = "Anonymous";
}
foundusr.LastUsedName = postUsername;
await UsersRepository.updateUserAsync(foundusr);
PostData updatedPost = await PostsRepository.getPostByIdAsync(post.PostID);
if (updatedPost.IsLocked)
{
hasErr = true;
postErr = "This Post is locked.";
return;
}
BoardData b = await BoardsRepository.getBoardByTagAsync(post.Board);
if (b.isLocked)
{
hasErr = true;
postErr = "This board is currently locked.";
return;
}
bool hasImage = selectedFile != null;
int thisGET = b.NumberOfGETs + 1;
++b.NumberOfGETs;
await BoardsRepository.updateBoardAsync(b);
CommentData commentToCreate;
if (hasImage)
{
Stream stream = selectedFile.OpenReadStream(maxAllowedSize: 512000 * 2 * 10); // max 10MB
var file = Path.GetRandomFileName() + "." + selectedFile.Name.Split(".")[selectedFile.Name.Split(".").Length - 1];
var path = $"{env.WebRootPath}/img/dynamic/comment/{@post.Board}/{@file}";
FileStream fs = File.Create(path);
await stream.CopyToAsync(fs);
stream.Close();
fs.Close();
var imageToUpload = new ImageData
{
Board = post.Board,
ImageLocation = $"/img/dynamic/comment/{@post.Board}/{@file}"
};
int imageID = await ImagesRepository.createImageAsync(imageToUpload);
commentToCreate = new CommentData()
{
PostID = post.PostID,
UserID = userID,
ImageID = imageID,
Content = postContent,
Username = postUsername,
Board = post.Board,
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
GET = thisGET,
shouldAnon = postAnon
};
}
else
{
commentToCreate = new CommentData()
{
PostID = post.PostID,
UserID = userID,
Content = postContent,
Username = postUsername,
Board = post.Board,
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
GET = thisGET,
shouldAnon = postAnon
};
}
int commentId = await CommentsRepository.createCommentAsync(commentToCreate);
if (commentId == -1)
{
//Open comment unsucessfull
navigationManager.NavigateTo("/sys/UnSuccessfulPost");
hasErr = true;
postErr = "There was an error and the comment could not be created. Please notify the admin.";
Console.WriteLine("Shit sucks and did not work.");
return;
}
//comment successfull
Console.WriteLine("Post created");
navigationManager.NavigateTo($"/{post.Board}/thread/{post.PostID}", true, true);
opened = false;
}
}