2023-02-13 17:45:14 +00:00
|
|
|
using ImageBoardServerApp.Data.Repository;
|
|
|
|
|
|
|
|
namespace ImageBoardServerApp.Data;
|
|
|
|
|
|
|
|
public class TheManager
|
|
|
|
{
|
|
|
|
|
|
|
|
private static long getDiff(PostData post)
|
|
|
|
{
|
|
|
|
return (DateTimeOffset.Now.ToUnixTimeMilliseconds() - post.CreatedAt);
|
|
|
|
}
|
|
|
|
|
2023-02-14 11:46:52 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the value without a minus, if it exists
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="num"></param>
|
|
|
|
/// <returns></returns>
|
2023-02-13 17:45:14 +00:00
|
|
|
private static long getValue(long num)
|
|
|
|
{
|
|
|
|
return num < 0 ? num * -1 : num;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long getBumpValue(PostData post)
|
|
|
|
{
|
2023-03-01 13:44:06 +00:00
|
|
|
return (post.IsSticky ? 999999999999999999 + getDiff(post) : 10 * 60000 - getDiff(post) + ( 60000 * (post.Comments.Count + 1))) ;
|
2023-02-13 17:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task<List<PostData>> getPostList(string boardTag)
|
|
|
|
{
|
|
|
|
List<PostData> threads = await PostsRepository.getPostsByBoardAsync(boardTag);
|
2023-02-14 11:46:52 +00:00
|
|
|
return threads.OrderBy(getBumpValue).Reverse().ToList();
|
2023-02-13 17:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task bumpThreads(BoardData board)
|
|
|
|
{
|
|
|
|
List<PostData> sortedThreads = await getPostList(board.Tag);
|
|
|
|
if (sortedThreads.Count > board.maxThreads + 1)
|
|
|
|
{
|
|
|
|
for (int i = board.maxThreads + 1; i >= sortedThreads.Count; ++i)
|
|
|
|
{
|
2023-02-24 23:08:36 +00:00
|
|
|
if (sortedThreads[i].IsSticky)
|
|
|
|
continue;
|
2023-02-13 17:45:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 23:49:50 +00:00
|
|
|
|
|
|
|
public static async Task<bool> isGETComment(string board, int GET)
|
|
|
|
{
|
|
|
|
return await CommentsRepository.getCommentByGETAsync(board, GET) != null;
|
|
|
|
}
|
2023-02-13 17:45:14 +00:00
|
|
|
}
|