/// <summary> /// Deletes an individual post /// </summary> /// <param name="topic"></param> /// <param name="post"></param> /// <param name="memberPointsService"></param> /// <param name="resetLastPost"></param> private static void DeleteIndividualPost(Topic topic, Post post, MemberPointsService memberPointsService, bool resetLastPost = true) { if (resetLastPost) { var lastPost = topic.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(); if (lastPost != null && lastPost.Id == post.Id && topic.Posts.Count > 1) { // Get the new last post and update the topic topic.LastPost = topic.Posts.Where(x => x.Id != post.Id).OrderByDescending(x => x.DateCreated).FirstOrDefault(); } else if (lastPost != null && lastPost.Id == post.Id && topic.Posts.Count <= 1) { topic.LastPost = null; } // 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); // Delete all the points the memeber who made this post has gained memberPointsService.DeletePostPoints(post); // now delete the post ContextPerRequest.Db.Post.Remove(post); }
/// <summary> /// Add a new post /// </summary> /// <param name="postContent"> </param> /// <param name="topic"> </param> /// <param name="user"></param> /// <param name="permissionService"></param> /// <param name="categoryPermissionService"></param> /// <param name="memberPointsService"></param> /// <param name="permissions"> </param> /// <param name="memberService"></param> /// <returns>True if post added</returns> public Post AddNewPost(string postContent, Topic topic, Member user, PermissionService permissionService, MemberService memberService, CategoryPermissionService categoryPermissionService, MemberPointsService memberPointsService, out PermissionSet permissions) { // Get the permissions for the category that this topic is in permissions = permissionService.GetPermissions(topic.Category, user.Groups.FirstOrDefault(), memberService, categoryPermissionService); // 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 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 memberService.AddPostCount(user); return(newPost); }
public bool Delete(Member member, UnitOfWork unitOfWork, UploadedFileService uploadedFileService, PostService postService, MemberPointsService memberPointsService, PollService pollService, TopicService topicService, TopicNotificationService topicNotificationService, ActivityService activityService, PrivateMessageService privateMessageService, BadgeService badgeService, VoteService voteService, CategoryNotificationService categoryNotificationService) { if (DeleteAllAssociatedMemberInfo(member.Id, unitOfWork, uploadedFileService, postService, memberPointsService, pollService, topicService, topicNotificationService, activityService, privateMessageService, badgeService, voteService, categoryNotificationService)) { var baseMember = _memberService.GetById(member.Id); _memberService.Delete(baseMember); return(true); } return(false); }
/// <summary> /// This method deletes/clears all member data, but not the actual member itself. /// Perfect for clearing spammers accounts before banning them. It needs a UnitOfWork passed in /// because it has to do a lot of saving and removing. so make sure you wrap it in a using statement /// NOTE: It calls it's own commit at the end of this method /// </summary> /// <param name="userId"></param> /// <param name="unitOfWork"></param> /// <param name="uploadedFileService"></param> /// <param name="postService"></param> /// <param name="memberPointsService"></param> /// <param name="pollService"></param> /// <param name="topicService"></param> /// <param name="topicNotificationService"></param> /// <param name="activityService"></param> /// <param name="privateMessageService"></param> /// <param name="badgeService"></param> /// <param name="voteService"></param> /// <param name="categoryNotificationService"></param> /// <returns></returns> public bool DeleteAllAssociatedMemberInfo(int userId, UnitOfWork unitOfWork, UploadedFileService uploadedFileService, PostService postService, MemberPointsService memberPointsService, PollService pollService, TopicService topicService, TopicNotificationService topicNotificationService, ActivityService activityService, PrivateMessageService privateMessageService, BadgeService badgeService, VoteService voteService, CategoryNotificationService categoryNotificationService) { try { // Delete all file uploads var files = uploadedFileService.GetAllByUser(userId); var filesList = new List <UploadedFile>(); filesList.AddRange(files); foreach (var file in filesList) { // store the file path as we'll need it to delete on the file system var filePath = file.FilePath; // Now delete it uploadedFileService.Delete(file); // And finally delete from the file system System.IO.File.Delete(HttpContext.Current.Server.MapPath(filePath)); } // Delete all posts - exlcuding topic starters as they are about to be deleted var groupedPosts = postService.GetAllByMember(userId).Where(x => !x.IsTopicStarter).GroupBy(x => x.Topic); // Loop through all posts per topic foreach (var group in groupedPosts) { var postList = new List <Post>(); postList.AddRange(group); // The Topic var topic = group.Key; // The last post var lastPost = group.Key.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault(); // Loop through the posts foreach (var post in postList) { post.Files.Clear(); 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); // Delete all the points the memeber who made this post has gained memberPointsService.DeletePostPoints(post); // now delete the post ContextPerRequest.Db.Post.Remove(post); } unitOfWork.SaveChanges(); } // Also clear their poll votes var userPollVotes = pollService.GetMembersPollVotes(userId); if (userPollVotes.Any()) { var pollList = new List <PollVote>(); pollList.AddRange(userPollVotes); foreach (var vote in pollList) { pollService.Delete(vote); } } unitOfWork.SaveChanges(); // Also clear their polls var userPolls = pollService.GetMembersPolls(userId); if (userPolls.Any()) { var polls = new List <Poll>(); polls.AddRange(userPolls); foreach (var poll in polls) { //Delete the poll answers var pollAnswers = poll.PollAnswers; if (pollAnswers.Any()) { var pollAnswersList = new List <PollAnswer>(); pollAnswersList.AddRange(pollAnswers); foreach (var answer in pollAnswersList) { answer.Poll = null; pollService.Delete(answer); } } poll.PollAnswers.Clear(); pollService.Delete(poll); } } unitOfWork.SaveChanges(); // Delete all topics var topics = topicService.GetAllTopicsByUser(userId); var topicList = new List <Topic>(); topicList.AddRange(topics); var memberIds = new List <int>(); foreach (var topic in topicList) { //var topicStarterPost = topic.Posts.FirstOrDefault(x => x.IsTopicStarter); //postService.Delete(topicStarterPost, postService, , memberPointsService, topicNotificationService); var postsToDelete = new List <Post>(); postsToDelete.AddRange(topic.Posts); memberIds.AddRange(postsToDelete.Select(x => x.MemberId).Distinct()); foreach (var postFromTopic in postsToDelete) { postFromTopic.Files.Clear(); // Remove this post from the topic so we can delete it without any errors topic.Posts.Remove(postFromTopic); // Delete all the points the memeber who made this post has gained memberPointsService.DeletePostPoints(postFromTopic); } if (topic.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(topic.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { topicNotificationService.Delete(topicNotification); } } ContextPerRequest.Db.Topic.Remove(topic); } // Sync the members post count. For all members who had a post deleted. var members = GetAllById(memberIds); SyncMembersPostCount(members); // Now clear all activities for this user var usersActivities = activityService.GetDataByUserId(userId); activityService.Delete(usersActivities.ToList()); // Delete all private messages from this user var msgsToDelete = new List <PrivateMessage>(); msgsToDelete.AddRange(privateMessageService.GetAllByUserSentOrReceived(userId)); foreach (var msgToDelete in msgsToDelete) { privateMessageService.DeleteMessage(msgToDelete); } // Delete all badge times last checked var badgeTypesTimeLastCheckedToDelete = new List <BadgeTypeTimeLastChecked>(); badgeTypesTimeLastCheckedToDelete.AddRange(badgeService.BadgeTypeTimeLastCheckedByMember(userId)); foreach (var badgeTypeTimeLastCheckedToDelete in badgeTypesTimeLastCheckedToDelete) { badgeService.DeleteTimeLastChecked(badgeTypeTimeLastCheckedToDelete); } // Delete all points from this user var pointsToDelete = new List <MemberPoints>(); pointsToDelete.AddRange(memberPointsService.GetByUser(userId)); foreach (var pointToDelete in pointsToDelete) { memberPointsService.Delete(pointToDelete); } // Delete all topic notifications var topicNotificationsToDelete = new List <TopicNotification>(); topicNotificationsToDelete.AddRange(topicNotificationService.GetByUser(userId)); foreach (var topicNotificationToDelete in topicNotificationsToDelete) { topicNotificationService.Delete(topicNotificationToDelete); } // Delete all user's votes var votesToDelete = new List <Vote>(); votesToDelete.AddRange(voteService.GetAllVotesByUser(userId)); foreach (var voteToDelete in votesToDelete) { voteService.Delete(voteToDelete); } // Delete all user's badges var badgesToDelete = new List <BadgeToMember>(); badgesToDelete.AddRange(badgeService.GetAllBadgeToMembers(userId)); foreach (var badgeToDelete in badgesToDelete) { badgeService.DeleteBadgeToMember(badgeToDelete); } // Delete all user's category notifications var categoryNotificationsToDelete = new List <CategoryNotification>(); categoryNotificationsToDelete.AddRange(categoryNotificationService.GetByUser(userId)); foreach (var categoryNotificationToDelete in categoryNotificationsToDelete) { categoryNotificationService.Delete(categoryNotificationToDelete); } unitOfWork.Commit(); return(true); } catch (Exception ex) { AppHelpers.LogError("Error trying to delete Dialogue member", ex); } return(false); }
/// <summary> /// Delete a post /// </summary> /// <param name="unitOfWork"></param> /// <param name="post"></param> /// <param name="memberService"></param> /// <param name="memberPointsService"></param> /// <param name="topicNotificationService"></param> /// <returns> True if parent was deleted too</returns> public bool Delete(UnitOfWork unitOfWork, Post post, MemberService memberService, MemberPointsService memberPointsService, TopicNotificationService topicNotificationService) { // Get the topic var topic = post.Topic; // The member who created this post var postMember = memberService.Get(post.MemberId); var topicDeleted = false; // See if we need to delete the topic or not if (post.IsTopicStarter) { topic.LastPost = null; // Delete all posts if (topic.Posts != null) { var postsToDelete = new List <Post>(); postsToDelete.AddRange(topic.Posts); var memberIds = postsToDelete.Select(x => x.MemberId).Distinct().ToList(); foreach (var postFromTopic in postsToDelete) { post.Files.Clear(); DeleteIndividualPost(topic, postFromTopic, memberPointsService, false); unitOfWork.SaveChanges(); } // Sync the members post count. For all members who had a post deleted. var members = memberService.GetAllById(memberIds); memberService.SyncMembersPostCount(members); } if (topic.TopicNotifications != null) { var notificationsToDelete = new List <TopicNotification>(); notificationsToDelete.AddRange(topic.TopicNotifications); foreach (var topicNotification in notificationsToDelete) { topicNotificationService.Delete(topicNotification); } } topic.Posts?.Clear(); topic.TopicNotifications?.Clear(); topic.Category = null; topic.LastPost = null; ContextPerRequest.Db.Topic.Remove(topic); // Set to true topicDeleted = true; } else { DeleteIndividualPost(topic, post, memberPointsService); memberService.SyncMembersPostCount(new List <Member> { postMember }); } return(topicDeleted); }
/// <summary> /// Mark a topic as solved /// </summary> /// <param name="topic"></param> /// <param name="post"></param> /// <param name="marker"></param> /// <param name="solutionWriter"></param> /// <param name="memberPointsService"></param> /// <returns>True if topic has been marked as solved</returns> public bool SolveTopic(Topic topic, Post post, Member marker, Member solutionWriter, MemberPointsService memberPointsService) { 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) { memberPointsService.Add(new MemberPoints { Points = Dialogue.Settings().PointsAddedForASolution, Member = solutionWriter, MemberId = solutionWriter.Id, RelatedPostId = post.Id }); } solved = true; } return(solved); }
/// <summary> /// Processes the user for the specified badge type /// </summary> /// <param name="badgeType"></param> /// <param name="user"></param> /// <param name="memberPointsService"></param> /// <param name="activityService"></param> /// <returns>True if badge was awarded</returns> public bool ProcessBadge(BadgeType badgeType, Member user, MemberPointsService memberPointsService, ActivityService activityService) { var databaseUpdateNeeded = false; if (_badges.ContainsKey(badgeType)) { if (!RecentlyProcessed(badgeType, user)) { databaseUpdateNeeded = true; var badgeSet = _badges[badgeType]; foreach (var badgeMapping in badgeSet) { if (!BadgeCanBeAwarded(user, badgeMapping)) { continue; } // Instantiate the badge and execute the rule var badge = GetInstance <IBadge>(badgeMapping); // Award badge? if (badge != null && badge.Rule(user)) { // Re-fetch the badge otherwise system will try and create new badges! var dbBadge = GetallBadges().FirstOrDefault(x => x.Id == badgeMapping.DbBadge.Id); if (dbBadge != null) { if (dbBadge.AwardsPoints != null && dbBadge.AwardsPoints > 0) { var points = new MemberPoints { DateAdded = DateTime.UtcNow, Points = (int)dbBadge.AwardsPoints, MemberId = user.Id }; memberPointsService.Add(points); } var exists = ContextPerRequest.Db.BadgeToMember .FirstOrDefault(x => x.DialogueBadgeId == dbBadge.Id && x.MemberId == user.Id); if (exists == null) { // Add the badge mapping var badgeMember = new BadgeToMember { DialogueBadgeId = dbBadge.Id, MemberId = user.Id }; ContextPerRequest.Db.BadgeToMember.Add(badgeMember); // This needs to be //ContextPerRequest.Db.Badge.Add(dbBadge); activityService.BadgeAwarded(badgeMapping.DbBadge, user, DateTime.UtcNow); } } } } } } return(databaseUpdateNeeded); }