2023-06-08 21:31:17 +00:00
|
|
|
using System.Security.Cryptography;
|
2023-05-17 11:17:53 +00:00
|
|
|
using ImageBoardServerApp.Data;
|
2023-02-13 17:45:14 +00:00
|
|
|
using ImageBoardServerApp.Data.Repository;
|
|
|
|
|
2023-05-17 11:17:53 +00:00
|
|
|
namespace ImageBoardServerApp.Util;
|
2023-02-13 17:45:14 +00:00
|
|
|
|
|
|
|
public class TheManager
|
|
|
|
{
|
2023-06-14 14:15:56 +00:00
|
|
|
public static string version = "v1.0.0-rc2";
|
2023-06-13 18:04:17 +00:00
|
|
|
|
2023-02-13 17:45:14 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-06-13 14:24:38 +00:00
|
|
|
|
2023-02-13 17:45:14 +00:00
|
|
|
public static long getBumpValue(PostData post)
|
|
|
|
{
|
2023-06-13 14:24:38 +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)
|
|
|
|
{
|
2023-06-13 21:34:16 +00:00
|
|
|
Console.WriteLine("Bumping..");
|
2023-02-13 17:45:14 +00:00
|
|
|
List<PostData> sortedThreads = await getPostList(board.Tag);
|
|
|
|
if (sortedThreads.Count > board.maxThreads + 1)
|
|
|
|
{
|
2023-06-13 21:34:16 +00:00
|
|
|
Console.WriteLine("Pruning..");
|
|
|
|
foreach (var pd in sortedThreads.TakeLast(sortedThreads.Count() - board.maxThreads))
|
2023-02-13 17:45:14 +00:00
|
|
|
{
|
2023-06-13 21:34:16 +00:00
|
|
|
if (pd.IsSticky)
|
2023-02-24 23:08:36 +00:00
|
|
|
continue;
|
2023-06-13 21:34:16 +00:00
|
|
|
Console.WriteLine($"Removing Post{pd.PostID}");
|
|
|
|
await deleteThread(pd);
|
2023-02-13 17:45:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-13 14:24:38 +00:00
|
|
|
|
2023-02-13 17:45:14 +00:00
|
|
|
public static async Task deleteThread(PostData post)
|
|
|
|
{
|
2023-06-13 14:24:38 +00:00
|
|
|
foreach (var c in post.Comments)
|
2023-02-13 17:45:14 +00:00
|
|
|
{
|
|
|
|
if (c.Image != null)
|
|
|
|
{
|
2023-06-13 14:24:38 +00:00
|
|
|
await deleteImage(c.Image);
|
2023-02-13 17:45:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-13 14:24:38 +00:00
|
|
|
|
|
|
|
await deleteImage(post.Image);
|
2023-02-13 17:45:14 +00:00
|
|
|
|
|
|
|
await PostsRepository.deletePostAsync(post.PostID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async Task deleteComment(CommentData comment)
|
|
|
|
{
|
|
|
|
if (comment.Image != null)
|
|
|
|
{
|
2023-06-13 14:24:38 +00:00
|
|
|
await deleteImage(comment.Image);
|
2023-02-13 17:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await CommentsRepository.deleteCommentAsync(comment.CommentID);
|
|
|
|
}
|
|
|
|
|
2023-06-13 14:24:38 +00:00
|
|
|
public static async Task deleteImage(ImageData imageData)
|
2023-02-13 17:45:14 +00:00
|
|
|
{
|
|
|
|
string path = $"./wwwroot{imageData.ImageLocation}";
|
2023-06-13 14:24:38 +00:00
|
|
|
Console.WriteLine(path);
|
2023-02-13 17:45:14 +00:00
|
|
|
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-06-08 21:31:17 +00:00
|
|
|
|
|
|
|
public static string getmd5Hash()
|
|
|
|
{
|
|
|
|
var bytes = new byte[16];
|
|
|
|
using (var rng = new RNGCryptoServiceProvider())
|
|
|
|
{
|
|
|
|
rng.GetBytes(bytes);
|
|
|
|
}
|
2023-06-13 14:24:38 +00:00
|
|
|
|
2023-06-08 21:31:17 +00:00
|
|
|
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
|
|
|
|
}
|
2023-06-13 17:08:43 +00:00
|
|
|
|
|
|
|
public static DateTime ConvertToDateTime(long timestamp)
|
|
|
|
{
|
2023-06-13 21:34:16 +00:00
|
|
|
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
|
|
|
dateTime = dateTime.AddMilliseconds(timestamp).ToLocalTime();
|
|
|
|
return dateTime;
|
2023-06-13 17:08:43 +00:00
|
|
|
}
|
2023-02-13 17:45:14 +00:00
|
|
|
}
|