feat: Support user notifications preferences
This commit is contained in:
parent
8104411066
commit
f1e0f82691
@ -69,7 +69,7 @@ public class AuthController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/updateUser")
|
@PostMapping("/updateUser")
|
||||||
@Operation(summary = "Update user information", description = "Update authenticated user's fcmToken, displayName, avatar, password, or username")
|
@Operation(summary = "Update user information", description = "Update authenticated user's fcmToken, displayName, avatar, password, username, or subscriptions")
|
||||||
public ResponseEntity<Map<String, Object>> updateUser(@Valid @RequestBody UpdateUserRequest request) {
|
public ResponseEntity<Map<String, Object>> updateUser(@Valid @RequestBody UpdateUserRequest request) {
|
||||||
try {
|
try {
|
||||||
User user = userService.updateUser(
|
User user = userService.updateUser(
|
||||||
@ -77,7 +77,8 @@ public class AuthController {
|
|||||||
request.getDisplayName(),
|
request.getDisplayName(),
|
||||||
request.getAvatar(),
|
request.getAvatar(),
|
||||||
request.getPassword(),
|
request.getPassword(),
|
||||||
request.getUsername()
|
request.getUsername(),
|
||||||
|
request.getSubscriptions()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create user data without password
|
// Create user data without password
|
||||||
@ -89,7 +90,8 @@ public class AuthController {
|
|||||||
"avatar", user.getAvatar() != null ? user.getAvatar() : "",
|
"avatar", user.getAvatar() != null ? user.getAvatar() : "",
|
||||||
"fcmToken", user.getFcmToken() != null ? user.getFcmToken() : "",
|
"fcmToken", user.getFcmToken() != null ? user.getFcmToken() : "",
|
||||||
"activated", user.isActivated(),
|
"activated", user.isActivated(),
|
||||||
"role", user.getRole()
|
"role", user.getRole(),
|
||||||
|
"subscriptions", user.getSubscriptions()
|
||||||
);
|
);
|
||||||
|
|
||||||
return ResponseEntity.ok(Map.of(
|
return ResponseEntity.ok(Map.of(
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package online.wesal.wesal.dto;
|
package online.wesal.wesal.dto;
|
||||||
|
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class UpdateUserRequest {
|
public class UpdateUserRequest {
|
||||||
|
|
||||||
@ -15,6 +16,8 @@ public class UpdateUserRequest {
|
|||||||
@Size(min = 6, message = "Password must be at least 8 characters long")
|
@Size(min = 6, message = "Password must be at least 8 characters long")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
|
private List<String> subscriptions;
|
||||||
|
|
||||||
public UpdateUserRequest() {}
|
public UpdateUserRequest() {}
|
||||||
|
|
||||||
public String getFcmToken() {
|
public String getFcmToken() {
|
||||||
@ -56,4 +59,12 @@ public class UpdateUserRequest {
|
|||||||
public void setUsername(String username) {
|
public void setUsername(String username) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getSubscriptions() {
|
||||||
|
return subscriptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubscriptions(List<String> subscriptions) {
|
||||||
|
this.subscriptions = subscriptions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -4,6 +4,11 @@ import jakarta.persistence.*;
|
|||||||
import jakarta.validation.constraints.Email;
|
import jakarta.validation.constraints.Email;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
@ -40,6 +45,9 @@ public class User {
|
|||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String role = "USER";
|
private String role = "USER";
|
||||||
|
|
||||||
|
@Column(columnDefinition = "TEXT")
|
||||||
|
private String subscriptions = "[\"newinvites\",\"invitesfollowup\",\"appnews\"]";
|
||||||
|
|
||||||
public User() {}
|
public User() {}
|
||||||
|
|
||||||
public User(String email, String password, String displayName) {
|
public User(String email, String password, String displayName) {
|
||||||
@ -119,4 +127,25 @@ public class User {
|
|||||||
public void setRole(String role) {
|
public void setRole(String role) {
|
||||||
this.role = role;
|
this.role = role;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getSubscriptions() {
|
||||||
|
if (subscriptions == null || subscriptions.isEmpty()) {
|
||||||
|
return Arrays.asList("newinvites", "invitesfollowup", "appnews");
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
return mapper.readValue(subscriptions, new TypeReference<List<String>>() {});
|
||||||
|
} catch (Exception e) {
|
||||||
|
return Arrays.asList("newinvites", "invitesfollowup", "appnews");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubscriptions(List<String> subscriptions) {
|
||||||
|
try {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
this.subscriptions = mapper.writeValueAsString(subscriptions);
|
||||||
|
} catch (Exception e) {
|
||||||
|
this.subscriptions = "[\"newinvites\",\"invitesfollowup\",\"appnews\"]";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -7,6 +7,7 @@ import org.springframework.security.core.Authentication;
|
|||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
@ -56,7 +57,7 @@ public class UserService {
|
|||||||
return userRepository.save(user);
|
return userRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User updateUser(String fcmToken, String displayName, String avatar, String password, String username) {
|
public User updateUser(String fcmToken, String displayName, String avatar, String password, String username, List<String> subscriptions) {
|
||||||
User user = getCurrentUser();
|
User user = getCurrentUser();
|
||||||
boolean wasNotActivated = !user.isActivated();
|
boolean wasNotActivated = !user.isActivated();
|
||||||
|
|
||||||
@ -86,6 +87,10 @@ public class UserService {
|
|||||||
user.setUsername(username);
|
user.setUsername(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (subscriptions != null && !subscriptions.isEmpty()) {
|
||||||
|
user.setSubscriptions(subscriptions);
|
||||||
|
}
|
||||||
|
|
||||||
User savedUser = userRepository.save(user);
|
User savedUser = userRepository.save(user);
|
||||||
|
|
||||||
if (wasNotActivated) {
|
if (wasNotActivated) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user