Skip to content

Commit

Permalink
Dislikes (#7896)
Browse files Browse the repository at this point in the history
* eco news dislikes

* self like dislike test

* self like dislike check on events

* event like dislike tests fix

* issues
  • Loading branch information
holotsvan authored Dec 9, 2024
1 parent b0a34ff commit 281188d
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 102 deletions.
16 changes: 14 additions & 2 deletions service/src/main/java/greencity/service/CommentServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,12 @@ public void like(Long commentId, UserVO userVO, Locale locale) {
Comment comment = commentRepo.findByIdAndStatusNot(commentId, CommentStatus.DELETED)
.orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_BY_ID + commentId));

boolean isAuthor = comment.getUser().getId().equals(userVO.getId());

if (isAuthor) {
throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
}

if (removeLikeIfExists(comment, userVO)) {
return;
}
Expand Down Expand Up @@ -556,12 +562,18 @@ public void dislike(Long commentId, UserVO userVO, Locale locale) {
Comment comment = commentRepo.findByIdAndStatusNot(commentId, CommentStatus.DELETED)
.orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_BY_ID + commentId));

if (removeDislikeIfExists(comment, userVO)) {
return;
boolean isAuthor = comment.getUser().getId().equals(userVO.getId());

if (isAuthor) {
throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
}

removeLikeIfExists(comment, userVO);

if (removeDislikeIfExists(comment, userVO)) {
return;
}

comment.getUsersDisliked().add(modelMapper.map(userVO, User.class));

commentRepo.save(comment);
Expand Down
92 changes: 65 additions & 27 deletions service/src/main/java/greencity/service/EcoNewsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,29 +428,24 @@ private Page<EcoNews> getCommonEcoNews(Page<EcoNews> sortedPage, Page<EcoNews> p
@Override
public void like(UserVO userVO, Long id) {
EcoNews ecoNews = findEcoNewsById(id);
boolean isAuthor = ecoNews.getAuthor().getId().equals(userVO.getId());
if (isAuthor) {
throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
}

ecoNews.getUsersDislikedNews().removeIf(u -> u.getId().equals(userVO.getId()));
if (removeLikeIfExists(ecoNews, userVO, ecoNews.getAuthor())) {
return;
}

boolean isLiked = ecoNews.getUsersLikedNews().stream()
.anyMatch(u -> u.getId().equals(userVO.getId()));
removeDislikeIfExists(ecoNews, userVO);

boolean isAuthor = ecoNews.getAuthor().getId().equals(userVO.getId());
ecoNews.getUsersLikedNews().add(modelMapper.map(userVO, User.class));
achievementCalculation.calculateAchievement(userVO,
AchievementCategoryType.LIKE_NEWS, AchievementAction.ASSIGN);
ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("LIKE_NEWS"), userVO);

if (isLiked) {
achievementCalculation.calculateAchievement(userVO,
AchievementCategoryType.LIKE_NEWS, AchievementAction.DELETE);
ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_NEWS"),
userVO);
ecoNews.getUsersLikedNews().removeIf(u -> u.getId().equals(userVO.getId()));
} else {
if (isAuthor) {
throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
}
achievementCalculation.calculateAchievement(userVO,
AchievementCategoryType.LIKE_NEWS, AchievementAction.ASSIGN);
ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("LIKE_NEWS"), userVO);
ecoNews.getUsersLikedNews().add(modelMapper.map(userVO, User.class));
}
boolean isLiked = ecoNews.getUsersLikedNews().stream()
.anyMatch(u -> u.getId().equals(userVO.getId()));

