diff --git a/src/main/java/com/nonononoki/alovoa/config/EventListenerConfig.java b/src/main/java/com/nonononoki/alovoa/config/EventListenerConfig.java index a867f3c3..48a32790 100644 --- a/src/main/java/com/nonononoki/alovoa/config/EventListenerConfig.java +++ b/src/main/java/com/nonononoki/alovoa/config/EventListenerConfig.java @@ -102,12 +102,26 @@ private void setDefaultUserMiscInfo() { new UserMiscInfo(UserMiscInfo.RELATIONSHIP_OPEN), new UserMiscInfo(UserMiscInfo.RELATIONSHIP_OTHER), new UserMiscInfo(UserMiscInfo.KIDS_NO), - new UserMiscInfo(UserMiscInfo.KIDS_YES) + new UserMiscInfo(UserMiscInfo.KIDS_YES), + new UserMiscInfo(UserMiscInfo.RELATIONSHIP_TYPE_MONOGAMOUS), + new UserMiscInfo(UserMiscInfo.RELATIONSHIP_TYPE_POLYAMOROUS), + new UserMiscInfo(UserMiscInfo.GENDER_IDENTITY_CIS), + new UserMiscInfo(UserMiscInfo.GENDER_IDENTITY_TRANS), + new UserMiscInfo(UserMiscInfo.POLITICS_MODERATE), + new UserMiscInfo(UserMiscInfo.POLITICS_LEFT), + new UserMiscInfo(UserMiscInfo.POLITICS_RIGHT), + new UserMiscInfo(UserMiscInfo.RELIGION_NO), + new UserMiscInfo(UserMiscInfo.RELIGION_YES), + new UserMiscInfo(UserMiscInfo.FAMILY_WANT), + new UserMiscInfo(UserMiscInfo.FAMILY_NOT_WANT), + new UserMiscInfo(UserMiscInfo.FAMILY_NOT_UNSURE) )); userMiscInfoList.removeIf(u -> userMiscInfoRepo.existsByValue(u.getValue())); - userMiscInfoRepo.saveAllAndFlush(userMiscInfoList); + if(!userMiscInfoList.isEmpty()) { + userMiscInfoRepo.saveAllAndFlush(userMiscInfoList); + } } public void setDefaultAdmin() { diff --git a/src/main/java/com/nonononoki/alovoa/entity/user/Gender.java b/src/main/java/com/nonononoki/alovoa/entity/user/Gender.java index e5141019..a95e4490 100644 --- a/src/main/java/com/nonononoki/alovoa/entity/user/Gender.java +++ b/src/main/java/com/nonononoki/alovoa/entity/user/Gender.java @@ -1,24 +1,17 @@ package com.nonononoki.alovoa.entity.user; -import java.util.List; - -import jakarta.persistence.CascadeType; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.ManyToMany; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Transient; - import com.fasterxml.jackson.annotation.JsonIgnore; import com.nonononoki.alovoa.entity.User; - +import jakarta.persistence.*; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; +import java.util.List; + @Getter @Setter +@EqualsAndHashCode @Entity public class Gender { @@ -31,16 +24,18 @@ public class Gender { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @EqualsAndHashCode.Exclude private Long id; @JsonIgnore @ManyToMany + @EqualsAndHashCode.Exclude private List users; @JsonIgnore @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "gender") + @EqualsAndHashCode.Exclude private List user; private String text; - } diff --git a/src/main/java/com/nonononoki/alovoa/entity/user/Message.java b/src/main/java/com/nonononoki/alovoa/entity/user/Message.java index 8e9f6508..619f8566 100644 --- a/src/main/java/com/nonononoki/alovoa/entity/user/Message.java +++ b/src/main/java/com/nonononoki/alovoa/entity/user/Message.java @@ -15,11 +15,13 @@ import com.nonononoki.alovoa.component.TextEncryptorConverter; import com.nonononoki.alovoa.entity.User; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @Getter @Setter +@EqualsAndHashCode @Entity public class Message { diff --git a/src/main/java/com/nonononoki/alovoa/entity/user/UserInterest.java b/src/main/java/com/nonononoki/alovoa/entity/user/UserInterest.java index b9195f44..5aeff20a 100644 --- a/src/main/java/com/nonononoki/alovoa/entity/user/UserInterest.java +++ b/src/main/java/com/nonononoki/alovoa/entity/user/UserInterest.java @@ -9,42 +9,28 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.nonononoki.alovoa.entity.User; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; +import java.util.Objects; + @Getter @Setter +@EqualsAndHashCode @Entity public class UserInterest { @JsonIgnore @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @EqualsAndHashCode.Exclude private Long id; private String text; @JsonIgnore @ManyToOne + @EqualsAndHashCode.Exclude private User user; - - @Override - public boolean equals(Object o) { - - if (o == this) { - return true; - } - - if (!(o instanceof UserInterest)) { - return false; - } - - UserInterest i = (UserInterest) o; - return i.getText().equals(text); - } - - @Override - public int hashCode() { - return this.text.hashCode(); - } } diff --git a/src/main/java/com/nonononoki/alovoa/entity/user/UserMiscInfo.java b/src/main/java/com/nonononoki/alovoa/entity/user/UserMiscInfo.java index 65b68617..b1803240 100644 --- a/src/main/java/com/nonononoki/alovoa/entity/user/UserMiscInfo.java +++ b/src/main/java/com/nonononoki/alovoa/entity/user/UserMiscInfo.java @@ -13,6 +13,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.nonononoki.alovoa.entity.User; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -20,6 +21,7 @@ @Getter @Setter @Entity +@EqualsAndHashCode @NoArgsConstructor public class UserMiscInfo { @@ -64,12 +66,43 @@ public class UserMiscInfo { @Transient public static final long DRUGS_OTHER_SOMETIMES = 44; + @Transient + public static final long RELATIONSHIP_TYPE_MONOGAMOUS = 51; + @Transient + public static final long RELATIONSHIP_TYPE_POLYAMOROUS = 52; + + @Transient + public static final long GENDER_IDENTITY_CIS = 61; + @Transient + public static final long GENDER_IDENTITY_TRANS = 62; + + @Transient + public static final long POLITICS_MODERATE = 71; + @Transient + public static final long POLITICS_LEFT = 72; + @Transient + public static final long POLITICS_RIGHT = 73; + + @Transient + public static final long RELIGION_NO = 81; + @Transient + public static final long RELIGION_YES = 82; + + @Transient + public static final long FAMILY_WANT = 91; + @Transient + public static final long FAMILY_NOT_WANT = 92; + @Transient + public static final long FAMILY_NOT_UNSURE = 93; + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @EqualsAndHashCode.Exclude private Long id; @JsonIgnore @ManyToMany + @EqualsAndHashCode.Exclude private List users; @Column(unique=true) diff --git a/src/main/java/com/nonononoki/alovoa/model/ProfileOnboardingDto.java b/src/main/java/com/nonononoki/alovoa/model/ProfileOnboardingDto.java index d970f426..7db17fde 100644 --- a/src/main/java/com/nonononoki/alovoa/model/ProfileOnboardingDto.java +++ b/src/main/java/com/nonononoki/alovoa/model/ProfileOnboardingDto.java @@ -8,19 +8,19 @@ @Data public class ProfileOnboardingDto { - - private Long intention; - - private List preferredGenders; - private String profilePictureMime; + private Long intention; - private String description; - - private Set interests; + private List preferredGenders; - private boolean notificationLike; + private String profilePictureMime; + + private String description; + + private Set interests; + + private boolean notificationLike; + + private boolean notificationChat; - private boolean notificationChat; - } diff --git a/src/main/java/com/nonononoki/alovoa/model/UserGdpr.java b/src/main/java/com/nonononoki/alovoa/model/UserGdpr.java index 52e4ecb5..f359fdb5 100644 --- a/src/main/java/com/nonononoki/alovoa/model/UserGdpr.java +++ b/src/main/java/com/nonononoki/alovoa/model/UserGdpr.java @@ -34,6 +34,7 @@ public class UserGdpr { private List images; private List prompts; private List donations; + private Set miscInfo; private List messageSent; private UserDates dates; private boolean showZodiac; @@ -60,6 +61,7 @@ public static UserGdpr userToUserGdpr(User user) { u.setIntention(user.getIntention()); u.setInterests(user.getInterests()); u.setPreferedGenders(user.getPreferedGenders()); + u.setMiscInfo(user.getMiscInfos()); u.setAudio(user.getAudio()); u.setProfilePicture(user.getProfilePicture()); diff --git a/src/main/java/com/nonononoki/alovoa/service/UserService.java b/src/main/java/com/nonononoki/alovoa/service/UserService.java index d38dc718..05e2a119 100644 --- a/src/main/java/com/nonononoki/alovoa/service/UserService.java +++ b/src/main/java/com/nonononoki/alovoa/service/UserService.java @@ -27,7 +27,6 @@ import org.springframework.http.*; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; @@ -36,10 +35,16 @@ import javax.sound.sampled.*; import java.awt.*; import java.awt.image.BufferedImage; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; -import java.security.*; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.List; @@ -59,10 +64,18 @@ public class UserService { private static final String MIME_MPEG = "mpeg"; private static final String MIME_MP3 = "mp3"; - private final Set drugsAlcohol = new HashSet<>(Set.of(UserMiscInfo.DRUGS_ALCOHOL_YES, UserMiscInfo.DRUGS_ALCOHOL_SOMETIMES, UserMiscInfo.DRUGS_ALCOHOL_NO)); - private final Set drugsTobacco = new HashSet<>(Set.of(UserMiscInfo.DRUGS_TOBACCO_YES, UserMiscInfo.DRUGS_TOBACCO_SOMETIMES, UserMiscInfo.DRUGS_TOBACCO_NO)); - private final Set drugsCannabis = new HashSet<>(Set.of(UserMiscInfo.DRUGS_CANNABIS_YES, UserMiscInfo.DRUGS_CANNABIS_SOMETIMES, UserMiscInfo.DRUGS_CANNABIS_NO)); - private final Set drugsOther = new HashSet<>(Set.of(UserMiscInfo.DRUGS_OTHER_YES, UserMiscInfo.DRUGS_OTHER_SOMETIMES, UserMiscInfo.DRUGS_OTHER_NO)); + private final Set drugsAlcoholMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.DRUGS_ALCOHOL_YES, UserMiscInfo.DRUGS_ALCOHOL_SOMETIMES, UserMiscInfo.DRUGS_ALCOHOL_NO)); + private final Set drugsTobaccoMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.DRUGS_TOBACCO_YES, UserMiscInfo.DRUGS_TOBACCO_SOMETIMES, UserMiscInfo.DRUGS_TOBACCO_NO)); + private final Set drugsCannabisMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.DRUGS_CANNABIS_YES, UserMiscInfo.DRUGS_CANNABIS_SOMETIMES, UserMiscInfo.DRUGS_CANNABIS_NO)); + private final Set drugsOtherMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.DRUGS_OTHER_YES, UserMiscInfo.DRUGS_OTHER_SOMETIMES, UserMiscInfo.DRUGS_OTHER_NO)); + + private final Set kidsMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.KIDS_NO, UserMiscInfo.KIDS_YES)); + private final Set relationshipMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.RELATIONSHIP_SINGLE, UserMiscInfo.RELATIONSHIP_TAKEN, UserMiscInfo.RELATIONSHIP_OPEN)); + private final Set politicsMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.POLITICS_MODERATE, UserMiscInfo.POLITICS_LEFT, UserMiscInfo.POLITICS_RIGHT)); + private final Set genderIdentityMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.GENDER_IDENTITY_CIS, UserMiscInfo.GENDER_IDENTITY_TRANS)); + private final Set religionMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.RELIGION_NO, UserMiscInfo.RELIGION_YES)); + private final Set familyMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.FAMILY_WANT, UserMiscInfo.FAMILY_NOT_WANT, UserMiscInfo.FAMILY_NOT_UNSURE)); + private final Set relationshipTypeMiscInfoSet = new HashSet<>(Set.of(UserMiscInfo.RELATIONSHIP_TYPE_POLYAMOROUS, UserMiscInfo.RELATIONSHIP_TYPE_MONOGAMOUS)); @Autowired private AuthService authService; @@ -374,6 +387,7 @@ public void deleteAccountConfirm(UserDeleteAccountDto dto) mailService.sendAccountDeleteConfirm(user); } + @SuppressWarnings("deprecation") public void updateProfilePicture(byte[] bytes, String mimeType) throws AlovoaException, IOException { User user = authService.getCurrentUser(true); user.setVerificationPicture(null); @@ -507,33 +521,39 @@ public void updatePreferedGender(long genderId, boolean activated) throws Alovoa public Set updateUserMiscInfo(long infoValue, boolean activated) throws AlovoaException { User user = authService.getCurrentUser(true); + Set list = user.getMiscInfos(); if (list == null) { list = new HashSet<>(); } - UserMiscInfo info = userMiscInfoRepo.findByValue(infoValue); + if (kidsMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(kidsMiscInfoSet, infoValue)); + } else if (relationshipMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(relationshipMiscInfoSet, infoValue)); + } else if (drugsAlcoholMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(drugsAlcoholMiscInfoSet, infoValue)); + } else if (drugsTobaccoMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(drugsAlcoholMiscInfoSet, infoValue)); + } else if (drugsCannabisMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(drugsCannabisMiscInfoSet, infoValue)); + } else if (drugsOtherMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(drugsOtherMiscInfoSet, infoValue)); + } else if (politicsMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(politicsMiscInfoSet, infoValue)); + } else if (genderIdentityMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(genderIdentityMiscInfoSet, infoValue)); + } else if (religionMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(religionMiscInfoSet, infoValue)); + } else if (familyMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(familyMiscInfoSet, infoValue)); + } else if (relationshipTypeMiscInfoSet.contains(infoValue)) { + list.removeIf(conditionToRemoveIfExistent(relationshipTypeMiscInfoSet, infoValue)); + } + if (activated) { + UserMiscInfo info = userMiscInfoRepo.findByValue(infoValue); list.add(info); - - if (infoValue >= UserMiscInfo.KIDS_NO && infoValue <= UserMiscInfo.KIDS_YES) { - list.removeIf(o -> o.getValue() != infoValue && o.getValue() >= UserMiscInfo.KIDS_NO - && o.getValue() <= UserMiscInfo.KIDS_YES); - } else if (infoValue >= UserMiscInfo.RELATIONSHIP_SINGLE && infoValue <= UserMiscInfo.RELATIONSHIP_OTHER) { - list.removeIf(o -> o.getValue() != infoValue && o.getValue() >= UserMiscInfo.RELATIONSHIP_SINGLE - && o.getValue() <= UserMiscInfo.RELATIONSHIP_OTHER); - } else if (drugsAlcohol.contains(infoValue)) { - list.removeIf(conditionToRemoveIfExistent(drugsAlcohol, infoValue)); - } else if (drugsTobacco.contains(infoValue)) { - list.removeIf(conditionToRemoveIfExistent(drugsAlcohol, infoValue)); - } else if (drugsCannabis.contains(infoValue)) { - list.removeIf(conditionToRemoveIfExistent(drugsCannabis, infoValue)); - } else if (drugsOther.contains(infoValue)) { - list.removeIf(conditionToRemoveIfExistent(drugsOther, infoValue)); - } - - } else { - list.remove(info); } user.setMiscInfos(list); userRepo.saveAndFlush(user); @@ -895,6 +915,7 @@ public ResponseEntity getUserdata(UUID uuid) throws AlovoaException, J return new ResponseEntity<>(resource, headers, HttpStatus.OK); } + @SuppressWarnings("deprecation") public String getAudio(UUID uuid) throws NumberFormatException, AlovoaException { User user = findUserByUuid(uuid); @@ -910,6 +931,7 @@ public void deleteAudio() throws AlovoaException { userRepo.saveAndFlush(user); } + @SuppressWarnings("deprecation") public void updateAudio(byte[] bytes, String mimeType) throws AlovoaException, UnsupportedAudioFileException, IOException { User user = authService.getCurrentUser(true); diff --git a/src/test/java/com/nonononoki/alovoa/service/UserServiceTest.java b/src/test/java/com/nonononoki/alovoa/service/UserServiceTest.java index ee66a3e6..820db957 100644 --- a/src/test/java/com/nonononoki/alovoa/service/UserServiceTest.java +++ b/src/test/java/com/nonononoki/alovoa/service/UserServiceTest.java @@ -5,10 +5,7 @@ import com.nonononoki.alovoa.component.TextEncryptorConverter; import com.nonononoki.alovoa.entity.Captcha; import com.nonononoki.alovoa.entity.User; -import com.nonononoki.alovoa.entity.user.Gender; -import com.nonononoki.alovoa.entity.user.UserDeleteToken; -import com.nonononoki.alovoa.entity.user.UserInterest; -import com.nonononoki.alovoa.entity.user.UserSettings; +import com.nonononoki.alovoa.entity.user.*; import com.nonononoki.alovoa.model.*; import com.nonononoki.alovoa.repo.ConversationRepository; import com.nonononoki.alovoa.repo.UserRepository; @@ -39,6 +36,7 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; @@ -162,6 +160,21 @@ void test() throws Exception { }); } + //MISC INFO + userService.updateUserMiscInfo(UserMiscInfo.DRUGS_ALCOHOL_NO, true); + userService.updateUserMiscInfo(UserMiscInfo.FAMILY_WANT, true); + userService.updateUserMiscInfo(UserMiscInfo.DRUGS_CANNABIS_YES, true); + userService.updateUserMiscInfo(UserMiscInfo.DRUGS_TOBACCO_SOMETIMES, true); + userService.updateUserMiscInfo(UserMiscInfo.GENDER_IDENTITY_CIS, true); + userService.updateUserMiscInfo(UserMiscInfo.RELIGION_NO, true); + userService.updateUserMiscInfo(UserMiscInfo.RELATIONSHIP_OTHER, true); + userService.updateUserMiscInfo(UserMiscInfo.RELATIONSHIP_TYPE_POLYAMOROUS, true); + user1 = userRepo.findById(user1.getId()).get(); + assertEquals(7, user1.getMiscInfos().size()); + userService.updateUserMiscInfo(UserMiscInfo.RELATIONSHIP_TYPE_MONOGAMOUS, false); + user1 = userRepo.findById(user1.getId()).get(); + assertEquals(6, user1.getMiscInfos().size()); + Mockito.when(authService.getCurrentUser()).thenReturn(user2); Mockito.when(authService.getCurrentUser(true)).thenReturn(user2); byte[] img2 = Tools.resourceToBytes("img/profile2.png"); @@ -211,11 +224,11 @@ void test() throws Exception { userService.deleteAudio(); assertNull(user3.getAudio()); + User finalUser = user1; assertThrows(Exception.class, () -> { - deleteTest(user1); + deleteTest(finalUser); }); - //PROFILE PICTURE byte[] imgLong = Tools.resourceToBytes("img/long.jpeg"); userService.updateProfilePicture(imgLong, "jpeg"); @@ -227,7 +240,7 @@ void test() throws Exception { Mockito.when(authService.getCurrentUser(true)).thenReturn(user1); UUID uuid = user1.getUuid(); ResponseEntity userData = userService.getUserdata(uuid); - InputStream inputStream = userData.getBody().getInputStream(); + InputStream inputStream = Objects.requireNonNull(userData.getBody()).getInputStream(); String userDataString = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines() .collect(Collectors.joining("\n")); UserGdpr gdpr = objectMapper.readValue(userDataString, UserGdpr.class); @@ -242,15 +255,12 @@ void test() throws Exception { objectMapper.writeValueAsString(gdpr.getGender())); assertEquals(objectMapper.writeValueAsString(authService.getCurrentUser().getIntention()), objectMapper.writeValueAsString(gdpr.getIntention())); - // TODO Comparing Sets -// assertEquals(authService.getCurrentUser().getPreferedGenders(), gdpr.getPreferedGenders()); -// assertEquals(authService.getCurrentUser().getInterests(), -// gdpr.getInterests()); -// assertEquals(authService.getCurrentUser().getMessageSent(), -// gdpr.getMessageSent()); - -// assertEquals(objectMapper.writeValueAsString(authService.getCurrentUser().getWebPush()), -// objectMapper.writeValueAsString(gdpr.getWebPush())); + assertEquals(authService.getCurrentUser().getPreferedGenders(), gdpr.getPreferedGenders()); + assertIterableEquals(authService.getCurrentUser().getInterests(), + gdpr.getInterests()); + assertIterableEquals(authService.getCurrentUser().getMessageSent(), + gdpr.getMessageSent()); + assertEquals(authService.getCurrentUser().getMiscInfos(), gdpr.getMiscInfo()); assertEquals(authService.getCurrentUser().getLocationLatitude(), gdpr.getLocationLatitude()); assertEquals(authService.getCurrentUser().getLocationLongitude(), gdpr.getLocationLongitude()); assertEquals(authService.getCurrentUser().getPreferedMaxAge(), gdpr.getPreferedMaxAge());