示例#1
0
 private Task CacheItAsync(IAppEntity app, bool publish)
 {
     return(Task.WhenAll(
                replicatedCache.AddAsync(GetCacheKey(app.Id), app, CacheDuration, publish),
                replicatedCache.AddAsync(GetCacheKey(app.Name), app, CacheDuration, publish)));
 }
示例#2
0
 private Task CacheItAsync(IAppEntity app)
 {
     return(Task.WhenAll(
                grainCache.AddAsync(GetCacheKey(app.Id), app, CacheDuration),
                grainCache.AddAsync(GetCacheKey(app.Name), app, CacheDuration)));
 }
        public async Task <List <(IContentEntity Content, ISchemaEntity Schema)> > QueryAsync(IAppEntity app, Status[]?status, HashSet <Guid> ids, bool includeDraft = true)
        {
            Guard.NotNull(app);

            using (Profiler.TraceMethod <MongoContentRepository>("QueryAsyncByIdsWithoutSchema"))
            {
                var result = await queryContentsById.DoAsync(app.Id, null, ids, status, includeDraft);

                return(result);
            }
        }
示例#4
0
        public async Task <List <Guid>?> SearchAsync(string?queryText, IAppEntity app, SearchFilter?filter, SearchScope scope)
        {
            var serveField = GetServeField(scope);

            var query = new
            {
                query = new
                {
                    @bool = new
                    {
                        must = new List <object>
                        {
                            new
                            {
                                term = new Dictionary <string, object>
                                {
                                    ["appId.keyword"] = app.Id
                                }
                            },
                            new
                            {
                                term = new Dictionary <string, string>
                                {
                                    [serveField] = "true"
                                }
                            },
                            new
                            {
                                multi_match = new
                                {
                                    fields = new[]
                                    {
                                        "texts.*"
                                    },
                                    query = queryText
                                }
                            }
                        },
                        should = new List <object>()
                    }
                },
                _source = new[]
                {
                    "contentId"
                },
                size = 2000
            };

            if (filter?.SchemaIds.Count > 0)
            {
                var bySchema = new
                {
                    term = new Dictionary <string, object>
                    {
                        ["schemaId.keyword"] = filter.SchemaIds
                    }
                };

                if (filter.Must)
                {
                    [email protected](bySchema);
                }
                else
                {
                    [email protected](bySchema);
                }
            }

            var result = await client.SearchAsync <DynamicResponse>(IndexName, CreatePost(query));

            if (!result.Success)
            {
                throw result.OriginalException;
            }

            var ids = new List <Guid>();

            foreach (var item in result.Body.hits.hits)
            {
                if (item != null)
                {
                    ids.Add(Guid.Parse(item["_source"]["contentId"]));
                }
            }

            return(ids);
        }
示例#5
0
        public async Task <List <DomainId> > SearchAsync(IAppEntity app, GeoQuery query, SearchScope scope,
                                                         CancellationToken ct = default)
        {
            Guard.NotNull(app, nameof(app));
            Guard.NotNull(query, nameof(query));

            var serveField = GetServeField(scope);

            var elasticQuery = new
            {
                query = new
                {
                    @bool = new
                    {
                        filter = new object[]
                        {
                            new
                            {
                                term = new Dictionary <string, object>
                                {
                                    ["schemaId.keyword"] = query.SchemaId.ToString()
                                }
                            },
                            new
                            {
                                term = new Dictionary <string, string>
                                {
                                    ["geoField.keyword"] = query.Field
                                }
                            },
                            new
                            {
                                term = new Dictionary <string, string>
                                {
                                    [serveField] = "true"
                                }
                            },
                            new
                            {
                                geo_distance = new
                                {
                                    geoObject = new
                                    {
                                        lat = query.Latitude,
                                        lon = query.Longitude
                                    },
                                    distance = $"{query.Radius}m"
                                }
                            }
                        },
                    }
                },
                _source = new[]
                {
                    "contentId"
                },
                size = query.Take
            };

            return(await SearchAsync(elasticQuery, ct));
        }
示例#6
0
 public GraphQLQueryContext(IAppEntity app, IAssetRepository assetRepository, IContentQueryService contentQuery, ClaimsPrincipal user,
                            IGraphQLUrlGenerator urlGenerator)
     : base(app, assetRepository, contentQuery, user)
 {
     UrlGenerator = urlGenerator;
 }
