public PartialViewResult AjaxMorePosts(GetMorePostsViewModel getMorePostsViewModel) { // Get the topic var topic = ServiceFactory.TopicService.Get(getMorePostsViewModel.TopicId); // Get the permissions for the category that this topic is in var permissions = ServiceFactory.PermissionService.GetPermissions(topic.Category, _membersGroups); // If this user doesn't have access to this topic then just return nothing if (permissions[AppConstants.PermissionDenyAccess].IsTicked) { return(null); } var orderBy = !string.IsNullOrEmpty(getMorePostsViewModel.Order) ? AppHelpers.EnumUtils.ReturnEnumValueFromString <PostOrderBy>(getMorePostsViewModel.Order) : PostOrderBy.Standard; var viewModel = new ShowMorePostsViewModel { Topic = topic, Permissions = permissions, User = CurrentMember }; // Map the posts to the posts viewmodel // Get all favourites for this user var favourites = new List <Favourite>(); if (CurrentMember != null) { favourites.AddRange(ServiceFactory.FavouriteService.GetAllByMember(CurrentMember.Id)); } // Get the posts var posts = ServiceFactory.PostService.GetPagedPostsByTopic(getMorePostsViewModel.PageIndex, Settings.PostsPerPage, int.MaxValue, topic.Id, orderBy); // Get all votes for all the posts var postIds = posts.Select(x => x.Id).ToList(); var allPostVotes = ServiceFactory.VoteService.GetAllVotesForPosts(postIds); viewModel.Posts = new List <ViewPostViewModel>(); foreach (var post in posts) { var postViewModel = PostMapper.MapPostViewModel(permissions, post, CurrentMember, Settings, topic, allPostVotes, favourites); viewModel.Posts.Add(postViewModel); } return(PartialView(PathHelper.GetThemePartialViewPath("AjaxMorePosts"), viewModel)); }
/// <summary> /// Used to render the Topic (virtual node) /// </summary> /// <param name="model"></param> /// <param name="topicname"> /// The topic slug which we use to look up the topic /// </param> /// <param name="p"></param> /// <returns></returns> public ActionResult Show(RenderModel model, string topicname, int?p = null) { var tagPage = model.Content as DialogueVirtualPage; if (tagPage == null) { throw new InvalidOperationException("The RenderModel.Content instance must be of type " + typeof(DialogueVirtualPage)); } if (string.IsNullOrEmpty(topicname)) { return(ErrorToHomePage("Please enter a Topic name")); } // Set the page index var pageIndex = AppHelpers.ReturnCurrentPagingNo(); using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { // Get the topic var topic = ServiceFactory.TopicService.GetTopicBySlug(topicname); if (topic != null) { // Note: Don't use topic.Posts as its not a very efficient SQL statement // Use the post service to get them as it includes other used entities in one // statement rather than loads of sql selects var sortQuerystring = Request.QueryString[AppConstants.PostOrderBy]; var orderBy = !string.IsNullOrEmpty(sortQuerystring) ? AppHelpers.EnumUtils.ReturnEnumValueFromString <PostOrderBy>(sortQuerystring) : PostOrderBy.Standard; // Store the amount per page var amountPerPage = Settings.PostsPerPage; var hasCommentHash = Request.Url != null && Request.Url.PathAndQuery.IndexOf("#comment-", StringComparison.CurrentCultureIgnoreCase) >= 0; if (sortQuerystring == PostOrderBy.All.ToString() || hasCommentHash) { // Overide to show all posts amountPerPage = int.MaxValue; } // Get the posts var posts = ServiceFactory.PostService.GetPagedPostsByTopic(pageIndex, amountPerPage, int.MaxValue, topic.Id, orderBy); // Get the permissions for the category that this topic is in var permissions = ServiceFactory.PermissionService.GetPermissions(topic.Category, _membersGroups); // If this user doesn't have access to this topic then // redirect with message if (permissions[AppConstants.PermissionDenyAccess].IsTicked) { return(ErrorToHomePage("No Permission")); } // See if the user has subscribed to this topic or not var isSubscribed = UserIsAuthenticated && (ServiceFactory.TopicNotificationService.GetByUserAndTopic(CurrentMember, topic).Any()); // Populate the view model for this page var viewModel = new ShowTopicViewModel(model.Content) { Topic = topic, PageIndex = posts.PageIndex, TotalCount = posts.TotalCount, Permissions = permissions, User = CurrentMember, IsSubscribed = isSubscribed, UserHasAlreadyVotedInPoll = false, TotalPages = posts.TotalPages }; // Get all votes for all the posts var postIds = posts.Select(x => x.Id).ToList(); var allPostVotes = ServiceFactory.VoteService.GetAllVotesForPosts(postIds); // Get all favourites for this user viewModel.Favourites = new List <Favourite>(); if (CurrentMember != null) { viewModel.Favourites.AddRange(ServiceFactory.FavouriteService.GetAllByMember(CurrentMember.Id)); } // Map the topic Start // Get the topic starter post var topicStarter = ServiceFactory.PostService.GetTopicStarterPost(topic.Id); viewModel.TopicStarterPost = PostMapper.MapPostViewModel(permissions, topicStarter, CurrentMember, Settings, topic, topicStarter.Votes.ToList(), viewModel.Favourites); // Map the posts to the posts viewmodel viewModel.Posts = new List <ViewPostViewModel>(); foreach (var post in posts) { var postViewModel = PostMapper.MapPostViewModel(permissions, post, CurrentMember, Settings, topic, allPostVotes, viewModel.Favourites); viewModel.Posts.Add(postViewModel); } // If there is a quote querystring var quote = Request["quote"]; if (!string.IsNullOrEmpty(quote)) { try { // Got a quote var postToQuote = ServiceFactory.PostService.Get(new Guid(quote)); viewModel.PostContent = postToQuote.PostContent; } catch (Exception ex) { LogError(ex); } } // See if the topic has a poll, and if so see if this user viewing has already voted if (topic.Poll != null) { // There is a poll and a user // see if the user has voted or not var votes = topic.Poll.PollAnswers.SelectMany(x => x.PollVotes).ToList(); if (UserIsAuthenticated) { viewModel.UserHasAlreadyVotedInPoll = (votes.Count(x => x.MemberId == CurrentMember.Id) > 0); } viewModel.TotalVotesInPoll = votes.Count(); } // update the topic view count only if this topic doesn't belong to the user looking at it var addView = true; if (UserIsAuthenticated && CurrentMember.Id != topic.MemberId) { addView = false; } if (!AppHelpers.UserIsBot() && addView) { // Cool, user doesn't own this topic topic.Views = (topic.Views + 1); try { unitOfWork.Commit(); } catch (Exception ex) { LogError(ex); } } return(View(PathHelper.GetThemeViewPath("Topic"), viewModel)); } } return(ErrorToHomePage("Something went wrong. Please try again")); }
public ActionResult Search(DialoguePage page) { var term = Request["term"]; if (!string.IsNullOrEmpty(term)) { using (UnitOfWorkManager.NewUnitOfWork()) { // Set the page index var pageIndex = AppHelpers.ReturnCurrentPagingNo(); // Returns the formatted string to search on var formattedSearchTerm = AppHelpers.ReturnSearchString(term); // Create an empty viewmodel var viewModel = new SearchViewModel(page) { Posts = new List <ViewPostViewModel>(), AllPermissionSets = new Dictionary <Category, PermissionSet>(), PageIndex = pageIndex, TotalCount = 0, Term = term }; // if there are no results from the filter return an empty search view model. if (string.IsNullOrWhiteSpace(formattedSearchTerm)) { return(View(PathHelper.GetThemeViewPath("Search"), viewModel)); } //// Get all the topics based on the search value var posts = ServiceFactory.PostService.SearchPosts(pageIndex, Settings.PostsPerPage, AppConstants.ActiveTopicsListSize, term); // Get all the categories for this topic collection var topics = posts.Select(x => x.Topic).Distinct().ToList(); var categoryIds = topics.Select(x => x.CategoryId).Distinct().ToList(); var categories = ServiceFactory.CategoryService.Get(categoryIds); // create the view model viewModel = new SearchViewModel(page) { AllPermissionSets = new Dictionary <Category, PermissionSet>(), PageIndex = pageIndex, TotalCount = posts.TotalCount, Term = formattedSearchTerm, TotalPages = posts.TotalPages }; // Current members favourites var favourites = new List <Favourite>(); if (CurrentMember != null) { favourites = ServiceFactory.FavouriteService.GetAllByMember(CurrentMember.Id); } // Get all votes for all the posts var postIds = posts.Select(x => x.Id).ToList(); var allPostVotes = ServiceFactory.VoteService.GetAllVotesForPosts(postIds); // loop through the categories and get the permissions foreach (var category in categories) { var permissionSet = ServiceFactory.PermissionService.GetPermissions(category, _membersGroup); viewModel.AllPermissionSets.Add(category, permissionSet); } // Map the posts to the posts viewmodel viewModel.Posts = new List <ViewPostViewModel>(); foreach (var post in posts) { // TODO - See if we can make this more efficient var cat = categories.FirstOrDefault(x => x.Id == post.Topic.CategoryId); var postViewModel = PostMapper.MapPostViewModel(viewModel.AllPermissionSets[cat], post, CurrentMember, Settings, post.Topic, allPostVotes, favourites, true); viewModel.Posts.Add(postViewModel); } viewModel.PageTitle = string.Concat(Lang("Search.PageTitle"), AppHelpers.SafePlainText(term)); return(View(PathHelper.GetThemeViewPath("Search"), viewModel)); } } return(Redirect(Settings.ForumRootUrl)); }
public PartialViewResult CreatePost(CreateAjaxPostViewModel post) { PermissionSet permissions; Post newPost; Topic topic; var postContent = string.Empty; using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { // Quick check to see if user is locked out, when logged in if (CurrentMember.IsLockedOut | !CurrentMember.IsApproved) { ServiceFactory.MemberService.LogOff(); throw new Exception(Lang("Errors.NoAccess")); } // Check for banned links if (ServiceFactory.BannedLinkService.ContainsBannedLink(post.PostContent)) { throw new Exception(Lang("Errors.BannedLink")); } topic = ServiceFactory.TopicService.Get(post.Topic); postContent = ServiceFactory.BannedWordService.SanitiseBannedWords(post.PostContent); var akismetHelper = new AkismetHelper(); newPost = ServiceFactory.PostService.AddNewPost(postContent, topic, CurrentMember, out permissions); if (!akismetHelper.IsSpam(newPost)) { try { unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.Rollback(); LogError(ex); throw new Exception(Lang("Errors.GenericMessage")); } } else { unitOfWork.Rollback(); throw new Exception(Lang("Errors.PossibleSpam")); } } //Check for moderation if (newPost.Pending) { return(PartialView(PathHelper.GetThemePartialViewPath("PostModeration"))); } // All good send the notifications and send the post back using (UnitOfWorkManager.NewUnitOfWork()) { // Create the view model var viewModel = PostMapper.MapPostViewModel(permissions, newPost, CurrentMember, Settings, topic, new List <Vote>(), new List <Favourite>()); // Success send any notifications NotifyNewTopics(topic, postContent); return(PartialView(PathHelper.GetThemePartialViewPath("Post"), viewModel)); } }
public ActionResult CreatePost(CreateAjaxPostViewModel post) { PermissionSet permissions; Post newPost; Topic topic; var postContent = string.Empty; //get user post count > 5 var currentMemberPostCount = ServiceFactory.PostService.GetByMember(CurrentMember.Id).Count(); if (CurrentMember.Badges == null) { CurrentMember.Badges = ServiceFactory.BadgeService.GetallMembersBadges(CurrentMember.Id); } var hasBadge = CurrentMember.Badges != null && CurrentMember.Badges.Any(x => x.Name == "UserFivePost"); using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { // Quick check to see if user is locked out, when logged in if (CurrentMember.IsLockedOut | !CurrentMember.IsApproved) { ServiceFactory.MemberService.LogOff(); throw new Exception("No Access"); } // Check for banned links if (ServiceFactory.BannedLinkService.ContainsBannedLink(post.PostContent)) { throw new Exception("Banned Link"); } topic = ServiceFactory.TopicService.Get(post.Topic); postContent = ServiceFactory.BannedWordService.SanitiseBannedWords(post.PostContent); var akismetHelper = new AkismetHelper(); newPost = ServiceFactory.PostService.AddNewPost(postContent, topic, CurrentMember, !hasBadge, out permissions); if (!akismetHelper.IsSpam(newPost)) { try { unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.Rollback(); LogError(ex); //throw new Exception("Something went wrong. Please try again"); return(ErrorToHomePage("Something went wrong. Please try again")); } } else { unitOfWork.Rollback(); throw new Exception(Lang("Errors.PossibleSpam")); } } //Check for moderation if (newPost.Pending || (currentMemberPostCount < 5 && !hasBadge)) { // return PartialView(PathHelper.GetThemePartialViewPath("PostModeration")); NotifyCategoryAdmin(topic); //return MessageToHomePage("Awaiting Moderation"); ShowMessage(new GenericMessageViewModel { Message = Lang("Awaiting Moderation"), MessageType = GenericMessages.Warning }); return(Redirect(topic.Category.Url)); } // All good send the notifications and send the post back using (UnitOfWorkManager.NewUnitOfWork()) { // Create the view model var viewModel = PostMapper.MapPostViewModel(permissions, newPost, CurrentMember, Settings, topic, new List <Vote>(), new List <Favourite>()); // Success send any notifications NotifyNewTopics(topic, postContent); var urlReferrer = Request.UrlReferrer; if (urlReferrer != null) { return(Redirect(urlReferrer.AbsolutePath)); } return(PartialView(PathHelper.GetThemePartialViewPath("Post"), viewModel)); } }