Ejemplo n.º 1
0
        public static ViewPostViewModel MapPostViewModel(PermissionSet permissions,
                                                         Post post, Member currentMember,
                                                         DialogueSettings settings, Topic topic,
                                                         List <Vote> allPostVotes, List <Favourite> favourites, bool showTopicLinks = false)
        {
            var postViewModel = new ViewPostViewModel
            {
                Permissions      = permissions,
                Post             = post,
                User             = currentMember,
                ParentTopic      = topic,
                Votes            = allPostVotes.Where(x => x.Post.Id == post.Id).ToList(),
                LoggedOnMemberId = currentMember != null ? currentMember.Id : 0,
                AllowedToVote    = (currentMember != null && currentMember.Id != post.MemberId &&
                                    currentMember.TotalPoints > settings.AmountOfPointsBeforeAUserCanVote),
                PostCount      = post.Member.PostCount,
                IsAdminOrMod   = HttpContext.Current.User.IsInRole(AppConstants.AdminRoleName) || permissions[AppConstants.PermissionModerate].IsTicked,
                HasFavourited  = favourites.Any(x => x.PostId == post.Id),
                IsTopicStarter = post.IsTopicStarter,
                ShowTopicLinks = showTopicLinks
            };

            postViewModel.UpVotes   = postViewModel.Votes.Count(x => x.Amount > 0);
            postViewModel.DownVotes = postViewModel.Votes.Count(x => x.Amount < 0);
            return(postViewModel);
        }
        public ActionResult ViewPost(string slug)
        {
            if (string.IsNullOrWhiteSpace(slug))
            {
                return(RedirectToAction(nameof(PostController.Index)));
            }

            var post = Context.Posts.FirstOrDefault(p => p.Slug == slug);

            if (post == null)
            {
                return(RedirectToAction(nameof(PostController.Index)));
            }

            var model = new ViewPostViewModel();

            model.Body        = post.Body;
            model.DateCreated = post.DateCreated;
            model.DateUpdated = post.DateUpdated;
            model.Title       = post.Title;
            model.MediaUrl    = post.MediaUrl;
            model.Comments    = post
                                .Comments
                                .Select(p => new CommentPostViewModel
            {
                Body = p.Body
            }).ToList();

            return(View(model));
        }
Ejemplo n.º 3
0
        public ViewResult ViewPost(int id)
        {
            Post post  = postRepository.Posts.Include("User").Include("Channel").FirstOrDefault(p => p.Id == id);
            var  model = new ViewPostViewModel {
                Post     = post,
                Buzzes   = buzzRepository.Buzzes.Where(b => b.Post.Id == post.Id).ToHashSet(),
                Comments = commentRepository.Comments.Where(c => c.Post.Id == post.Id).ToHashSet(),
                New      = new Comment(-1)
            };

            return(View(model));
        }
Ejemplo n.º 4
0
        public ActionResult AddComment(ViewPostViewModel model)
        {
            ModelState.Clear();

            model.New.Post = postRepository.Posts.Find(model.PostId);
            model.New.User = (User)HttpContext.Session["user"];

            TryValidateModel(model.New, "New");

            if (ModelState.IsValid)
            {
                Comment comment = commentRepository.CreateOrUpdate(model.New);
                TempData["message"] = "Comment added";
            }


            return(RedirectToAction("ViewPost", model.PostId));
        }
Ejemplo n.º 5
0
        public ActionResult ViewPost(string title, int id)
        {
            if (string.IsNullOrWhiteSpace(title))
            {
                return(RedirectToAction(nameof(PostController.ListPosts)));
            }


            var userId = User.Identity.GetUserId();

            var selectedPost = DbContext.Posts.FirstOrDefault(post =>
                                                              post.SlugTitle == title &&
                                                              post.UserId == userId);

            if (selectedPost == null)
            {
                return(RedirectToAction(nameof(PostController.ListPosts)));
            }

            var postToView = new ViewPostViewModel();

            postToView.Title       = selectedPost.Title;
            postToView.Id          = selectedPost.Id;
            postToView.Body        = selectedPost.Body;
            postToView.DateCreated = selectedPost.DateCreated;
            postToView.DateUpdated = DateTime.Today;
            Comments = DbContext.Comments.ToList();

            var postComments = Comments
                               .Where(comment => comment.PostId == id)
                               .Select(comment => new Comment
            {
                Id          = comment.Id,
                Body        = comment.Body,
                DateCreated = comment.DateCreated
            }).ToList();

            postToView.Comments = postComments;

            return(View(postToView));
        }
