Search in sources :

Example 51 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class AnswerPostService method preparePostAndBroadcast.

/**
 * Helper method to prepare the post included in the websocket message and initiate the broadcasting
 *
 * @param updatedAnswerPost answer post that was updated
 * @param course            course the answer post belongs to
 */
private void preparePostAndBroadcast(AnswerPost updatedAnswerPost, Course course) {
    // we need to explicitly (and newly) add the updated answer post to the answers of the broadcast post to share up-to-date information
    Post updatedPost = updatedAnswerPost.getPost();
    // remove and add operations on sets identify an AnswerPost by its id; to update a certain property of an existing answer post,
    // we need to remove the existing AnswerPost (based on unchanged id in updatedAnswerPost) and add the updatedAnswerPost afterwards
    updatedPost.removeAnswerPost(updatedAnswerPost);
    updatedPost.addAnswerPost(updatedAnswerPost);
    broadcastForPost(new MetisPostDTO(updatedPost, MetisPostAction.UPDATE_POST), course);
}
Also used : AnswerPost(de.tum.in.www1.artemis.domain.metis.AnswerPost) Post(de.tum.in.www1.artemis.domain.metis.Post) MetisPostDTO(de.tum.in.www1.artemis.web.websocket.dto.MetisPostDTO)

Example 52 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class PostService method deletePostById.

/**
 * Checks course, user and post validity,
 * determines authority to delete post and deletes the post
 *
 * @param courseId id of the course the post belongs to
 * @param postId   id of the post to delete
 */
public void deletePostById(Long courseId, Long postId) {
    final User user = userRepository.getUserWithGroupsAndAuthorities();
    // checks
    final Course course = preCheckUserAndCourse(user, courseId);
    Post post = postRepository.findByIdElseThrow(postId);
    mayInteractWithPostElseThrow(post, user, course);
    preCheckPostValidity(post);
    mayUpdateOrDeletePostingElseThrow(post, user, course);
    // delete
    postRepository.deleteById(postId);
    broadcastForPost(new MetisPostDTO(post, MetisPostAction.DELETE_POST), course);
}
Also used : User(de.tum.in.www1.artemis.domain.User) Post(de.tum.in.www1.artemis.domain.metis.Post) MetisPostDTO(de.tum.in.www1.artemis.web.websocket.dto.MetisPostDTO) Course(de.tum.in.www1.artemis.domain.Course)

Example 53 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class PostService method getAllPostsByCourseWideContext.

/**
 * Checks course, user and post validity,
 * retrieves and filters posts with a certain course-wide context by course id
 * and ensures that sensitive information is filtered out
 *
 * @param postContextFilter filter object
 * @return page of posts for a certain course-wide context
 */
public List<Post> getAllPostsByCourseWideContext(PostContextFilter postContextFilter) {
    final User user = userRepository.getUserWithGroupsAndAuthorities();
    // checks
    final Course course = preCheckUserAndCourse(user, postContextFilter.getCourseId());
    authorizationCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.STUDENT, course, null);
    // retrieve posts
    List<Post> coursePosts;
    // retrieve posts
    coursePosts = postRepository.findPostsForCourse(postContextFilter.getCourseId(), postContextFilter.getCourseWideContext(), postContextFilter.getFilterToUnresolved(), postContextFilter.getFilterToOwn(), postContextFilter.getFilterToAnsweredOrReacted(), user.getId());
    // protect sample solution, grading instructions, etc.
    coursePosts.stream().map(Post::getExercise).filter(Objects::nonNull).forEach(Exercise::filterSensitiveInformation);
    return coursePosts;
}
Also used : User(de.tum.in.www1.artemis.domain.User) Exercise(de.tum.in.www1.artemis.domain.Exercise) Post(de.tum.in.www1.artemis.domain.metis.Post) Course(de.tum.in.www1.artemis.domain.Course)

Example 54 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class PostService method getAllExercisePosts.

/**
 * Checks course, user, exercise and post validity,
 * retrieves and filters posts for an exercise by its id
 * and ensures that sensitive information is filtered out
 *
 * @param postContextFilter filter object
 * @return page of posts that belong to the exercise
 */
