private string MarkPostUpOrDown(Post post, Member postWriter, Member voter, PostType postType) { // Check this user is not the post owner if (voter.Id != postWriter.Id) { // Not the same person, now check they haven't voted on this post before if (post.Votes.All(x => x.MemberId != CurrentMember.Id)) { // Points to add or subtract to a user var usersPoints = (postType == PostType.Negative) ? (-Settings.PointsDeductedForNegativeVote) : (Settings.PointsAddedForPositiveVote); // Update the users points who wrote the post MemberPointsService.Add(new MemberPoints { Points = usersPoints, Member = postWriter, MemberId = postWriter.Id, RelatedPostId = post.Id }); // Update the post with the new vote of the voter var vote = new Vote { Post = post, Member = voter, MemberId = voter.Id, Amount = (postType == PostType.Negative) ? (-1) : (1), VotedByMember = CurrentMember, DateVoted = DateTime.Now }; VoteService.Add(vote); // Update the post with the new points amount var allVotes = post.Votes.ToList(); var allVoteCount = allVotes.Sum(x => x.Amount); //var newPointTotal = (postType == PostType.Negative) ? (post.VoteCount - 1) : (post.VoteCount + 1); post.VoteCount = allVoteCount; int postTypeVoteCount; if (postType == PostType.Positive) { postTypeVoteCount = allVotes.Count(x => x.Amount > 0); } else { postTypeVoteCount = allVotes.Count(x => x.Amount < 0); } return(string.Concat(postTypeVoteCount, ",", allVoteCount)); } } return("0"); }
public ActionResult Create(CreateTopicViewModel topicViewModel) { if (ModelState.IsValid) { // Quick check to see if user is locked out, when logged in if (CurrentMember.IsLockedOut || CurrentMember.DisablePosting == true || !CurrentMember.IsApproved) { MemberService.LogOff(); return(ErrorToHomePage(Lang("Errors.NoPermission"))); } var successfullyCreated = false; var moderate = false; Category category; var topic = new Topic(); using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { // Before we do anything DB wise, check it contains no bad links if (BannedLinkService.ContainsBannedLink(topicViewModel.TopicContent)) { ShowMessage(new GenericMessageViewModel { Message = Lang("Errors.BannedLink"), MessageType = GenericMessages.Danger }); return(Redirect(Urls.GenerateUrl(Urls.UrlType.TopicCreate))); } // Not using automapper for this one only, as a topic is a post and topic in one category = CategoryService.Get(topicViewModel.Category); // First check this user is allowed to create topics in this category var permissions = PermissionService.GetPermissions(category, _membersGroup, MemberService, CategoryPermissionService); // Check this users role has permission to create a post if (permissions[AppConstants.PermissionDenyAccess].IsTicked || permissions[AppConstants.PermissionReadOnly].IsTicked || !permissions[AppConstants.PermissionCreateTopics].IsTicked) { // Throw exception so Ajax caller picks it up ModelState.AddModelError(string.Empty, Lang("Errors.NoPermission")); } else { // We get the banned words here and pass them in, so its just one call // instead of calling it several times and each call getting all the words back topic = new Topic { Name = BannedWordService.SanitiseBannedWords(topicViewModel.TopicName, Dialogue.Settings().BannedWords), Category = category, CategoryId = category.Id, Member = CurrentMember, MemberId = CurrentMember.Id }; // See if the user has actually added some content to the topic if (!string.IsNullOrEmpty(topicViewModel.TopicContent)) { // Check for any banned words topicViewModel.TopicContent = BannedWordService.SanitiseBannedWords(topicViewModel.TopicContent, Dialogue.Settings().BannedWords); // See if this is a poll and add it to the topic if (topicViewModel.PollAnswers != null && topicViewModel.PollAnswers.Any(x => !string.IsNullOrEmpty(x.Answer))) { // Do they have permission to create a new poll if (permissions[AppConstants.PermissionCreatePolls].IsTicked) { // Create a new Poll var newPoll = new Poll { Member = CurrentMember, MemberId = CurrentMember.Id }; // Create the poll PollService.Add(newPoll); // Save the poll in the context so we can add answers unitOfWork.SaveChanges(); // Now sort the answers var newPollAnswers = new List <PollAnswer>(); foreach (var pollAnswer in topicViewModel.PollAnswers) { // Attach newly created poll to each answer pollAnswer.Poll = newPoll; PollService.Add(pollAnswer); newPollAnswers.Add(pollAnswer); } // Attach answers to poll newPoll.PollAnswers = newPollAnswers; // Save the new answers in the context unitOfWork.SaveChanges(); // Add the poll to the topic topic.Poll = newPoll; } else { //No permission to create a Poll so show a message but create the topic ShowMessage(new GenericMessageViewModel { Message = Lang("Errors.NoPermissionPolls"), MessageType = GenericMessages.Info }); } } // Check for moderation if (category.ModerateAllTopicsInThisCategory) { topic.Pending = true; moderate = true; } // Create the topic topic = TopicService.Add(topic); // Save the changes unitOfWork.SaveChanges(); // Now create and add the post to the topic TopicService.AddLastPost(topic, topicViewModel.TopicContent, PostService); // Update the users points score for posting MemberPointsService.Add(new MemberPoints { Points = Settings.PointsAddedPerNewPost, Member = CurrentMember, MemberId = CurrentMember.Id, RelatedPostId = topic.LastPost.Id }); // Now check its not spam var akismetHelper = new AkismetHelper(); if (akismetHelper.IsSpam(topic)) { // Could be spam, mark as pending topic.Pending = true; } // Subscribe the user to the topic as they have checked the checkbox if (topicViewModel.SubscribeToTopic) { // Create the notification var topicNotification = new TopicNotification { Topic = topic, Member = CurrentMember, MemberId = CurrentMember.Id }; //save TopicNotificationService.Add(topicNotification); } try { unitOfWork.Commit(); if (!moderate) { successfullyCreated = true; } // Update the users post count MemberService.AddPostCount(CurrentMember); } catch (Exception ex) { unitOfWork.Rollback(); LogError(ex); ModelState.AddModelError(string.Empty, Lang("Errors.GenericMessage")); } } else { ModelState.AddModelError(string.Empty, Lang("Errors.GenericMessage")); } } } using (UnitOfWorkManager.NewUnitOfWork()) { if (successfullyCreated) { // Success so now send the emails NotifyNewTopics(category); // Redirect to the newly created topic return(Redirect($"{topic.Url}?postbadges=true")); } if (moderate) { // Moderation needed // Tell the user the topic is awaiting moderation return(MessageToHomePage(Lang("Moderate.AwaitingModeration"))); } } } ShowMessage(); return(Redirect(Urls.GenerateUrl(Urls.UrlType.TopicCreate))); }