Ejemplo n.º 6
0
        public IActionResult Post(string postUrl)
        {
            if (postUrl == null)
            {
                return(NotFound());
            }
            var p = _postService.GetPostByUrlOnly(postUrl);
            List <LatestPostViewModel> posts = _postService.GetAllPosts()
                                               .Select(l => new LatestPostViewModel
            {
                Id          = l.ID,
                Title       = l.Title,
                IconUri     = l.IconUri,
                Description = l.Description,
                DateCreated = l.DateCreated,
                Fullname    = l.ApplicationUser.FullName,
                PostUrl     = l.PostUrl
            }).ToList();

            if (p != null)
            {
                ViewPostViewModel post = new ViewPostViewModel()
                {
                    Id          = p.ID,
                    Content     = p.Content,
                    DateCreated = p.DateCreated,
                    Description = p.Description,
                    IconUri     = p.IconUri,
                    Tags        = p.Tags,
                    Title       = p.Title,
                    UserName    = p.ApplicationUser.FullName,
                    Posts       = posts,
                    PostUrl     = p.PostUrl
                };

                return(View(post));
            }
            return(NotFound());
        }
Ejemplo n.º 7
0
        public ActionResult ViewPost(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            BlogPost blogPost = _db.BlogPosts.FirstOrDefault(p => p.LinkText == id);

            if (blogPost == null)
            {
                return(HttpNotFound());
            }

            // Get the previous post- the latest one before the current one
            var prevPost = _db.BlogPosts
                           .Where(p => p.PostDate < blogPost.PostDate || (p.PostDate == blogPost.PostDate && p.PostTime < blogPost.PostTime))
                           .OrderByDescending(p => p.PostDate)
                           .ThenByDescending(p => p.PostTime)
                           .FirstOrDefault();

            // Get the next post- the earliest one after the current one
            var nextPost = _db.BlogPosts
                           .Where(p => p.PostDate > blogPost.PostDate || (p.PostDate == blogPost.PostDate && p.PostTime > blogPost.PostTime))
                           .OrderBy(p => p.PostDate)
                           .ThenBy(p => p.PostTime)
                           .FirstOrDefault();

            var viewModel = new ViewPostViewModel
            {
                Post         = blogPost,
                PreviousPost = prevPost,
                NextPost     = nextPost,
                PostRequiresSyntaxHighlight = PostContainsSyntaxHighlight(blogPost)
            };

            return(View("ViewPost", viewModel));
        }
Ejemplo n.º 8
0
        public ActionResult CreatePost(CreateAjaxPostViewModel post)
        {
            PermissionSet permissions;
            Post          newPost;
            Topic         topic;

            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                // Quick check to see if user is locked out, when logged in
                if (LoggedOnUser.IsLockedOut | !LoggedOnUser.IsApproved)
                {
                    FormsAuthentication.SignOut();
                    throw new Exception(LocalizationService.GetResourceString("Errors.NoAccess"));
                }

                topic = _topicService.Get(post.Topic);

                var postContent = _bannedWordService.SanitiseBannedWords(post.PostContent);

                var akismetHelper = new AkismetHelper(SettingsService);

                newPost = _postService.AddNewPost(postContent, topic, LoggedOnUser, out permissions);

                if (!akismetHelper.IsSpam(newPost))
                {
                    try
                    {
                        unitOfWork.Commit();

                        // Successful, add this post to the Lucene index
                        if (_luceneService.CheckIndexExists())
                        {
                            _luceneService.AddUpdate(_luceneService.MapToModel(newPost));
                        }
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                        throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage"));
                    }
                }
                else
                {
                    unitOfWork.Rollback();
                    throw new Exception(LocalizationService.GetResourceString("Errors.PossibleSpam"));
                }
            }

            //Check for moderation
            if (newPost.Pending == true)
            {
                return(PartialView("_PostModeration"));
            }
            else
            {
                // All good send the notifications and send the post back
                using (UnitOfWorkManager.NewUnitOfWork())
                {
                    // Create the view model
                    var viewModel = new ViewPostViewModel
                    {
                        Permissions = permissions,
                        Post        = newPost,
                        User        = LoggedOnUser,
                        ParentTopic = topic
                    };

                    // Success send any notifications
                    NotifyNewTopics(topic);

                    return(PartialView("_Post", viewModel));
                }
            }
        }