public async Task <int> EditCommentPOST(string contentItemId, bool isPublic, string id, string User, CommentPart viewModel, Func <ContentItem, Task> conditionallyPublish) { var content = await _contentManager.GetAsync(contentItemId, VersionOptions.DraftRequired); if (content == null) { return(StatusCodes.Status204NoContent); } if (isPublic) { content.Content.PublicComment = JToken.FromObject(viewModel); } else { content.Content.AdminComment = JToken.FromObject(viewModel); } content.Owner = User; content.DisplayText = id; await conditionallyPublish(content); _session.Save(content); var typeDefinition = _contentDefinitionManager.GetTypeDefinition(content.ContentType); return(StatusCodes.Status201Created); }
private async Task <IActionResult> EditPOST(string contentItemId, string returnUrl, CompleteActionOutCome onCompleteActionOutCome, Func <ContentItem, Task> conditionallyPublish) { var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.DraftRequired); if (contentItem == null) { return(NotFound()); } if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.EditContent, contentItem)) { return(Forbid()); } //string previousRoute = null; //if (contentItem.Has<IAliasAspect>() && // !string.IsNullOrWhiteSpace(returnUrl) // && Request.IsLocalUrl(returnUrl) // // only if the original returnUrl is the content itself // && String.Equals(returnUrl, Url.ItemDisplayUrl(contentItem), StringComparison.OrdinalIgnoreCase) // ) //{ // previousRoute = contentItem.As<IAliasAspect>().Path; //} var model = await _contentItemDisplayManager.UpdateEditorAsync(contentItem, _updateModelAccessor.ModelUpdater, false); if (!ModelState.IsValid) { _session.Cancel(); //return View("Edit", model); // return Json("ModelState is invalid"); return(LocalRedirect(returnUrl)); } // The content item needs to be marked as saved in case the drivers or the handlers have // executed some query which would flush the saved entities inside the above UpdateEditorAsync. _session.Save(contentItem); await conditionallyPublish(contentItem); return(await RedirectSelectActionResult(contentItem, onCompleteActionOutCome, returnUrl)); /*if (returnUrl == null) * { * return RedirectToAction("Edit", new RouteValueDictionary { { "ContentItemId", contentItem.ContentItemId } }); * } * else if (stayOnSamePage) * { * return RedirectToAction("Edit", new RouteValueDictionary { { "ContentItemId", contentItem.ContentItemId }, { "returnUrl", returnUrl } }); * } * else * { * return LocalRedirect(returnUrl); * }*/ }
private async Task RegisterUserVote(ClaimsPrincipal user, ContentItem contentItem, int rating) { var currentVote = await GetUserVote(user, contentItem); //also update in content item part so part driver does not require extra queiry var voteUpDownPart = contentItem.As <VotingPart>(); if (currentVote != null && (currentVote.Value + rating == 0)) { voteUpDownPart.UserVote = rating; await _votingStore.DeleteAsync(currentVote); // _votingService.RemoveVote(currentVote); } else { if (currentVote != null) { voteUpDownPart.UserVote = rating; await _votingStore.ChangeVoteAsync(currentVote, rating); } else { //add new vote var vote = new VoteItem() { User = User.Identity.Name, CorrelationId = contentItem?.ContentItemId, Value = rating, Dimension = Constants.DimensionRating, CreatedUtc = _clock.UtcNow, Hostname = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString() }; await _votingStore.SaveAsync(vote); voteUpDownPart.UserVote = rating; } } contentItem.Apply(voteUpDownPart); _session.Save(contentItem); }
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> Update([FromForm] DashboardPartViewModel[] parts) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminDashboard)) { return(StatusCode(401)); } var contentItemIds = parts.Select(i => i.ContentItemId).ToArray(); // Load the latest version first if any var latestItems = await _contentManager.GetAsync(contentItemIds, true); var publishedItems = await _contentManager.GetAsync(contentItemIds, false); if (latestItems == null) { return(StatusCode(404)); } foreach (var contentItem in latestItems) { var dashboardPart = contentItem.As <DashboardPart>(); if (dashboardPart == null) { return(StatusCode(403)); } var partViewModel = parts.Where(m => m.ContentItemId == contentItem.ContentItemId).FirstOrDefault(); dashboardPart.Position = partViewModel?.Position ?? 0; dashboardPart.Width = partViewModel?.Width ?? 1; dashboardPart.Height = partViewModel?.Height ?? 1; contentItem.Apply(dashboardPart); _session.Save(contentItem); if (contentItem.IsPublished() == false) { var publishedVersion = publishedItems.Where(p => p.ContentItemId == contentItem.ContentItemId).FirstOrDefault(); var publishedMetaData = publishedVersion?.As <DashboardPart>(); if (publishedVersion != null && publishedMetaData != null) { publishedMetaData.Position = partViewModel.Position; publishedMetaData.Width = partViewModel.Width; publishedMetaData.Height = partViewModel.Height; publishedVersion.Apply(publishedMetaData); _session.Save(publishedVersion); } } } if (Request.Headers != null && Request.Headers["X-Requested-With"] == "XMLHttpRequest") { return(StatusCode(200)); } else { return(RedirectToAction(nameof(Manage))); } }
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)); }