Esempio n. 1
0
 /// <inheritdoc />
 public void RefreshContext(IMvcForumContext context)
 {
     _context = context;
     _membershipUserPointsService.RefreshContext(context);
     _settingsService.RefreshContext(context);
     _notificationService.RefreshContext(context);
     _favouriteService.RefreshContext(context);
     _postService.RefreshContext(context);
     _roleService.RefreshContext(context);
     _pollService.RefreshContext(context);
 }
Esempio n. 2
0
 /// <inheritdoc />
 public void RefreshContext(ICoreForumContext context)
 {
     _context = context;
     _roleService.RefreshContext(context);
     _membershipUserPointsService.RefreshContext(context);
     _settingsService.RefreshContext(context);
     _voteService.RefreshContext(context);
     _uploadedFileService.RefreshContext(context);
     _favouriteService.RefreshContext(context);
     _postEditService.RefreshContext(context);
 }
Esempio n. 3
0
 /// <inheritdoc />
 public void RefreshContext(IMvcForumContext context)
 {
     _context = context;
     _settingsService.RefreshContext(context);
     _localizationService.RefreshContext(context);
     _activityService.RefreshContext(context);
     _voteService.RefreshContext(context);
     _favouriteService.RefreshContext(context);
     _membershipUserPointsService.RefreshContext(context);
     _topicService.RefreshContext(context);
     _groupService.RefreshContext(context);
     _postService.RefreshContext(context);
     _notificationService.RefreshContext(context);
     _pollService.RefreshContext(context);
 }
Esempio n. 4
0
 /// <inheritdoc />
 public void RefreshContext(ICoreForumContext context)
 {
     _context = context;
     _settingsService.RefreshContext(context);
     _localizationService.RefreshContext(context);
     _activityService.RefreshContext(context);
     _voteService.RefreshContext(context);
     _badgeService.RefreshContext(context);
     _privateMessageService.RefreshContext(context);
     _favouriteService.RefreshContext(context);
     _membershipUserPointsService.RefreshContext(context);
     _topicService.RefreshContext(context);
     _categoryService.RefreshContext(context);
     _postService.RefreshContext(context);
     _notificationService.RefreshContext(context);
     _pollService.RefreshContext(context);
 }
Esempio n. 5
0
 /// <inheritdoc />
 public void RefreshContext(ICoreForumContext context)
 {
     _context = context;
     _membershipUserPointsService.RefreshContext(context);
 }
