Exemplo n.º 1
0
        public virtual PartialViewResult ListSections()
        {
            // TODO - How can we cache this effectively??
            // Get all sections, and include all Groups

            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

            // Model for the sections
            var allSections = new List <SectionListViewModel>();

            // Get sections from the DB
            var dbSections = _groupService.GetAllSections();

            // Get all Groups grouped by section
            var groupedGroups = _groupService.GetAllMainGroupsInSummaryGroupedBySection(LoggedOnReadOnlyUser?.Id);

            // Loop sections
            foreach (var dbSection in dbSections)
            {
                var GroupsInSection   = groupedGroups[dbSection.Id];
                var allPermissionSets = ViewModelMapping.GetPermissionsForGroups(GroupsInSection, _roleService, loggedOnUsersRole, true);

                allSections.Add(new SectionListViewModel
                {
                    Section           = dbSection,
                    AllPermissionSets = allPermissionSets
                });
            }

            return(PartialView(allSections));
        }
Exemplo n.º 2
0
        public virtual ActionResult GetPostEditHistory(Guid id)
        {
            var post = _postService.Get(id);

            if (post != null)
            {
                User.GetMembershipUser(MembershipService);
                var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

                // Check permissions
                var permissions = RoleService.GetPermissions(post.Topic.Group, loggedOnUsersRole);
                if (permissions[ForumConfiguration.Instance.PermissionEditPosts].IsTicked)
                {
                    // Good to go
                    var postEdits = _postEditService.GetByPost(id);
                    var viewModel = new PostEditHistoryViewModel
                    {
                        PostEdits = postEdits
                    };
                    return(PartialView(viewModel));
                }
            }

            return(Content(LocalizationService.GetResourceString("Errors.GenericMessage")));
        }
Exemplo n.º 3
0
        public virtual FileResult Download(Guid id)
        {
            var uploadedFileById = _uploadedFileService.Get(id);

            if (uploadedFileById != null)
            {
                User.GetMembershipUser(MembershipService);
                var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

                // Check the user has permission to download this file
                var fileGroup       = uploadedFileById.Post.Topic.Group;
                var allowedGroupIds = _groupService.GetAllowedGroups(loggedOnUsersRole, loggedOnUsersRole.Id).Select(x => x.Id);
                if (allowedGroupIds.Contains(fileGroup.Id))
                {
                    //if(AppHelpers.FileIsImage(uploadedFileById.FilePath))
                    //{

                    //}

                    var fileBytes = System.IO.File.ReadAllBytes(HostingEnvironment.MapPath(uploadedFileById.FilePath));
                    return(File(fileBytes, MediaTypeNames.Application.Octet, uploadedFileById.Filename));
                }
            }
            return(null);
        }
Exemplo n.º 4
0
        public PartialViewResult GetPost(Post post)
        {
            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);
            var permissions       = RoleService.GetPermissions(post.Topic.Group, loggedOnUsersRole);

            var viewModel = ViewModelMapping.CreatePostViewModel(post, post.Votes.ToList(), permissions, post.Topic, LoggedOnReadOnlyUser, SettingsService.GetSettings(), post.Favourites.ToList());

            return(PartialView("_post", viewModel));
        }
Exemplo n.º 5
0
        public virtual ActionResult LatestRss()
        {
            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

            // Allowed Groups for a guest - As that's all we want latest RSS to show
            var guestRole     = RoleService.GetRole(Constants.GuestRoleName);
            var allowedGroups = _groupService.GetAllowedGroups(guestRole, LoggedOnReadOnlyUser?.Id);

            // get an rss lit ready
            var rssTopics = new List <RssItem>();

            // Get the latest topics
            var topics = _topicService.GetRecentRssTopics(50, allowedGroups);

            // Get all the Groups for this topic collection
            var Groups = topics.Select(x => x.Group).Distinct();

            // create permissions
            var permissions = new Dictionary <Group, PermissionSet>();

            // loop through the Groups and get the permissions
            foreach (var Group in Groups)
            {
                var permissionSet = RoleService.GetPermissions(Group, loggedOnUsersRole);
                permissions.Add(Group, permissionSet);
            }

            // Now loop through the topics and remove any that user does not have permission for
            foreach (var topic in topics)
            {
                // Get the permissions for this topic via its parent Group
                var permission = permissions[topic.Group];

                // Add only topics user has permission to
                if (!permission[ForumConfiguration.Instance.PermissionDenyAccess].IsTicked)
                {
                    if (topic.Posts.Any())
                    {
                        var firstOrDefault = topic.Posts.FirstOrDefault(x => x.IsTopicStarter);
                        if (firstOrDefault != null)
                        {
                            rssTopics.Add(new RssItem
                            {
                                Description   = firstOrDefault.PostContent,
                                Link          = topic.NiceUrl,
                                Title         = topic.Name,
                                PublishedDate = topic.CreateDate
                            });
                        }
                    }
                }
            }

            return(new RssResult(rssTopics, LocalizationService.GetResourceString("Rss.LatestActivity.Title"),
                                 LocalizationService.GetResourceString("Rss.LatestActivity.Description")));
        }
