Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Frontend: Added Notifications + Adjusted Notification Endpoints #894

Merged
merged 21 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.idea/workspace.xml
.idea/shelf
.idea/intellij-javadocs-4.0.1.xml
.idea/material_theme_project_new.xml
.idea/modules.xml
.idea/dataSources
.idea/dataSources.local.xml
Expand Down
63 changes: 34 additions & 29 deletions src/main/java/de/tinf22b6/dhbwhub/mapper/NotificationMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,58 @@
import de.tinf22b6.dhbwhub.proposal.simplified_models.HomepageNotificationProposal;

public class NotificationMapper {
public static String POST_URL = "/post/?id=";
public static String EVENT_URL = "/event/?id=";
public static String USER_URL = "/user/?id=";
public static String FRIENDS_URL = "/friends";

public static HomepageNotificationProposal mapToSingleNotification(PostLikeNotification notification) {
return new HomepageNotificationProposal(
notification.getId(),
notification.getAccumulatedId(),
String.format("Your Post '%s' has been liked by the user %s", notification.getPost().getTitle(), notification.getTriggeringUser().getAccount().getUsername()),
"post/post-thread/" + notification.getPostId(),
NotificationType.TYPE_POST_LIKE.name()
String.format("Your post '%s' has been liked by the user '%s'.", notification.getPost().getTitle(), notification.getTriggeringUser().getAccount().getUsername()),
POST_URL + notification.getPostId(),
NotificationType.TYPE_POST_LIKE.getType()
);
}

public static HomepageNotificationProposal mapToSingleNotification(PostCommentNotification notification) {
return new HomepageNotificationProposal(
notification.getId(),
notification.getAccumulatedId(),
String.format("Your Post '%s' has been commented by the user %s", notification.getPost().getTitle(), notification.getTriggeringUser().getAccount().getUsername()),
"post/post-thread/" + notification.getPostId(),
NotificationType.TYPE_POST_COMMENT.name()
String.format("Your post '%s' has been commented by the user '%s'.", notification.getPost().getTitle(), notification.getTriggeringUser().getAccount().getUsername()),
POST_URL + notification.getPostId(),
NotificationType.TYPE_POST_COMMENT.getType()
);
}

public static HomepageNotificationProposal mapToSingleNotification(FollowNotification notification) {
return new HomepageNotificationProposal(
notification.getId(),
notification.getAccumulatedId(),
String.format("The user '%s' follows you now", notification.getTriggeringUser().getAccount().getUsername()),
"user/user-info/" + notification.getTriggeringUser().getId(),
NotificationType.TYPE_FOLLOW.name()
String.format("The user '%s' follows you now.", notification.getTriggeringUser().getAccount().getUsername()),
USER_URL + notification.getTriggeringUser().getId(),
NotificationType.TYPE_FOLLOW.getType()
);
}

public static HomepageNotificationProposal mapToSingleNotification(CommentLikeNotification notification) {
return new HomepageNotificationProposal(
notification.getId(),
notification.getAccumulatedId(),
String.format("Your comment on the post '%s' has been liked by the user %s", notification.getPost().getTitle(), notification.getTriggeringUser().getAccount().getUsername()),
"post/post-thread/" + notification.getPostId(),
NotificationType.TYPE_COMMENT_LIKE.name()
String.format("Your comment on the post '%s' has been liked by the user '%s'.", notification.getPost().getTitle(), notification.getTriggeringUser().getAccount().getUsername()),
POST_URL + notification.getPostId(),
NotificationType.TYPE_COMMENT_LIKE.getType()
);
}

public static HomepageNotificationProposal mapToSingleNotification(EventCommentLikeNotification notification) {
return new HomepageNotificationProposal(
notification.getId(),
notification.getAccumulatedId(),
String.format("Your comment on the Event-Post '%s' has been liked by the user %s", notification.getEventPost().getTitle(), notification.getTriggeringUser().getAccount().getUsername()),
"event/event-thread/" + notification.getEventPostId(),
NotificationType.TYPE_EVENT_COMMENT_LIKE.name()
String.format("Your comment on the event '%s' has been liked by the user '%s'.", notification.getEventPost().getTitle(), notification.getTriggeringUser().getAccount().getUsername()),
EVENT_URL + notification.getEventPostId(),
NotificationType.TYPE_EVENT_COMMENT_LIKE.getType()
);
}

Expand All @@ -68,29 +73,29 @@ public static HomepageNotificationProposal mapToGroupNotification(PostLikeNotifi
return new HomepageNotificationProposal(
notification.getId(),
notification.getAccumulatedId(),
String.format("Your Post '%s' has been liked by %d users", notification.getPost().getTitle(), size),
"post/post-thread/" + notification.getPostId(),
NotificationType.TYPE_POST_LIKE.name()
String.format("Your post '%s' has been liked by %d users.", notification.getPost().getTitle(), size),
POST_URL + notification.getPostId(),
NotificationType.TYPE_POST_LIKE.getType()
);
}

public static HomepageNotificationProposal mapToGroupNotification(PostCommentNotification notification, int size) {
return new HomepageNotificationProposal(
notification.getId(),
notification.getAccumulatedId(),
String.format("Your Post '%s' has been commented by %d users", notification.getPost().getTitle(), size),
"post/post-thread/" + notification.getPostId(),
NotificationType.TYPE_POST_COMMENT.name()
String.format("Your post '%s' has been commented by %d users.", notification.getPost().getTitle(), size),
POST_URL + notification.getPostId(),
NotificationType.TYPE_POST_COMMENT.getType()
);
}

public static HomepageNotificationProposal mapToGroupNotification(FollowNotification notification, int size) {
return new HomepageNotificationProposal(
notification.getId(),
notification.getAccumulatedId(),
String.format("You have %d new follower now", size),
"friends",
NotificationType.TYPE_FOLLOW.name()
String.format("You have %d new follower now.", size),
FRIENDS_URL,
NotificationType.TYPE_FOLLOW.getType()
);
}

Expand All @@ -99,18 +104,18 @@ public static HomepageNotificationProposal mapToGroupNotification(CommentLikeNot
notification.getId(),
notification.getAccumulatedId(),
String.format("Your comment on the post '%s' has been liked by %d users", notification.getPost().getTitle(), size),
"post/post-thread/" + notification.getPostId(),
NotificationType.TYPE_COMMENT_LIKE.name()
POST_URL + notification.getPostId(),
NotificationType.TYPE_COMMENT_LIKE.getType()
);
}