示例#7
0
        public override async Task <List <DomainId>?> SearchAsync(IAppEntity app, TextQuery query, SearchScope scope,
                                                                  CancellationToken ct = default)
        {
            Guard.NotNull(app);
            Guard.NotNull(query);

            var(search, take) = query;

            if (string.IsNullOrWhiteSpace(search))
            {
                return(null);
            }

            var luceneQuery = QueryParser.Parse(search);

            var serveField = scope == SearchScope.All ? "fa" : "fp";

            var compound = new BsonDocument
            {
                ["must"] = new BsonArray
                {
                    QueryVisitor.Visit(luceneQuery)
                },
                ["filter"] = new BsonArray
                {
                    new BsonDocument
                    {
                        ["text"] = new BsonDocument
                        {
                            ["path"]  = "_ai",
                            ["query"] = app.Id.ToString()
                        }
                    },
                    new BsonDocument
                    {
                        ["equals"] = new BsonDocument
                        {
                            ["path"]  = serveField,
                            ["value"] = true
                        }
                    }
                }
            };

            if (query.PreferredSchemaId != null)
            {
                compound["should"] = new BsonArray
                {
                    new BsonDocument
                    {
                        ["text"] = new BsonDocument
                        {
                            ["path"]  = "_si",
                            ["query"] = query.PreferredSchemaId.Value.ToString()
                        }
                    }
                };
            }
            else if (query.RequiredSchemaIds?.Count > 0)
            {
                compound["should"] = new BsonArray(query.RequiredSchemaIds.Select(x =>
                                                                                  new BsonDocument
                {
                    ["text"] = new BsonDocument
                    {
                        ["path"]  = "_si",
                        ["query"] = x.ToString()
                    }
                }));

                compound["minimumShouldMatch"] = 1;
            }

            var searchQuery = new BsonDocument
            {
                ["compound"] = compound,
            };

            if (index != null)
            {
                searchQuery["index"] = index;
            }

            var results =
                await Collection.Aggregate().Search(searchQuery).Limit(take)
                .Project <MongoTextResult>(
                    Projection.Include(x => x.ContentId)
                    )
                .ToListAsync(ct);

            return(results.Select(x => x.ContentId).ToList());
        }
示例#8
0
        private static JsonSchema BuildJsonSchema(Schema schema, IAppEntity app, bool withHiddenFields)
        {
            var dataSchema = schema.BuildJsonSchema(app.PartitionResolver(), (n, s) => s, withHiddenFields);

            return(new ContentSchemaBuilder().CreateContentSchema(schema, dataSchema));
        }
示例#9
0
 private static string BuildJsonCacheKey(IAppEntity app, ISchemaEntity schema, bool withHidden)
 {
     return($"JSON/{app.Version}/{schema.Id}_{schema.Version}/{withHidden}");
 }
示例#10
0
        private ContributorsDto WithPlan(IAppEntity app, IAppPlansProvider plans)
        {
            MaxContributors = plans.GetPlanForApp(app).MaxContributors;

            return(this);
        }
 Task IDeleter.DeleteAppAsync(IAppEntity app,
                              CancellationToken ct)
 {
     return(Collection.DeleteManyAsync(Filter.Eq(x => x.IndexedAppId, app.Id), ct));
 }
示例#12
0
 public string?GenerateAssetSourceUrl(IAppEntity app, IAssetEntity asset)
 {
     return(assetStore.GeneratePublicUrl(asset.Id.ToString(), asset.FileVersion, null));
 }
示例#13
0
 public string GenerateContentUrl(IAppEntity app, ISchemaEntity schema, IContentEntity content)
 {
     return(urlsOptions.BuildUrl($"api/content/{app.Name}/{schema.SchemaDef.Name}/{content.Id}"));
 }
示例#14
0
        private static JsonSchema BuildJsonSchema(Schema schema, IAppEntity app, bool withHiddenFields)
        {
            var dataSchema = schema.BuildJsonSchema(app.PartitionResolver(), (n, s) => s, withHiddenFields);

            return(BuildJsonSchema(schema.DisplayName(), dataSchema));
        }
