|
|
|
@ -5,6 +5,7 @@ |
|
|
|
// All rights reserved. Licensed under the MIT license.
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
@ -13,8 +14,10 @@ using System.Threading.Tasks; |
|
|
|
using Fluid; |
|
|
|
using Fluid.Ast; |
|
|
|
using Fluid.Tags; |
|
|
|
using GraphQL.Utilities; |
|
|
|
using Squidex.Domain.Apps.Core.Rules.EnrichedEvents; |
|
|
|
using Squidex.Domain.Apps.Core.Templates; |
|
|
|
using Squidex.Domain.Apps.Entities.Apps; |
|
|
|
using Squidex.Infrastructure; |
|
|
|
|
|
|
|
#pragma warning disable CA1826 // Do not use Enumerable methods on indexable collections
|
|
|
|
@ -23,23 +26,22 @@ namespace Squidex.Domain.Apps.Entities.Contents |
|
|
|
{ |
|
|
|
public sealed class ReferencesFluidExtension : IFluidExtension |
|
|
|
{ |
|
|
|
private readonly IAppProvider appProvider; |
|
|
|
private readonly IContentQueryService contentQuery; |
|
|
|
private readonly IServiceProvider serviceProvider; |
|
|
|
|
|
|
|
private sealed class ReferenceTag : ArgumentsTag |
|
|
|
{ |
|
|
|
private readonly ReferencesFluidExtension root; |
|
|
|
private readonly IServiceProvider serviceProvider; |
|
|
|
|
|
|
|
public ReferenceTag(ReferencesFluidExtension root) |
|
|
|
public ReferenceTag(IServiceProvider serviceProvider) |
|
|
|
{ |
|
|
|
this.root = root; |
|
|
|
this.serviceProvider = serviceProvider; |
|
|
|
} |
|
|
|
|
|
|
|
public override async ValueTask<Completion> WriteToAsync(TextWriter writer, TextEncoder encoder, TemplateContext context, FilterArgument[] arguments) |
|
|
|
{ |
|
|
|
if (arguments.Length == 2 && context.GetValue("event")?.ToObjectValue() is EnrichedEvent enrichedEvent) |
|
|
|
{ |
|
|
|
var app = await root.appProvider.GetAppAsync(enrichedEvent.AppId.Id, false); |
|
|
|
var app = await GetAppAsync(enrichedEvent); |
|
|
|
|
|
|
|
if (app == null) |
|
|
|
{ |
|
|
|
@ -57,7 +59,9 @@ namespace Squidex.Domain.Apps.Entities.Contents |
|
|
|
var domainId = DomainId.Create(id); |
|
|
|
var domainIds = new List<DomainId> { domainId }; |
|
|
|
|
|
|
|
var contents = await root.contentQuery.QueryAsync(requestContext, Q.Empty.WithIds(domainIds)); |
|
|
|
var contentQuery = serviceProvider.GetRequiredService<IContentQueryService>(); |
|
|
|
|
|
|
|
var contents = await contentQuery.QueryAsync(requestContext, Q.Empty.WithIds(domainIds)); |
|
|
|
var content = contents.FirstOrDefault(); |
|
|
|
|
|
|
|
if (content != null) |
|
|
|
@ -70,16 +74,20 @@ namespace Squidex.Domain.Apps.Entities.Contents |
|
|
|
|
|
|
|
return Completion.Normal; |
|
|
|
} |
|
|
|
|
|
|
|
private Task<IAppEntity?> GetAppAsync(EnrichedEvent enrichedEvent) |
|
|
|
{ |
|
|
|
var appProvider = serviceProvider.GetRequiredService<IAppProvider>(); |
|
|
|
|
|
|
|
return appProvider.GetAppAsync(enrichedEvent.AppId.Id, false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public ReferencesFluidExtension(IAppProvider appProvider, IContentQueryService contentQuery) |
|
|
|
public ReferencesFluidExtension(IServiceProvider serviceProvider) |
|
|
|
{ |
|
|
|
Guard.NotNull(contentQuery, nameof(contentQuery)); |
|
|
|
Guard.NotNull(appProvider, nameof(appProvider)); |
|
|
|
|
|
|
|
this.contentQuery = contentQuery; |
|
|
|
Guard.NotNull(serviceProvider, nameof(serviceProvider)); |
|
|
|
|
|
|
|
this.appProvider = appProvider; |
|
|
|
this.serviceProvider = serviceProvider; |
|
|
|
} |
|
|
|
|
|
|
|
public void RegisterGlobalTypes(IMemberAccessStrategy memberAccessStrategy) |
|
|
|
@ -94,7 +102,7 @@ namespace Squidex.Domain.Apps.Entities.Contents |
|
|
|
|
|
|
|
public void RegisterLanguageExtensions(FluidParserFactory factory) |
|
|
|
{ |
|
|
|
factory.RegisterTag("reference", new ReferenceTag(this)); |
|
|
|
factory.RegisterTag("reference", new ReferenceTag(serviceProvider)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|