/// <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 Show(string slug, int?p) { // Set the page index var pageIndex = p ?? 1; using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { // Get the topic var topic = _topicService.GetTopicBySlug(slug); 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) ? EnumUtils.ReturnEnumValueFromString <PostOrderBy>(sortQuerystring) : PostOrderBy.Standard; var posts = _postService.GetPagedPostsByTopic(pageIndex, SettingsService.GetSettings().PostsPerPage, int.MaxValue, topic.Id, orderBy); // Get the topic starter post var topicStarter = _postService.GetTopicStarterPost(topic.Id); // 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 // redirect with message if (permissions[AppConstants.PermissionDenyAccess].IsTicked) { return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission"))); } // See if the user has subscribed to this topic or not var isSubscribed = UserIsAuthenticated && (_topicNotificationService.GetByUserAndTopic(LoggedOnUser, topic).Any()); // Populate the view model for this page var viewModel = new ShowTopicViewModel { Topic = topic, Posts = posts, PageIndex = posts.PageIndex, TotalCount = posts.TotalCount, Permissions = permissions, User = LoggedOnUser, IsSubscribed = isSubscribed, UserHasAlreadyVotedInPoll = false, TopicStarterPost = topicStarter }; // 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.User.Id == LoggedOnUser.Id) > 0); } viewModel.TotalVotesInPoll = votes.Count(); } // 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 = !(UserIsAuthenticated && LoggedOnUser.Id == topic.User.Id); if (!BotUtils.UserIsBot() && addView) { // Cool, user doesn't own this topic topic.Views = (topic.Views + 1); try { unitOfWork.Commit(); } catch (Exception ex) { LoggingService.Error(ex); } } return(View(viewModel)); } } return(ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"))); }