Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public class ForumService {
private final InfoThreadMapper infoThreadMapper;
private final QuestionThreadRepository questionThreadRepository;

/**
* Add user to Forum
* @param forumId id of the forum
* @param userId id of the user
* @return forum the user was added to
*/
public Forum addUserToForum(UUID forumId, UUID userId) {
ForumEntity forum = forumRepository.findById(forumId).orElseThrow(() ->
new EntityNotFoundException("Forum with id: " + forumId + " not found!"));
Expand All @@ -56,6 +62,12 @@ public Forum addUserToForum(UUID forumId, UUID userId) {
return forumMapper.forumEntityToForum(forum);
}

/**
* Add user to Forum
* @param courseId id of the course of the forum
* @param userId id of the user
* @return forum the user was added to
*/
public Forum addUserToForumCourseId(UUID courseId, UUID userId) {
ForumEntity forum = forumRepository.findByCourseId(courseId).orElseThrow(() ->
new EntityNotFoundException("Forum for the course with the id: " + courseId + " not found!"));
Expand All @@ -69,6 +81,11 @@ public Forum getForumById(UUID id) {
new EntityNotFoundException("Forum with the id " + id + " not found")));
}

/**
* Returns the forum by the courseId and creates a forum if it does not exists.
* @param id of the course
* @return forum of the course
*/
public Forum getForumByCourseId(UUID id) {
ForumEntity forum = forumRepository.findByCourseId(id).orElseGet(() -> createForum(id));
return forumMapper.forumEntityToForum(forum);
Expand All @@ -79,11 +96,23 @@ public ThreadEntity getThreadById(UUID id) {
new EntityNotFoundException("Thread with the id " + id + " not found"));
}

/**
* Returns the thread by the given contentReferences
* @param threadContentReferenceEntities references between the content and the thread
* @return threads for the given contentReferences
*/
public List<Thread> getThreadsByThreadContentReferences(List<ThreadContentReferenceEntity> threadContentReferenceEntities) {
return threadContentReferenceEntities.stream().map(ThreadContentReferenceEntity::getThread)
.map(threadMapper::mapThread).toList();
}

/**
* Adds a post to the thread
* @param post Post that gets added
* @param thread Thread the post gets added to
* @param userId Id of the user that writes the post
* @return Post that was added to the thread
*/
public Post addPostToThread(InputPost post, ThreadEntity thread, UUID userId) {
String censored_content = profanityFilter.censor(post.getContent());
log.info("Censored post content: {}", censored_content);
Expand All @@ -109,6 +138,12 @@ public Post addPostToThread(InputPost post, ThreadEntity thread, UUID userId) {
return postMapper.mapToPostWithThread(postEntity, thread);
}

/**
* Upvotes a post and deletes the downvote if the user downvoted the post before
* @param postEntity post that the user upvotes
* @param userId id of the user
* @return upvoted post
*/
public Post upvotePost(PostEntity postEntity, UUID userId) {
postEntity.getDownvotedByUsers().removeIf(id -> id.equals(userId));
if (!postEntity.getUpvotedByUsers().contains(userId)) {
Expand All @@ -119,6 +154,12 @@ public Post upvotePost(PostEntity postEntity, UUID userId) {
return postMapper.mapToPost(postRepository.save(postEntity));
}

/**
* Downvotes a post and deletes the upvote if the user downvoted the post before
* @param postEntity post that the user downvotes
* @param userId id of the user
* @return downvoted post
*/
public Post downvotePost(PostEntity postEntity, UUID userId) {
postEntity.getUpvotedByUsers().removeIf(id -> id.equals(userId));
if (!postEntity.getDownvotedByUsers().contains(userId)) {
Expand All @@ -129,6 +170,13 @@ public Post downvotePost(PostEntity postEntity, UUID userId) {
return postMapper.mapToPost(postRepository.save(postEntity));
}

/**
* Creates a question Thread
* @param thread thread that gets created
* @param forum forum the thread gets created in
* @param userId user that creates the thread
* @return created thread
*/
public QuestionThread createQuestionThread(InputQuestionThread thread, ForumEntity forum ,UUID userId) {
String question_censored = profanityFilter.censor(thread.getQuestion().getContent());
String title_censored = profanityFilter.censor(thread.getTitle());
Expand All @@ -154,6 +202,13 @@ public QuestionThread createQuestionThread(InputQuestionThread thread, ForumEnti
return questionThreadMapper.mapQuestionThread(threadEntity);
}

/**
* Creates a info Thread
* @param thread thread that gets created
* @param forum forum the thread gets created in
* @param userId user that creates the thread
* @return created thread
*/
public InfoThread createInfoThread(InputInfoThread thread, ForumEntity forum, UUID userId) {
String info_censored = profanityFilter.censor(thread.getInfo().getContent());
String title_censored = profanityFilter.censor(thread.getTitle());
Expand Down Expand Up @@ -193,6 +248,14 @@ private void addThreatToContentOnThreadCreation(ThreadEntity thread, UUID conten
thread.setThreadContentReference(new ThreadContentReferenceEntity(thread, contentId, timeStampSeconds, pageNumber));
}

/**
* Updates the given post
* @param post updates to the post
* @param postEntity the post that gets updated
* @param userId user that updates the post
* @return updated post
* @throws AuthenticationException if another user tries to change the post
*/
public Post updatePost(InputPost post, PostEntity postEntity, UUID userId) throws AuthenticationException {
if (!postEntity.getAuthorId().equals(userId)) {
throw new AuthenticationException("User is not authorized to update this post");
Expand All @@ -203,6 +266,13 @@ public Post updatePost(InputPost post, PostEntity postEntity, UUID userId) throw
return postMapper.mapToPost(postRepository.save(postEntity));
}

/**
* Deletes the given post
* @param post post that gets deleted
* @param user user that deletes the ost
* @return deleted post
* @throws AuthenticationException if the user is different of the user that created the post and not superuser or course creator
*/
public Post deletePost(PostEntity post, LoggedInUser user) throws AuthenticationException {
if (!post.getAuthorId().equals(user.getId())
&& !(user.getRealmRoles().contains(LoggedInUser.RealmRole.COURSE_CREATOR)
Expand All @@ -219,6 +289,13 @@ public Post deletePost(PostEntity post, LoggedInUser user) throws Authentication
return realPost;
}

/**
* Deletes the given thread
* @param thread thread that gets deleted
* @param user user that deletes the thread
* @return deleted thread
* @throws AuthenticationException if the user is different of the user that created the thread and not superuser or course creator
*/
public Thread deleteThread(ThreadEntity thread, LoggedInUser user) throws AuthenticationException {
if (!thread.getCreatorId().equals(user.getId())
&& !(user.getRealmRoles().contains(LoggedInUser.RealmRole.COURSE_CREATOR)
Expand All @@ -233,6 +310,14 @@ public Thread deleteThread(ThreadEntity thread, LoggedInUser user) throws Authen
return realThread;
}

/**
* Adds thread to a contentId
* @param thread thread that gets added to the content
* @param contentId id of the content
* @param timeStamp timestamp of the video where the thread is created
* @param pageNumber pageNumber of the slides where the thread is created
* @return reference between the thread and the content
*/
public ThreadContentReference addThreadToContent(ThreadEntity thread, UUID contentId, Integer timeStamp, Integer pageNumber) {
ThreadContentReferenceEntity threadContentReferenceEntity = new ThreadContentReferenceEntity(thread, contentId, timeStamp, pageNumber);
threadContentReferenceRepository.save(threadContentReferenceEntity);
Expand All @@ -241,6 +326,11 @@ public ThreadContentReference addThreadToContent(ThreadEntity thread, UUID conte
return modelMapper.map(threadContentReferenceEntity, ThreadContentReference.class);
}

/**
* Selects the answer for a question thread
* @param postId post that is the answer
* @return QuestionThread where the post was selected
*/
public QuestionThread addAnswerToQuestionThread(UUID postId) {
PostEntity answer = postRepository.findById(postId).orElseThrow(
() -> new EntityNotFoundException("Post with the id " + postId + NOT_FOUND)
Expand Down Expand Up @@ -271,6 +361,11 @@ private ForumEntity createForum(UUID courseId) {
return forum;
}

/**
* Last 4 activities of the forum
* @param forum forum with the activities
* @return up to 4 activities
*/
public List<ForumActivityEntry> forumActivity(Forum forum) {
List<ForumActivityEntry> activities = new ArrayList<>();

Expand All @@ -293,6 +388,12 @@ public List<ForumActivityEntry> forumActivity(Forum forum) {
}


/**
* Returns the forum activities of the other user
* @param userId user that requests the activities
* @param otherUserId user the activities have been requested
* @return list of forum activities
*/
public List<ForumActivityEntry> otherUserForumActivityByUserId(UUID userId, UUID otherUserId) {
List<ForumEntity> forumEntitiesUser = forumRepository.findAllByUserIdsContaining(userId);
List<ForumEntity> forumEntitiesOtherUser = forumRepository.findAllByUserIdsContaining(otherUserId);
Expand All @@ -316,6 +417,11 @@ public List<ForumActivityEntry> otherUserForumActivityByUserId(UUID userId, UUID
return activities;
}

/**
* Returns the forum activities of a user
* @param userId user that did the forum activities
* @return list of forum activities
*/
public List<ForumActivityEntry> forumActivityByUserId(UUID userId) {
List<ForumEntity> forumEntities = forumRepository.findAllByUserIdsContaining(userId);

Expand All @@ -329,7 +435,7 @@ public List<ForumActivityEntry> forumActivityByUserId(UUID userId) {
return activities;
}

public List<ForumActivityEntry> extractForumActivitiesForUser(
private List<ForumActivityEntry> extractForumActivitiesForUser(
List<ForumEntity> forumEntities,
UUID userId,
ForumMapper forumMapper
Expand Down Expand Up @@ -366,7 +472,11 @@ public List<ForumActivityEntry> extractForumActivitiesForUser(
return activities;
}


/**
* Returns the open questions for a forum
* @param forum forum the questions are open in
* @return list of threads with open questions
*/
public List<Thread> openQuestions(Forum forum) {
double alpha = 0.6;
double beta = 0.4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Result updateResult(InputResult result) {
responses.add(response);
});

double correctness = (double)requiredScore.get() / (double)achievedScore.get();
double correctness = (double)achievedScore.get() / (double) requiredScore.get();

final ContentProgressedEvent userProgressLogEvent = ContentProgressedEvent.builder()
.userId(resultEntity.getUserId())
Expand Down Expand Up @@ -499,7 +499,7 @@ private void createUploadUrl(FileEntity fileEntity) {
.method(Method.PUT)
.bucket(BUCKET_ID)
.object(fileEntity.getId().toString())
.expiry(UPLOAD_URL_EXPIRY.toMinutesPart(), TimeUnit.MINUTES)
.expiry((int) UPLOAD_URL_EXPIRY.toMinutes(), TimeUnit.MINUTES)
.build());
fileEntity.setUploadUrl(uploadUrl);
fileRepository.save(fileEntity);
Expand All @@ -512,7 +512,7 @@ private void createDownloadUrl(FileEntity fileEntity) {
.method(Method.GET)
.bucket(BUCKET_ID)
.object(fileEntity.getId().toString())
.expiry(DOWNLOAD_URL_EXPIRY.toHoursPart(), TimeUnit.HOURS)
.expiry((int) DOWNLOAD_URL_EXPIRY.toHours(), TimeUnit.HOURS)
.build());
fileEntity.setDownloadUrl(downloadUrl);
fileRepository.save(fileEntity);
Expand Down
Loading