Пример #1
0
        public async Task <IActionResult> ApplyFollowAction(string contentItemId, string returnUrl)
        {
            var targetContentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Published);

            if (targetContentItem == null)// || !contentItem.Has(nameof(FollowActionPart))  )
            {
                // return NotFound();
                return(Redirect(returnUrl));
            }

            if (User == null)
            {
                //  return NotFound();
                return(Redirect(returnUrl));
            }


            var currentVote = (await _votingStore.GetAsync(vote =>
                                                           vote.User == User.Identity.Name &&
                                                           vote.CorrelationId == targetContentItem.ContentItemId &&
                                                           vote.Dimension == Constants.DimensionFollow)).FirstOrDefault();

            //also update in content item part so part driver does not require extra queiry
            var followingPart = targetContentItem.As <FollowPart>();

            if (followingPart == null)
            {
                var newPart = targetContentItem.Weld(new FollowPart()
                {
                    IsFollowing = false
                });
                targetContentItem.Apply(newPart);
            }
            var actualFollowingPart = targetContentItem.As <FollowPart>();

            string successMessage;

            if (currentVote != null)
            {
                //already following . so unfollow



                await _votingStore.DeleteAsync(currentVote);

                actualFollowingPart.IsFollowing = false;

                successMessage = "Removed from Following.";
            }
            else
            {
                //add new vote
                var vote = new VoteItem
                {
                    User          = User.Identity.Name,
                    CorrelationId = targetContentItem?.ContentItemId,
                    Value         = 1,
                    Dimension     = Constants.DimensionFollow,
                    CreatedUtc    = _clock.UtcNow,
                    Hostname      = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString()
                };


                await _votingStore.SaveAsync(vote);

                actualFollowingPart.IsFollowing = true;
                successMessage = "Added to Following.";

                //favoritePart.IsFavorite = true;
                //  _votingStore.Vote(content, currentUser.UserName, _httpContextAccessor.Current().Request.UserHostAddress, 1, Constants.Dimension);
            }

            targetContentItem.Apply(actualFollowingPart);
            _session.Save(targetContentItem);
            _notifier.Success(T[successMessage]);

            //log notification
            await _notificationEventHandlers.InvokeAsync(eventdata => eventdata.LogNotificationAsync(
                                                             "Follow",
                                                             User.Identity.Name,
                                                             targetContentItem?.ContentItemId,
                                                             "Followed by user",
                                                             $"{User.Identity.Name} followed you"),
                                                         Logger);

            //send realtime notification to receiver
            //push notification to follower about number of his/her FollowerNotification.
            await _pushNotificationService.RefreshUserFollowersNotification(targetContentItem?.ContentItemId);

            if (returnUrl == null)
            {
                // return RedirectToAction("Edit", new RouteValueDictionary { { "ContentItemId", contentItem.ContentItemId } });
                return(LocalRedirect("/"));
            }
//           else if (stayOnSamePage)
//           {
//               return RedirectToAction("Edit", new RouteValueDictionary { { "ContentItemId", contentItem.ContentItemId }, { "returnUrl", returnUrl } });
//           }

            return(LocalRedirect(returnUrl));
        }