Esempio n. 6
0
        /// <inheritdoc />
        public async Task <IPipelineProcess <MembershipUser> > Process(IPipelineProcess <MembershipUser> input,
                                                                       IMvcForumContext context)
        {
            _voteService.RefreshContext(context);
            _notificationService.RefreshContext(context);
            _favouriteService.RefreshContext(context);
            _membershipUserPointsService.RefreshContext(context);
            _activityService.RefreshContext(context);
            _pollService.RefreshContext(context);
            _topicService.RefreshContext(context);
            _groupService.RefreshContext(context);
            _postService.RefreshContext(context);

            try
            {
                // Delete all topics
                var topics = input.EntityToProcess.Topics;
                if (topics != null && topics.Any())
                {
                    var topicList = new List <Topic>();
                    topicList.AddRange(topics);
                    foreach (var topic in topicList)
                    {
                        var topicDeleteResult = await _topicService.Delete(topic);

                        if (!topicDeleteResult.Successful)
                        {
                            input.AddError(topicDeleteResult.ProcessLog.FirstOrDefault());
                            return(input);
                        }
                    }
                    input.EntityToProcess.Topics.Clear();
                    await context.SaveChangesAsync();
                }

                // Now sorts Last Posts on topics and delete all the users posts
                var posts = input.EntityToProcess.Posts;
                if (posts != null && posts.Any())
                {
                    var postIds = posts.Select(x => x.Id).ToList();

                    // Get all Groups
                    var allGroups = _groupService.GetAll(input.EntityToProcess.Id);

                    // Need to see if any of these are last posts on Topics
                    // If so, need to swap out last post
                    var lastPostTopics = _topicService.GetTopicsByLastPost(postIds, allGroups.ToList());
                    foreach (var topic in lastPostTopics.Where(x => x.User.Id != input.EntityToProcess.Id))
                    {
                        var lastPost = topic.Posts.Where(x => !postIds.Contains(x.Id))
                                       .OrderByDescending(x => x.DateCreated)
                                       .FirstOrDefault();
                        topic.LastPost = lastPost;
                    }

                    await context.SaveChangesAsync();

                    // Delete all posts
                    var postList = new List <Post>();
                    postList.AddRange(posts);
                    foreach (var post in postList)
                    {
                        // Delete post via pipeline
                        var postDeleteResult = await _postService.Delete(post, true);

                        if (!postDeleteResult.Successful)
                        {
                            input.AddError(postDeleteResult.ProcessLog.FirstOrDefault());
                            return(input);
                        }
                    }

                    input.EntityToProcess.UploadedFiles.Clear();
                    input.EntityToProcess.Posts.Clear();

                    await context.SaveChangesAsync();
                }

                // User Votes
                if (input.EntityToProcess.Votes != null)
                {
                    var votesToDelete = new List <Vote>();
                    votesToDelete.AddRange(input.EntityToProcess.Votes);
                    votesToDelete.AddRange(input.EntityToProcess.VotesGiven);
                    foreach (var d in votesToDelete)
                    {
                        _voteService.Delete(d);
                    }
                    input.EntityToProcess.Votes.Clear();
                    input.EntityToProcess.VotesGiven.Clear();
                    await context.SaveChangesAsync();
                }

                // User Group notifications
                if (input.EntityToProcess.GroupNotifications != null)
                {
                    var toDelete = new List <GroupNotification>();
                    toDelete.AddRange(input.EntityToProcess.GroupNotifications);
                    foreach (var obj in toDelete)
                    {
                        _notificationService.Delete(obj);
                    }
                    input.EntityToProcess.GroupNotifications.Clear();
                    await context.SaveChangesAsync();
                }

                // User Favourites
                if (input.EntityToProcess.Favourites != null)
                {
                    var toDelete = new List <Favourite>();
                    toDelete.AddRange(input.EntityToProcess.Favourites);
                    foreach (var obj in toDelete)
                    {
                        _favouriteService.Delete(obj);
                    }
                    input.EntityToProcess.Favourites.Clear();
                    await context.SaveChangesAsync();
                }

                if (input.EntityToProcess.TopicNotifications != null)
                {
                    var notificationsToDelete = new List <TopicNotification>();
                    notificationsToDelete.AddRange(input.EntityToProcess.TopicNotifications);
                    foreach (var topicNotification in notificationsToDelete)
                    {
                        _notificationService.Delete(topicNotification);
                    }
                    input.EntityToProcess.TopicNotifications.Clear();
                }

                // Also clear their points
                var userPoints = input.EntityToProcess.Points;
                if (userPoints.Any())
                {
                    var pointsList = new List <MembershipUserPoints>();
                    pointsList.AddRange(userPoints);
                    foreach (var point in pointsList)
                    {
                        point.User = null;
                        await _membershipUserPointsService.Delete(point);
                    }
                    input.EntityToProcess.Points.Clear();
                }

                // Now clear all activities for this user
                var usersActivities = _activityService.GetDataFieldByGuid(input.EntityToProcess.Id);
                _activityService.Delete(usersActivities.ToList());
                await context.SaveChangesAsync();

                // Also clear their poll votes
                var userPollVotes = input.EntityToProcess.PollVotes;
                if (userPollVotes.Any())
                {
                    var pollList = new List <PollVote>();
                    pollList.AddRange(userPollVotes);
                    foreach (var vote in pollList)
                    {
                        vote.User = null;
                        _pollService.Delete(vote);
                    }
                    input.EntityToProcess.PollVotes.Clear();
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                input.AddError(ex.Message);
                _loggingService.Error(ex);
            }

            return(input);
        }
        /// <inheritdoc />
        public async Task <IPipelineProcess <Post> > Process(IPipelineProcess <Post> input, IMvcForumContext context)
        {
            // Refresh contexts for all services
            _postEditService.RefreshContext(context);
            _membershipUserPointsService.RefreshContext(context);
            _settingsService.RefreshContext(context);
            _activityService.RefreshContext(context);
            _notificationService.RefreshContext(context);

            try
            {
                // Get the Current user from ExtendedData
                var username     = input.ExtendedData[Constants.ExtendedDataKeys.Username] as string;
                var loggedOnUser = await context.MembershipUser.FirstOrDefaultAsync(x => x.UserName == username);

                // Is this an edit? If so, create a post edit
                var isEdit = false;
                if (input.ExtendedData.ContainsKey(Constants.ExtendedDataKeys.IsEdit))
                {
                    isEdit = input.ExtendedData[Constants.ExtendedDataKeys.IsEdit] as bool? == true;
                }

                if (isEdit)
                {
                    // Get the original post
                    var originalPost = await context.Post.Include(x => x.Topic).FirstOrDefaultAsync(x => x.Id == input.EntityToProcess.Id);

                    // Get content from Extended data
                    var postedContent = input.ExtendedData[Constants.ExtendedDataKeys.Content] as string;
                    input.EntityToProcess.PostContent = postedContent;

                    // This is an edit of a post
                    input.EntityToProcess.DateEdited = DateTime.Now;

                    // Grab the original name out the extended data
                    var topicName = input.ExtendedData[Constants.ExtendedDataKeys.Name] as string;
                    if (!originalPost.PostContent.Equals(postedContent, StringComparison.OrdinalIgnoreCase) || !originalPost.Topic.Name.Equals(topicName, StringComparison.OrdinalIgnoreCase))
                    {
                        // Create a post edit
                        var postEdit = new PostEdit
                        {
                            Post                = input.EntityToProcess,
                            DateEdited          = input.EntityToProcess.DateEdited,
                            EditedBy            = loggedOnUser,
                            OriginalPostContent = originalPost.PostContent,
                            OriginalPostTitle   = originalPost.IsTopicStarter ? originalPost.Topic.Name : string.Empty
                        };

                        // Add the post edit too
                        _postEditService.Add(postEdit);
                    }
                }
                else
                {
                    // Add the post
                    context.Post.Add(input.EntityToProcess);
                }

                // Now do a save
                await context.SaveChangesAsync();

                // Update the users points score and post count for posting a new post
                if (!isEdit)
                {
                    // make it last post if this is a new post
                    input.EntityToProcess.Topic.LastPost = input.EntityToProcess;

                    await _membershipUserPointsService.Add(new MembershipUserPoints
                    {
                        Points      = _settingsService.GetSettings().PointsAddedPerPost,
                        User        = input.EntityToProcess.User,
                        PointsFor   = PointsFor.Post,
                        PointsForId = input.EntityToProcess.Id
                    });

                    // Add post activity if it's not an edit, or topic starter and it's not pending
                    if (input.EntityToProcess.IsTopicStarter == false && input.EntityToProcess.Pending != true)
                    {
                        _activityService.PostCreated(input.EntityToProcess);
                    }
                }


                if (input.EntityToProcess.IsTopicStarter == false && input.EntityToProcess.Pending != true)
                {
                    // Send notifications
                    _notificationService.Notify(input.EntityToProcess.Topic, loggedOnUser, NotificationType.Post);
                }

                // Now do a final save
                await context.SaveChangesAsync();

                input.ProcessLog.Add("Post created successfully");
            }
            catch (Exception ex)
            {
                input.AddError(ex.Message);
                _loggingService.Error(ex);
            }

            return(input);
        }
Esempio n. 8
0
        /// <inheritdoc />
        public async Task <IPipelineProcess <Post> > Process(IPipelineProcess <Post> input, IMvcForumContext context)
        {
            // Refresh the context in each of these services
            _voteService.RefreshContext(context);
            _membershipUserPointsService.RefreshContext(context);
            _uploadedFileService.RefreshContext(context);
            _favouriteService.RefreshContext(context);
            _postEditService.RefreshContext(context);
            _roleService.RefreshContext(context);
            _localizationService.RefreshContext(context);

            try
            {
                // Get the Current user from ExtendedData
                var username     = input.ExtendedData[Constants.ExtendedDataKeys.Username] as string;
                var loggedOnUser = await context.MembershipUser.FirstOrDefaultAsync(x => x.UserName == username);

                var loggedOnUsersRole = loggedOnUser.GetRole(_roleService);
                var permissions       = _roleService.GetPermissions(input.EntityToProcess.Topic.Category, loggedOnUsersRole);

                if (input.EntityToProcess.User.Id == loggedOnUser.Id ||
                    permissions[ForumConfiguration.Instance.PermissionDeletePosts].IsTicked)
                {
                    // Get the topic
                    var topic = input.EntityToProcess.Topic;

                    var votes = _voteService.GetVotesByPost(input.EntityToProcess.Id);

                    #region Deleting Points

                    // Remove the points the user got for this post
                    await _membershipUserPointsService.Delete(input.EntityToProcess.User, PointsFor.Post, input.EntityToProcess.Id);

                    // Also get all the votes and delete anything to do with those
                    foreach (var postVote in votes)
                    {
                        await _membershipUserPointsService.Delete(PointsFor.Vote, postVote.Id);
                    }

                    // Also the mark as solution
                    await _membershipUserPointsService.Delete(PointsFor.Solution, input.EntityToProcess.Id);

                    #endregion

                    await context.SaveChangesAsync();

                    #region Deleting Votes

                    var votesToDelete = new List <Vote>();
                    votesToDelete.AddRange(votes);
                    foreach (var vote in votesToDelete)
                    {
                        input.EntityToProcess.Votes.Remove(vote);
                        _voteService.Delete(vote);
                    }
                    input.EntityToProcess.Votes.Clear();

                    #endregion

                    #region Files

                    // Clear files attached to post
                    var filesToDelete = new List <UploadedFile>();
                    filesToDelete.AddRange(input.EntityToProcess.Files);
                    foreach (var uploadedFile in filesToDelete)
                    {
                        // store the file path as we'll need it to delete on the file system
                        var filePath = uploadedFile.FilePath;

                        input.EntityToProcess.Files.Remove(uploadedFile);
                        _uploadedFileService.Delete(uploadedFile);

                        // And finally delete from the file system
                        if (!string.IsNullOrWhiteSpace(filePath))
                        {
                            var mapped = HostingEnvironment.MapPath(filePath);
                            if (mapped != null)
                            {
                                File.Delete(mapped);
                            }
                        }
                    }
                    input.EntityToProcess.Files.Clear();

                    #endregion

                    #region Favourites

                    var postFavourites = new List <Favourite>();
                    postFavourites.AddRange(input.EntityToProcess.Favourites);
                    foreach (var postFavourite in postFavourites)
                    {
                        input.EntityToProcess.Favourites.Remove(postFavourite);
                        _favouriteService.Delete(postFavourite);
                    }
                    input.EntityToProcess.Favourites.Clear();

                    #endregion

                    #region Post Edits

                    var postEdits = new List <PostEdit>();
                    postEdits.AddRange(input.EntityToProcess.PostEdits);
                    foreach (var postEdit in postEdits)
                    {
                        input.EntityToProcess.PostEdits.Remove(postEdit);
                        _postEditService.Delete(postEdit);
                    }
                    input.EntityToProcess.PostEdits.Clear();

                    #endregion

                    await context.SaveChangesAsync();

                    // Before we delete the post, we need to check if this is the last post in the topic
                    // and if so update the topic

                    if (input.ExtendedData.ContainsKey(Constants.ExtendedDataKeys.IgnoreLastPost))
                    {
                        var ignoreLastPost = input.ExtendedData[Constants.ExtendedDataKeys.IgnoreLastPost] as bool?;
                        if (ignoreLastPost == false)
                        {
                            var lastPost = topic.Posts.OrderByDescending(x => x.DateCreated).FirstOrDefault();

                            if (lastPost != null && lastPost.Id == input.EntityToProcess.Id)
                            {
                                // Get the new last post and update the topic
                                topic.LastPost = topic.Posts.Where(x => x.Id != input.EntityToProcess.Id).OrderByDescending(x => x.DateCreated).FirstOrDefault();
                            }

                            if (topic.Solved && input.EntityToProcess.IsSolution)
                            {
                                topic.Solved = false;
                            }

                            // Save the topic
                            await context.SaveChangesAsync();
                        }
                    }

                    // Remove from the topic
                    topic.Posts.Remove(input.EntityToProcess);

                    // now delete the post
                    context.Post.Remove(input.EntityToProcess);

                    // Save changes
                    await context.SaveChangesAsync();
                }
                else
                {
                    input.AddError(_localizationService.GetResourceString("Errors.NoPermission"));
                }
            }
            catch (System.Exception ex)
            {
                input.AddError(ex.Message);
                _loggingService.Error(ex);
            }

            return(input);
        }
Esempio n. 9
0
 /// <inheritdoc />
 public void RefreshContext(IMvcForumContext context)
 {
     _context = context;
     _localizationService.RefreshContext(context);
     _membershipUserPointsService.RefreshContext(context);
 }