Example #1
0
        public async Task <IActionResult> Display(int id, int pageId = 1, int target = -1)
        {
            ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

            var viewModel = await GetDisplayPageModel(id, pageId, target);

            if (string.IsNullOrEmpty(viewModel.RedirectPath))
            {
                return(await ForumViewResult.ViewResult(this, viewModel));
            }
            else
            {
                return(Redirect(viewModel.RedirectPath));
            }
        }
Example #2
0
        public async Task <IActionResult> Reply(ControllerModels.Messages.CreateReplyInput input)
        {
            if (input.Id > 0)
            {
                var message = await DbContext.Messages.FirstOrDefaultAsync(m => m.Id == input.Id);

                if (message is null || message.Deleted)
                {
                    throw new HttpNotFoundError();
                }
            }

            if (ModelState.IsValid)
            {
                var result = await MessageRepository.CreateReply(input);

                ModelState.AddModelErrors(result.Errors);

                if (ModelState.IsValid)
                {
                    var hubAction = "new-reply";

                    if (result.Merged)
                    {
                        hubAction = "updated-message";
                    }

                    await ForumHub.Clients.All.SendAsync(hubAction, new HubModels.Message {
                        TopicId   = result.TopicId,
                        MessageId = result.MessageId
                    });

                    var redirectPath = base.Url.DisplayMessage(result.TopicId, result.MessageId);
                    return(Redirect(redirectPath));
                }
            }

            ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

            var viewModel = new ViewModels.Messages.ReplyForm {
                Id        = input.Id.ToString(),
                Body      = input.Body,
                ElementId = $"message-reply-{input.Id}"
            };

            return(await ForumViewResult.ViewResult(this, viewModel));
        }
Example #3
0
        public async Task <IActionResult> Reply(int id = 0)
        {
            ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

            var message = await DbContext.Messages.FirstOrDefaultAsync(m => m.Id == id);

            if (message is null || message.Deleted)
            {
                throw new HttpNotFoundError();
            }

            var viewModel = new ViewModels.Messages.ReplyForm {
                Id        = id.ToString(),
                ElementId = $"message-reply-{id}"
            };

            return(await ForumViewResult.ViewResult(this, viewModel));
        }
Example #4
0
        public async Task <IActionResult> Create(ControllerModels.Topics.CreateTopicInput input)
        {
            if (ModelState.IsValid)
            {
                // Creating a topic via bookmarklet
                if (Request.Method == "GET" && input.BoardId != null)
                {
                    input.SelectedBoards.Add((int)input.BoardId);
                }
                else
                {
                    foreach (var board in await BoardRepository.Records())
                    {
                        if (Request.Form.TryGetValue("Selected_" + board.Id, out var boardSelected))
                        {
                            if (boardSelected == "True")
                            {
                                input.SelectedBoards.Add(board.Id);
                            }
                        }
                    }
                }

                var result = await TopicRepository.CreateTopic(input);

                ModelState.AddModelErrors(result.Errors);

                if (ModelState.IsValid)
                {
                    var redirectPath = Url.DisplayMessage(result.TopicId, result.MessageId);
                    return(Redirect(redirectPath));
                }
            }

            ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

            var viewModel = new ViewModels.Topics.CreateTopicForm {
                BoardId = input.BoardId.ToString(),
                Body    = input.Body
            };

            return(await ForumViewResult.ViewResult(this, viewModel));
        }
Example #5
0
        public async Task <IActionResult> Edit(int id)
        {
            ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

            var record = await DbContext.Messages.FirstOrDefaultAsync(m => m.Id == id);

            if (record is null)
            {
                throw new HttpNotFoundError();
            }

            var viewModel = new ViewModels.Messages.EditMessageForm {
                Id        = id.ToString(),
                Body      = record.OriginalBody,
                ElementId = $"edit-message-{id}"
            };

            return(await ForumViewResult.ViewResult(this, viewModel));
        }