Exemplo n.º 6
0
        public virtual ActionResult MovePost(Guid id)
        {
            User.GetMembershipUser(MembershipService);
            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

            // Firstly check if this is a post and they are allowed to move it
            var post = _postService.Get(id);

            if (post == null)
            {
                return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")));
            }

            var permissions   = RoleService.GetPermissions(post.Topic.Group, loggedOnUsersRole);
            var allowedGroups = _groupService.GetAllowedGroups(loggedOnUsersRole, LoggedOnReadOnlyUser?.Id);

            // Does the user have permission to this posts Group
            var cat = allowedGroups.FirstOrDefault(x => x.Id == post.Topic.Group.Id);

            if (cat == null)
            {
                return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission")));
            }

            // Does this user have permission to move
            if (!permissions[ForumConfiguration.Instance.PermissionEditPosts].IsTicked)
            {
                return(NoPermission(post.Topic));
            }

            var topics = _topicService.GetAllSelectList(allowedGroups, 30);

            topics.Insert(0, new SelectListItem
            {
                Text  = LocalizationService.GetResourceString("Topic.Choose"),
                Value = ""
            });

            var postViewModel = ViewModelMapping.CreatePostViewModel(post, post.Votes.ToList(), permissions, post.Topic,
                                                                     LoggedOnReadOnlyUser, SettingsService.GetSettings(), post.Favourites.ToList());

            postViewModel.MinimalPost = true;
            var viewModel = new MovePostViewModel
            {
                Post             = postViewModel,
                PostId           = post.Id,
                LatestTopics     = topics,
                MoveReplyToPosts = true
            };

            return(View(viewModel));
        }
Exemplo n.º 7
0
        private MembershipRole GetGroupMembershipRole(Guid groupId)
        {
            var role = _groupService.GetGroupRole(groupId, LoggedOnReadOnlyUser?.Id);

            if (LoggedOnReadOnlyUser == null)
            {
                return(role);
            }

            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

            return(loggedOnUsersRole.RoleName == MvcForum.Core.Constants.Constants.AdminRoleName ? loggedOnUsersRole : role);
        }
Exemplo n.º 8
0
        public virtual PartialViewResult MyGroupsList()
        {
            // TODO - OutputCache and add clear to post/topic/Group delete/create/edit

            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);
            var catViewModel      = new GroupListSummaryViewModel
            {
                AllPermissionSets =
                    ViewModelMapping.GetPermissionsForGroups(_groupService.GetAllMyGroupsInSummary(LoggedOnReadOnlyUser.Id),
                                                             _roleService, loggedOnUsersRole)
            };

            return(PartialView("_MyGroups", catViewModel));
        }
Exemplo n.º 9
0
        public virtual ActionResult Index()
        {
            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

            // Show both pending topics and also pending posts
            // Use ajax for paging too
            var allowedGroups = _groupService.GetAllowedGroups(loggedOnUsersRole, LoggedOnReadOnlyUser?.Id);
            var viewModel     = new ModerateViewModel
            {
                Posts  = _postService.GetPendingPosts(allowedGroups, loggedOnUsersRole),
                Topics = _topicService.GetPendingTopics(allowedGroups, loggedOnUsersRole)
            };

            return(View(viewModel));
        }
