예제 #1
0
        public void Extend(ExecutionContext context)
        {
            context.Engine.SetValue("contentAction", new EventDelegate(() =>
            {
                if (context.TryGetValue("event", out var temp) && temp is EnrichedContentEvent contentEvent)
                {
                    return(contentEvent.Status.ToString());
                }

                return(JsValue.Null);
            }));

            context.Engine.SetValue("contentUrl", new EventDelegate(() =>
            {
                if (context.TryGetValue("event", out var temp) && temp is EnrichedContentEvent contentEvent)
                {
                    return(urlGenerator.ContentUI(contentEvent.AppId, contentEvent.SchemaId, contentEvent.Id));
                }

                return(JsValue.Null);
            }));

            context.Engine.SetValue("assetContentUrl", new EventDelegate(() =>
            {
                if (context.TryGetValue("event", out var temp) && temp is EnrichedAssetEvent assetEvent)
                {
                    return(urlGenerator.AssetContent(assetEvent.AppId, assetEvent.Id.ToString()));
                }

                return(JsValue.Null);
            }));

            context.Engine.SetValue("assetContentAppUrl", new EventDelegate(() =>
            {
                if (context.TryGetValue("event", out var temp) && temp is EnrichedAssetEvent assetEvent)
                {
                    return(urlGenerator.AssetContent(assetEvent.AppId, assetEvent.Id.ToString()));
                }

                return(JsValue.Null);
            }));

            context.Engine.SetValue("assetContentSlugUrl", new EventDelegate(() =>
            {
                if (context.TryGetValue("event", out var temp) && temp is EnrichedAssetEvent assetEvent)
                {
                    return(urlGenerator.AssetContent(assetEvent.AppId, assetEvent.FileName.Slugify()));
                }

                return(JsValue.Null);
            }));
        }
예제 #2
0
        public RuleEventFormatterTests()
        {
            A.CallTo(() => urlGenerator.ContentUI(appId, schemaId, contentId))
            .Returns("content-url");

            A.CallTo(() => urlGenerator.AssetContent(assetId))
            .Returns("asset-content-url");

            A.CallTo(() => user.Id)
            .Returns("user123");

            A.CallTo(() => user.Email)
            .Returns("*****@*****.**");

            A.CallTo(() => user.Claims)
            .Returns(new List <Claim> {
                new Claim(SquidexClaimTypes.DisplayName, "me")
            });

            var extensions = new IScriptExtension[]
            {
                new DateTimeScriptExtension(),
                new EventScriptExtension(urlGenerator),
                new StringScriptExtension()
            };

            var cache = new MemoryCache(Options.Create(new MemoryCacheOptions()));

            sut = new RuleEventFormatter(TestUtils.DefaultSerializer, urlGenerator, new JintScriptEngine(cache, extensions));
        }
예제 #3
0
        public static ValueConverter ResolveAssetUrls(NamedId <DomainId> appId, IReadOnlyCollection <string>?fields, IUrlGenerator urlGenerator)
        {
            if (fields?.Any() != true)
            {
                return(Noop);
            }

            Func <IField, IField?, bool> shouldHandle;

            if (fields.Contains("*"))
            {
                shouldHandle = (field, parent) => true;
            }
            else
            {
                var paths = fields.Select(x => x.Split('.')).ToList();

                shouldHandle = (field, parent) =>
                {
                    for (var i = 0; i < paths.Count; i++)
                    {
                        var path = paths[i];

                        if (parent != null)
                        {
                            if (path.Length == 2 && path[0] == parent.Name && path[1] == field.Name)
                            {
                                return(true);
                            }
                        }
                        else
                        {
                            if (path.Length == 1 && path[0] == field.Name)
                            {
                                return(true);
                            }
                        }
                    }

                    return(false);
                };
            }

            return((value, field, parent) =>
            {
                if (field is IField <AssetsFieldProperties> && value is JsonArray array && shouldHandle(field, parent))
                {
                    for (var i = 0; i < array.Count; i++)
                    {
                        var id = array[i].ToString();

                        array[i] = JsonValue.Create(urlGenerator.AssetContent(appId, id));
                    }
                }

                return value;
            });
        }
예제 #4
0
        private FluidValue AssetContentUrl(FluidValue input, FilterArguments arguments, TemplateContext context)
        {
            var value = input.ToObjectValue();

            switch (value)
            {
            case DomainId id:
            {
                if (context.GetValue("event")?.ToObjectValue() is EnrichedAssetEvent assetEvent)
                {
                    var result = urlGenerator.AssetContent(assetEvent.AppId, id.ToString());

                    return(new StringValue(result));
                }

                break;
            }

            case EnrichedAssetEvent assetEvent:
            {
                var result = urlGenerator.AssetContent(assetEvent.AppId, assetEvent.Id.ToString());

                return(new StringValue(result));
            }
            }

            return(NilValue.Empty);
        }
예제 #5
0
        private FluidValue AssetContentUrl(FluidValue input, FilterArguments arguments, TemplateContext context)
        {
            if (input is ObjectValue objectValue)
            {
                if (objectValue.ToObjectValue() is Guid guid && guid != Guid.Empty)
                {
                    var result = urlGenerator.AssetContent(guid);

                    return(new StringValue(result));
                }
            }

            return(NilValue.Empty);
        }
