private async Task <VoteItem> GetAnonUserVoteAsync(ContentItem contentItem) { var currentContext = _httpContextAccessor.HttpContext; var anonHostname = currentContext.Connection.RemoteIpAddress.ToString(); if (!string.IsNullOrWhiteSpace(currentContext.Request.Headers["X-Forwarded-For"])) { anonHostname += "-" + currentContext.Request.Headers["X-Forwarded-For"]; } return((await _votingStore.GetAsync(vote => vote.User == "Anonymous" && vote.Hostname == anonHostname && vote.CorrelationId == contentItem.ContentItemId && vote.Dimension == Constants.DimensionRating)).FirstOrDefault()); }
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)); }
public async Task <IActionResult> ApplyFollowAction(string contentItemId, string returnUrl) { var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Published); if (contentItem == null || !contentItem.Has("FavoritePart")) { // 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 == contentItem.ContentItemId && vote.Dimension == "Favorite")).FirstOrDefault(); string sucessMessage = string.Empty; if (currentVote != null) { //already following . so unfollow // favoritePart.IsFavorite = false; await _votingStore.DeleteAsync(currentVote); sucessMessage = "Removed from Following."; } else { //add new vote var vote = new VoteItem() { User = User.Identity.Name, CorrelationId = contentItem?.ContentItemId, Value = 1, Dimension = "Favorite", CreatedUtc = _clock.UtcNow, Hostname = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString() }; await _votingStore.SaveAsync(vote); sucessMessage = "Added to Following."; //favoritePart.IsFavorite = true; // _votingStore.Vote(content, currentUser.UserName, _httpContextAccessor.Current().Request.UserHostAddress, 1, Constants.Dimension); } _session.Save(contentItem); _notifier.Success(T[sucessMessage]); 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 } }); // } else { return(LocalRedirect(returnUrl)); } }
public async Task <IActionResult> ApplyRatingAction(string contentItemId, decimal rating, string returnUrl) { var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Published); if (contentItem == null)// || !contentItem.Has(nameof(FollowActionPart)) ) { // return NotFound(); return(Redirect(returnUrl)); } if (User == null) { // return NotFound(); return(Redirect(returnUrl)); //for anon voting /*var anonHostname =HttpContext.Connection.RemoteIpAddress.ToString(); * if (!string.IsNullOrWhiteSpace(HttpContext.Request.Headers["X-Forwarded-For"])) * anonHostname += "-" + HttpContext.Request.Headers["X-Forwarded-For"]; * * var currentVote =(await _votingStore.GetAsync(vote => ((VoteItemIndex)vote).User == "Anonymous" && ((VoteItemIndex)vote).Hostname == anonHostname && vote.CorrelationId == contentItem.ContentItemId)).FirstOrDefault(); * if (rating > 0 && currentVote == null) // anonymous votes are only set once per anonHostname * { * var vote = new VoteItem(){User = "******", * CorrelationId =contentItem?.ContentItemId, * Value = rating, * CreatedUtc = _clock.UtcNow, * // Dimension = Constants.Dimension, * Hostname =anonHostname}; * * * await _votingStore.SaveAsync(vote); * } * // _votingService.Vote(content, "Anonymous", anonHostname, rating);*/ } var currentVote = (await _votingStore.GetAsync(vote => vote.User == User.Identity.Name && vote.CorrelationId == contentItem.ContentItemId && vote.Dimension == Constants.DimensionRating)).FirstOrDefault(); //also update in content item part so part driver does not require extra queiry var ratingPart = contentItem.As <RatingPart>(); if (ratingPart == null) { var newPart = contentItem.Weld(new RatingPart() { UserRating = 0 }); contentItem.Apply(newPart); } var actualRatingPart = contentItem.As <RatingPart>(); actualRatingPart.UserRating = rating; contentItem.Apply(actualRatingPart); string successMessage; if (rating == -1) // clear { if (currentVote != null) { await _votingStore.DeleteAsync(currentVote); } } else // vote!! { if (currentVote != null) { await _votingStore.ChangeVoteAsync(currentVote, Convert.ToDouble(rating)); } // _session.Save(contentItem); // await _votingStore.SaveAsync(vote); else { //add new vote var vote = new VoteItem() { User = User.Identity.Name, CorrelationId = contentItem?.ContentItemId, Value = Convert.ToDouble(rating), Dimension = Constants.DimensionRating, CreatedUtc = _clock.UtcNow, Hostname = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString() }; await _votingStore.SaveAsync(vote); } // _votingService.Vote(content, currentUser.UserName, HttpContext.Request.UserHostAddress, rating); } successMessage = "Added to Rating."; //favoritePart.IsFavorite = true; // _votingStore.Vote(content, currentUser.UserName, _httpContextAccessor.Current().Request.UserHostAddress, 1, Constants.Dimension); // contentItem.Apply(actualRatingPart); _session.Save(contentItem); _notifier.Success(T[successMessage]); 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)); }
public async Task <IActionResult> ApplyBookmarkAction(string contentItemId, string returnUrl) { var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Published); if (contentItem == 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 == contentItem.ContentItemId && vote.Dimension == Constants.DimensionBookmark)).FirstOrDefault(); //also update in content item part so part driver does not require extra queiry var bookmarkPart = contentItem.As <BookmarkPart>(); if (bookmarkPart == null) { var newPart = contentItem.Weld(new BookmarkPart() { IsBookmarked = false }); contentItem.Apply(newPart); } var actualBookmarkPart = contentItem.As <BookmarkPart>(); string successMessage; if (currentVote != null) { //already following . so unfollow // favoritePart.IsFavorite = false; await _votingStore.DeleteAsync(currentVote); actualBookmarkPart.IsBookmarked = false; successMessage = "Removed from bookmarks."; } else { //add new vote var vote = new VoteItem { User = User.Identity.Name, CorrelationId = contentItem?.ContentItemId, Value = 1, Dimension = Constants.DimensionBookmark, CreatedUtc = _clock.UtcNow, Hostname = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString() }; await _votingStore.SaveAsync(vote); actualBookmarkPart.IsBookmarked = true; successMessage = "Added to Bookmarks."; //favoritePart.IsFavorite = true; // _votingStore.Vote(content, currentUser.UserName, _httpContextAccessor.Current().Request.UserHostAddress, 1, Constants.Dimension); } contentItem.Apply(actualBookmarkPart); _session.Save(contentItem); _notifier.Success(T[successMessage]); 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)); }