bulletboards/ImageBoardServerApp/Shared/Components/Forms/PostForm.razor

216 lines
7.1 KiB
Text
Raw Permalink Normal View History

@using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Auth
2023-01-18 12:56:24 +00:00
@using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Util
2023-01-18 12:56:24 +00:00
@inject NavigationManager NavigationManager
2023-01-27 22:50:12 +00:00
@inject IWebHostEnvironment env
@inject AuthenticationStateProvider authStateProvider
2023-01-18 12:56:24 +00:00
<div class="toggler">
2023-01-18 12:56:24 +00:00
<span>[</span>
<button onclick="@ToggleOpened">@toggleText</button>
2023-01-18 12:56:24 +00:00
<span>]</span>
</div>
@if (opened)
{
<div class="pd centered">
<span>Post to /@board.Tag/ - @board.Topic</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">
<RadzenTextBox Placeholder="Title" MaxLength="128" @bind-Value="@postTitle" Class="w-100"/>
</div>
<div class="pd centered marg">
<RadzenTextArea Placeholder="Content..." @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>
2023-01-18 12:56:24 +00:00
</div>
@if (hasErr)
{
<span class="postError">@postErr</span>
}
<div class="pd centered marg">
<FormInfo/>
<InputFile OnChange="@SingleUpload" type="file" accept="image/*"/>
2023-01-18 12:56:24 +00:00
<RadzenButton class="pd" Click="@onPostClick" Text="Post!"></RadzenButton>
</div>
2023-01-18 12:56:24 +00:00
</div>
}
@code {
private bool opened = false;
private string toggleText = "Open Post Formula";
private void ToggleOpened()
{
opened = !opened;
toggleText = opened ? "Close Post Formula" : "Open Post Formula";
}
[Parameter]
[Required]
public BoardData board { get; set; } = new BoardData();
string postUsername { get; set; }
string postTitle { 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;
if (!foundusr.ConfirmedEmail)
{
hasErr = true;
postErr = "You cannot post without an verified email.";
return;
}
await base.OnAfterRenderAsync(firstRender);
}
2023-01-27 22:50:12 +00:00
private IBrowserFile selectedFile;
2023-01-18 12:56:24 +00:00
private async Task SingleUpload(InputFileChangeEventArgs e)
{
2023-01-27 22:50:12 +00:00
selectedFile = e.GetMultipleFiles()[0];
this.StateHasChanged();
2023-01-18 12:56:24 +00:00
}
2023-02-02 21:13:03 +00:00
string postErr { get; set; }
bool hasErr { get; set; } = false;
2023-01-18 12:56:24 +00:00
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);
2023-02-02 21:13:03 +00:00
if (foundusr == null)
{
hasErr = true;
postErr = "You are not logged in.";
return;
}
if (!foundusr.ConfirmedEmail)
{
hasErr = true;
postErr = "You cannot post without an verified email.";
return;
}
int userID = foundusr.UserID;
if (DateTimeOffset.Now.ToUnixTimeMilliseconds() - foundusr.TimeBanned > 0)
2023-02-13 20:39:39 +00:00
{
foundusr.TimeBanned = -1;
}
2023-02-02 21:13:03 +00:00
if (foundusr.TimeBanned != -1)
{
var dt = TheManager.ConvertToDateTime(foundusr.TimeBanned);
hasErr = true;
postErr = "You are banned for \"" + foundusr.BanReason + "\" and may not post until " + dt.Year + "." + dt.Month + "." + dt.Day + " " + dt.Hour + ":" + dt.Minute + "::" + dt.Second;
return;
2023-02-02 21:13:03 +00:00
}
2023-03-04 22:09:26 +00:00
BoardData b = await BoardsRepository.getBoardByTagAsync(board.Tag);
if (b.isLocked)
{
hasErr = true;
postErr = "This board is currently locked.";
return;
}
foundusr.lastActionTimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
if (postUsername == null || postUsername == "")
{
postUsername = "Anonymous";
}
foundusr.LastUsedName = postUsername;
await UsersRepository.updateUserAsync(foundusr);
//TODO Add check if data is image
if (selectedFile == null || selectedFile.Size >= 512000 * 2 * 10)
2023-01-27 22:50:12 +00:00
{
hasErr = true;
postErr = "You did not attach a file or the selected file is bigger then the 10MiB file limit.";
2023-01-27 22:50:12 +00:00
return;
}
Stream stream = selectedFile.OpenReadStream(maxAllowedSize: 512000 * 2 * 10); // max 10MB
var file = Path.GetRandomFileName() + "." + selectedFile.Name.Split(".")[selectedFile.Name.Split(".").Length - 1];
2023-02-07 20:47:28 +00:00
var path = $"{env.WebRootPath}/img/dynamic/op/{@board.Tag}/{@file}";
2023-01-27 22:50:12 +00:00
FileStream fs = File.Create(path);
await stream.CopyToAsync(fs);
stream.Close();
fs.Close();
2023-01-18 12:56:24 +00:00
var imageToUpload = new ImageData
{
Board = board.Tag,
2023-02-07 20:47:28 +00:00
ImageLocation = $"/img/dynamic/op/{@board.Tag}/{@file}"
2023-01-18 12:56:24 +00:00
};
int imageID = await ImagesRepository.createImageAsync(imageToUpload);
int thisGET = b.NumberOfGETs + 1;
b.NumberOfGETs++;
await BoardsRepository.updateBoardAsync(b);
2023-01-18 12:56:24 +00:00
var postToPost = new PostData
{
UserID = userID,
ImageID = imageID,
Username = postUsername,
Title = postTitle,
Content = postContent,
2023-01-25 16:26:21 +00:00
Interactions = 0,
2023-01-18 12:56:24 +00:00
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
Board = board.Tag,
IsLocked = false,
IsSticky = false,
GET = thisGET,
shouldAnon = postAnon
2023-01-18 12:56:24 +00:00
};
int postId = await PostsRepository.createPostAsync(postToPost);
await TheManager.bumpThreads(board);
2023-01-18 12:56:24 +00:00
if (postId != -1)
{
//Open post successfull
NavigationManager.NavigateTo($"/{board.Tag}/thread/{postId}", true, true);
2023-01-18 12:56:24 +00:00
Console.WriteLine("Post created");
}
else
{
//Open post unsucessfull
hasErr = true;
postErr = "There was an error and the post could not be created. Please notify the admin.";
2023-01-18 12:56:24 +00:00
Console.WriteLine("Shit sucks and did not work.");
}
}
2023-01-18 12:56:24 +00:00
}