Exemplo n.º 1
0
        public async Task <ActionResult <CommentsListResponse> > SlideComments([FromQuery] SlideCommentsParameters parameters)
        {
            var courseId = parameters.CourseId;
            var slideId  = parameters.SlideId;

            var isInstructor = await courseRolesRepo.HasUserAccessToCourseAsync(UserId, courseId, CourseRoleType.Instructor).ConfigureAwait(false);

            var slide = await GetSlide(courseId, slideId, isInstructor);

            if (slide == null)
            {
                return(StatusCode((int)HttpStatusCode.NotFound, $"No slide with id {slideId}"));
            }

            if (parameters.ForInstructors)
            {
                if (!IsAuthenticated)
                {
                    return(StatusCode((int)HttpStatusCode.Unauthorized, $"You should be authenticated to view instructor comments."));
                }
                if (!isInstructor)
                {
                    return(StatusCode((int)HttpStatusCode.Forbidden, $"You have no access to instructor comments on {courseId}. You should be instructor or course admin."));
                }
            }

            var comments = await commentsRepo.GetSlideTopLevelCommentsAsync(courseId, slideId).ConfigureAwait(false);

            comments = comments.Where(c => c.IsForInstructorsOnly == parameters.ForInstructors).ToList();
            return(await GetSlideCommentsResponseAsync(comments, courseId, parameters).ConfigureAwait(false));
        }
Exemplo n.º 2
0
        private async Task <ActionResult <CommentsListResponse> > GetSlideCommentsResponseAsync(List <Comment> comments, string courseId, SlideCommentsParameters parameters)
        {
            var canUserSeeNotApprovedComments = await CanUserSeeNotApprovedCommentsAsync(UserId, courseId).ConfigureAwait(false);

            comments = FilterVisibleComments(comments, canUserSeeNotApprovedComments);

            var totalCount = comments.Count;

            comments = comments.Skip(parameters.Offset).Take(parameters.Count).ToList();

            var replies = await commentsRepo.GetRepliesAsync(comments.Select(c => c.Id)).ConfigureAwait(false);

            var allComments = comments.Concat(replies.SelectMany(g => g.Value)).ToList();

            var commentLikesCount = await commentLikesRepo.GetLikesCountsAsync(allComments.Select(c => c.Id)).ConfigureAwait(false);

            var likedByUserCommentsIds = (await commentLikesRepo.GetCommentsLikedByUserAsync(courseId, parameters.SlideId, UserId).ConfigureAwait(false)).ToHashSet();
            var isInstructor           = await courseRolesRepo.HasUserAccessToCourseAsync(User.GetUserId(), courseId, CourseRoleType.Instructor).ConfigureAwait(false);

            var canViewAllGroupMembers = isInstructor && await groupAccessesRepo.CanUserSeeAllCourseGroupsAsync(User.GetUserId(), courseId).ConfigureAwait(false);

            var userAvailableGroupsIds = !isInstructor ? null : (await groupAccessesRepo.GetAvailableForUserGroupsAsync(User.GetUserId(), false, true, true).ConfigureAwait(false)).Select(g => g.Id).ToHashSet();
            var authorsIds             = allComments.Select(c => c.Author.Id).Distinct().ToList();
            var authors2Groups         = !isInstructor ? null : await groupMembersRepo.GetUsersGroupsAsync(courseId, authorsIds, true).ConfigureAwait(false);

            return(new CommentsListResponse
            {
                TopLevelComments = BuildCommentsListResponse(comments, canUserSeeNotApprovedComments, replies, commentLikesCount, likedByUserCommentsIds,
                                                             authors2Groups, userAvailableGroupsIds, canViewAllGroupMembers,
                                                             addCourseIdAndSlideId: false, addParentCommentId: true, addReplies: true),
                Pagination = new PaginationResponse
                {
                    Offset = parameters.Offset,
                    Count = comments.Count,
                    TotalCount = totalCount,
                }
            });
        }