public List<Post> getAllExercisePosts(PostContextFilter postContextFilter) {
    final User user = userRepository.getUserWithGroupsAndAuthorities();
    // checks
    preCheckUserAndCourse(user, postContextFilter.getCourseId());
    preCheckExercise(user, postContextFilter.getCourseId(), postContextFilter.getExerciseId());
    // retrieve posts
    List<Post> exercisePosts;
    exercisePosts = postRepository.findPostsByExerciseId(postContextFilter.getExerciseId(), postContextFilter.getFilterToUnresolved(), postContextFilter.getFilterToOwn(), postContextFilter.getFilterToAnsweredOrReacted(), user.getId());
    // protect sample solution, grading instructions, etc.
    exercisePosts.forEach(post -> post.getExercise().filterSensitiveInformation());
    return exercisePosts;
}
Also used : User(de.tum.in.www1.artemis.domain.User) Post(de.tum.in.www1.artemis.domain.metis.Post)

Example 55 with Post

use of de.tum.in.www1.artemis.domain.metis.Post in project ArTEMiS by ls1intum.

the class PostService method createPost.

/**
 * Checks course, user and post validity,
 * determines the post's author, persists the post,
 * and sends a notification to affected user groups
 *
 * @param courseId id of the course the post belongs to
 * @param post     post to create
 * @return created post that was persisted
 */
public Post createPost(Long courseId, Post post) {
    final User user = this.userRepository.getUserWithGroupsAndAuthorities();
    // checks
    if (post.getId() != null) {
        throw new BadRequestAlertException("A new post cannot already have an ID", METIS_POST_ENTITY_NAME, "idexists");
    }
    final Course course = preCheckUserAndCourse(user, courseId);
    mayInteractWithPostElseThrow(post, user, course);
    preCheckPostValidity(post);
    // set author to current user
    post.setAuthor(user);
    // set default value display priority -> NONE
    post.setDisplayPriority(DisplayPriority.NONE);
    if (post.getCourseWideContext() == CourseWideContext.ANNOUNCEMENT) {
        // display priority of announcement is set to pinned per default
        post.setDisplayPriority(DisplayPriority.PINNED);
        Post savedPost = postRepository.save(post);
        sendNotification(savedPost, course);
        broadcastForPost(new MetisPostDTO(savedPost, MetisPostAction.CREATE_POST), course);
        return savedPost;
    }
    Post savedPost = postRepository.save(post);
    // handle posts for plagiarism cases specifically
    if (savedPost.getPlagiarismCase() != null) {
        plagiarismCaseService.savePostForPlagiarismCaseAndNotifyStudent(savedPost.getPlagiarismCase().getId(), savedPost);
    } else {
        broadcastForPost(new MetisPostDTO(savedPost, MetisPostAction.CREATE_POST), course);
        sendNotification(savedPost, course);
    }
    return savedPost;
}
Also used : BadRequestAlertException(de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException) User(de.tum.in.www1.artemis.domain.User) Post(de.tum.in.www1.artemis.domain.metis.Post) MetisPostDTO(de.tum.in.www1.artemis.web.websocket.dto.MetisPostDTO) Course(de.tum.in.www1.artemis.domain.Course)

Aggregations

Post (de.tum.in.www1.artemis.domain.metis.Post)155 WithMockUser (org.springframework.security.test.context.support.WithMockUser)150 Test (org.junit.jupiter.api.Test)136 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)118 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)97 User (de.tum.in.www1.artemis.domain.User)88 AnswerPost (de.tum.in.www1.artemis.domain.metis.AnswerPost)87 URI (java.net.URI)63 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)56 Course (de.tum.in.www1.artemis.domain.Course)52 Transactional (org.springframework.transaction.annotation.Transactional)37 Reaction (de.tum.in.www1.artemis.domain.metis.Reaction)36 Test (org.junit.Test)36 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)36 Exercise (de.tum.in.www1.artemis.domain.Exercise)35 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 Exam (de.tum.in.www1.artemis.domain.exam.Exam)20 Lecture (de.tum.in.www1.artemis.domain.Lecture)16 ProgrammingExercise (de.tum.in.www1.artemis.domain.ProgrammingExercise)16 SortingOrder (de.tum.in.www1.artemis.domain.enumeration.SortingOrder)16