public ActionResult SlideComments(string courseId, Guid slideId, bool showOnlyInstructorsOnlyComments = false) { var slide = courseManager.GetCourse(courseId).GetSlideById(slideId); var comments = commentsRepo.GetSlideComments(courseId, slideId).ToList(); var commentsPolicy = commentsRepo.GetCommentsPolicy(courseId); var commentsByParent = comments.GroupBy(x => x.ParentCommentId) .ToDictionary(x => x.Key, x => x.OrderBy(c => c.PublishTime).ToList()); /* Top-level comments (with ParentCommentId = -1) * are sorting by publish time, but pinned comments are always higher */ List <Comment> topLevelComments; if (commentsByParent.ContainsKey(-1)) { topLevelComments = commentsByParent.Get(-1) .OrderBy(x => !x.IsPinnedToTop).ThenBy(x => x.PublishTime).ToList(); } else { topLevelComments = new List <Comment>(); } var userId = User.Identity.GetUserId(); var commentsLikesCounts = commentsRepo.GetCommentsLikesCounts(comments); var commentsLikedByUser = commentsRepo.GetSlideCommentsLikedByUser(courseId, slideId, userId).ToImmutableHashSet(); var isAuthorizedAndCanComment = CanAddCommentHere(User, courseId, false); var canReply = CanAddCommentHere(User, courseId, true); var canModerateComments = CanModerateComments(User, courseId); var canSeeNotApprovedComments = canModerateComments; var canViewAuthorSubmissions = coursesRepo.HasCourseAccess(userId, courseId, CourseAccessType.ViewAllStudentsSubmissions) || User.HasAccessFor(courseId, CourseRole.CourseAdmin); var canViewProfiles = systemAccessesRepo.HasSystemAccess(userId, SystemAccessType.ViewAllProfiles) || User.IsSystemAdministrator(); var model = new SlideCommentsModel { CourseId = courseId, Slide = slide, IsAuthorizedAndCanComment = isAuthorizedAndCanComment, CanReply = canReply, CanModerateComments = canModerateComments, CanSeeNotApprovedComments = canSeeNotApprovedComments, CanViewAuthorSubmissions = canViewAuthorSubmissions, TopLevelComments = topLevelComments, CommentsByParent = commentsByParent, CommentsLikesCounts = commentsLikesCounts, CommentsLikedByUser = commentsLikedByUser, CurrentUser = User.Identity.IsAuthenticated ? userManager.FindById(userId) : null, CommentsPolicy = commentsPolicy, CanViewAuthorProfiles = canViewProfiles, CanViewAndAddCommentsForInstructorsOnly = CanViewAndAddCommentsForInstructorsOnly(User, courseId), ShowOnlyInstructorsOnlyComments = showOnlyInstructorsOnlyComments }; return(PartialView(model)); }
public ActionResult SlideComments(string courseId, Guid slideId) { var slide = courseManager.GetCourse(courseId).GetSlideById(slideId); var comments = commentsRepo.GetSlideComments(courseId, slideId).ToList(); var commentsPolicy = commentsRepo.GetCommentsPolicy(courseId); var commentsByParent = comments.GroupBy(x => x.ParentCommentId) .ToDictionary(x => x.Key, x => x.OrderBy(c => c.PublishTime).ToList()); /* Top-level comments (with ParentCommentId = -1) * are sorting by publish time, but pinned comments are always higher */ List <Comment> topLevelComments; if (commentsByParent.ContainsKey(-1)) { topLevelComments = commentsByParent.Get(-1) .OrderBy(x => !x.IsPinnedToTop).ThenBy(x => x.PublishTime).ToList(); } else { topLevelComments = new List <Comment>(); } var commentsLikesCounts = commentsRepo.GetCommentsLikesCounts(comments); var commentsLikedByUser = commentsRepo.GetSlideCommentsLikedByUser(courseId, slideId, User.Identity.GetUserId()).ToImmutableHashSet(); var isInstructor = User.HasAccessFor(courseId, CourseRole.Instructor); var isAuthorizedAndCanComment = CanAddCommentHere(User, courseId, false); var canReply = CanAddCommentHere(User, courseId, true); var canModerateComments = User.Identity.IsAuthenticated && isInstructor; var canSeeNotApprovedComments = User.Identity.IsAuthenticated && isInstructor; var model = new SlideCommentsModel { CourseId = courseId, Slide = slide, IsAuthorizedAndCanComment = isAuthorizedAndCanComment, CanReply = canReply, CanModerateComments = canModerateComments, CanSeeNotApprovedComments = canSeeNotApprovedComments, TopLevelComments = topLevelComments, CommentsByParent = commentsByParent, CommentsLikesCounts = commentsLikesCounts, CommentsLikedByUser = commentsLikedByUser, CurrentUser = User.Identity.IsAuthenticated ? userManager.FindById(User.Identity.GetUserId()) : null, CommentsPolicy = commentsPolicy, }; return(PartialView(model)); }