ForumNotifyRenderer.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using SharedKernel.Extensions;
  2. using System.Text.RegularExpressions;
  3. namespace Application.Common;
  4. /// <summary>
  5. /// 게시판 알림 메일 템플릿의 {변수} 토큰 빌더.
  6. /// EmailTemplateExtensions.Render() 의 입력으로 사용한다.
  7. /// </summary>
  8. internal static class ForumNotifyRenderer
  9. {
  10. private static readonly Regex HtmlTagRegex = new("<[^>]+>", RegexOptions.Compiled);
  11. private static readonly Regex WhitespaceRegex = new(@"\s+", RegexOptions.Compiled);
  12. /// <summary>HTML 태그 제거 후 maxLength 자로 자른 미리보기 텍스트</summary>
  13. public static string ToPreview(string? html, int maxLength = 200)
  14. {
  15. if (string.IsNullOrWhiteSpace(html))
  16. {
  17. return string.Empty;
  18. }
  19. var text = HtmlTagRegex.Replace(html, " ");
  20. text = System.Net.WebUtility.HtmlDecode(text);
  21. text = WhitespaceRegex.Replace(text, " ").Trim();
  22. return text.Length <= maxLength ? text : text[..maxLength] + "…";
  23. }
  24. /// <summary>게시글 작성 알림용 토큰 — {SiteName} {BoardName} {PostID} {PostSubject} {AuthorName} {AuthorEmail} {CreatedAt} {IsSecret} {PostContentPreview}</summary>
  25. public static (string Key, string Value)[] PostTokens(string? siteName, string boardName, Domain.Entities.Forum.Posts.Post post) => [
  26. ("SiteName", siteName ?? string.Empty),
  27. ("BoardName", boardName),
  28. ("PostID", post.ID.ToString()),
  29. ("PostSubject", post.Subject),
  30. ("AuthorName", post.Name ?? post.SID ?? "-"),
  31. ("AuthorEmail", post.Email ?? "-"),
  32. ("CreatedAt", post.CreatedAt.GetDateAt()),
  33. ("IsSecret", post.IsSecret ? "비밀글" : "공개"),
  34. ("PostContentPreview", ToPreview(post.Content))
  35. ];
  36. /// <summary>댓글/답글 작성 알림용 토큰 — 게시글 토큰 + {CommentContent} {CommentAuthorName} {CommentCreatedAt}</summary>
  37. public static (string Key, string Value)[] CommentTokens(string? siteName, string boardName, Domain.Entities.Forum.Posts.Post post, Domain.Entities.Forum.Comments.Comment comment) => [
  38. ..PostTokens(siteName, boardName, post),
  39. ("CommentContent", ToPreview(comment.Content, 500)),
  40. ("CommentAuthorName", comment.Name ?? comment.SID ?? "-"),
  41. ("CommentCreatedAt", comment.CreatedAt.GetDateAt())
  42. ];
  43. }