| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | using Chronicis.Shared.Enums; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace Chronicis.Api.Services; |
| | | 7 | | |
| | | 8 | | /// <inheritdoc /> |
| | | 9 | | public class ArticleHierarchyService : IArticleHierarchyService |
| | | 10 | | { |
| | | 11 | | private readonly ChronicisDbContext _context; |
| | | 12 | | private readonly ILogger<ArticleHierarchyService> _logger; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Safety limit to prevent runaway walks in case of corrupted data. |
| | | 16 | | /// No realistic hierarchy should exceed this depth. |
| | | 17 | | /// </summary> |
| | | 18 | | private const int MaxDepth = 200; |
| | | 19 | | |
| | 17 | 20 | | public ArticleHierarchyService(ChronicisDbContext context, ILogger<ArticleHierarchyService> logger) |
| | | 21 | | { |
| | 17 | 22 | | _context = context; |
| | 17 | 23 | | _logger = logger; |
| | 17 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public async Task<List<BreadcrumbDto>> BuildBreadcrumbsAsync(Guid articleId, HierarchyWalkOptions? options = null) |
| | | 28 | | { |
| | 14 | 29 | | options ??= new HierarchyWalkOptions(); |
| | | 30 | | |
| | | 31 | | // 1. Walk up the parent chain |
| | 14 | 32 | | var articleBreadcrumbs = await WalkAncestorsAsync(articleId, options); |
| | | 33 | | |
| | | 34 | | // 2. Resolve world breadcrumb |
| | 14 | 35 | | var result = new List<BreadcrumbDto>(); |
| | | 36 | | |
| | 14 | 37 | | if (options.IncludeWorldBreadcrumb) |
| | | 38 | | { |
| | 13 | 39 | | var worldBreadcrumb = await ResolveWorldBreadcrumbAsync(articleId, options); |
| | 13 | 40 | | if (worldBreadcrumb != null) |
| | | 41 | | { |
| | 12 | 42 | | result.Add(worldBreadcrumb); |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // 3. Insert virtual group breadcrumbs if requested |
| | 14 | 47 | | if (options.IncludeVirtualGroups) |
| | | 48 | | { |
| | 3 | 49 | | var virtualGroups = await ResolveVirtualGroupsAsync(articleId, articleBreadcrumbs, options); |
| | 3 | 50 | | result.AddRange(virtualGroups); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // 4. Append the article chain |
| | 14 | 54 | | result.AddRange(articleBreadcrumbs); |
| | | 55 | | |
| | 14 | 56 | | return result; |
| | 14 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public async Task<string> BuildPathAsync(Guid articleId, HierarchyWalkOptions? options = null) |
| | | 61 | | { |
| | 1 | 62 | | var breadcrumbs = await BuildBreadcrumbsAsync(articleId, options); |
| | 5 | 63 | | return string.Join("/", breadcrumbs.Select(b => b.Slug)); |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public async Task<string> BuildDisplayPathAsync(Guid articleId, bool stripFirstLevel = true) |
| | | 68 | | { |
| | | 69 | | // Display paths: walk up collecting titles, no world breadcrumb, no virtual groups |
| | 3 | 70 | | var options = new HierarchyWalkOptions |
| | 3 | 71 | | { |
| | 3 | 72 | | PublicOnly = false, |
| | 3 | 73 | | IncludeWorldBreadcrumb = false, |
| | 3 | 74 | | IncludeVirtualGroups = false, |
| | 3 | 75 | | IncludeCurrentArticle = true |
| | 3 | 76 | | }; |
| | | 77 | | |
| | 3 | 78 | | var breadcrumbs = await WalkAncestorsAsync(articleId, options); |
| | | 79 | | |
| | 10 | 80 | | var titles = breadcrumbs.Select(b => b.Title).ToList(); |
| | | 81 | | |
| | | 82 | | // Strip the first level (top-level article / world root) when requested |
| | 3 | 83 | | if (stripFirstLevel && titles.Count > 1) |
| | | 84 | | { |
| | 1 | 85 | | titles.RemoveAt(0); |
| | | 86 | | } |
| | | 87 | | |
| | 3 | 88 | | return string.Join(" / ", titles); |
| | 3 | 89 | | } |
| | | 90 | | |
| | | 91 | | // ──────────────────────────────────────────────────────────────── |
| | | 92 | | // Core walk algorithm |
| | | 93 | | // ──────────────────────────────────────────────────────────────── |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Walks up the parent chain from <paramref name="articleId"/> to the root, |
| | | 97 | | /// collecting breadcrumbs in root-to-leaf order. |
| | | 98 | | /// Includes cycle protection via a visited set and a hard depth limit. |
| | | 99 | | /// </summary> |
| | | 100 | | private async Task<List<BreadcrumbDto>> WalkAncestorsAsync(Guid articleId, HierarchyWalkOptions options) |
| | | 101 | | { |
| | 17 | 102 | | var breadcrumbs = new List<BreadcrumbDto>(); |
| | 17 | 103 | | var visited = new HashSet<Guid>(); |
| | 17 | 104 | | var currentId = (Guid?)articleId; |
| | | 105 | | |
| | 48 | 106 | | while (currentId.HasValue) |
| | | 107 | | { |
| | | 108 | | // Cycle detection |
| | 35 | 109 | | if (!visited.Add(currentId.Value)) |
| | | 110 | | { |
| | 1 | 111 | | _logger.LogError( |
| | 1 | 112 | | "Cycle detected in article hierarchy at {ArticleId}. Visited: {Visited}", |
| | 1 | 113 | | currentId.Value, string.Join(", ", visited)); |
| | 1 | 114 | | break; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | // Hard depth limit |
| | 34 | 118 | | if (visited.Count > MaxDepth) |
| | | 119 | | { |
| | 0 | 120 | | _logger.LogError( |
| | 0 | 121 | | "Max hierarchy depth ({MaxDepth}) exceeded walking from article {ArticleId}", |
| | 0 | 122 | | MaxDepth, articleId); |
| | 0 | 123 | | break; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | // Build the base query |
| | 34 | 127 | | IQueryable<Chronicis.Shared.Models.Article> query = _context.Articles |
| | 34 | 128 | | .AsNoTracking() |
| | 34 | 129 | | .Where(a => a.Id == currentId); |
| | | 130 | | |
| | 34 | 131 | | if (options.PublicOnly) |
| | | 132 | | { |
| | 8 | 133 | | query = query.Where(a => a.Visibility == ArticleVisibility.Public); |
| | | 134 | | } |
| | | 135 | | |
| | 34 | 136 | | var article = await query |
| | 34 | 137 | | .Select(a => new |
| | 34 | 138 | | { |
| | 34 | 139 | | a.Id, |
| | 34 | 140 | | a.Title, |
| | 34 | 141 | | a.Slug, |
| | 34 | 142 | | a.ParentId, |
| | 34 | 143 | | a.Type, |
| | 34 | 144 | | a.WorldId, |
| | 34 | 145 | | a.CampaignId, |
| | 34 | 146 | | a.ArcId |
| | 34 | 147 | | }) |
| | 34 | 148 | | .FirstOrDefaultAsync(); |
| | | 149 | | |
| | 34 | 150 | | if (article == null) |
| | | 151 | | break; |
| | | 152 | | |
| | | 153 | | // Skip the current article if the caller only wants ancestors |
| | 31 | 154 | | var isTarget = (article.Id == articleId); |
| | 31 | 155 | | if (!isTarget || options.IncludeCurrentArticle) |
| | | 156 | | { |
| | 30 | 157 | | breadcrumbs.Insert(0, new BreadcrumbDto |
| | 30 | 158 | | { |
| | 30 | 159 | | Id = article.Id, |
| | 30 | 160 | | Title = article.Title ?? "(Untitled)", |
| | 30 | 161 | | Slug = article.Slug, |
| | 30 | 162 | | Type = article.Type, |
| | 30 | 163 | | IsWorld = false |
| | 30 | 164 | | }); |
| | | 165 | | } |
| | | 166 | | |
| | 31 | 167 | | currentId = article.ParentId; |
| | | 168 | | } |
| | | 169 | | |
| | 17 | 170 | | return breadcrumbs; |
| | 17 | 171 | | } |
| | | 172 | | |
| | | 173 | | // ──────────────────────────────────────────────────────────────── |
| | | 174 | | // World breadcrumb resolution |
| | | 175 | | // ──────────────────────────────────────────────────────────────── |
| | | 176 | | |
| | | 177 | | private async Task<BreadcrumbDto?> ResolveWorldBreadcrumbAsync(Guid articleId, HierarchyWalkOptions options) |
| | | 178 | | { |
| | | 179 | | // Use pre-resolved world if supplied |
| | 13 | 180 | | if (options.World != null) |
| | | 181 | | { |
| | 4 | 182 | | return new BreadcrumbDto |
| | 4 | 183 | | { |
| | 4 | 184 | | Id = options.World.Id, |
| | 4 | 185 | | Title = options.World.Name, |
| | 4 | 186 | | Slug = options.World.Slug, |
| | 4 | 187 | | Type = default, |
| | 4 | 188 | | IsWorld = true |
| | 4 | 189 | | }; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | // Otherwise look it up from the article's WorldId |
| | 9 | 193 | | var worldId = await _context.Articles |
| | 9 | 194 | | .AsNoTracking() |
| | 9 | 195 | | .Where(a => a.Id == articleId) |
| | 9 | 196 | | .Select(a => a.WorldId) |
| | 9 | 197 | | .FirstOrDefaultAsync(); |
| | | 198 | | |
| | 9 | 199 | | if (!worldId.HasValue) |
| | 1 | 200 | | return null; |
| | | 201 | | |
| | 8 | 202 | | var world = await _context.Worlds |
| | 8 | 203 | | .AsNoTracking() |
| | 8 | 204 | | .Where(w => w.Id == worldId.Value) |
| | 8 | 205 | | .Select(w => new { w.Id, w.Name, w.Slug }) |
| | 8 | 206 | | .FirstOrDefaultAsync(); |
| | | 207 | | |
| | 8 | 208 | | if (world == null) |
| | 0 | 209 | | return null; |
| | | 210 | | |
| | 8 | 211 | | return new BreadcrumbDto |
| | 8 | 212 | | { |
| | 8 | 213 | | Id = world.Id, |
| | 8 | 214 | | Title = world.Name, |
| | 8 | 215 | | Slug = world.Slug, |
| | 8 | 216 | | Type = default, |
| | 8 | 217 | | IsWorld = true |
| | 8 | 218 | | }; |
| | 13 | 219 | | } |
| | | 220 | | |
| | | 221 | | // ──────────────────────────────────────────────────────────────── |
| | | 222 | | // Virtual group resolution (public world breadcrumbs) |
| | | 223 | | // ──────────────────────────────────────────────────────────────── |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Resolves virtual group breadcrumbs (Campaigns/Arc or Player Characters/Wiki) |
| | | 227 | | /// based on the article's type and campaign/arc associations. |
| | | 228 | | /// Mirrors the prior behaviour of PublicWorldService.BuildPublicBreadcrumbsAsync. |
| | | 229 | | /// </summary> |
| | | 230 | | private async Task<List<BreadcrumbDto>> ResolveVirtualGroupsAsync( |
| | | 231 | | Guid articleId, |
| | | 232 | | List<BreadcrumbDto> articleBreadcrumbs, |
| | | 233 | | HierarchyWalkOptions options) |
| | | 234 | | { |
| | 3 | 235 | | var groups = new List<BreadcrumbDto>(); |
| | | 236 | | |
| | 3 | 237 | | var targetArticle = await _context.Articles |
| | 3 | 238 | | .AsNoTracking() |
| | 3 | 239 | | .Where(a => a.Id == articleId) |
| | 3 | 240 | | .Select(a => new { a.CampaignId, a.ArcId, a.ParentId, a.Type }) |
| | 3 | 241 | | .FirstOrDefaultAsync(); |
| | | 242 | | |
| | 3 | 243 | | if (targetArticle == null) |
| | 0 | 244 | | return groups; |
| | | 245 | | |
| | 3 | 246 | | if (targetArticle.CampaignId.HasValue) |
| | | 247 | | { |
| | | 248 | | // Session-type article: add Campaign breadcrumb |
| | 1 | 249 | | var campaign = await _context.Campaigns |
| | 1 | 250 | | .AsNoTracking() |
| | 1 | 251 | | .Where(c => c.Id == targetArticle.CampaignId.Value) |
| | 1 | 252 | | .Select(c => new { c.Id, c.Name }) |
| | 1 | 253 | | .FirstOrDefaultAsync(); |
| | | 254 | | |
| | 1 | 255 | | if (campaign != null) |
| | | 256 | | { |
| | 1 | 257 | | groups.Add(new BreadcrumbDto |
| | 1 | 258 | | { |
| | 1 | 259 | | Id = campaign.Id, |
| | 1 | 260 | | Title = campaign.Name, |
| | 1 | 261 | | Slug = campaign.Name.ToLowerInvariant().Replace(" ", "-"), |
| | 1 | 262 | | Type = default, |
| | 1 | 263 | | IsWorld = false |
| | 1 | 264 | | }); |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | // Add Arc breadcrumb if present |
| | 1 | 268 | | if (targetArticle.ArcId.HasValue) |
| | | 269 | | { |
| | 1 | 270 | | var arc = await _context.Arcs |
| | 1 | 271 | | .AsNoTracking() |
| | 1 | 272 | | .Where(a => a.Id == targetArticle.ArcId.Value) |
| | 1 | 273 | | .Select(a => new { a.Id, a.Name }) |
| | 1 | 274 | | .FirstOrDefaultAsync(); |
| | | 275 | | |
| | 1 | 276 | | if (arc != null) |
| | | 277 | | { |
| | 1 | 278 | | groups.Add(new BreadcrumbDto |
| | 1 | 279 | | { |
| | 1 | 280 | | Id = arc.Id, |
| | 1 | 281 | | Title = arc.Name, |
| | 1 | 282 | | Slug = arc.Name.ToLowerInvariant().Replace(" ", "-"), |
| | 1 | 283 | | Type = default, |
| | 1 | 284 | | IsWorld = false |
| | 1 | 285 | | }); |
| | | 286 | | } |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | else |
| | | 290 | | { |
| | | 291 | | // Non-session: determine root article type by walking up the collected breadcrumbs |
| | 2 | 292 | | var rootArticleType = targetArticle.Type; |
| | 2 | 293 | | if (articleBreadcrumbs.Count > 0) |
| | | 294 | | { |
| | 2 | 295 | | rootArticleType = articleBreadcrumbs[0].Type; |
| | | 296 | | } |
| | | 297 | | else |
| | | 298 | | { |
| | | 299 | | // Breadcrumbs may be empty if IncludeCurrentArticle was false; |
| | | 300 | | // walk up manually to find root type |
| | 0 | 301 | | var currentParentId = targetArticle.ParentId; |
| | 0 | 302 | | while (currentParentId.HasValue) |
| | | 303 | | { |
| | 0 | 304 | | var parentArticle = await _context.Articles |
| | 0 | 305 | | .AsNoTracking() |
| | 0 | 306 | | .Where(a => a.Id == currentParentId.Value) |
| | 0 | 307 | | .Select(a => new { a.Type, a.ParentId }) |
| | 0 | 308 | | .FirstOrDefaultAsync(); |
| | | 309 | | |
| | 0 | 310 | | if (parentArticle == null) |
| | | 311 | | break; |
| | | 312 | | |
| | 0 | 313 | | rootArticleType = parentArticle.Type; |
| | 0 | 314 | | currentParentId = parentArticle.ParentId; |
| | | 315 | | } |
| | 0 | 316 | | } |
| | | 317 | | |
| | 2 | 318 | | if (rootArticleType == ArticleType.Character) |
| | | 319 | | { |
| | 1 | 320 | | groups.Add(new BreadcrumbDto |
| | 1 | 321 | | { |
| | 1 | 322 | | Id = Guid.Empty, |
| | 1 | 323 | | Title = "Player Characters", |
| | 1 | 324 | | Slug = "characters", |
| | 1 | 325 | | Type = default, |
| | 1 | 326 | | IsWorld = false |
| | 1 | 327 | | }); |
| | | 328 | | } |
| | 1 | 329 | | else if (rootArticleType == ArticleType.WikiArticle) |
| | | 330 | | { |
| | 1 | 331 | | groups.Add(new BreadcrumbDto |
| | 1 | 332 | | { |
| | 1 | 333 | | Id = Guid.Empty, |
| | 1 | 334 | | Title = "Wiki", |
| | 1 | 335 | | Slug = "wiki", |
| | 1 | 336 | | Type = default, |
| | 1 | 337 | | IsWorld = false |
| | 1 | 338 | | }); |
| | | 339 | | } |
| | | 340 | | } |
| | | 341 | | |
| | 3 | 342 | | return groups; |
| | 3 | 343 | | } |
| | | 344 | | } |