示例#1
0
        public async Task <IActionResult> RandomAsset()
        {
            Console.WriteLine($"/pollinator/atom/randomAsset{Request.QueryString}");

            string functionString = Request.Query["asset.function"];

            SporeServerAsset[] assets = null;

            // make sure we can parse the function string
            if (Int64.TryParse(functionString, out Int64 function))
            {
                // we only support modeltypes for now,
                // TODO: support archetypes/herdtypes
                if (Enum.IsDefined(typeof(SporeModelType), function))
                {
                    var user = await _userManager.GetUserAsync(User);

                    assets = await _assetManager.GetRandomAssetsAsync(user.Id, (SporeModelType)function);
                }
                else
                {
                    Console.WriteLine($"unsupported randomAsset function: {function}");
                }
            }

            return(AtomFeedBuilder.CreateFromTemplate(
                       new RandomAssetTemplate(assets)
                       ).ToContentResult());
        }
示例#2
0
        public async Task <IActionResult> DownloadQueue()
        {
            Console.WriteLine($"/pollinator/atom/downloadQueue{Request.QueryString}");

            var user = await _userManager.GetUserAsync(User);

            return(AtomFeedBuilder.CreateFromTemplate(
                       new DownloadQueueTemplate(user, null)
                       ).ToContentResult());
        }
示例#3
0
        public async Task <IActionResult> Aggregator(Int64 id)
        {
            Console.WriteLine($"/pollinator/atom/aggregator/{id}{Request.QueryString}");

            var aggregator = await _aggregatorManager.FindByIdAsync(id);

            // make sure the aggregator exists
            if (aggregator == null)
            {
                return(NotFound());
            }

            var subscriberCount = await _aggregatorSubscriptionManager.GetSubscriberCountAsync(aggregator);

            return(AtomFeedBuilder.CreateFromTemplate(
                       new AggregatorTemplate(aggregator, subscriberCount)
                       ).ToContentResult());
        }
示例#4
0
        public async Task <IActionResult> Asset(Int64?id)
        {
            Console.WriteLine($"/pollinator/atom/asset/{id}{Request.QueryString}");

            var assets = new List <SporeServerAsset>();

            if (id != null)
            {
                // parameter is asset id
                var asset = await _assetManager.FindByIdAsync((Int64)id, true);

                if (asset != null)
                {
                    assets.Add(asset);
                }
            }
            else
            {
                // loop over all id queries
                foreach (var idQuery in Request.Query["id"])
                {
                    // make sure we can parse the id
                    // and that the asset exists
                    if (Int64.TryParse(idQuery, out Int64 assetId))
                    {
                        var asset = await _assetManager.FindByIdAsync(assetId, true);

                        if (asset != null)
                        {
                            assets.Add(asset);
                        }
                    }
                }
            }

            Console.WriteLine(AtomFeedBuilder.CreateFromTemplate(
                                  new AssetTemplate(assets.ToArray())
                                  ).ToContentResult().Content);

            return(AtomFeedBuilder.CreateFromTemplate(
                       new AssetTemplate(assets.ToArray())
                       ).ToContentResult());
        }
示例#5
0
        public async Task <IActionResult> AtomUser(Int64 userId)
        {
            Console.WriteLine($"/pollinator/atom/user/{userId}{Request.QueryString}");

            var user = await _userManager.FindByIdAsync(userId.ToString());

            // make sure the user exists
            if (user == null)
            {
                return(NotFound());
            }

            var assets = await _assetManager.FindAllByUserIdAsync(userId);

            Console.WriteLine(AtomFeedBuilder.CreateFromTemplate(
                                  new UserTemplate(user, assets)
                                  ).ToContentResult().Content);

            return(AtomFeedBuilder.CreateFromTemplate(
                       new UserTemplate(user, assets)
                       ).ToContentResult());
        }
示例#6
0
        public async Task <IActionResult> HandShake()
        {
            var user = await _userManager.GetUserAsync(User);

            var asset = await _assetManager.FindByIdAsync(user.NextAssetId);

            var aggregators = await _aggregatorManager.FindByAuthorAsync(user);

            var userSubscriptions = await _userSubscriptionManager.FindAllByAuthorAsync(user);

            var aggregatorSubscriptions = await _aggregatorSubscriptionManager.FindAllByAuthorAsync(user);

            var aggregatorSubscriptionCounts = new List <Int32>();

            // retrieve subscription count for each aggregator
            foreach (var aggregator in aggregators)
            {
                var aggregatorSubscriptionCount = await _aggregatorSubscriptionManager.GetSubscriberCountAsync(aggregator);

                aggregatorSubscriptionCounts.Add(aggregatorSubscriptionCount);
            }

            // reserve new asset when
            //      * user has no reserved asset
            //      * user has a used asset
            if (user.NextAssetId == 0 ||
                (asset != null && asset.Used))
            {
                // when reserving a new asset fails, error out
                if (!await _assetManager.ReserveAsync(user))
                {
                    return(StatusCode(500));
                }
            }

            return(AtomFeedBuilder.CreateFromTemplate(
                       new HandshakeTemplate(user, aggregators, aggregatorSubscriptionCounts.ToArray(), userSubscriptions, aggregatorSubscriptions)
                       ).ToContentResult());
        }