public static HomepageNotificationProposal mapToGroupNotification(EventCommentLikeNotification notification, int size) {
return new HomepageNotificationProposal(
notification.getId(),
notification.getAccumulatedId(),
String.format("Your comment on the Event-Post '%s' has been liked by %d users", notification.getEventPost().getTitle(), size),
"event/event-thread/" + notification.getEventPostId(),
NotificationType.TYPE_EVENT_COMMENT_LIKE.name()
String.format("Your comment on the event '%s' has been liked by %d users.", notification.getEventPost().getTitle(), size),
EVENT_URL + notification.getEventPostId(),
NotificationType.TYPE_EVENT_COMMENT_LIKE.getType()
);
}

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/de/tinf22b6/dhbwhub/model/NotificationType.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package de.tinf22b6.dhbwhub.model;

public enum NotificationType {
TYPE_POST_COMMENT("Type-Post-Comment"),
TYPE_POST_LIKE("Type-Post-Like"),
TYPE_COMMENT_LIKE("Type-Comment-Like"),
TYPE_FOLLOW("Type-Follow"),
TYPE_EVENT_COMMENT_LIKE("Type-Event-Comment-Like");
TYPE_POST_COMMENT("TYPE-POST-COMMENT"),
TYPE_POST_LIKE("TYPE-POST-LIKE"),
TYPE_COMMENT_LIKE("TYPE-COMMENT-LIKE"),
TYPE_FOLLOW("TYPE-FOLLOW"),
TYPE_EVENT_COMMENT_LIKE("TYPE-EVENT-COMMENT-LIKE");

private String type;
NotificationType(String type) {
this.type = type;
}

public String getType() {
return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,24 @@ public void deletePostCommentNotification(PostCommentNotification notification)
public void deletePostLikeNotification(PostLikeNotification notification) {
postLikeNotificationRepository.delete(notification);
}

public boolean checkIfCommentLikeEntryExists(Long triggeringUserId, Long postId) {
return commentLikeNotificationRepository.findByTriggeringUserIdAndPostId(triggeringUserId, postId).orElse(null) != null;
}

public boolean checkIfEventCommentLikeEntryExists(Long triggeringUserId, Long eventPostId) {
return eventCommentLikeNotificationRepository.findByTriggeringUserIdAndEventPostId(triggeringUserId, eventPostId).orElse(null) != null;
}

public boolean checkIfFollowEntryExists(Long triggeringUserId, Long userId) {
return followNotificationRepository.findByTriggeringUserIdAndUserId(triggeringUserId, userId).orElse(null) != null;
}

public boolean checkIfPostCommentEntryExists(Long triggeringUserId, Long postId) {
return postCommentNotificationRepository.findByTriggeringUserIdAndPostId(triggeringUserId, postId).orElse(null) != null;
}

public boolean checkIfPostLikeEntryExists(Long triggeringUserId, Long postId) {
return postLikeNotificationRepository.findByTriggeringUserIdAndPostId(triggeringUserId, postId).orElse(null) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface SpringCommentLikeNotificationRepository extends JpaRepository<CommentLikeNotification, Long> {
List<CommentLikeNotification> findByAccumulatedId(Long accumulatedId);

@Query(value = "SELECT * FROM comment_like_notification WHERE seen = false AND user_id = ?1", nativeQuery = true)
List<CommentLikeNotification> findUnseenNotifications(Long userId);

@Query(value = "SELECT * FROM comment_like_notification WHERE triggering_user_id = ?1 AND post_id = ?2", nativeQuery = true)
Optional<CommentLikeNotification> findByTriggeringUserIdAndPostId(Long triggeringUserId, Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface SpringEventCommentLikeNotificationRepository extends JpaRepository<EventCommentLikeNotification, Long> {
List<EventCommentLikeNotification> findByAccumulatedId(Long accumulatedId);

@Query(value = "SELECT * FROM event_comment_like_notification WHERE seen = false AND user_id = ?1", nativeQuery = true)
List<EventCommentLikeNotification> findUnseenNotifications(Long userId);

@Query(value = "SELECT * FROM event_comment_like_notification WHERE triggering_user_id = ?1 AND event_post_id = ?2", nativeQuery = true)
Optional<EventCommentLikeNotification> findByTriggeringUserIdAndEventPostId(Long triggeringUserId, Long eventPostId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface SpringFollowNotificationRepository extends JpaRepository<FollowNotification, Long> {
List<FollowNotification> findByAccumulatedId(Long accumulatedId);

@Query(value = "SELECT * FROM follow_notification WHERE seen = false AND user_id = ?1", nativeQuery = true)
List<FollowNotification> findUnseenNotifications(Long userId);

@Query(value = "SELECT * FROM follow_notification WHERE triggering_user_id = ?1 AND user_id = ?2", nativeQuery = true)
Optional<FollowNotification> findByTriggeringUserIdAndUserId(Long triggeringUserId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface SpringPostCommentNotificationRepository extends JpaRepository<PostCommentNotification, Long> {
List<PostCommentNotification> findByAccumulatedId(Long accumulatedId);

@Query(value = "SELECT * FROM post_comment_notification WHERE seen = false AND user_id = ?1", nativeQuery = true)
List<PostCommentNotification> findUnseenNotifications(Long userId);

@Query(value = "SELECT * FROM post_comment_notification WHERE triggering_user_id = ?1 AND post_id = ?2", nativeQuery = true)
Optional<PostCommentNotification> findByTriggeringUserIdAndPostId(Long triggeringUserId, Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface SpringPostLikeNotificationRepository extends JpaRepository<PostLikeNotification, Long> {
List<PostLikeNotification> findByAccumulatedId(Long accumulatedId);

@Query(value = "SELECT * FROM post_like_notification WHERE seen = false AND user_id = ?1", nativeQuery = true)
List<PostLikeNotification> findUnseenNotifications(Long userId);

@Query(value = "SELECT * FROM post_like_notification WHERE triggering_user_id = ?1 AND post_id = ?2", nativeQuery = true)
Optional<PostLikeNotification> findByTriggeringUserIdAndPostId(Long triggeringUserId, Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public CommentThreadViewProposal create(CreateCommentProposal proposal) {


// Notify Post-Author
if(!Objects.equals(post.getUser().getId(), user.getId())){
if(!Objects.equals(post.getUser().getId(), user.getId()) &&
!notificationRepository.checkIfPostCommentEntryExists(user.getId(), post.getId())){
PostCommentNotification notification = NotificationMapper.mapToPostCommentNotification(comment, user);
notification.setAccumulatedId(null);
notificationRepository.savePostCommentNotification(notification);
Expand Down Expand Up @@ -130,7 +131,8 @@ public int increaseLikes(LikeCommentProposal likeCommentProposal) {
logtableRepository.saveComment(likeLogtableComment);

// Notify User
if(!Objects.equals(comment.getUser().getId(), user.getId())){
if(!Objects.equals(comment.getUser().getId(), user.getId()) &&
!notificationRepository.checkIfCommentLikeEntryExists(user.getId(), comment.getPost().getId())){
CommentLikeNotification notification = NotificationMapper.mapToCommentLikeNotification(comment, user);
notification.setAccumulatedId(null);
notificationRepository.saveCommentLikeNotification(notification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ public int adjustCommentLikes(LikeEventCommentProposal likeEventCommentProposal,
}
logtableRepository.saveEventComment(likeLogtableEventComment);

if (!Objects.equals(eventComment.getUser().getId(), user.getId())) {
if (!Objects.equals(eventComment.getUser().getId(), user.getId()) &&
!notificationRepository.checkIfEventCommentLikeEntryExists(user.getId(), eventComment.getEventPost().getId())) {
EventCommentLikeNotification notification = NotificationMapper.mapToEventCommentLikeNotification(eventComment, user);
notification.setAccumulatedId(null);
notificationRepository.saveEventCommentLikeNotification(notification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ public FriendlistProposal followUser(FollowUserProposal proposal) {
Friendship friendship = FriendshipMapper.mapToFriendship(requester, receiver);

// notify receiver
FollowNotification followNotification = NotificationMapper.mapToFollowNotification(requester,receiver);
followNotification.setAccumulatedId(null);
notificationRepository.saveFollowNotification(followNotification);
if (!notificationRepository.checkIfFollowEntryExists(requester.getId(), receiver.getId())){
FollowNotification followNotification = NotificationMapper.mapToFollowNotification(requester,receiver);
followNotification.setAccumulatedId(null);
notificationRepository.saveFollowNotification(followNotification);
}

return FriendshipMapper.mapToFriendlist(repository.save(friendship));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ private void deleteGroupNotifications(DeleteNotificationProposal deleteNotificat
String type = deleteNotificationProposal.getType();
Long groupId = deleteNotificationProposal.getGroupId();

if (type.equals(NotificationType.TYPE_COMMENT_LIKE.name())){
if (type.equals(NotificationType.TYPE_COMMENT_LIKE.getType())){
repository.findCommentLikeNotificationsByGroup(groupId).forEach(repository::deleteCommentLikeNotification);
} else if (type.equals(NotificationType.TYPE_FOLLOW.name())){
} else if (type.equals(NotificationType.TYPE_FOLLOW.getType())){
repository.findFollowNotificationsByGroup(groupId).forEach(repository::deleteFollowNotification);
} else if (type.equals(NotificationType.TYPE_POST_LIKE.name())){
} else if (type.equals(NotificationType.TYPE_POST_LIKE.getType())){
repository.findPostLikeNotificationsByGroup(groupId).forEach(repository::deletePostLikeNotification);
} else if (type.equals(NotificationType.TYPE_POST_COMMENT.name())){
} else if (type.equals(NotificationType.TYPE_POST_COMMENT.getType())){
repository.findPostCommentNotificationsByGroup(groupId).forEach(repository::deletePostCommentNotification);
} else {
} else if (type.equals(NotificationType.TYPE_EVENT_COMMENT_LIKE.getType())){
repository.findEventCommentLikeNotificationsByGroup(groupId).forEach(repository::deleteEventCommentLikeNotification);
}
}
Expand All @@ -179,15 +179,15 @@ private void deleteSingleNotifications(DeleteNotificationProposal deleteNotifica
String type = deleteNotificationProposal.getType();
Long notificationId = deleteNotificationProposal.getNotificationId();

if (type.equals(NotificationType.TYPE_COMMENT_LIKE.name())){
if (type.equals(NotificationType.TYPE_COMMENT_LIKE.getType())){
repository.deleteCommentLikeNotification(repository.findCommentLikeNotificationById(notificationId));
} else if (type.equals(NotificationType.TYPE_FOLLOW.name())){
} else if (type.equals(NotificationType.TYPE_FOLLOW.getType())){
repository.deleteFollowNotification(repository.findFollowNotificationById(notificationId));
} else if (type.equals(NotificationType.TYPE_POST_LIKE.name())){
} else if (type.equals(NotificationType.TYPE_POST_LIKE.getType())){
repository.deletePostLikeNotification(repository.findPostLikeNotificationById(notificationId));
} else if (type.equals(NotificationType.TYPE_POST_COMMENT.name())){
} else if (type.equals(NotificationType.TYPE_POST_COMMENT.getType())){
repository.deletePostCommentNotification(repository.findPostCommentNotificationById(notificationId));
} else {
} else if (type.equals(NotificationType.TYPE_EVENT_COMMENT_LIKE.getType())){
repository.deleteEventCommentLikeNotification(repository.findEventCommentLikeNotificationById(notificationId));
}
}
Expand Down
Loading
Loading