public PartialViewResult AjaxMorePosts(GetMorePostsViewModel getMorePostsViewModel) { // Get the topic var topic = _topicService.Get(getMorePostsViewModel.TopicId); // Get the permissions for the category that this topic is in var permissions = RoleService.GetPermissions(topic.Category, UsersRole); // 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) ? EnumUtils.ReturnEnumValueFromString <PostOrderBy>(getMorePostsViewModel.Order) : PostOrderBy.Standard; var posts = _postService.GetPagedPostsByTopic(getMorePostsViewModel.PageIndex, SettingsService.GetSettings().PostsPerPage, int.MaxValue, topic.Id, orderBy); var postIds = posts.Select(x => x.Id).ToList(); var votes = _voteService.GetVotesByPosts(postIds); var favs = _favouriteService.GetAllPostFavourites(postIds); var viewModel = new ShowMorePostsViewModel { Posts = ViewModelMapping.CreatePostViewModels(posts, votes, permissions, topic, LoggedOnUser, SettingsService.GetSettings(), favs), Topic = topic, Permissions = permissions }; return(PartialView(viewModel)); }
public ActionResult Index(int?p, string term) { if (!string.IsNullOrEmpty(term)) { using (UnitOfWorkManager.NewUnitOfWork()) { if (!string.IsNullOrEmpty(term)) { term = term.Trim(); } // Get the global settings var settings = SettingsService.GetSettings(); // Get allowed categories var allowedCategories = _categoryService.GetAllowedCategories(UsersRole); // Set the page index var pageIndex = p ?? 1; // Get all the topics based on the search value var posts = _postService.SearchPosts(pageIndex, SiteConstants.Instance.SearchListSize, int.MaxValue, term, allowedCategories); // Get all the permissions for these topics var topicPermissions = ViewModelMapping.GetPermissionsForTopics(posts.Select(x => x.Topic), RoleService, UsersRole); // 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")); }
public virtual async Task <ActionResult> Show(string slug, int?p) { // Set the page index var pageIndex = p ?? 1; var loggedOnReadOnlyUser = User.GetMembershipUser(MembershipService); var loggedOnUsersRole = loggedOnReadOnlyUser.GetRole(RoleService); // Get the topic var topic = _topicService.GetTopicBySlug(slug); if (topic != null) { var settings = SettingsService.GetSettings(); // 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[Constants.PostOrderBy]; var orderBy = !string.IsNullOrWhiteSpace(sortQuerystring) ? EnumUtils.ReturnEnumValueFromString <PostOrderBy>(sortQuerystring) : PostOrderBy.Standard; // Store the amount per page var amountPerPage = settings.PostsPerPage; if (sortQuerystring == Constants.AllPosts) { // Overide to show all posts amountPerPage = int.MaxValue; } // Get the posts var posts = await _postService.GetPagedPostsByTopic(pageIndex, amountPerPage, int.MaxValue, topic.Id, orderBy); // Get the topic starter post var starterPost = _postService.GetTopicStarterPost(topic.Id); // Get the permissions for the category that this topic is in var permissions = RoleService.GetPermissions(topic.Category, loggedOnUsersRole); // If this user doesn't have access to this topic then // redirect with message if (permissions[ForumConfiguration.Instance.PermissionDenyAccess].IsTicked) { return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission"))); } // Set editor permissions ViewBag.ImageUploadType = permissions[ForumConfiguration.Instance.PermissionInsertEditorImages].IsTicked ? "forumimageinsert" : "image"; var postIds = posts.Select(x => x.Id).ToList(); var votes = _voteService.GetVotesByPosts(postIds); var favourites = _favouriteService.GetAllPostFavourites(postIds); var viewModel = ViewModelMapping.CreateTopicViewModel(topic, permissions, posts, postIds, starterPost, posts.PageIndex, posts.TotalCount, posts.TotalPages, loggedOnReadOnlyUser, settings, _notificationService, _pollService, votes, favourites, true); // If there is a quote querystring var quote = Request["quote"]; if (!string.IsNullOrWhiteSpace(quote)) { try { // Got a quote var postToQuote = _postService.Get(new Guid(quote)); viewModel.QuotedPost = postToQuote.PostContent; viewModel.ReplyTo = postToQuote.Id; viewModel.ReplyToUsername = postToQuote.User.UserName; } catch (Exception ex) { LoggingService.Error(ex); } } var reply = Request["reply"]; if (!string.IsNullOrWhiteSpace(reply)) { try { // Set the reply var toReply = _postService.Get(new Guid(reply)); viewModel.ReplyTo = toReply.Id; viewModel.ReplyToUsername = toReply.User.UserName; } catch (Exception ex) { LoggingService.Error(ex); } } var updateDatabase = false; // User has permission lets update the topic view count // but only if this topic doesn't belong to the user looking at it var addView = !(User.Identity.IsAuthenticated && loggedOnReadOnlyUser.Id == topic.User.Id); if (addView) { updateDatabase = true; } // Check the poll - To see if it has one, and whether it needs to be closed. if (viewModel.Poll?.Poll?.ClosePollAfterDays != null && viewModel.Poll.Poll.ClosePollAfterDays > 0 && !viewModel.Poll.Poll.IsClosed) { // Check the date the topic was created var endDate = viewModel.Poll.Poll.DateCreated.AddDays((int)viewModel.Poll.Poll.ClosePollAfterDays); if (DateTime.Now > endDate) { topic.Poll.IsClosed = true; viewModel.Topic.Poll.IsClosed = true; updateDatabase = true; } } if (!BotUtils.UserIsBot() && updateDatabase) { if (addView) { // Increase the topic views topic.Views = topic.Views + 1; } try { Context.SaveChanges(); } catch (Exception ex) { LoggingService.Error(ex); } } return(View(viewModel)); } return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); }
public virtual async Task <ActionResult> Index(int?p, string term) { if (!string.IsNullOrWhiteSpace(term)) { if (!string.IsNullOrWhiteSpace(term)) { term = term.Trim(); } var loggedOnReadOnlyUser = User.GetMembershipUser(MembershipService); var loggedOnUsersRole = loggedOnReadOnlyUser.GetRole(RoleService); // Get the global settings var settings = SettingsService.GetSettings(); // Get allowed categories var allowedCategories = _categoryService.GetAllowedCategories(loggedOnUsersRole); // 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, allowedCategories); // 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")); }