| | | 1 | | using Chronicis.Api.Repositories; |
| | | 2 | | using Chronicis.Shared.Extensions; |
| | | 3 | | using Microsoft.Extensions.Caching.Memory; |
| | | 4 | | |
| | | 5 | | namespace Chronicis.Api.Services.ExternalLinks; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Consolidated service for external link operations. |
| | | 9 | | /// Resolves providers via <see cref="IExternalLinkProviderRegistry"/>, |
| | | 10 | | /// centralizes caching patterns, and standardizes exception handling. |
| | | 11 | | /// </summary> |
| | | 12 | | public class ExternalLinkService : IExternalLinkService |
| | | 13 | | { |
| | 1 | 14 | | private static readonly TimeSpan SuggestionCacheDuration = TimeSpan.FromMinutes(2); |
| | 1 | 15 | | private static readonly TimeSpan ContentCacheDuration = TimeSpan.FromMinutes(5); |
| | | 16 | | |
| | | 17 | | private readonly IExternalLinkProviderRegistry _registry; |
| | | 18 | | private readonly IResourceProviderRepository _resourceProviderRepository; |
| | | 19 | | private readonly IMemoryCache _cache; |
| | | 20 | | private readonly ILogger<ExternalLinkService> _logger; |
| | | 21 | | |
| | 22 | 22 | | public ExternalLinkService( |
| | 22 | 23 | | IExternalLinkProviderRegistry registry, |
| | 22 | 24 | | IResourceProviderRepository resourceProviderRepository, |
| | 22 | 25 | | IMemoryCache cache, |
| | 22 | 26 | | ILogger<ExternalLinkService> logger) |
| | | 27 | | { |
| | 22 | 28 | | _registry = registry; |
| | 22 | 29 | | _resourceProviderRepository = resourceProviderRepository; |
| | 22 | 30 | | _cache = cache; |
| | 22 | 31 | | _logger = logger; |
| | 22 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public async Task<IReadOnlyList<ExternalLinkSuggestion>> GetSuggestionsAsync( |
| | | 36 | | Guid? worldId, |
| | | 37 | | string source, |
| | | 38 | | string query, |
| | | 39 | | CancellationToken ct) |
| | | 40 | | { |
| | 8 | 41 | | if (string.IsNullOrWhiteSpace(source)) |
| | | 42 | | { |
| | 1 | 43 | | return Array.Empty<ExternalLinkSuggestion>(); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // Check world-level provider enablement |
| | 7 | 47 | | if (worldId.HasValue) |
| | | 48 | | { |
| | 2 | 49 | | var worldProviders = await _resourceProviderRepository.GetWorldProvidersAsync(worldId.Value); |
| | 4 | 50 | | var enabledProvider = worldProviders.FirstOrDefault(p => p.Provider.Code == source && p.IsEnabled); |
| | | 51 | | |
| | 2 | 52 | | if (enabledProvider == default) |
| | | 53 | | { |
| | 1 | 54 | | _logger.LogDebugSanitized( |
| | 1 | 55 | | "Provider {Source} is not enabled for world {WorldId}", source, worldId); |
| | 1 | 56 | | return Array.Empty<ExternalLinkSuggestion>(); |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | 6 | 60 | | query ??= string.Empty; |
| | | 61 | | |
| | 6 | 62 | | var cacheKey = BuildSuggestionCacheKey(source, query); |
| | 6 | 63 | | if (_cache.TryGetValue<IReadOnlyList<ExternalLinkSuggestion>>(cacheKey, out var cached) && cached != null) |
| | | 64 | | { |
| | 1 | 65 | | return cached; |
| | | 66 | | } |
| | | 67 | | |
| | 5 | 68 | | var provider = _registry.GetProvider(source); |
| | 5 | 69 | | if (provider == null) |
| | | 70 | | { |
| | 1 | 71 | | return Array.Empty<ExternalLinkSuggestion>(); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | IReadOnlyList<ExternalLinkSuggestion> suggestions; |
| | | 75 | | try |
| | | 76 | | { |
| | 4 | 77 | | suggestions = await provider.SearchAsync(query, ct); |
| | 3 | 78 | | } |
| | 1 | 79 | | catch (Exception ex) |
| | | 80 | | { |
| | 1 | 81 | | _logger.LogErrorSanitized( |
| | 1 | 82 | | ex, |
| | 1 | 83 | | "External link provider {Source} failed to search for query {Query}", |
| | 1 | 84 | | source, query); |
| | 1 | 85 | | return Array.Empty<ExternalLinkSuggestion>(); |
| | | 86 | | } |
| | | 87 | | |
| | 3 | 88 | | _cache.Set(cacheKey, suggestions, SuggestionCacheDuration); |
| | 3 | 89 | | return suggestions; |
| | 8 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | | 93 | | public async Task<ExternalLinkContent?> GetContentAsync( |
| | | 94 | | string source, |
| | | 95 | | string id, |
| | | 96 | | CancellationToken ct) |
| | | 97 | | { |
| | 6 | 98 | | if (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(id)) |
| | | 99 | | { |
| | 2 | 100 | | return null; |
| | | 101 | | } |
| | | 102 | | |
| | 4 | 103 | | var cacheKey = BuildContentCacheKey(source, id); |
| | 4 | 104 | | if (_cache.TryGetValue<ExternalLinkContent>(cacheKey, out var cached) && cached != null) |
| | | 105 | | { |
| | 1 | 106 | | return cached; |
| | | 107 | | } |
| | | 108 | | |
| | 3 | 109 | | var provider = _registry.GetProvider(source); |
| | 3 | 110 | | if (provider == null) |
| | | 111 | | { |
| | 1 | 112 | | return null; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | ExternalLinkContent content; |
| | | 116 | | try |
| | | 117 | | { |
| | 2 | 118 | | content = await provider.GetContentAsync(id, ct); |
| | 1 | 119 | | } |
| | 1 | 120 | | catch (Exception ex) |
| | | 121 | | { |
| | 1 | 122 | | _logger.LogErrorSanitized( |
| | 1 | 123 | | ex, |
| | 1 | 124 | | "External link provider {Source} failed to get content for {Id}", |
| | 1 | 125 | | source, id); |
| | 1 | 126 | | return null; |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | _cache.Set(cacheKey, content, ContentCacheDuration); |
| | 1 | 130 | | return content; |
| | 6 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <inheritdoc /> |
| | | 134 | | public bool TryValidateSource(string source, out string error) |
| | | 135 | | { |
| | 4 | 136 | | if (string.IsNullOrWhiteSpace(source)) |
| | | 137 | | { |
| | 1 | 138 | | error = "Source is required."; |
| | 1 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | |
| | 3 | 142 | | var provider = _registry.GetProvider(source); |
| | 3 | 143 | | if (provider == null) |
| | | 144 | | { |
| | 2 | 145 | | var available = _registry |
| | 2 | 146 | | .GetAllProviders() |
| | 1 | 147 | | .Select(p => p.Key) |
| | 0 | 148 | | .OrderBy(key => key, StringComparer.OrdinalIgnoreCase) |
| | 2 | 149 | | .ToList(); |
| | | 150 | | |
| | 2 | 151 | | error = available.Count == 0 |
| | 2 | 152 | | ? $"Unknown external link source '{source}'." |
| | 2 | 153 | | : $"Unknown external link source '{source}'. Available sources: {string.Join(", ", available)}."; |
| | 2 | 154 | | return false; |
| | | 155 | | } |
| | | 156 | | |
| | 1 | 157 | | error = string.Empty; |
| | 1 | 158 | | return true; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <inheritdoc /> |
| | | 162 | | public bool TryValidateId(string source, string id, out string error) |
| | | 163 | | { |
| | 4 | 164 | | if (string.IsNullOrWhiteSpace(id)) |
| | | 165 | | { |
| | 1 | 166 | | error = "Id is required."; |
| | 1 | 167 | | return false; |
| | | 168 | | } |
| | | 169 | | |
| | 3 | 170 | | if (Uri.TryCreate(id, UriKind.Absolute, out _)) |
| | | 171 | | { |
| | 1 | 172 | | error = "External link id must be a relative path."; |
| | 1 | 173 | | return false; |
| | | 174 | | } |
| | | 175 | | |
| | 2 | 176 | | if (!Uri.TryCreate(id, UriKind.Relative, out _)) |
| | | 177 | | { |
| | 0 | 178 | | error = "External link id must be a relative path."; |
| | 0 | 179 | | return false; |
| | | 180 | | } |
| | | 181 | | |
| | 2 | 182 | | if (source.Equals("srd", StringComparison.OrdinalIgnoreCase) |
| | 2 | 183 | | && !id.StartsWith("/api/", StringComparison.OrdinalIgnoreCase)) |
| | | 184 | | { |
| | 1 | 185 | | error = "SRD ids must start with /api/."; |
| | 1 | 186 | | return false; |
| | | 187 | | } |
| | | 188 | | |
| | 1 | 189 | | error = string.Empty; |
| | 1 | 190 | | return true; |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | // -- Cache key helpers -- |
| | | 194 | | |
| | | 195 | | internal static string BuildSuggestionCacheKey(string source, string query) |
| | 7 | 196 | | => $"external-links:suggestions:{source}:{query}".ToLowerInvariant(); |
| | | 197 | | |
| | | 198 | | internal static string BuildContentCacheKey(string source, string id) |
| | 5 | 199 | | => $"external-links:content:{source}:{id}".ToLowerInvariant(); |
| | | 200 | | } |