Exemplo n.º 10
0
        public virtual ActionResult GetAllPostLikes(Guid id)
        {
            User.GetMembershipUser(MembershipService);
            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);
            var post        = _postService.Get(id);
            var permissions = RoleService.GetPermissions(post.Topic.Group, loggedOnUsersRole);
            var votes       = _voteService.GetVotesByPosts(new List <Guid> {
                id
            });
            var viewModel = ViewModelMapping.CreatePostViewModel(post, votes[id], permissions, post.Topic,
                                                                 LoggedOnReadOnlyUser, SettingsService.GetSettings(), new List <Favourite>());
            var upVotes = viewModel.Votes.Where(x => x.Amount > 0).ToList();

            return(View(upVotes));
        }
Exemplo n.º 11
0
        public virtual PartialViewResult PopularTags(int amountToTake)
        {
            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

            var cacheKey  = string.Concat("PopularTags", amountToTake, loggedOnUsersRole.Id);
            var viewModel = CacheService.Get <PopularTagViewModel>(cacheKey);

            if (viewModel == null)
            {
                var allowedGroups = _groupService.GetAllowedGroups(loggedOnUsersRole, LoggedOnReadOnlyUser?.Id);
                var popularTags   = _topicTagService.GetPopularTags(amountToTake, allowedGroups);
                viewModel = new PopularTagViewModel {
                    PopularTags = popularTags
                };
                CacheService.Set(cacheKey, viewModel, CacheTimes.SixHours);
            }
            return(PartialView(viewModel));
        }
Exemplo n.º 12
0
        public virtual PartialViewResult ListMainGroups()
        {
            // TODO - OutputCache and add clear to post/topic/Group delete/create/edit
            if (LoggedOnReadOnlyUser is null)
            {
                throw new ArgumentNullException(nameof(LoggedOnReadOnlyUser));
            }

            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);
            var catViewModel      = new GroupListSummaryViewModel
            {
                AllPermissionSets =
                    ViewModelMapping.GetPermissionsForGroups(_groupService.GetAllMainGroupsInSummary(LoggedOnReadOnlyUser.Id),
                                                             _roleService, loggedOnUsersRole)
            };

            return(PartialView(catViewModel));
        }
Exemplo n.º 13
0
        public virtual async Task <ActionResult> MovePostAsync(MovePostViewModel viewModel)
        {
            // Firstly check if this is a post and they are allowed to move it
            var post = _postService.Get(viewModel.PostId);

            if (post == null)
            {
                return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")));
            }

            var moveResult = await _postService.Move(post, viewModel.TopicId, viewModel.TopicTitle,
                                                     viewModel.MoveReplyToPosts);

            if (moveResult.Successful)
            {
                // On Update redirect to the topic
                return(RedirectToAction("Show", "Topic", new { slug = moveResult.EntityToProcess.Topic.Slug }));
            }

            // Add a model error to show issue
            ModelState.AddModelError("", moveResult.ProcessLog.FirstOrDefault());

            // Sort the view model before sending back
            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);
            var permissions       = RoleService.GetPermissions(post.Topic.Group, loggedOnUsersRole);
            var allowedGroups     = _groupService.GetAllowedGroups(loggedOnUsersRole, LoggedOnReadOnlyUser?.Id);

            // Repopulate the topics
            var topics = _topicService.GetAllSelectList(allowedGroups, 30);

            topics.Insert(0, new SelectListItem
            {
                Text  = LocalizationService.GetResourceString("Topic.Choose"),
                Value = ""
            });

            viewModel.LatestTopics = topics;
            viewModel.Post         = ViewModelMapping.CreatePostViewModel(post, post.Votes.ToList(), permissions,
                                                                          post.Topic, LoggedOnReadOnlyUser, SettingsService.GetSettings(), post.Favourites.ToList());
            viewModel.Post.MinimalPost = true;
            viewModel.PostId           = post.Id;

            return(View(viewModel));
        }
