public static ViewPostViewModel MapPostViewModel(PermissionSet permissions, Post post, Member currentMember, DialogueSettings settings, Topic topic, List<Vote> allPostVotes, List<Favourite> favourites, bool showTopicLinks = false) { var postViewModel = new ViewPostViewModel { Permissions = permissions, Post = post, User = currentMember, ParentTopic = topic, Votes = allPostVotes.Where(x => x.Post.Id == post.Id).ToList(), LoggedOnMemberId = currentMember != null ? currentMember.Id : 0, AllowedToVote = (currentMember != null && currentMember.Id != post.MemberId && currentMember.TotalPoints > settings.AmountOfPointsBeforeAUserCanVote), PostCount = post.Member.PostCount, IsAdminOrMod = HttpContext.Current.User.IsInRole(AppConstants.AdminRoleName) || permissions[AppConstants.PermissionModerate].IsTicked, HasFavourited = favourites.Any(x => x.PostId == post.Id), IsTopicStarter = post.IsTopicStarter, ShowTopicLinks = showTopicLinks }; postViewModel.UpVotes = postViewModel.Votes.Count(x => x.Amount > 0); postViewModel.DownVotes = postViewModel.Votes.Count(x => x.Amount < 0); return postViewModel; }
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 ServiceFactory.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 }; ServiceFactory.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; var postTypeVoteCount = 0; 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 void DeletePostPoints(Post post) { // Gets all points the member who made the post has gained from this post var membersPointsForThisPost = ContextPerRequest.Db.MemberPoints.Where(x => x.RelatedPostId == post.Id && x.MemberId == post.MemberId); // Now loop through and remove them foreach (var points in membersPointsForThisPost) { Delete(points); } }
/// <summary> /// Check whether a post is spam or not /// </summary> /// <param name="post"></param> /// <returns></returns> public bool IsSpam(Post post) { // If akisment is is not enable always return false if (Dialogue.Settings().EnableAkismetSpamControl == false || string.IsNullOrEmpty(Dialogue.Settings().AkismetKey)) return false; // Akisment must be enabled var comment = new Comment { blog = AppHelpers.CheckLinkHasHttp(Dialogue.Settings().ForumRootUrlWithDomain), comment_type = "comment", comment_author = post.Member.UserName, comment_author_email = post.Member.Email, comment_content = post.PostContent, permalink = String.Empty, referrer = HttpContext.Current.Request.ServerVariables["HTTP_REFERER"], user_agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"], user_ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] }; var validator = new Validator(Dialogue.Settings().AkismetKey); return validator.IsSpam(comment); }
/// <summary> /// Add a last post to a topic. Must be part of a separate database update /// in EF because of circular dependencies. So save the topic before calling this. /// </summary> /// <param name="topic"></param> /// <param name="postContent"></param> /// <returns></returns> public Topic AddLastPost(Topic topic, string postContent) { topic = SanitizeTopic(topic); // Create the post var post = new Post { DateCreated = DateTime.UtcNow, IsTopicStarter = true, DateEdited = DateTime.UtcNow, PostContent = AppHelpers.GetSafeHtml(postContent), MemberId = topic.MemberId, Topic = topic }; // Add the post ServiceFactory.PostService.Add(post); topic.LastPost = post; return topic; }
/// <summary> /// Add a new post /// </summary> /// <param name="postContent"> </param> /// <param name="topic"> </param> /// <param name="user"></param> /// <param name="permissions"> </param> /// <returns>True if post added</returns> public Post AddNewPost(string postContent, Topic topic, Member user, out PermissionSet permissions) { // Get the permissions for the category that this topic is in permissions = ServiceFactory.PermissionService.GetPermissions(topic.Category, user.Groups.FirstOrDefault()); // Check this users role has permission to create a post if (permissions[AppConstants.PermissionDenyAccess].IsTicked || permissions[AppConstants.PermissionReadOnly].IsTicked) { // Throw exception so Ajax caller picks it up throw new ApplicationException(AppHelpers.Lang("Errors.NoPermission")); } // Has permission so create the post var newPost = new Post { PostContent = postContent, Member = user, MemberId = user.Id, Topic = topic, IpAddress = AppHelpers.GetUsersIpAddress(), DateCreated = DateTime.UtcNow, DateEdited = DateTime.UtcNow }; newPost = SanitizePost(newPost); var category = topic.Category; if (category.ModerateAllPostsInThisCategory == true) { newPost.Pending = true; } // create the post Add(newPost); // Update the users points score and post count for posting ServiceFactory.MemberPointsService.Add(new MemberPoints { Points = Dialogue.Settings().PointsAddedPerNewPost, MemberId = user.Id, RelatedPostId = newPost.Id }); // add the last post to the topic topic.LastPost = newPost; // Add post to members count ServiceFactory.MemberService.AddPostCount(user); return newPost; }
public Post SanitizePost(Post post) { post.PostContent = AppHelpers.GetSafeHtml(post.PostContent); return post; }
/// <summary> /// Delete a post /// </summary> /// <param name="post"></param> /// <returns> True if parent topic should now be deleted (caller's responsibility)</returns> public bool Delete(Post post) { // Here is where we can check for reasons not to delete the post // And change the value below if not var deleteTopic = false; // Get the member who made this post var postMember = ServiceFactory.MemberService.Get(post.MemberId); // Before we delete the post, we need to check if this is the last post in the topic // and if so update the topic var topic = post.Topic; var lastPost = topic.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(); if (lastPost != null && lastPost.Id == post.Id) { // Get the new last post and update the topic topic.LastPost = topic.Posts.Where(x => x.Id != post.Id).OrderByDescending(x => x.DateCreated).FirstOrDefault(); } // Mark topic as not solved if the post we are deleting was the solution if (topic.Solved && post.IsSolution) { topic.Solved = false; } // Remove this post from the topic so we can delete it without any errors topic.Posts.Remove(post); // Topic should be deleted, so make sure it has no last post to avoid circular dependency deleteTopic = post.IsTopicStarter; if (deleteTopic) { topic.LastPost = null; } // Delete all the points the memeber who made this post has gained ServiceFactory.MemberPointsService.DeletePostPoints(post); // now delete the post ContextPerRequest.Db.Post.Remove(post); // Sync this members post count, so it's always accurate ServiceFactory.PostService.SyncMembersPostCount(new List<Member> { postMember }); return deleteTopic; }
public Post Add(Post post) { post = SanitizePost(post); return ContextPerRequest.Db.Post.Add(post); }
/// <summary> /// Mark a topic as solved /// </summary> /// <param name="topic"></param> /// <param name="post"></param> /// <param name="marker"></param> /// <param name="solutionWriter"></param> /// <returns>True if topic has been marked as solved</returns> public bool SolveTopic(Topic topic, Post post, Member marker, Member solutionWriter) { var solved = false; // Make sure this user owns the topic, if not do nothing if (topic.MemberId == marker.Id) { // Update the post post.IsSolution = true; // Update the topic topic.Solved = true; // Assign points // Do not give points to the user if they are marking their own post as the solution if (marker.Id != solutionWriter.Id) { ServiceFactory.MemberPointsService.Add(new MemberPoints { Points = Dialogue.Settings().PointsAddedForASolution, Member = solutionWriter, MemberId = solutionWriter.Id, RelatedPostId = post.Id }); } solved = true; } return solved; }