示例#15
0
 private RolesDto GetResponse(IAppEntity result)
 {
     return(RolesDto.FromApp(result, Resources));
 }
 public Task <List <(IContentEntity Content, ISchemaEntity Schema)> > QueryAsync(IAppEntity app, HashSet <Guid> ids, SearchScope scope)
 {
     if (scope == SearchScope.All)
     {
         return(collectionAll.QueryAsync(app, ids));
     }
     else
     {
         return(collectionPublished.QueryAsync(app, ids));
     }
 }
示例#17
0
        public GraphQLTestBase()
        {
            app = Mocks.App(appId, Language.DE, Language.GermanGermany);

            A.CallTo(() => userResolver.QueryManyAsync(A <string[]> ._))
            .ReturnsLazily(x =>
            {
                var ids = x.GetArgument <string[]>(0) !;

                var users = ids.Select(id => UserMocks.User(id, $"{id}@email.com", $"name_{id}"));

                return(Task.FromResult(users.ToDictionary(x => x.Id)));
            });

            var schemaDef =
                new Schema(schemaId.Name)
                .Publish()
                .AddNumber(16, "2_numbers", Partitioning.Invariant,
                           new NumberFieldProperties())
                .AddNumber(17, "2-numbers", Partitioning.Invariant,
                           new NumberFieldProperties())
                .AddNumber(18, "content", Partitioning.Invariant,
                           new NumberFieldProperties())
                .AddJson(1, "my-json", Partitioning.Invariant,
                         new JsonFieldProperties())
                .AddString(2, "my-string", Partitioning.Language,
                           new StringFieldProperties())
                .AddString(3, "my-string2", Partitioning.Invariant,
                           new StringFieldProperties())
                .AddString(4, "my-localized", Partitioning.Language,
                           new StringFieldProperties())
                .AddNumber(5, "my-number", Partitioning.Invariant,
                           new NumberFieldProperties())
                .AddNumber(6, "my_number", Partitioning.Invariant,
                           new NumberFieldProperties())
                .AddAssets(7, "my-assets", Partitioning.Invariant,
                           new AssetsFieldProperties())
                .AddBoolean(8, "my-boolean", Partitioning.Invariant,
                            new BooleanFieldProperties())
                .AddDateTime(9, "my-datetime", Partitioning.Invariant,
                             new DateTimeFieldProperties())
                .AddReferences(10, "my-references", Partitioning.Invariant,
                               new ReferencesFieldProperties {
                SchemaId = schemaRefId1.Id
            })
                .AddReferences(11, "my-union", Partitioning.Invariant,
                               new ReferencesFieldProperties())
                .AddReferences(12, "my-invalid", Partitioning.Invariant,
                               new ReferencesFieldProperties {
                SchemaId = DomainId.NewGuid()
            })
                .AddGeolocation(13, "my-geolocation", Partitioning.Invariant,
                                new GeolocationFieldProperties())
                .AddTags(14, "my-tags", Partitioning.Invariant,
                         new TagsFieldProperties())
                .AddArray(20, "my-empty-array", Partitioning.Invariant, null,
                          new ArrayFieldProperties())
                .AddArray(15, "my-array", Partitioning.Invariant, f => f
                          .AddBoolean(121, "nested-boolean")
                          .AddNumber(122, "nested-number")
                          .AddNumber(123, "nested_number"))
                .SetScripts(new SchemaScripts {
                Query = "<query-script>"
            });

            schema = Mocks.Schema(appId, schemaId, schemaDef);

            var schemaRef1Def =
                new Schema(schemaRefId1.Name)
                .Publish()
                .AddString(1, "ref1-field", Partitioning.Invariant);

            schemaRef1 = Mocks.Schema(appId, schemaRefId1, schemaRef1Def);

            var schemaRef2Def =
                new Schema(schemaRefId2.Name)
                .Publish()
                .AddString(1, "ref2-field", Partitioning.Invariant);

            schemaRef2 = Mocks.Schema(appId, schemaRefId2, schemaRef2Def);

            var schemaInvalidNameDef =
                new Schema(schemaInvalidNameId.Name)
                .Publish()
                .AddString(1, "my-field", Partitioning.Invariant);

            schemaInvalidName = Mocks.Schema(appId, schemaInvalidNameId, schemaInvalidNameDef);

            requestContext = new Context(Mocks.FrontendUser(), app);

            sut = CreateSut();
        }