Exemplo n.º 14
0
        public virtual async Task <ActionResult> ModerateTopicAsync(ModerateActionViewModel viewModel, CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                User.GetMembershipUser(MembershipService);
                var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

                var topic       = _topicService.Get(viewModel.TopicId);
                var permissions = RoleService.GetPermissions(topic.Group, loggedOnUsersRole);

                // Is this user allowed to moderate - We use EditPosts for now until we change the permissions system
                if (!permissions[ForumConfiguration.Instance.PermissionEditPosts].IsTicked)
                {
                    return(Content(LocalizationService.GetResourceString("Errors.NoPermission")));
                }

                if (viewModel.IsApproved)
                {
                    topic.Pending = false;
                    _activityService.TopicCreated(topic);
                }
                else
                {
                    var topicResult = await _topicService.Delete(topic);

                    if (!topicResult.Successful)
                    {
                        return(Content(topicResult.ProcessLog.FirstOrDefault()));
                    }
                }

                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                Context.RollBack();
                LoggingService.Error(ex);
                return(Content(ex.Message));
            }


            return(Content("allgood"));
        }
Exemplo n.º 15
0
        /// <summary>
        ///     Edit user
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual ActionResult Edit(Guid id)
        {
            User.GetMembershipUser(MembershipService);
            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);
            var loggedOnUserId    = LoggedOnReadOnlyUser?.Id ?? Guid.Empty;

            var permissions = RoleService.GetPermissions(null, loggedOnUsersRole);

            // Check is has permissions
            if (User.IsInRole(Constants.AdminRoleName) || loggedOnUserId == id ||
                permissions[ForumConfiguration.Instance.PermissionEditMembers].IsTicked)
            {
                var user      = MembershipService.GetUser(id);
                var viewModel = user.PopulateMemberViewModel();

                return(View(viewModel));
            }

            return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission")));
        }
Exemplo n.º 16
0
        public virtual ActionResult ModeratePost(ModerateActionViewModel viewModel)
        {
            try
            {
                User.GetMembershipUser(MembershipService);
                var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

                var post        = _postService.Get(viewModel.PostId);
                var permissions = RoleService.GetPermissions(post.Topic.Group, loggedOnUsersRole);
                if (!permissions[ForumConfiguration.Instance.PermissionEditPosts].IsTicked)
                {
                    return(Content(LocalizationService.GetResourceString("Errors.NoPermission")));
                }

                if (viewModel.IsApproved)
                {
                    post.Pending = false;
                    _activityService.PostCreated(post);
                }
                else
                {
                    _postService.Delete(post, false);
                }

                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                Context.RollBack();
                LoggingService.Error(ex);
                return(Content(ex.Message));
            }


            return(Content("allgood"));
        }
Exemplo n.º 17
0
        public virtual async Task <ActionResult> Index(int?p, string term)
        {
            if (!string.IsNullOrWhiteSpace(term))
            {
                if (!string.IsNullOrWhiteSpace(term))
                {
                    term = term.Trim();
                }

                var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

                // Get the global settings
                var settings = SettingsService.GetSettings();

                // Get allowed Groups
                var allowedGroups = _groupService.GetAllowedGroups(loggedOnUsersRole, LoggedOnReadOnlyUser?.Id);


                // Set the page index
                var pageIndex = p ?? 1;

                // Get all the topics based on the search value
                var posts = await _postService.SearchPosts(pageIndex,
                                                           ForumConfiguration.Instance.SearchListSize,
                                                           int.MaxValue,
                                                           term,
                                                           allowedGroups);

                // Get all the permissions for these topics
                var topicPermissions =
                    ViewModelMapping.GetPermissionsForTopics(posts.Select(x => x.Topic), RoleService,
                                                             loggedOnUsersRole);

                // Get the post Ids
                var postIds = posts.Select(x => x.Id).ToList();

                // Get all votes for these posts
                var votes = _voteService.GetVotesByPosts(postIds);

                // Get all favourites for these posts
                var favs = _favouriteService.GetAllPostFavourites(postIds);

                // Create the post view models
                var viewModels = ViewModelMapping.CreatePostViewModels(posts.ToList(), votes, topicPermissions,
                                                                       LoggedOnReadOnlyUser, settings, favs);

                // create the view model
                var viewModel = new SearchViewModel
                {
                    Posts      = viewModels,
                    PageIndex  = pageIndex,
                    TotalCount = posts.TotalCount,
                    TotalPages = posts.TotalPages,
                    Term       = term
                };

                return(View(viewModel));
            }

            return(RedirectToAction("Index", "Home"));
        }
