| | | 1 | | using Services.TransactionCommentAi; |
| | | 2 | | |
| | | 3 | | namespace Services.TransactionUtilsServices; |
| | | 4 | | |
| | | 5 | | public static class TransactionCommentDisplayFormatter |
| | | 6 | | { |
| | 1 | 7 | | private static readonly HashSet<string> MetaLabels = new(StringComparer.OrdinalIgnoreCase) |
| | 1 | 8 | | { |
| | 1 | 9 | | "Тип", |
| | 1 | 10 | | "Когда", |
| | 1 | 11 | | "Пользователь", |
| | 1 | 12 | | "Заказ", |
| | 1 | 13 | | "ID транзакции" |
| | 1 | 14 | | }; |
| | | 15 | | |
| | 1 | 16 | | private static readonly HashSet<string> HiddenMetaLabels = new(StringComparer.OrdinalIgnoreCase) |
| | 1 | 17 | | { |
| | 1 | 18 | | "ID транзакции" |
| | 1 | 19 | | }; |
| | | 20 | | |
| | 1 | 21 | | private static readonly HashSet<string> OrderHistoryRedundantMetaLabels = new(StringComparer.OrdinalIgnoreCase) |
| | 1 | 22 | | { |
| | 1 | 23 | | "Тип", |
| | 1 | 24 | | "Когда", |
| | 1 | 25 | | "Пользователь", |
| | 1 | 26 | | "Заказ" |
| | 1 | 27 | | }; |
| | | 28 | | |
| | | 29 | | public static TransactionCommentDisplayModel Parse(string? comment) => |
| | 7 | 30 | | WithExpandable(ParseCore(comment)); |
| | | 31 | | |
| | | 32 | | public static TransactionCommentDisplayModel ParseForOrderHistory(string? comment) |
| | | 33 | | { |
| | 2 | 34 | | var parsed = ParseCore(comment); |
| | 2 | 35 | | if (parsed.Kind != TransactionCommentDisplayKind.Structured) |
| | 0 | 36 | | return WithExpandable(parsed); |
| | | 37 | | |
| | 2 | 38 | | var meta = parsed.Meta |
| | 6 | 39 | | .Where(m => !OrderHistoryRedundantMetaLabels.Contains(m.Label)) |
| | 2 | 40 | | .ToList(); |
| | | 41 | | |
| | 2 | 42 | | var sections = parsed.Sections |
| | 2 | 43 | | .Select(s => string.Equals(s.Title, "Что произошло", StringComparison.OrdinalIgnoreCase) |
| | 2 | 44 | | ? new TransactionCommentBodySection(null, s.Body, s.IsWarning) |
| | 2 | 45 | | : s) |
| | 2 | 46 | | .ToList(); |
| | | 47 | | |
| | 2 | 48 | | return WithExpandable(new TransactionCommentDisplayModel |
| | 2 | 49 | | { |
| | 2 | 50 | | Kind = TransactionCommentDisplayKind.Structured, |
| | 2 | 51 | | DisplayMode = TransactionCommentDisplayMode.OrderHistoryCard, |
| | 2 | 52 | | Raw = parsed.Raw, |
| | 2 | 53 | | Meta = meta, |
| | 2 | 54 | | Sections = sections |
| | 2 | 55 | | }); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private static TransactionCommentDisplayModel WithExpandable(TransactionCommentDisplayModel model) => |
| | 9 | 59 | | new() |
| | 9 | 60 | | { |
| | 9 | 61 | | Kind = model.Kind, |
| | 9 | 62 | | DisplayMode = model.DisplayMode, |
| | 9 | 63 | | Raw = model.Raw, |
| | 9 | 64 | | Meta = model.Meta, |
| | 9 | 65 | | Sections = model.Sections, |
| | 9 | 66 | | IsExpandable = ComputeExpandable(model) |
| | 9 | 67 | | }; |
| | | 68 | | |
| | | 69 | | private static TransactionCommentDisplayModel ParseCore(string? comment) |
| | | 70 | | { |
| | 9 | 71 | | if (string.IsNullOrWhiteSpace(comment)) |
| | 1 | 72 | | return new TransactionCommentDisplayModel { Kind = TransactionCommentDisplayKind.Empty }; |
| | | 73 | | |
| | 8 | 74 | | if (TransactionCommentAiConstants.IsGeneratingPlaceholder(comment)) |
| | | 75 | | { |
| | 1 | 76 | | return new TransactionCommentDisplayModel |
| | 1 | 77 | | { |
| | 1 | 78 | | Kind = TransactionCommentDisplayKind.Generating, |
| | 1 | 79 | | Raw = comment.Trim() |
| | 1 | 80 | | }; |
| | | 81 | | } |
| | | 82 | | |
| | 7 | 83 | | var trimmed = comment.Trim(); |
| | 7 | 84 | | if (LooksLikeHtml(trimmed)) |
| | | 85 | | { |
| | 1 | 86 | | return new TransactionCommentDisplayModel |
| | 1 | 87 | | { |
| | 1 | 88 | | Kind = TransactionCommentDisplayKind.Html, |
| | 1 | 89 | | Raw = trimmed |
| | 1 | 90 | | }; |
| | | 91 | | } |
| | | 92 | | |
| | 6 | 93 | | return ParseStructured(trimmed); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | private static bool LooksLikeHtml(string text) => |
| | 7 | 97 | | text.Contains('<', StringComparison.Ordinal) |
| | 7 | 98 | | && (text.Contains("</", StringComparison.Ordinal) |
| | 7 | 99 | | || text.Contains("/>", StringComparison.Ordinal) |
| | 7 | 100 | | || text.Contains("class=", StringComparison.OrdinalIgnoreCase)); |
| | | 101 | | |
| | | 102 | | private static TransactionCommentDisplayModel ParseStructured(string text) |
| | | 103 | | { |
| | 6 | 104 | | var lines = text.Replace("\r\n", "\n", StringComparison.Ordinal).Split('\n'); |
| | 6 | 105 | | var meta = new List<TransactionCommentMetaItem>(); |
| | 6 | 106 | | var sections = new List<TransactionCommentBodySection>(); |
| | | 107 | | |
| | 6 | 108 | | var index = 0; |
| | 18 | 109 | | while (index < lines.Length) |
| | | 110 | | { |
| | 18 | 111 | | var line = lines[index].Trim(); |
| | 18 | 112 | | if (string.IsNullOrEmpty(line)) |
| | | 113 | | { |
| | 4 | 114 | | index++; |
| | 4 | 115 | | break; |
| | | 116 | | } |
| | | 117 | | |
| | 14 | 118 | | if (!TryParseMetaLine(line, out var label, out var value)) |
| | | 119 | | break; |
| | | 120 | | |
| | 12 | 121 | | if (!HiddenMetaLabels.Contains(label)) |
| | 12 | 122 | | meta.Add(new TransactionCommentMetaItem(label, value)); |
| | | 123 | | |
| | 12 | 124 | | index++; |
| | | 125 | | } |
| | | 126 | | |
| | 6 | 127 | | while (index < lines.Length && string.IsNullOrWhiteSpace(lines[index])) |
| | 0 | 128 | | index++; |
| | | 129 | | |
| | 6 | 130 | | var bodyLines = new List<string>(); |
| | 6 | 131 | | string? currentTitle = null; |
| | | 132 | | |
| | | 133 | | void FlushSection() |
| | | 134 | | { |
| | 10 | 135 | | var body = string.Join('\n', bodyLines).Trim(); |
| | 10 | 136 | | if (string.IsNullOrEmpty(body) && string.IsNullOrEmpty(currentTitle)) |
| | 4 | 137 | | return; |
| | | 138 | | |
| | 6 | 139 | | var isWarning = (!string.IsNullOrEmpty(currentTitle) && currentTitle.StartsWith('⚠')) |
| | 6 | 140 | | || body.StartsWith('⚠'); |
| | 6 | 141 | | sections.Add(new TransactionCommentBodySection(currentTitle, body, isWarning)); |
| | 6 | 142 | | bodyLines.Clear(); |
| | 6 | 143 | | currentTitle = null; |
| | 6 | 144 | | } |
| | | 145 | | |
| | 26 | 146 | | for (; index < lines.Length; index++) |
| | | 147 | | { |
| | 10 | 148 | | var trimmedLine = lines[index].Trim(); |
| | 10 | 149 | | if (string.IsNullOrEmpty(trimmedLine)) |
| | | 150 | | { |
| | 0 | 151 | | if (bodyLines.Count > 0) |
| | 0 | 152 | | bodyLines.Add(string.Empty); |
| | 0 | 153 | | continue; |
| | | 154 | | } |
| | | 155 | | |
| | 10 | 156 | | if (IsSectionHeader(trimmedLine)) |
| | | 157 | | { |
| | 4 | 158 | | FlushSection(); |
| | 4 | 159 | | currentTitle = trimmedLine.TrimEnd(':').Trim(); |
| | 4 | 160 | | continue; |
| | | 161 | | } |
| | | 162 | | |
| | 6 | 163 | | bodyLines.Add(trimmedLine); |
| | | 164 | | } |
| | | 165 | | |
| | 6 | 166 | | FlushSection(); |
| | | 167 | | |
| | 8 | 168 | | if (meta.Count > 0 || sections.Any(s => !string.IsNullOrWhiteSpace(s.Title))) |
| | | 169 | | { |
| | 4 | 170 | | return new TransactionCommentDisplayModel |
| | 4 | 171 | | { |
| | 4 | 172 | | Kind = TransactionCommentDisplayKind.Structured, |
| | 4 | 173 | | Raw = text, |
| | 4 | 174 | | Meta = meta, |
| | 4 | 175 | | Sections = sections |
| | 4 | 176 | | }; |
| | | 177 | | } |
| | | 178 | | |
| | 2 | 179 | | if (sections.Count > 0) |
| | | 180 | | { |
| | 2 | 181 | | return new TransactionCommentDisplayModel |
| | 2 | 182 | | { |
| | 2 | 183 | | Kind = TransactionCommentDisplayKind.Plain, |
| | 2 | 184 | | Raw = sections[0].Body |
| | 2 | 185 | | }; |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | return new TransactionCommentDisplayModel |
| | 0 | 189 | | { |
| | 0 | 190 | | Kind = TransactionCommentDisplayKind.Plain, |
| | 0 | 191 | | Raw = text |
| | 0 | 192 | | }; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | private static bool ComputeExpandable(TransactionCommentDisplayModel model) |
| | | 196 | | { |
| | 9 | 197 | | if (model.Kind is not (TransactionCommentDisplayKind.Structured or TransactionCommentDisplayKind.Plain)) |
| | 3 | 198 | | return false; |
| | | 199 | | |
| | 10 | 200 | | var bodyLength = model.Sections.Sum(s => s.Body.Length); |
| | 6 | 201 | | return Math.Max(model.Raw.Length, bodyLength) > 280; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | private static bool TryParseMetaLine(string line, out string label, out string value) |
| | | 205 | | { |
| | 14 | 206 | | label = string.Empty; |
| | 14 | 207 | | value = string.Empty; |
| | | 208 | | |
| | 14 | 209 | | var colonIndex = line.IndexOf(':'); |
| | 14 | 210 | | if (colonIndex <= 0) |
| | 2 | 211 | | return false; |
| | | 212 | | |
| | 12 | 213 | | label = line[..colonIndex].Trim(); |
| | 12 | 214 | | if (!MetaLabels.Contains(label)) |
| | 0 | 215 | | return false; |
| | | 216 | | |
| | 12 | 217 | | value = line[(colonIndex + 1)..].Trim(); |
| | 12 | 218 | | return true; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | private static bool IsSectionHeader(string line) |
| | | 222 | | { |
| | 10 | 223 | | if (line.StartsWith('⚠')) |
| | 1 | 224 | | return true; |
| | | 225 | | |
| | 9 | 226 | | if (!line.EndsWith(':')) |
| | 6 | 227 | | return false; |
| | | 228 | | |
| | 3 | 229 | | var title = line.TrimEnd(':').Trim(); |
| | 3 | 230 | | return title.Equals("Что произошло", StringComparison.OrdinalIgnoreCase) |
| | 3 | 231 | | || title.StartsWith("Отгрузка в Ozon", StringComparison.OrdinalIgnoreCase) |
| | 3 | 232 | | || title.StartsWith("1С", StringComparison.OrdinalIgnoreCase) |
| | 3 | 233 | | || title.StartsWith("Ozon", StringComparison.OrdinalIgnoreCase); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | public static IReadOnlyList<string> SplitParagraphs(string body) |
| | | 237 | | { |
| | 1 | 238 | | if (string.IsNullOrWhiteSpace(body)) |
| | 0 | 239 | | return []; |
| | | 240 | | |
| | 1 | 241 | | return body |
| | 1 | 242 | | .Split("\n\n", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) |
| | 2 | 243 | | .Select(p => p.Replace("\n", " ", StringComparison.Ordinal).Trim()) |
| | 2 | 244 | | .Where(p => !string.IsNullOrWhiteSpace(p)) |
| | 1 | 245 | | .ToList(); |
| | | 246 | | } |
| | | 247 | | } |