| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using SharedKernel.Extensions;
- using System.Text.RegularExpressions;
- namespace Application.Common;
- /// <summary>
- /// 게시판 알림 메일 템플릿의 {변수} 토큰 빌더.
- /// EmailTemplateExtensions.Render() 의 입력으로 사용한다.
- /// </summary>
- internal static class ForumNotifyRenderer
- {
- private static readonly Regex HtmlTagRegex = new("<[^>]+>", RegexOptions.Compiled);
- private static readonly Regex WhitespaceRegex = new(@"\s+", RegexOptions.Compiled);
- /// <summary>HTML 태그 제거 후 maxLength 자로 자른 미리보기 텍스트</summary>
- 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] + "…";
- }
- /// <summary>게시글 작성 알림용 토큰 — {SiteName} {BoardName} {PostID} {PostSubject} {AuthorName} {AuthorEmail} {CreatedAt} {IsSecret} {PostContentPreview}</summary>
- 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))
- ];
- /// <summary>댓글/답글 작성 알림용 토큰 — 게시글 토큰 + {CommentContent} {CommentAuthorName} {CommentCreatedAt}</summary>
- 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())
- ];
- }
|