Exemplo n.º 18
0
        public virtual ActionResult DeleteUploadedFile(Guid id)
        {
            if (id != Guid.Empty)
            {
                Topic topic = null;
                try
                {
                    User.GetMembershipUser(MembershipService);
                    var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

                    // Get the file and associated objects we'll need
                    var uploadedFile = _uploadedFileService.Get(id);
                    var post         = uploadedFile.Post;
                    topic = post.Topic;

                    if (loggedOnUsersRole.RoleName == Constants.AdminRoleName ||
                        uploadedFile.MembershipUser.Id == LoggedOnReadOnlyUser?.Id)
                    {
                        // Ok to delete file
                        // Remove it from the post
                        post.Files.Remove(uploadedFile);

                        // store the file path as we'll need it to delete on the file system
                        var filePath = uploadedFile.FilePath;

                        // Now delete it
                        _uploadedFileService.Delete(uploadedFile);


                        // And finally delete from the file system
                        System.IO.File.Delete(HostingEnvironment.MapPath(filePath));
                    }
                    else
                    {
                        TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                        {
                            Message     = LocalizationService.GetResourceString("Errors.NoPermission"),
                            MessageType = GenericMessages.danger
                        };
                        Redirect(topic.NiceUrl);
                    }

                    //Commit
                    Context.SaveChanges();

                    TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                    {
                        Message     = LocalizationService.GetResourceString("File.SuccessfullyDeleted"),
                        MessageType = GenericMessages.success
                    };
                    return(Redirect(topic.NiceUrl));
                }
                catch (Exception ex)
                {
                    Context.RollBack();
                    LoggingService.Error(ex);
                    TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                    {
                        Message     = LocalizationService.GetResourceString("Errors.GenericMessage"),
                        MessageType = GenericMessages.danger
                    };
                    return(topic != null
                        ? Redirect(topic.NiceUrl)
                        : ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")));
                }
            }
            return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")));
        }
Exemplo n.º 19
0
        public virtual ActionResult UploadPostFiles(AttachFileToPostViewModel attachFileToPostViewModel)
        {
            var topic = new Topic();

            try
            {
                User.GetMembershipUser(MembershipService);
                var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

                // First this to do is get the post
                var post = _postService.Get(attachFileToPostViewModel.UploadPostId);
                if (post != null)
                {
                    // Now get the topic
                    topic = post.Topic;

                    // Check we get a valid post back and have some file
                    if (attachFileToPostViewModel.Files != null && attachFileToPostViewModel.Files.Any())
                    {
                        // Now get the Group
                        var Group = topic.Group;

                        // Get the permissions for this Group, and check they are allowed to update and
                        // not trying to be a sneaky mofo
                        var permissions = RoleService.GetPermissions(Group, loggedOnUsersRole);
                        if (permissions[ForumConfiguration.Instance.PermissionAttachFiles].IsTicked == false)
                        {
                            TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                            {
                                Message     = LocalizationService.GetResourceString("Errors.NoPermission"),
                                MessageType = GenericMessages.danger
                            };

                            return(Redirect(topic.NiceUrl));
                        }

                        // woot! User has permission and all seems ok
                        // Before we save anything, check the user already has an upload folder and if not create one
                        var uploadFolderPath = HostingEnvironment.MapPath(
                            string.Concat(ForumConfiguration.Instance.UploadFolderPath, LoggedOnReadOnlyUser?.Id));
                        if (!Directory.Exists(uploadFolderPath))
                        {
                            Directory.CreateDirectory(uploadFolderPath);
                        }

                        // Loop through each file and get the file info and save to the users folder and Db
                        foreach (var file in attachFileToPostViewModel.Files)
                        {
                            if (file != null)
                            {
                                // If successful then upload the file
                                var uploadResult = file.UploadFile(uploadFolderPath, LocalizationService);
                                if (!uploadResult.UploadSuccessful)
                                {
                                    TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                                    {
                                        Message     = uploadResult.ErrorMessage,
                                        MessageType = GenericMessages.danger
                                    };
                                    return(Redirect(topic.NiceUrl));
                                }

                                // Add the filename to the database
                                var loggedOnUser = MembershipService.GetUser(LoggedOnReadOnlyUser?.Id);
                                var uploadedFile = new UploadedFile
                                {
                                    Filename       = uploadResult.UploadedFileName,
                                    Post           = post,
                                    MembershipUser = loggedOnUser
                                };
                                _uploadedFileService.Add(uploadedFile);
                            }
                        }

                        //Commit
                        Context.SaveChanges();

                        // Redirect to the topic with a success message
                        TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                        {
                            Message     = LocalizationService.GetResourceString("Post.FilesUploaded"),
                            MessageType = GenericMessages.success
                        };

                        return(Redirect(topic.NiceUrl));
                    }
                    // Else return with error to home page
                    return(topic != null
                        ? Redirect(topic.NiceUrl)
                        : ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")));
                }
                // Else return with error to home page
                return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")));
            }
            catch (Exception ex)
            {
                Context.RollBack();
                LoggingService.Error(ex);
                TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                {
                    Message     = LocalizationService.GetResourceString("Errors.GenericMessage"),
                    MessageType = GenericMessages.danger
                };
                return(topic != null
                    ? Redirect(topic.NiceUrl)
                    : ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage")));
            }
        }
Exemplo n.º 20
0
        public virtual async Task <ActionResult> DeletePostAsync(Guid id)
        {
            User.GetMembershipUser(MembershipService);
            var loggedOnUsersRole = LoggedOnReadOnlyUser.GetRole(RoleService);

            // Got to get a lot of things here as we have to check permissions
            // Get the post
            var post   = _postService.Get(id);
            var postId = post.Id;

            // get this so we know where to redirect after
            var isTopicStarter = post.IsTopicStarter;

            // Get the topic
            var topic    = post.Topic;
            var topicUrl = topic.NiceUrl;

            // get the users permissions
            var permissions = RoleService.GetPermissions(topic.Group, loggedOnUsersRole);

            if (post.User.Id == LoggedOnReadOnlyUser?.Id ||
                permissions[ForumConfiguration.Instance.PermissionDeletePosts].IsTicked)
            {
                try
                {
                    // Delete post / topic
                    if (post.IsTopicStarter)
                    {
                        // Delete entire topic
                        var result = await _topicService.Delete(topic);

                        if (!result.Successful)
                        {
                            TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                            {
                                Message     = result.ProcessLog.FirstOrDefault(),
                                MessageType = GenericMessages.success
                            };

                            return(Redirect(topic.NiceUrl));
                        }
                    }
                    else
                    {
                        // Deletes single post and associated data
                        var result = await _postService.Delete(post, false);

                        if (!result.Successful)
                        {
                            TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                            {
                                Message     = result.ProcessLog.FirstOrDefault(),
                                MessageType = GenericMessages.success
                            };

                            return(Redirect(topic.NiceUrl));
                        }

                        // Remove in replyto's
                        var relatedPosts = _postService.GetReplyToPosts(postId);
                        foreach (var relatedPost in relatedPosts)
                        {
                            relatedPost.InReplyTo = null;
                        }
                    }

                    Context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Context.RollBack();
                    LoggingService.Error(ex);
                    ShowMessage(new GenericMessageViewModel
                    {
                        Message     = LocalizationService.GetResourceString("Errors.GenericMessage"),
                        MessageType = GenericMessages.danger
                    });
                    return(Redirect(topicUrl));
                }
            }

            // Deleted successfully
            if (isTopicStarter)
            {
                // Redirect to root as this was a topic and deleted
                TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
                {
                    Message     = LocalizationService.GetResourceString("Topic.Deleted"),
                    MessageType = GenericMessages.success
                };
                return(RedirectToAction("Index", "Home"));
            }

            // Show message that post is deleted
            TempData[Constants.MessageViewBagName] = new GenericMessageViewModel
            {
                Message     = LocalizationService.GetResourceString("Post.Deleted"),
                MessageType = GenericMessages.success
            };

            return(Redirect(topic.NiceUrl));
        }