From 101d4eab4fcb9a7b68680ee9f1b7ed73e7267092 Mon Sep 17 00:00:00 2001 From: Paul Rangger Date: Mon, 18 Nov 2024 14:31:33 +0100 Subject: [PATCH] Bugfix for notifications --- .../ConversationNotificationService.java | 15 ++++++--- .../SingleUserNotificationService.java | 33 ++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/tum/cit/aet/artemis/communication/service/notifications/ConversationNotificationService.java b/src/main/java/de/tum/cit/aet/artemis/communication/service/notifications/ConversationNotificationService.java index d009074927b2..cf5cc2c67cc8 100644 --- a/src/main/java/de/tum/cit/aet/artemis/communication/service/notifications/ConversationNotificationService.java +++ b/src/main/java/de/tum/cit/aet/artemis/communication/service/notifications/ConversationNotificationService.java @@ -41,11 +41,15 @@ public class ConversationNotificationService { private final SingleUserNotificationRepository singleUserNotificationRepository; + private final SingleUserNotificationService singleUserNotificationService; + public ConversationNotificationService(ConversationNotificationRepository conversationNotificationRepository, - GeneralInstantNotificationService generalInstantNotificationService, SingleUserNotificationRepository singleUserNotificationRepository) { + GeneralInstantNotificationService generalInstantNotificationService, SingleUserNotificationRepository singleUserNotificationRepository, + SingleUserNotificationService singleUserNotificationService) { this.conversationNotificationRepository = conversationNotificationRepository; this.generalInstantNotificationService = generalInstantNotificationService; this.singleUserNotificationRepository = singleUserNotificationRepository; + this.singleUserNotificationService = singleUserNotificationService; } /** @@ -83,7 +87,7 @@ public ConversationNotification createNotification(Post createdMessage, Conversa String[] placeholders = createPlaceholdersNewMessageChannelText(course.getTitle(), createdMessage.getContent(), createdMessage.getCreationDate().toString(), conversationName, createdMessage.getAuthor().getName(), conversationType); ConversationNotification notification = createConversationMessageNotification(course.getId(), createdMessage, notificationType, notificationText, true, placeholders); - save(notification, mentionedUsers, placeholders); + save(notification, mentionedUsers, placeholders, createdMessage); return notification; } @@ -93,11 +97,12 @@ public static String[] createPlaceholdersNewMessageChannelText(String courseTitl return new String[] { courseTitle, messageContent, messageCreationDate, conversationName, authorName, conversationType }; } - private void save(ConversationNotification notification, Set mentionedUsers, String[] placeHolders) { + private void save(ConversationNotification notification, Set mentionedUsers, String[] placeHolders, Post createdMessage) { conversationNotificationRepository.save(notification); - Set mentionedUserNotifications = mentionedUsers.stream().map(mentionedUser -> SingleUserNotificationFactory - .createNotification(notification.getMessage(), NotificationType.CONVERSATION_USER_MENTIONED, notification.getText(), placeHolders, mentionedUser)) + Set mentionedUserNotifications = singleUserNotificationService + .filterAllowedRecipientsInMentionedUsers(mentionedUsers, createdMessage.getConversation()).map(mentionedUser -> SingleUserNotificationFactory + .createNotification(notification.getMessage(), NotificationType.CONVERSATION_USER_MENTIONED, notification.getText(), placeHolders, mentionedUser)) .collect(Collectors.toSet()); singleUserNotificationRepository.saveAll(mentionedUserNotifications); } diff --git a/src/main/java/de/tum/cit/aet/artemis/communication/service/notifications/SingleUserNotificationService.java b/src/main/java/de/tum/cit/aet/artemis/communication/service/notifications/SingleUserNotificationService.java index 4e242f93e0fb..33def5698b9f 100644 --- a/src/main/java/de/tum/cit/aet/artemis/communication/service/notifications/SingleUserNotificationService.java +++ b/src/main/java/de/tum/cit/aet/artemis/communication/service/notifications/SingleUserNotificationService.java @@ -443,23 +443,34 @@ public void notifyInvolvedUsersAboutNewMessageReply(Post post, SingleUserNotific usersInvolved.add(post.getAuthor()); } - mentionedUsers.stream().filter(user -> { - boolean isChannelAndCourseWide = post.getConversation() instanceof Channel channel && channel.getIsCourseWide(); - boolean isChannelVisibleToStudents = !(post.getConversation() instanceof Channel channel) || conversationService.isChannelVisibleToStudents(channel); - boolean isChannelVisibleToMentionedUser = isChannelVisibleToStudents - || authorizationCheckService.isAtLeastTeachingAssistantInCourse(post.getConversation().getCourse(), user); - - // Only send a notification to the mentioned user if... - // (for course-wide channels) ...the course-wide channel is visible - // (for all other cases) ...the user is a member of the conversation - return (isChannelAndCourseWide && isChannelVisibleToMentionedUser) || conversationService.isMember(post.getConversation().getId(), user.getId()); - }).forEach(mentionedUser -> notifyUserAboutNewMessageReply(savedAnswerMessage, notification, mentionedUser, author, CONVERSATION_USER_MENTIONED)); + filterAllowedRecipientsInMentionedUsers(mentionedUsers, post.getConversation()) + .forEach(mentionedUser -> notifyUserAboutNewMessageReply(savedAnswerMessage, notification, mentionedUser, author, CONVERSATION_USER_MENTIONED)); Conversation conv = conversationService.getConversationById(post.getConversation().getId()); usersInvolved.stream().filter(userInvolved -> !mentionedUsers.contains(userInvolved)) .forEach(userInvolved -> notifyUserAboutNewMessageReply(savedAnswerMessage, notification, userInvolved, author, getAnswerMessageNotificationType(conv))); } + /** + * Filters which of the mentioned users are permitted to receive a notification + * + * @param mentionedUsers users mentioned in the answer message + * @param conversation the conversation of the created post/notification, used for filtering + * @return the stream of mentioned users which are permitted to receive the notification for the given conversation + */ + public Stream filterAllowedRecipientsInMentionedUsers(Set mentionedUsers, Conversation conversation) { + return mentionedUsers.stream().filter(user -> { + boolean isChannelAndCourseWide = conversation instanceof Channel channel && channel.getIsCourseWide(); + boolean isChannelVisibleToStudents = !(conversation instanceof Channel channel) || conversationService.isChannelVisibleToStudents(channel); + boolean isChannelVisibleToMentionedUser = isChannelVisibleToStudents || authorizationCheckService.isAtLeastTeachingAssistantInCourse(conversation.getCourse(), user); + + // Only send a notification to the mentioned user if... + // (for course-wide channels) ...the course-wide channel is visible + // (for all other cases) ...the user is a member of the conversation + return (isChannelAndCourseWide && isChannelVisibleToMentionedUser) || conversationService.isMember(conversation.getId(), user.getId()); + }); + } + /** * Saves the given notification in database and sends it to the client via websocket. * Also creates and sends an instant notification.