예제 #6
0
        private void ResolveAssetsUrls(ISchemaEntity schema, ResolvedComponents components,
                                       IGrouping <DomainId, ContentEntity> contents, ILookup <DomainId, IEnrichedAssetEntity> assets)
        {
            foreach (var field in schema.SchemaDef.ResolvingAssets())
            {
                foreach (var content in contents)
                {
                    content.ReferenceData ??= new ContentData();

                    var fieldReference = content.ReferenceData.GetOrAdd(field.Name, _ => new ContentFieldData()) !;

                    if (content.Data.TryGetValue(field.Name, out var fieldData) && fieldData != null)
                    {
                        foreach (var(partitionKey, partitionValue) in fieldData)
                        {
                            var referencedAsset =
                                field.GetReferencedIds(partitionValue, components)
                                .Select(x => assets[x])
                                .SelectMany(x => x)
                                .FirstOrDefault();

                            if (referencedAsset != null)
                            {
                                IJsonValue array;

                                if (IsImage(referencedAsset))
                                {
                                    var url = urlGenerator.AssetContent(
                                        referencedAsset.AppId,
                                        referencedAsset.Id.ToString());

                                    array = JsonValue.Array(url, referencedAsset.FileName);
                                }
                                else
                                {
                                    array = JsonValue.Array(referencedAsset.FileName);
                                }

                                requestCache.AddDependency(referencedAsset.UniqueId, referencedAsset.Version);

                                fieldReference.AddLocalized(partitionKey, array);
                            }
                        }
                    }
                }
            }
        }
예제 #7
0
        private FluidValue AssetContentUrl(FluidValue input, FilterArguments arguments, TemplateContext context)
        {
            if (input is ObjectValue objectValue)
            {
                if (context.GetValue("event")?.ToObjectValue() is EnrichedEvent @event)
                {
                    if (objectValue.ToObjectValue() is DomainId id && id != DomainId.Empty)
                    {
                        var result = urlGenerator.AssetContent(@event.AppId, id);

                        return(new StringValue(result));
                    }
                }
            }

            return(NilValue.Empty);
        }
예제 #8
0
        public ResolveAssetsTests()
        {
            requestContext = new Context(Mocks.FrontendUser(), Mocks.App(appId, Language.DE));

            var schemaDef =
                new Schema(schemaId.Name)
                .AddAssets(1, "asset1", Partitioning.Invariant, new AssetsFieldProperties
            {
                ResolveFirst = true,
                MinItems     = 2,
                MaxItems     = 3
            })
                .AddAssets(2, "asset2", Partitioning.Language, new AssetsFieldProperties
            {
                ResolveFirst = true,
                MinItems     = 1,
                MaxItems     = 1
            })
                .SetFieldsInLists("asset1", "asset2");

            A.CallTo(() => urlGenerator.AssetContent(appId, A <string> ._))
            .ReturnsLazily(ctx => $"url/to/{ctx.GetArgument<string>(1)}");

            schemaProvider = x =>
            {
                if (x == schemaId.Id)
                {
                    return(Task.FromResult(Mocks.Schema(appId, schemaId, schemaDef)));
                }
                else
                {
                    throw new DomainObjectNotFoundException(x.ToString());
                }
            };

            sut = new ResolveAssets(urlGenerator, assetQuery, requestCache);
        }
예제 #9
0
        public static FieldConverter ResolveAssetUrls(IReadOnlyCollection <string>?fields, IUrlGenerator urlGenerator)
        {
            if (fields?.Any() != true)
            {
                return((data, field) => data);
            }

            bool ShouldHandle(IField field, IField?parent = null)
            {
                if (field is IField <AssetsFieldProperties> )
                {
                    if (fields.Contains("*"))
                    {
                        return(true);
                    }

                    if (parent == null)
                    {
                        return(fields.Contains(field.Name));
                    }
                    else
                    {
                        return(fields.Contains($"{parent.Name}.{field.Name}"));
                    }
                }

                return(false);
            }

            void Resolve(IJsonValue value)
            {
                if (value is JsonArray array)
                {
                    for (var i = 0; i < array.Count; i++)
                    {
                        var id = array[i].ToString();

                        array[i] = JsonValue.Create(urlGenerator.AssetContent(Guid.Parse(id)));
                    }
                }
            }

            return((data, field) =>
            {
                if (ShouldHandle(field))
                {
                    foreach (var partition in data)
                    {
                        Resolve(partition.Value);
                    }
                }
                else if (field is IArrayField arrayField)
                {
                    foreach (var partition in data)
                    {
                        if (partition.Value is JsonArray array)
                        {
                            for (var i = 0; i < array.Count; i++)
                            {
                                if (array[i] is JsonObject arrayItem)
                                {
                                    foreach (var(key, value) in arrayItem)
                                    {
                                        if (arrayField.FieldsByName.TryGetValue(key, out var nestedField) && ShouldHandle(nestedField, field))
                                        {
                                            Resolve(value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                return data;
            });
        }
예제 #10
0
 public ValueConvertersTests()
 {
     A.CallTo(() => urlGenerator.AssetContent(A <Guid> ._))
     .ReturnsLazily(ctx => $"url/to/{ctx.GetArgument<Guid>(0)}");
 }
예제 #11
0
 public ValueConvertersTests()
 {
     A.CallTo(() => urlGenerator.AssetContent(appId, A <string> ._))
     .ReturnsLazily(ctx => $"url/to/{ctx.GetArgument<string>(1)}");
 }