public PageModels.TopicDisplayPage GetDisplayPageModel(int id, int pageId = 1, int target = 0) { var viewModel = new PageModels.TopicDisplayPage(); var record = DbContext.Messages.Find(id); if (record is null) { throw new HttpNotFoundError(); } var parentId = id; if (record.ParentId > 0) { parentId = record.ParentId; } var messageIdQuery = from message in DbContext.Messages where message.Id == parentId || message.ParentId == parentId select message.Id; var messageIds = messageIdQuery.ToList(); if (parentId != id) { viewModel.RedirectPath = GetRedirectPath(id, record.ParentId, messageIds); return(viewModel); } if (target > 0) { var targetPage = MessageRepository.GetPageNumber(target, messageIds); if (targetPage != pageId) { viewModel.RedirectPath = GetRedirectPath(target, id, messageIds); return(viewModel); } } var assignedBoardsQuery = from messageBoard in DbContext.MessageBoards join board in DbContext.Boards on messageBoard.BoardId equals board.Id where messageBoard.MessageId == record.Id select board; var assignedBoards = assignedBoardsQuery.ToList(); var boardRoles = RoleRepository.BoardRoles.Where(r => assignedBoards.Any(b => b.Id == r.BoardId)).Select(r => r.RoleId).ToList(); if (!UserContext.IsAdmin && boardRoles.Any() && !boardRoles.Intersect(UserContext.Roles).Any()) { throw new HttpForbiddenError(); } if (pageId < 1) { pageId = 1; } var take = SettingsRepository.MessagesPerPage(); var skip = take * (pageId - 1); var totalPages = Convert.ToInt32(Math.Ceiling(1.0 * messageIds.Count / take)); var pageMessageIds = messageIds.Skip(skip).Take(take).ToList(); record.ViewCount++; DbContext.Update(record); DbContext.SaveChanges(); var messages = TopicRepository.GetMessages(pageMessageIds); if (string.IsNullOrEmpty(record.ShortPreview)) { record.ShortPreview = "No subject"; } var showFavicons = SettingsRepository.ShowFavicons(); viewModel = new PageModels.TopicDisplayPage { Id = record.Id, TopicHeader = new ItemModels.TopicHeader { StartedById = record.PostedById, Subject = record.ShortPreview, Views = record.ViewCount, }, Messages = messages, Categories = BoardRepository.CategoryIndex(), AssignedBoards = new List <ViewModels.Boards.Items.IndexBoard>(), IsAuthenticated = UserContext.IsAuthenticated, CanManage = UserContext.IsAdmin || record.PostedById == UserContext.ApplicationUser.Id, TotalPages = totalPages, ReplyCount = record.ReplyCount, ViewCount = record.ViewCount, CurrentPage = pageId, ShowFavicons = showFavicons, ReplyForm = new ItemModels.ReplyForm { Id = record.Id } }; foreach (var assignedBoard in assignedBoards) { var indexBoard = BoardRepository.GetIndexBoard(assignedBoard); viewModel.AssignedBoards.Add(indexBoard); } var latestMessageTime = messages.Max(r => r.RecordTime); TopicRepository.MarkRead(record.Id, latestMessageTime, pageMessageIds); return(viewModel); }
public async Task <PageModels.TopicDisplayPage> GetDisplayPageModel(int id, int pageId = 1, int targetId = -1) { var viewModel = new PageModels.TopicDisplayPage(); var record = DbContext.Messages.Find(id); if (record is null) { throw new HttpNotFoundError(); } var topicId = id; if (record.ParentId > 0) { topicId = record.ParentId; } var messageIds = MessageRepository.GetMessageIds(topicId); if (topicId != id) { viewModel.RedirectPath = GetRedirectPath(id, record.ParentId, messageIds); } else if (targetId >= 0) { var targetPage = MessageRepository.GetPageNumber(targetId, messageIds); if (targetPage != pageId) { viewModel.RedirectPath = GetRedirectPath(targetId, id, messageIds); } } if (string.IsNullOrEmpty(viewModel.RedirectPath)) { var bookmarked = (await BookmarkRepository.Records()).Any(r => r.MessageId == topicId); var assignedBoards = await LoadTopicBoards(topicId); if (pageId < 1) { pageId = 1; } var take = UserContext.ApplicationUser.MessagesPerPage; var skip = take * (pageId - 1); var totalPages = Convert.ToInt32(Math.Ceiling(1.0 * messageIds.Count / take)); var pageMessageIds = messageIds.Skip(skip).Take(take).ToList(); record.ViewCount++; DbContext.Update(record); DbContext.SaveChanges(); var messages = await MessageRepository.GetMessages(pageMessageIds); if (string.IsNullOrEmpty(record.ShortPreview)) { record.ShortPreview = "No subject"; } viewModel = new PageModels.TopicDisplayPage { Id = record.Id, TopicHeader = new ItemModels.TopicHeader { StartedById = record.PostedById, Subject = record.ShortPreview, Views = record.ViewCount, }, Messages = messages, Categories = await BoardRepository.CategoryIndex(), AssignedBoards = new List <ViewModels.Boards.Items.IndexBoard>(), IsAuthenticated = UserContext.IsAuthenticated, IsOwner = record.PostedById == UserContext.ApplicationUser?.Id, IsAdmin = UserContext.IsAdmin, IsBookmarked = bookmarked, IsPinned = record.Pinned, ShowFavicons = UserContext.ApplicationUser.ShowFavicons, TotalPages = totalPages, ReplyCount = record.ReplyCount, ViewCount = record.ViewCount, CurrentPage = pageId, ReplyForm = new ViewModels.Messages.ReplyForm { Id = record.Id.ToString(), ElementId = $"topic-reply" } }; foreach (var assignedBoard in assignedBoards) { var indexBoard = await BoardRepository.GetIndexBoard(assignedBoard); viewModel.AssignedBoards.Add(indexBoard); } var latestMessageTime = messages.Max(r => r.RecordTime); TopicRepository.MarkRead(topicId, latestMessageTime, pageMessageIds); } return(viewModel); }
public async Task <IActionResult> Display(int id, int page = 1, int target = -1) { var topic = await DbContext.Topics.FindAsync(id); if (topic is null || topic.Deleted) { throw new HttpNotFoundError(); } if (page < 1) { var redirectUrl = Url.Action(nameof(Display), new { id, page = 1, target }); return(Redirect(redirectUrl)); } var messageIds = MessageRepository.GetMessageIds(topic.Id); if (target >= 0) { var targetPage = MessageRepository.GetPageNumber(target, messageIds); if (targetPage != page) { return(Redirect(GetRedirectPath(id, targetPage, target))); } } var take = CurrentUser.ApplicationUser.MessagesPerPage; var skip = take * (page - 1); var totalPages = Convert.ToInt32(Math.Ceiling(1.0 * messageIds.Count / take)); var pageMessageIds = messageIds.Skip(skip).Take(take).ToList(); var viewModel = new ViewModels.Topics.TopicDisplayPage { Id = topic.Id, FirstMessageId = topic.FirstMessageId, Subject = string.IsNullOrEmpty(topic.FirstMessageShortPreview) ? "No subject" : topic.FirstMessageShortPreview, AssignedBoards = new List <ViewModels.Boards.IndexBoard>(), IsAuthenticated = CurrentUser.IsAuthenticated, IsOwner = topic.FirstMessagePostedById == CurrentUser.Id, IsAdmin = CurrentUser.IsAdmin, IsPinned = topic.Pinned, ShowFavicons = CurrentUser.ApplicationUser.ShowFavicons, TotalPages = totalPages, ReplyCount = topic.ReplyCount, ViewCount = topic.ViewCount, CurrentPage = page, ReplyForm = new ViewModels.Messages.ReplyForm { Id = "0", TopicId = topic.Id.ToString(), ElementId = "topic-reply" } }; await isBookmarked(); await loadMessages(); await loadCategories(); await loadTopicBoards(); await loadSmileyViewData(); return(await ForumViewResult.ViewResult(this, viewModel)); async Task isBookmarked() { viewModel.IsBookmarked = await BookmarkRepository.IsBookmarked(topic.Id); } async Task loadMessages() { viewModel.Messages = await MessageRepository.GetMessages(pageMessageIds); var latestMessageTime = viewModel.Messages.Max(r => r.RecordTime); await TopicRepository.MarkRead(topic.Id, latestMessageTime, pageMessageIds); topic.ViewCount++; DbContext.Update(topic); await DbContext.SaveChangesAsync(); } async Task loadCategories() { viewModel.Categories = await BoardRepository.CategoryIndex(); } async Task loadTopicBoards() { var topicBoards = await BoardRepository.GetTopicBoards(topic.Id); foreach (var topicBoard in topicBoards) { var indexBoard = await BoardRepository.GetIndexBoard(topicBoard); viewModel.AssignedBoards.Add(indexBoard); } } async Task loadSmileyViewData() { ViewData[Constants.InternalKeys.SmileyViewData] = await SmileyRepository.GetSelectorList(); } }