mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-18 13:01:02 +08:00
Disable post viewed and user activity notifications by default
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package com.openisle.config;
|
||||
|
||||
import com.openisle.model.NotificationType;
|
||||
import com.openisle.model.User;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Ensure default notification preferences are applied to existing users.
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class NotificationPreferenceInitializer implements CommandLineRunner {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
List<User> users = userRepository.findAll();
|
||||
for (User user : users) {
|
||||
Set<NotificationType> disabled = user.getDisabledNotificationTypes();
|
||||
boolean changed = false;
|
||||
if (disabled.add(NotificationType.POST_VIEWED)) {
|
||||
changed = true;
|
||||
}
|
||||
if (disabled.add(NotificationType.USER_ACTIVITY)) {
|
||||
changed = true;
|
||||
}
|
||||
if (changed) {
|
||||
userRepository.save(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import lombok.Setter;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashSet;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -68,7 +68,10 @@ public class User {
|
||||
@CollectionTable(name = "user_disabled_notification_types", joinColumns = @JoinColumn(name = "user_id"))
|
||||
@Column(name = "notification_type")
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Set<NotificationType> disabledNotificationTypes = new HashSet<>();
|
||||
private Set<NotificationType> disabledNotificationTypes = EnumSet.of(
|
||||
NotificationType.POST_VIEWED,
|
||||
NotificationType.USER_ACTIVITY
|
||||
);
|
||||
|
||||
@CreationTimestamp
|
||||
@Column(nullable = false, updatable = false,
|
||||
|
||||
Reference in New Issue
Block a user