示例#18
0
        public async Task <List <(IContentEntity Content, ISchemaEntity Schema)> > QueryAsync(IAppEntity app, HashSet <Guid> ids, Status[]?status, bool includeDraft)
        {
            var find = Collection.Find(FilterFactory.IdsByApp(app.Id, ids, status));

            var contentItems = await find.WithoutDraft(includeDraft).ToListAsync();

            var schemaIds = contentItems.Select(x => x.IndexedSchemaId).ToList();
            var schemas   = await Task.WhenAll(schemaIds.Select(x => appProvider.GetSchemaAsync(app.Id, x)));

            var result = new List <(IContentEntity Content, ISchemaEntity Schema)>();

            foreach (var entity in contentItems)
            {
                var schema = schemas.FirstOrDefault(x => x?.Id == entity.IndexedSchemaId);

                if (schema != null)
                {
                    entity.ParseData(schema.SchemaDef, serializer);

                    result.Add((entity, schema));
                }
            }

            return(result);
        }
示例#19
0
        public GraphQLTestBase()
        {
            app = Mocks.App(appId, Language.DE, Language.GermanGermany);

            var schemaDef =
                new Schema(schemaId.Name)
                .Publish()
                .AddJson(1, "my-json", Partitioning.Invariant,
                         new JsonFieldProperties())
                .AddString(2, "my-string", Partitioning.Language,
                           new StringFieldProperties())
                .AddNumber(3, "my-number", Partitioning.Invariant,
                           new NumberFieldProperties())
                .AddNumber(4, "my_number", Partitioning.Invariant,
                           new NumberFieldProperties())
                .AddAssets(5, "my-assets", Partitioning.Invariant,
                           new AssetsFieldProperties())
                .AddBoolean(6, "my-boolean", Partitioning.Invariant,
                            new BooleanFieldProperties())
                .AddDateTime(7, "my-datetime", Partitioning.Invariant,
                             new DateTimeFieldProperties())
                .AddReferences(8, "my-references", Partitioning.Invariant,
                               new ReferencesFieldProperties {
                SchemaId = schemaRefId1.Id
            })
                .AddReferences(81, "my-union", Partitioning.Invariant,
                               new ReferencesFieldProperties())
                .AddReferences(9, "my-invalid", Partitioning.Invariant,
                               new ReferencesFieldProperties {
                SchemaId = Guid.NewGuid()
            })
                .AddGeolocation(10, "my-geolocation", Partitioning.Invariant,
                                new GeolocationFieldProperties())
                .AddTags(11, "my-tags", Partitioning.Invariant,
                         new TagsFieldProperties())
                .AddString(12, "my-localized", Partitioning.Language,
                           new StringFieldProperties())
                .AddArray(13, "my-array", Partitioning.Invariant, f => f
                          .AddBoolean(121, "nested-boolean")
                          .AddNumber(122, "nested-number")
                          .AddNumber(123, "nested_number"))
                .SetScripts(new SchemaScripts {
                Query = "<query-script>"
            });

            schema = Mocks.Schema(appId, schemaId, schemaDef);

            var schemaRef1Def =
                new Schema(schemaRefId1.Name)
                .Publish()
                .AddString(1, "ref1-field", Partitioning.Invariant);

            schemaRef1 = Mocks.Schema(appId, schemaRefId1, schemaRef1Def);

            var schemaRef2Def =
                new Schema(schemaRefId2.Name)
                .Publish()
                .AddString(1, "ref2-field", Partitioning.Invariant);

            schemaRef2 = Mocks.Schema(appId, schemaRefId2, schemaRef2Def);

            requestContext = new Context(Mocks.FrontendUser(), app);

            sut = CreateSut();
        }
示例#20
0
        public IAppLimitsPlan?GetPlanUpgradeForApp(IAppEntity app)
        {
            Guard.NotNull(app);

            return(GetPlanUpgrade(app.Plan?.PlanId));
        }
示例#21
0
 private ClientsDto GetResponse(IAppEntity app)
 {
     return(ClientsDto.FromApp(App, this));
 }
