Beispiel #1
0
        /// <summary>
        /// Retrieves category IDs from the update server: classifications, products and detectoids
        /// </summary>
        /// <param name="oldAnchor">The anchor returned by a previous call to this function. Can be null.</param>
        /// <returns>The list of category IDs and an anchor. If an anchor was passed in, the
        /// list of category IDs is a delta list of categories changed since the anchor was generated.</returns>
        private async Task <(string anchor, IEnumerable <Metadata.Identity> identities)> GetCategoryIds(string oldAnchor = null)
        {
            // Create a request for categories
            var revisionIdRequest = new GetRevisionIdListRequest();

            revisionIdRequest.GetRevisionIdList        = new GetRevisionIdListRequestBody();
            revisionIdRequest.GetRevisionIdList.cookie = AccessToken.AccessCookie;
            revisionIdRequest.GetRevisionIdList.filter = new ServerSyncFilter();
            if (!string.IsNullOrEmpty(oldAnchor))
            {
                revisionIdRequest.GetRevisionIdList.filter.Anchor = oldAnchor;
            }

            // GetConfig must be true to request just categories
            revisionIdRequest.GetRevisionIdList.filter.GetConfig = true;

            var revisionsIdReply = await ServerSyncClient.GetRevisionIdListAsync(revisionIdRequest);

            if (revisionsIdReply == null || revisionsIdReply.GetRevisionIdListResponse1 == null || revisionsIdReply.GetRevisionIdListResponse1.GetRevisionIdListResult == null)
            {
                throw new Exception("Failed to get revision ID list");
            }

            // Return IDs and the anchor for this query. The anchor can be used to get a delta list in the future.
            return(
                revisionsIdReply.GetRevisionIdListResponse1.GetRevisionIdListResult.Anchor,
                revisionsIdReply.GetRevisionIdListResponse1.GetRevisionIdListResult.NewRevisions.Select(rawId => new Metadata.Identity(rawId)));
        }
Beispiel #2
0
        /// <summary>
        /// Return a list of update ids
        /// </summary>
        /// <param name="request">Request data. Can specify categories or updates, filters, etc.</param>
        /// <returns></returns>
        public Task <RevisionIdList> GetRevisionIdListAsync(GetRevisionIdListRequest request)
        {
            var response = new RevisionIdList();

            response.Anchor = DateTime.Now.ToString();

            if (request.GetRevisionIdList.filter.GetConfig == true)
            {
                if (!string.IsNullOrEmpty(request.GetRevisionIdList.filter.Anchor) && DateTime.TryParse(request.GetRevisionIdList.filter.Anchor, out DateTime anchorTime))
                {
                    // If we have an anchor, return only categories that have changed after the anchor
                    response.NewRevisions = Categories
                                            .Select(u => u.Identity.Raw)
                                            .ToArray();
                }
                else
                {
                    // else return all categories
                    response.NewRevisions = Categories.Select(u => u.Identity.Raw).ToArray();
                }
            }
            else
            {
                var productsFilter        = request.GetRevisionIdList.filter.Categories;
                var classificationsFilter = request.GetRevisionIdList.filter.Classifications;

                var requestedUpdateIds = new List <Update>();

                if (productsFilter != null)
                {
                    // Apply the product filter
                    foreach (var product in productsFilter)
                    {
                        if (ProductsIndex.ContainsKey(product.Id))
                        {
                            requestedUpdateIds.AddRange(ProductsIndex[product.Id]);
                        }
                    }
                }
                else
                {
                    requestedUpdateIds.AddRange(FilteredUpdates.Values);
                }

                if (classificationsFilter != null)
                {
                    var classificationFilterIds = classificationsFilter.Select(filter => filter.Id).ToList();

                    // Remove all updates that don't have classifications
                    requestedUpdateIds.RemoveAll(u => !u.HasClassification);

                    // Remove all updates that don't have a classification that matches the filter
                    requestedUpdateIds.RemoveAll(
                        u => !u.ClassificationIds.Any(c => classificationFilterIds.Contains(c)));
                }

                // Deduplicate result and convert to raw identity format
                response.NewRevisions = requestedUpdateIds.GroupBy(u => u.Identity).Select(g => g.Key).Select(k => k.Raw).ToArray();
            }

            return(Task.FromResult(response));
        }