Use user settings api instead of additionalInfo

This commit is contained in:
ViacheslavKlimov 2023-08-14 17:43:39 +03:00
parent d3710b411f
commit ce06ea10ca
6 changed files with 24 additions and 25 deletions

View File

@ -448,8 +448,7 @@ public class NotificationController extends BaseController {
@GetMapping("/notification/settings/user") @GetMapping("/notification/settings/user")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
public UserNotificationSettings getUserNotificationSettings(@AuthenticationPrincipal SecurityUser user) { public UserNotificationSettings getUserNotificationSettings(@AuthenticationPrincipal SecurityUser user) {
return notificationSettingsService.getUserNotificationSettings(user.getTenantId(), return notificationSettingsService.getUserNotificationSettings(user.getTenantId(), user.getId(), true);
userService.findUserById(user.getTenantId(), user.getId()), true);
} }
} }

View File

@ -243,7 +243,7 @@ public class DefaultNotificationCenter extends AbstractSubscriptionService imple
} }
if (recipient instanceof User) { if (recipient instanceof User) {
UserNotificationSettings settings = notificationSettingsService.getUserNotificationSettings(ctx.getTenantId(), (User) recipient, false); UserNotificationSettings settings = notificationSettingsService.getUserNotificationSettings(ctx.getTenantId(), ((User) recipient).getId(), false);
if (!settings.isEnabled(ctx.getNotificationType(), deliveryMethod)) { if (!settings.isEnabled(ctx.getNotificationType(), deliveryMethod)) {
throw new RuntimeException("User disabled " + deliveryMethod.getName() + " notifications of this type"); throw new RuntimeException("User disabled " + deliveryMethod.getName() + " notifications of this type");
} }

View File

@ -22,7 +22,7 @@ import org.thingsboard.server.dao.notification.DefaultNotificationSettingsServic
import org.thingsboard.server.dao.notification.NotificationTargetService; import org.thingsboard.server.dao.notification.NotificationTargetService;
import org.thingsboard.server.dao.notification.NotificationTemplateService; import org.thingsboard.server.dao.notification.NotificationTemplateService;
import org.thingsboard.server.dao.settings.AdminSettingsService; import org.thingsboard.server.dao.settings.AdminSettingsService;
import org.thingsboard.server.dao.user.UserService; import org.thingsboard.server.dao.user.UserSettingsService;
@Service @Service
@Primary @Primary
@ -31,8 +31,8 @@ public class TestNotificationSettingsService extends DefaultNotificationSettings
public TestNotificationSettingsService(AdminSettingsService adminSettingsService, public TestNotificationSettingsService(AdminSettingsService adminSettingsService,
NotificationTargetService notificationTargetService, NotificationTargetService notificationTargetService,
NotificationTemplateService notificationTemplateService, NotificationTemplateService notificationTemplateService,
UserService userService) { UserSettingsService userSettingsService) {
super(adminSettingsService, notificationTargetService, notificationTemplateService, null, userService); super(adminSettingsService, notificationTargetService, notificationTemplateService, null, userSettingsService);
} }
@Override @Override

View File

@ -15,7 +15,6 @@
*/ */
package org.thingsboard.server.dao.notification; package org.thingsboard.server.dao.notification;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; import org.thingsboard.server.common.data.notification.settings.NotificationSettings;
@ -29,7 +28,7 @@ public interface NotificationSettingsService {
UserNotificationSettings saveUserNotificationSettings(TenantId tenantId, UserId userId, UserNotificationSettings settings); UserNotificationSettings saveUserNotificationSettings(TenantId tenantId, UserId userId, UserNotificationSettings settings);
UserNotificationSettings getUserNotificationSettings(TenantId tenantId, User user, boolean format); UserNotificationSettings getUserNotificationSettings(TenantId tenantId, UserId userId, boolean format);
void createDefaultNotificationConfigs(TenantId tenantId); void createDefaultNotificationConfigs(TenantId tenantId);

View File

@ -19,7 +19,7 @@ import lombok.Getter;
public enum UserSettingsType { public enum UserSettingsType {
GENERAL, VISITED_DASHBOARDS(true), QUICK_LINKS, DOC_LINKS, DASHBOARDS, GETTING_STARTED; GENERAL, VISITED_DASHBOARDS(true), QUICK_LINKS, DOC_LINKS, DASHBOARDS, GETTING_STARTED, NOTIFICATIONS;
@Getter @Getter
private final boolean reserved; private final boolean reserved;

View File

@ -15,8 +15,6 @@
*/ */
package org.thingsboard.server.dao.notification; package org.thingsboard.server.dao.notification;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
@ -26,7 +24,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.AdminSettings; import org.thingsboard.server.common.data.AdminSettings;
import org.thingsboard.server.common.data.CacheConstants; import org.thingsboard.server.common.data.CacheConstants;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.notification.NotificationType; import org.thingsboard.server.common.data.notification.NotificationType;
@ -44,8 +41,10 @@ import org.thingsboard.server.common.data.notification.targets.platform.TenantAd
import org.thingsboard.server.common.data.notification.targets.platform.UsersFilter; import org.thingsboard.server.common.data.notification.targets.platform.UsersFilter;
import org.thingsboard.server.common.data.notification.targets.platform.UsersFilterType; import org.thingsboard.server.common.data.notification.targets.platform.UsersFilterType;
import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.settings.UserSettings;
import org.thingsboard.server.common.data.settings.UserSettingsType;
import org.thingsboard.server.dao.settings.AdminSettingsService; import org.thingsboard.server.dao.settings.AdminSettingsService;
import org.thingsboard.server.dao.user.UserService; import org.thingsboard.server.dao.user.UserSettingsService;
import java.util.Collections; import java.util.Collections;
import java.util.EnumMap; import java.util.EnumMap;
@ -62,10 +61,9 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS
private final NotificationTargetService notificationTargetService; private final NotificationTargetService notificationTargetService;
private final NotificationTemplateService notificationTemplateService; private final NotificationTemplateService notificationTemplateService;
private final DefaultNotifications defaultNotifications; private final DefaultNotifications defaultNotifications;
private final UserService userService; private final UserSettingsService userSettingsService;
private static final String SETTINGS_KEY = "notifications"; private static final String SETTINGS_KEY = "notifications";
private static final String USER_SETTINGS_KEY = "notificationSettings";
@CacheEvict(cacheNames = CacheConstants.NOTIFICATION_SETTINGS_CACHE, key = "#tenantId") @CacheEvict(cacheNames = CacheConstants.NOTIFICATION_SETTINGS_CACHE, key = "#tenantId")
@Override @Override
@ -95,20 +93,23 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS
@Override @Override
public UserNotificationSettings saveUserNotificationSettings(TenantId tenantId, UserId userId, UserNotificationSettings settings) { public UserNotificationSettings saveUserNotificationSettings(TenantId tenantId, UserId userId, UserNotificationSettings settings) {
User user = userService.findUserById(tenantId, userId); UserSettings userSettings = new UserSettings();
ObjectNode additionalInfo = (ObjectNode) Optional.ofNullable(user.getAdditionalInfo()).orElseGet(JacksonUtil::newObjectNode); userSettings.setUserId(userId);
additionalInfo.set(USER_SETTINGS_KEY, JacksonUtil.valueToTree(settings)); userSettings.setType(UserSettingsType.NOTIFICATIONS);
user.setAdditionalInfo(additionalInfo); userSettings.setSettings(JacksonUtil.valueToTree(settings));
userService.saveUser(user); userSettingsService.saveUserSettings(tenantId, userSettings);
return formatUserNotificationSettings(settings); return formatUserNotificationSettings(settings);
} }
@Override @Override
public UserNotificationSettings getUserNotificationSettings(TenantId tenantId, User user, boolean format) { public UserNotificationSettings getUserNotificationSettings(TenantId tenantId, UserId userId, boolean format) {
UserNotificationSettings settings = Optional.ofNullable(user.getAdditionalInfo()) UserSettings userSettings = userSettingsService.findUserSettings(tenantId, userId, UserSettingsType.NOTIFICATIONS);
.filter(JsonNode::isObject).map(info -> info.get(USER_SETTINGS_KEY)).filter(JsonNode::isObject) UserNotificationSettings settings;
.map(json -> JacksonUtil.treeToValue(json, UserNotificationSettings.class)) if (userSettings != null) {
.orElse(UserNotificationSettings.DEFAULT); settings = JacksonUtil.treeToValue(userSettings.getSettings(), UserNotificationSettings.class);
} else {
settings = UserNotificationSettings.DEFAULT;
}
if (format) { if (format) {
settings = formatUserNotificationSettings(settings); settings = formatUserNotificationSettings(settings);
} }