bulletboards/ImageBoardServerApp/Shared/Components/Post.razor

237 lines
7.8 KiB
Text
Raw Normal View History

@using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Util
@using ImageBoardServerApp.Auth
@using System.ComponentModel.DataAnnotations
@using System.Text.RegularExpressions
@inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navigationManager;
2023-01-18 12:56:24 +00:00
<div class="post">
<div class="threadHeader">
@if (showOpenThread)
2023-02-07 20:47:28 +00:00
{
<span>[</span>
<a class="toggleOpened" onclick="@ToggleOpened">@toggleText</a>
<span>] </span>
2023-02-07 20:47:28 +00:00
}
<span class="title">@post.Title</span>
<span class="name">@post.Username </span>
@if (post.User.Role != "User" && !post.shouldAnon)
{
<span class="@post.User.Role">##@post.User.Role&nbsp;</span>
}
<span>&nbsp;</span>
<span class="date"> @getTimeFromUnix(post.CreatedAt)</span>
<span class="post-id">No.@post.GET</span>
@if (post.IsSticky)
{
<img src="img/static/sys/sticky.png" alt="Stickied"/>
}
@if (post.IsLocked)
{
<img src="img/static/sys/locked.png" alt="Locked"/>
}
2023-01-18 12:56:24 +00:00
</div>
@if (opened)
{
<div class="threadContent">
<div class="threadImage">
@if (@post.Image != null)
{
string isActiveClass = isActive ? "active" : "";
<img @onclick="() => isActive = !isActive" class="@isActiveClass" src="@($"{@post.Image.ImageLocation}?size=258x258")" alt="No Image found"/>
}
else
{
<span>[No Image]</span>
}
</div>
<div class="threadTextContainer">
@if (showOpenThread)
{
@foreach (string s in post.Content.Split("\n").Take(6))
{
var className = "";
@if (s.StartsWith(">") && !Regex.IsMatch(s, "^>{2,}"))
{
className = "greenText";
}
<p class="@className">
@foreach (string s2 in s.Split(" "))
{
@if (s2.StartsWith("https://") || s2.StartsWith("http://"))
{
<a href="@s2">@s2</a>
}
else
{
@s2
}
@(" ")
}
</p>
<p/>
}
@if (post.Content.Split("\n").Length > 6)
{
<p class="greenText">[Open thread to read more...]</p>
}
}
else
{
@foreach (string s in post.Content.Split("\n"))
{
var className = "";
@if (s.StartsWith(">") && !Regex.IsMatch(s, "^>{2,}"))
{
className = "greenText";
}
<p class="@className">
@foreach (string s2 in s.Split(" "))
{
@if (s2.StartsWith("https://") || s2.StartsWith("http://"))
{
<a href="@s2">@s2</a>
}
else
{
@s2
}
@(" ")
}
</p>
<p/>
}
}
</div>
</div>
<div class="threadFooter">
<AuthorizeView Roles="Admin, Mod">
<Authorized>
<span>[</span>
@if (post.IsSticky)
{
<button class="statButton" onclick="@stickyMe">Unsticky</button>
}
else
{
<button class="statButton" onclick="@stickyMe">Sticky</button>
}
@if (post.IsLocked)
{
<button class="statButton" onclick="@lockMe">Unlock</button>
}
else
{
<button class="statButton" onclick="@lockMe">Lock</button>
}
<span>]</span>
</Authorized>
</AuthorizeView>
<span>[</span>
<a class="repButton" href="@reportURL" target="_blank">Report</a>
@if (canDel)
{
<button class="delButton" onclick="@deletePost">Delete</button>
}
@if (showOpenThread)
{
<a class="opButton" href="@threadURL">@openThreadName</a>
}
else
{
<span class="openThread">@post.Comments.Count Comments</span>
}
<span>]</span>
</div>
}
</div>
2023-01-18 12:56:24 +00:00
@code {
private string reportURL { get; set; }
private string threadURL { get; set; }
private string openThreadName { get; set; }
public bool canDel { get; set; } = false;
public bool isActive { get; set; } = false;
private int linecnt = 0;
private async void lockMe()
{
post.IsLocked = !post.IsLocked;
await PostsRepository.updatePostAsync(post);
}
private async void stickyMe()
{
post.IsSticky = !post.IsSticky;
await PostsRepository.updatePostAsync(post);
}
2023-01-18 12:56:24 +00:00
private static DateTime getTimeFromUnix(double javaTimeStamp)
{
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddMilliseconds(javaTimeStamp).ToLocalTime();
2023-01-18 12:56:24 +00:00
return dateTime;
}
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.getUserByEmailRawAsync(usr.Identity.Name);
if (foundusr.Role != "User" || post.UserID == foundusr.UserID)
{
await TheManager.deleteThread(post);
navigationManager.NavigateTo($"/sys/click/red/_{boardTag}");
}
}
2023-01-18 12:56:24 +00:00
private bool opened = true;
private string toggleText = "-";
private string value;
2023-01-18 12:56:24 +00:00
private void ToggleOpened()
{
opened = !opened;
toggleText = opened ? "-" : "+";
}
protected override async Task OnInitializedAsync()
{
value = TheManager.getBumpValue(post) + "v";
reportURL = $"/sys/report/op/{post.Board}/{post.PostID}";
threadURL = $"/{post.Board}/thread/{post.PostID}";
openThreadName = $"({post.Comments.Count}) View Thread";
var cauthStateProvder = (CustomAuthenticationStateProvider)authStateProvider;
var user = await cauthStateProvder.GetAuthenticationStateAsync();
UserData foundusr = await UsersRepository.getUserByEmailRawAsync(user.User.Identity.Name);
if (foundusr != null && (foundusr.Role != "User" || post.UserID == foundusr.UserID))
{
canDel = true;
return;
}
canDel = false;
await base.OnInitializedAsync();
}
2023-01-18 12:56:24 +00:00
[Parameter]
[Required]
public PostData post { get; set; }
2023-01-25 22:45:36 +00:00
[Parameter]
[Required]
public bool showOpenThread { get; set; }
2023-01-18 12:56:24 +00:00
}