示例#22
0
 private void WithPlan(IAppEntity app, IAppPlansProvider plans)
 {
     MaxContributors = plans.GetPlanForApp(app).MaxContributors;
 }
示例#23
0
        public async Task <List <DomainId> > SearchAsync(IAppEntity app, TextQuery query, SearchScope scope,
                                                         CancellationToken ct = default)
        {
            Guard.NotNull(app, nameof(app));
            Guard.NotNull(query, nameof(query));

            var parsed = queryParser.Parse(query.Text);

            if (parsed == null)
            {
                return(null);
            }

            var serveField = GetServeField(scope);

            var elasticQuery = new
            {
                query = new
                {
                    @bool = new
                    {
                        filter = new List <object>
                        {
                            new
                            {
                                term = new Dictionary <string, object>
                                {
                                    ["appId.keyword"] = app.Id.ToString()
                                }
                            },
                            new
                            {
                                term = new Dictionary <string, string>
                                {
                                    [serveField] = "true"
                                }
                            }
                        },
                        must = new
                        {
                            query_string = new
                            {
                                query = parsed.Text
                            }
                        },
                        should = new List <object>()
                    }
                },
                _source = new[]
                {
                    "contentId"
                },
                size = query.Take
            };

            if (query.RequiredSchemaIds?.Count > 0)
            {
                var bySchema = new
                {
                    terms = new Dictionary <string, object>
                    {
                        ["schemaId.keyword"] = query.RequiredSchemaIds.Select(x => x.ToString()).ToArray()
                    }
                };

                [email protected](bySchema);
            }
            else if (query.PreferredSchemaId.HasValue)
            {
                var bySchema = new
                {
                    terms = new Dictionary <string, object>
                    {
                        ["schemaId.keyword"] = query.PreferredSchemaId.ToString()
                    }
                };

                [email protected](bySchema);
            }

            var json = JsonConvert.SerializeObject(elasticQuery, Formatting.Indented);

            return(await SearchAsync(elasticQuery, ct));
        }
示例#24
0
 private static int GetNumContributors(Role role, IAppEntity app)
 {
     return(app.Contributors.Count(x => role.Equals(x.Value)));
 }
示例#25
0
 private Task InvalidateItAsync(IAppEntity app)
 {
     return(grainCache.RemoveAsync(
                GetCacheKey(app.Id),
                GetCacheKey(app.Name)));
 }
示例#26
0
 private static int GetNumClients(Role role, IAppEntity app)
 {
     return(app.Clients.Count(x => role.Equals(x.Value.Role)));
 }
示例#27
0
        public async Task <List <(IContentEntity Content, ISchemaEntity Schema)> > QueryAsync(IAppEntity app, HashSet <DomainId> ids)
        {
            Guard.NotNull(app, nameof(app));

            using (Profiler.TraceMethod <MongoContentRepository>("QueryAsyncByIdsWithoutSchema"))
            {
                var result = await queryContentsById.DoAsync(app.Id, null, ids, false);

                return(result);
            }
        }
示例#28
0
 public Context(ClaimsPrincipal user, IAppEntity app)
     : this(user)
 {
     App = app;
 }
示例#29
0
 public string GenerateAssetUrl(IAppEntity app, IAssetEntity asset)
 {
     return(urlsOptions.BuildUrl($"api/assets/{asset.Id}?version={asset.FileVersion}"));
 }
示例#30
0
        public async Task <(ISchemaEntity Schema, IResultList <IContentEntity> Contents)> QueryAsync(IAppEntity app, string schemaIdOrName, ClaimsPrincipal user, bool archived, HashSet <Guid> ids, bool latest = false)
        {
            Guard.NotNull(ids, nameof(ids));
            Guard.NotNull(app, nameof(app));
            Guard.NotNull(user, nameof(user));
            Guard.NotNullOrEmpty(schemaIdOrName, nameof(schemaIdOrName));

            var schema = await FindSchemaAsync(app, schemaIdOrName);

            var parsedStatus = ParseStatus(user, archived);

            var contents = await contentRepository.QueryAsync(app, schema, parsedStatus.ToArray(), ids, latest);

            return(TransformContents(user, schema, contents));
        }