sendNotification(ecoNews, userVO, !isLiked);
ecoNewsRepo.save(modelMapper.map(ecoNews, EcoNews.class));
Expand Down Expand Up @@ -483,16 +478,21 @@ private String formatNewsTitle(String newsTitle) {
@Override
public void dislike(UserVO userVO, Long id) {
EcoNews ecoNews = findEcoNewsById(id);
if (ecoNews.getUsersLikedNews().stream().anyMatch(user -> user.getId().equals(userVO.getId()))) {
ecoNews.getUsersLikedNews().removeIf(u -> u.getId().equals(userVO.getId()));
userNotificationService.removeActionUserFromNotification(modelMapper.map(ecoNews.getAuthor(), UserVO.class),
userVO, id, NotificationType.ECONEWS_LIKE);
boolean isAuthor = ecoNews.getAuthor().getId().equals(userVO.getId());

if (isAuthor) {
throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
}
if (ecoNews.getUsersDislikedNews().stream().anyMatch(user -> user.getId().equals(userVO.getId()))) {
ecoNews.getUsersDislikedNews().removeIf(u -> u.getId().equals(userVO.getId()));
} else {
ecoNews.getUsersDislikedNews().add(modelMapper.map(userVO, User.class));

removeLikeIfExists(ecoNews, userVO, ecoNews.getAuthor());

if (removeDislikeIfExists(ecoNews, userVO)) {
ecoNewsRepo.save(ecoNews);
return;
}

ecoNews.getUsersDislikedNews().add(modelMapper.map(userVO, User.class));

ecoNewsRepo.save(ecoNews);
}

Expand Down Expand Up @@ -778,4 +778,42 @@ public void setHiddenValue(Long id, UserVO user, boolean value) {
public List<EcoNewsDto> getThreeInterestingEcoNews() {
return mapEcoNewsListToEcoNewsDtoList(ecoNewsRepo.findThreeInterestingEcoNews());
}

/**
* Removes a like from the eco news if the user has already liked it. Returns
* true if a like was removed, false otherwise.
*/
private boolean removeLikeIfExists(EcoNews ecoNews, UserVO userVO, User econewsAuthor) {
boolean userLiked = ecoNews.getUsersLikedNews().stream()
.anyMatch(user -> user.getId().equals(userVO.getId()));

if (userLiked) {
ecoNews.getUsersLikedNews().removeIf(user -> user.getId().equals(userVO.getId()));
achievementCalculation.calculateAchievement(userVO, AchievementCategoryType.LIKE_NEWS,
AchievementAction.DELETE);
ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_NEWS"), userVO);

if (econewsAuthor != null) {
userNotificationService.removeActionUserFromNotification(
modelMapper.map(econewsAuthor, UserVO.class), userVO, ecoNews.getId(), NotificationType.EVENT_LIKE);
}
return true;
}
return false;
}

/**
* Removes a dislike from the eco news if the user has already disliked it.
* Returns true if a dislike was removed, false otherwise.
*/
private boolean removeDislikeIfExists(EcoNews ecoNews, UserVO userVO) {
boolean userDisliked = ecoNews.getUsersDislikedNews().stream()
.anyMatch(user -> user.getId().equals(userVO.getId()));

if (userDisliked) {
ecoNews.getUsersDislikedNews().removeIf(user -> user.getId().equals(userVO.getId()));
return true;
}
return false;
}
}
17 changes: 11 additions & 6 deletions service/src/main/java/greencity/service/EventServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,11 @@ public Long getCountOfOrganizedEventsByUserId(Long userId) {
public void like(Long eventId, UserVO userVO) {
Event event = findEventId(eventId);
User eventAuthor = getEventAuthor(event);
boolean isAuthor = Objects.nonNull(event.getOrganizer()) && event.getOrganizer().getId().equals(userVO.getId());

if (isAuthor) {
throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
}

if (removeLikeIfExists(event, userVO, eventAuthor)) {
return;
Expand All @@ -787,16 +792,19 @@ public void like(Long eventId, UserVO userVO) {
AchievementAction.ASSIGN);
ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("LIKE_EVENT"), userVO);

if (eventAuthor != null) {
sendEventLikeNotification(eventAuthor, userVO, eventId, event);
}
sendEventLikeNotification(eventAuthor, userVO, eventId, event);

eventRepo.save(event);
}

@Override
public void dislike(UserVO userVO, Long eventId) {
Event event = findEventId(eventId);
boolean isAuthor = Objects.nonNull(event.getOrganizer()) && event.getOrganizer().getId().equals(userVO.getId());

if (isAuthor) {
throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
}

removeLikeIfExists(event, userVO, getEventAuthor(event));

Expand All @@ -806,9 +814,6 @@ public void dislike(UserVO userVO, Long eventId) {
}

event.getUsersDislikedEvents().add(modelMapper.map(userVO, User.class));
achievementCalculation.calculateAchievement(userVO, AchievementCategoryType.LIKE_EVENT,
AchievementAction.DELETE);
ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_EVENT"), userVO);

eventRepo.save(event);
}
Expand Down
22 changes: 13 additions & 9 deletions service/src/main/java/greencity/service/HabitServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,14 @@ public void deleteCustomHabit(Long customHabitId, String ownerEmail) {
@Override
public void like(Long habitId, UserVO userVO) {
Habit habit = findHabitById(habitId);
User habitAuthor = getHabitAuthor(habit);
User author = getHabitAuthor(habit);
boolean isAuthor = habit.getUserId().equals(userVO.getId());

if (removeLikeIfExists(habit, userVO, habitAuthor)) {
if (isAuthor) {
throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
}

if (removeLikeIfExists(habit, userVO, author)) {
return;
}

Expand All @@ -563,16 +568,19 @@ public void like(Long habitId, UserVO userVO) {
achievementCalculation.calculateAchievement(userVO, AchievementCategoryType.LIKE_HABIT,
AchievementAction.ASSIGN);

if (habitAuthor != null) {
sendHabitLikeNotification(habitAuthor, userVO, habitId, habit);
}
sendHabitLikeNotification(author, userVO, habitId, habit);

habitRepo.save(habit);
}

@Override
public void dislike(Long habitId, UserVO userVO) {
Habit habit = findHabitById(habitId);
boolean isAuthor = habit.getUserId().equals(userVO.getId());

if (isAuthor) {
throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION);
}

removeLikeIfExists(habit, userVO, getHabitAuthor(habit));

Expand All @@ -581,10 +589,6 @@ public void dislike(Long habitId, UserVO userVO) {
}

habit.getUsersDisliked().add(modelMapper.map(userVO, User.class));
achievementCalculation.calculateAchievement(userVO, AchievementCategoryType.LIKE_HABIT,
AchievementAction.DELETE);

ratingCalculation.ratingCalculation(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_HABIT"), userVO);

habitRepo.save(habit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,22 @@
import java.util.Locale;
import java.util.Optional;
import java.util.Set;

import static greencity.ModelUtils.*;
import static greencity.ModelUtils.getAddCommentDtoResponse;
import static greencity.ModelUtils.getAmountCommentLikesDto;
import static greencity.ModelUtils.getComment;
import static greencity.ModelUtils.getCommentDto;
import static greencity.ModelUtils.getCommentVO;
import static greencity.ModelUtils.getEcoNews;
import static greencity.ModelUtils.getEvent;
import static greencity.ModelUtils.getHabit;
import static greencity.ModelUtils.getHabitTranslation;
import static greencity.ModelUtils.getMultipartImageFiles;
import static greencity.ModelUtils.getUser;
import static greencity.ModelUtils.getUserNotCommentOwner;
import static greencity.ModelUtils.getUserSearchDto;
import static greencity.ModelUtils.getUserTagDto;
import static greencity.ModelUtils.getUserVO;
import static greencity.ModelUtils.getUserVONotCommentOwner;
import static greencity.constant.ErrorMessage.ECO_NEW_NOT_FOUND_BY_ID;
import static greencity.constant.ErrorMessage.HABIT_NOT_FOUND_BY_ID;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -1056,6 +1070,18 @@ void likeTest() {
verify(modelMapper).map(userVO, User.class);
}

@Test
void testLikeOwn() {
UserVO userVO = getUserVO();
Comment comment = getComment();
CommentVO commentVO = getCommentVO();
commentVO.setUsersLiked(new HashSet<>());

when(commentRepo.findByIdAndStatusNot(1L, CommentStatus.DELETED)).thenReturn(Optional.of(comment));

assertThrows(BadRequestException.class, () -> commentService.like(1L, userVO, Locale.ENGLISH));
}

@Test
void likeTest_OwnerUserLikesTheirComment_ShouldNotLike() {
Long commentId = 1L;
Expand All @@ -1079,12 +1105,7 @@ void likeTest_OwnerUserLikesTheirComment_ShouldNotLike() {
any(UserVO.class), any(UserVO.class), any(NotificationType.class),
anyLong(), anyString(), anyLong(), anyString());

commentService.like(commentId, userVO, Locale.ENGLISH);

assertFalse(comment.getUsersLiked().contains(user));

verify(commentRepo).findByIdAndStatusNot(commentId, CommentStatus.DELETED);
verify(modelMapper).map(userVO, User.class);
assertThrows(BadRequestException.class, () -> commentService.like(1L, userVO, Locale.ENGLISH));
}

@Test
Expand Down Expand Up @@ -1151,6 +1172,7 @@ void unlikeTest() {
Comment comment = getComment();
comment.setCurrentUserLiked(true);
comment.getUsersLiked().add(user);
comment.getUser().setId(2L);
RatingPoints ratingPoints = RatingPoints.builder().id(1L).name("UNDO_LIKE_COMMENT_OR_REPLY").points(-1).build();

when(ratingPointsRepo.findByNameOrThrow("UNDO_LIKE_COMMENT_OR_REPLY")).thenReturn(ratingPoints);
Expand Down Expand Up @@ -1213,6 +1235,7 @@ void dislikeTest() {
UserVO userVO = getUserVO();
User user = getUser();
Comment comment = getComment();
comment.getUser().setId(2L);
comment.setUsersDisliked(new HashSet<>());

when(commentRepo.findByIdAndStatusNot(1L, CommentStatus.DELETED)).thenReturn(Optional.of(comment));
Expand All @@ -1224,11 +1247,24 @@ void dislikeTest() {
assertEquals(1L, comment.getUsersDisliked().size());
}

@Test
void testDislikeOwn() {
UserVO userVO = getUserVO();
Comment comment = getComment();
CommentVO commentVO = getCommentVO();
commentVO.setUsersLiked(new HashSet<>());

when(commentRepo.findByIdAndStatusNot(1L, CommentStatus.DELETED)).thenReturn(Optional.of(comment));

assertThrows(BadRequestException.class, () -> commentService.dislike(1L, userVO, Locale.ENGLISH));
}

@Test
void givenEventLikedByUser_whenDislikedByUser_shouldRemoveLikeAndAddDislike() {
UserVO userVO = getUserVO();
User user = getUser();
Comment comment = getComment();
comment.getUser().setId(2L);
comment.setUsersLiked(new HashSet<>(Set.of(user)));
comment.setUsersDisliked(new HashSet<>());

Expand Down
Loading

0 comments on commit 281188d

Please sign in to comment.