using SharedKernel.Extensions;
using System.Text.RegularExpressions;
namespace Application.Common;
///
/// 게시판 알림 메일 템플릿의 {변수} 토큰 빌더.
/// EmailTemplateExtensions.Render() 의 입력으로 사용한다.
///
internal static class ForumNotifyRenderer
{
private static readonly Regex HtmlTagRegex = new("<[^>]+>", RegexOptions.Compiled);
private static readonly Regex WhitespaceRegex = new(@"\s+", RegexOptions.Compiled);
/// HTML 태그 제거 후 maxLength 자로 자른 미리보기 텍스트
public static string ToPreview(string? html, int maxLength = 200)
{
if (string.IsNullOrWhiteSpace(html))
{
return string.Empty;
}
var text = HtmlTagRegex.Replace(html, " ");
text = System.Net.WebUtility.HtmlDecode(text);
text = WhitespaceRegex.Replace(text, " ").Trim();
return text.Length <= maxLength ? text : text[..maxLength] + "…";
}
/// 게시글 작성 알림용 토큰 — {SiteName} {BoardName} {PostID} {PostSubject} {AuthorName} {AuthorEmail} {CreatedAt} {IsSecret} {PostContentPreview}
public static (string Key, string Value)[] PostTokens(string? siteName, string boardName, Domain.Entities.Forum.Posts.Post post) => [
("SiteName", siteName ?? string.Empty),
("BoardName", boardName),
("PostID", post.ID.ToString()),
("PostSubject", post.Subject),
("AuthorName", post.Name ?? post.SID ?? "-"),
("AuthorEmail", post.Email ?? "-"),
("CreatedAt", post.CreatedAt.GetDateAt()),
("IsSecret", post.IsSecret ? "비밀글" : "공개"),
("PostContentPreview", ToPreview(post.Content))
];
/// 댓글/답글 작성 알림용 토큰 — 게시글 토큰 + {CommentContent} {CommentAuthorName} {CommentCreatedAt}
public static (string Key, string Value)[] CommentTokens(string? siteName, string boardName, Domain.Entities.Forum.Posts.Post post, Domain.Entities.Forum.Comments.Comment comment) => [
..PostTokens(siteName, boardName, post),
("CommentContent", ToPreview(comment.Content, 500)),
("CommentAuthorName", comment.Name ?? comment.SID ?? "-"),
("CommentCreatedAt", comment.CreatedAt.GetDateAt())
];
}