Exemplo n.º 1
0
        public async Task <IActionResult> Subscribe()
        {
            Console.WriteLine($"/pollinator/atom/subscribe{Request.QueryString}");

            string uriQuery = Request.Query["uri"];

            var user = await GetUserFromUriQuery(uriQuery);

            var aggregator = await GetAggregatorFromUriQuery(uriQuery);

            if (user == null && aggregator == null)
            {
                return(Ok());
            }

            var author = await _userManager.GetUserAsync(User);

            if (user != null)
            { // user subscription
                // only add subscription when the subscription doesn't already exist
                // and if the requested user is not the author
                if (await _userSubscriptionManager.FindAsync(author, user) == null &&
                    user.Id != author.Id)
                {
                    // add subscription
                    if (!await _userSubscriptionManager.AddAsync(author, user))
                    {
                        return(StatusCode(500));
                    }
                }

                // redirect request to /user/{userId}
                return(await AtomUser(user.Id));
            }
            else
            { // aggregator subscription
                // only add subscription when the subscription doesn't already exist
                // and if the requested aggregator author is not the author
                if (await _aggregatorSubscriptionManager.FindAsync(author, aggregator) == null &&
                    author.Id != aggregator.AuthorId)
                {
                    // add subscription
                    if (!await _aggregatorSubscriptionManager.AddAsync(author, aggregator))
                    {
                        return(StatusCode(500));
                    }
                }

                // redirect request to /aggregator/{id}
                return(await Aggregator(aggregator.AggregatorId));
            }
        }