Example #6
0
        public async Task <IActionResult> Create(InputModels.MessageInput input)
        {
            if (ModelState.IsValid)
            {
                if (Request.Method == "GET" && input.BoardId != null)
                {
                    input.SelectedBoards.Add((int)input.BoardId);
                }
                else
                {
                    foreach (var board in await BoardRepository.Records())
                    {
                        if (Request.Form.TryGetValue("Selected_" + board.Id, out var boardSelected))
                        {
                            if (boardSelected == "True")
                            {
                                input.SelectedBoards.Add(board.Id);
                            }
                        }
                    }
                }

                var serviceResponse = await MessageRepository.CreateTopic(input);

                return(await ForumViewResult.RedirectFromService(this, serviceResponse, FailureCallback));
            }

            return(await FailureCallback());

            async Task <IActionResult> FailureCallback()
            {
                ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

                var viewModel = new ViewModels.Messages.CreateTopicForm {
                    Id      = "0",
                    BoardId = input.BoardId.ToString(),
                    Body    = input.Body
                };

                return(await ForumViewResult.ViewResult(this, viewModel));
            }
        }
Example #7
0
        public async Task <IActionResult> Reply(InputModels.MessageInput input)
        {
            if (ModelState.IsValid)
            {
                var serviceResponse = await MessageRepository.CreateReply(input);

                if (input.SideLoad)
                {
                    foreach (var kvp in serviceResponse.Errors)
                    {
                        ModelState.AddModelError(kvp.Key, kvp.Value);
                    }
                }
                else
                {
                    return(await ForumViewResult.RedirectFromService(this, serviceResponse, FailureCallback));
                }
            }

            if (input.SideLoad)
            {
                var errors = ModelState.Keys.Where(k => ModelState[k].Errors.Count > 0).Select(k => new { propertyName = k, errorMessage = ModelState[k].Errors[0].ErrorMessage });
                return(new JsonResult(errors));
            }
            else
            {
                return(await FailureCallback());
            }

            async Task <IActionResult> FailureCallback()
            {
                ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

                var viewModel = new ViewModels.Messages.ReplyForm {
                    Id        = input.Id.ToString(),
                    Body      = input.Body,
                    ElementId = $"message-reply-{input.Id}"
                };

                return(await ForumViewResult.ViewResult(this, viewModel));
            }
        }
Example #8
0
        public async Task <IActionResult> Create(int id = 0)
        {
            ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

            var board = (await BoardRepository.Records()).First(item => item.Id == id);

            if (Request.Query.TryGetValue("source", out var source))
            {
                return(await Create(new InputModels.MessageInput {
                    BoardId = id, Body = source
                }));
            }

            var viewModel = new ViewModels.Messages.CreateTopicForm {
                Id      = "0",
                BoardId = id.ToString()
            };

            return(await ForumViewResult.ViewResult(this, viewModel));
        }
Example #9
0
        public async Task <IActionResult> Create(int id = 0)
        {
            ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

            var boards = await BoardRepository.Records();

            var board = boards.First(item => item.Id == id);

            // Creating a topic via bookmarklet
            if (Request.Query.TryGetValue("source", out var source))
            {
                return(await Create(new ControllerModels.Topics.CreateTopicInput {
                    BoardId = id, Body = source
                }));
            }

            var viewModel = new ViewModels.Topics.CreateTopicForm {
                BoardId = id.ToString()
            };

            return(await ForumViewResult.ViewResult(this, viewModel));
        }
Example #10
0
        public async Task <IActionResult> Edit(int id)
        {
            ViewData["Smileys"] = await SmileyRepository.GetSelectorList();

            var message = await DbContext.Messages.FirstOrDefaultAsync(m => m.Id == id);

            if (message is null || message.Deleted)
            {
                throw new HttpNotFoundError();
            }

            if (message.PostedById != UserContext.ApplicationUser.Id && !UserContext.IsAdmin)
            {
                throw new HttpForbiddenError();
            }

            var viewModel = new ViewModels.Messages.EditMessageForm {
                Id        = id.ToString(),
                Body      = message.OriginalBody,
                ElementId = $"edit-message-{id}"
            };

            return(await ForumViewResult.ViewResult(this, viewModel));
        }
Example #11
0
        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();
            }
        }