Skip to content

Commit

Permalink
improve: add user custom config for notification dispatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
MDeLuise committed May 18, 2024
2 parents 46ed433 + 6a1941b commit a03d0a0
Show file tree
Hide file tree
Showing 32 changed files with 947 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.github.mdeluise.plantit.botanicalinfo.BotanicalInfo;
import com.github.mdeluise.plantit.diary.Diary;
import com.github.mdeluise.plantit.notification.dispatcher.NotificationDispatcherName;
import com.github.mdeluise.plantit.notification.dispatcher.config.AbstractNotificationDispatcherConfig;
import com.github.mdeluise.plantit.plant.Plant;
import com.github.mdeluise.plantit.security.apikey.ApiKey;
import jakarta.persistence.CascadeType;
Expand Down Expand Up @@ -65,6 +66,8 @@ public class User implements Serializable {
@Column(name = "dispatcher_name")
private Set<NotificationDispatcherName> notificationDispatchers = new HashSet<>();
private Date lastLogin;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<AbstractNotificationDispatcherConfig> configs = new HashSet<>();


public User(Long id, String username, String password) {
Expand Down Expand Up @@ -189,6 +192,16 @@ public void setNotificationDispatchers(Set<NotificationDispatcherName> notificat
}


public Set<AbstractNotificationDispatcherConfig> getConfigs() {
return configs;
}


public void setConfigs(Set<AbstractNotificationDispatcherConfig> configs) {
this.configs = configs;
}


public Date getLastLogin() {
return lastLogin;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ public User save(String username, String plainPassword, String email) {
@Transactional
public User updateInternal(Long id, User updatedUser) {
final User toUpdate = get(id);
if (updatedUser.getUsername() != null && !updatedUser.getUsername().isBlank() &&
!updatedUser.getUsername().equals(toUpdate.getUsername())) {
toUpdate.setUsername(updatedUser.getUsername());
}
toUpdate.setUsername(updatedUser.getUsername());
toUpdate.setLastLogin(updatedUser.getLastLogin());
return userRepository.save(toUpdate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.mdeluise.plantit.notification.dispatcher.NotificationDispatcher;
import com.github.mdeluise.plantit.notification.dispatcher.NotificationDispatcherName;
import com.github.mdeluise.plantit.notification.dispatcher.config.NotificationDispatcherConfig;
import com.github.mdeluise.plantit.reminder.Reminder;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -30,4 +31,14 @@ public NotificationDispatcherName getName() {
public boolean isEnabled() {
return true;
}


@Override
public void loadConfig(NotificationDispatcherConfig config) {
}


@Override
public void initConfig() {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.mdeluise.plantit.notification.dispatcher;

import com.github.mdeluise.plantit.notification.NotifyException;
import com.github.mdeluise.plantit.notification.dispatcher.config.NotificationDispatcherConfig;
import com.github.mdeluise.plantit.reminder.Reminder;

public interface NotificationDispatcher {
Expand All @@ -9,4 +10,8 @@ public interface NotificationDispatcher {
NotificationDispatcherName getName();

boolean isEnabled();

void loadConfig(NotificationDispatcherConfig config);

void initConfig();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import java.util.Collection;
import java.util.Set;

import com.github.mdeluise.plantit.notification.dispatcher.config.AbstractNotificationDispatcherConfig;
import com.github.mdeluise.plantit.notification.ntfy.NtfyNotificationDispatcherConfig;
import com.github.mdeluise.plantit.notification.ntfy.NtfyNotificationDispatcherConfigDTO;
import com.github.mdeluise.plantit.notification.ntfy.NtfyNotificationDispatcherDTOConverter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -17,11 +22,14 @@
@Tag(name = "Notifications", description = "Endpoints for notifications management")
public class NotificationDispatcherController {
private final NotificationDispatcherService notificationDispatcherService;
private final NtfyNotificationDispatcherDTOConverter ntfyNotificationDispatcherDTOConverter;


@Autowired
public NotificationDispatcherController(NotificationDispatcherService notificationDispatcherService) {
public NotificationDispatcherController(NotificationDispatcherService notificationDispatcherService,
NtfyNotificationDispatcherDTOConverter ntfyNotificationDispatcherDTOConverter) {
this.notificationDispatcherService = notificationDispatcherService;
this.ntfyNotificationDispatcherDTOConverter = ntfyNotificationDispatcherDTOConverter;
}


Expand All @@ -38,4 +46,21 @@ public ResponseEntity<String> setUserEnabled(@RequestBody Set<NotificationDispat
notificationDispatcherService.setNotificationDispatchersForUser(toEnable);
return ResponseEntity.ok("Success");
}


@GetMapping("/config/ntfy")
public NtfyNotificationDispatcherConfigDTO getConfig() {
final NtfyNotificationDispatcherConfig result =
(NtfyNotificationDispatcherConfig) notificationDispatcherService.getUserConfig(NotificationDispatcherName.NTFY)
.orElse(new NtfyNotificationDispatcherConfig());
return ntfyNotificationDispatcherDTOConverter.convertToDTO(result);
}


@PostMapping("/config/ntfy")
public ResponseEntity<String> setConfig(@RequestBody NtfyNotificationDispatcherConfigDTO config) {
final AbstractNotificationDispatcherConfig toSave = ntfyNotificationDispatcherDTOConverter.convertFromDTO(config);
notificationDispatcherService.setUserConfig(NotificationDispatcherName.NTFY, toSave);
return ResponseEntity.ok("Success");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import com.github.mdeluise.plantit.authentication.User;
import com.github.mdeluise.plantit.authentication.UserService;
import com.github.mdeluise.plantit.common.AuthenticatedUserService;
import com.github.mdeluise.plantit.notification.dispatcher.config.AbstractNotificationDispatcherConfig;
import com.github.mdeluise.plantit.notification.dispatcher.config.NotificationDispatcherConfigImplRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -16,14 +19,17 @@ public class NotificationDispatcherService {
private final AuthenticatedUserService authenticatedUserService;
private final UserService userService;
private final List<NotificationDispatcher> notificationDispatchers;
private final NotificationDispatcherConfigImplRepository notificationDispatcherConfigImplRepository;


@Autowired
public NotificationDispatcherService(AuthenticatedUserService authenticatedUserService, UserService userService,
List<NotificationDispatcher> listNotificationDispatchers) {
List<NotificationDispatcher> listNotificationDispatchers,
NotificationDispatcherConfigImplRepository notificationDispatcherConfigImplRepository) {
this.authenticatedUserService = authenticatedUserService;
this.userService = userService;
this.notificationDispatchers = listNotificationDispatchers;
this.notificationDispatcherConfigImplRepository = notificationDispatcherConfigImplRepository;
}


Expand All @@ -46,4 +52,27 @@ public Collection<NotificationDispatcherName> getAvailableNotificationDispatcher
.map(NotificationDispatcher::getName)
.collect(Collectors.toSet());
}


public Optional<AbstractNotificationDispatcherConfig> getUserConfig(NotificationDispatcherName name) {
return notificationDispatcherConfigImplRepository.findByServiceAndUser(
name, authenticatedUserService.getAuthenticatedUser());
}


public void setUserConfig(NotificationDispatcherName name, AbstractNotificationDispatcherConfig updated) {
final User authenticatedUser = authenticatedUserService.getAuthenticatedUser();
final Optional<AbstractNotificationDispatcherConfig> existingConfig =
notificationDispatcherConfigImplRepository.findByServiceAndUser(name, authenticatedUser);
AbstractNotificationDispatcherConfig toSave;
if (existingConfig.isPresent()) {
existingConfig.get().update(updated);
toSave = existingConfig.get();
} else {
updated.setUser(authenticatedUser);
updated.setServiceName(name);
toSave = updated;
}
notificationDispatcherConfigImplRepository.save(toSave);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.mdeluise.plantit.notification.dispatcher.config;

import com.github.mdeluise.plantit.authentication.User;
import com.github.mdeluise.plantit.notification.dispatcher.NotificationDispatcherName;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotNull;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractNotificationDispatcherConfig implements NotificationDispatcherConfig {
@Id
@GeneratedValue
private Long id;
@NotNull
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;
@NotNull
@Enumerated(EnumType.STRING)
private NotificationDispatcherName service;


public Long getId() {
return id;
}


public void setId(Long id) {
this.id = id;
}


@Override
public User getUser() {
return user;
}


@Override
public void setUser(User user) {
this.user = user;
}


@Override
public NotificationDispatcherName getServiceName() {
return service;
}


@Override
public void setServiceName(NotificationDispatcherName name) {
this.service = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.mdeluise.plantit.notification.dispatcher.config;

import com.github.mdeluise.plantit.authentication.User;
import com.github.mdeluise.plantit.notification.dispatcher.NotificationDispatcherName;

public interface NotificationDispatcherConfig {
User getUser();

void setUser(User user);

NotificationDispatcherName getServiceName();

void setServiceName(NotificationDispatcherName name);

void update(NotificationDispatcherConfig updated);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.mdeluise.plantit.notification.dispatcher.config;

import java.util.Optional;

import com.github.mdeluise.plantit.authentication.User;
import com.github.mdeluise.plantit.notification.dispatcher.NotificationDispatcherName;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface NotificationDispatcherConfigImplRepository extends JpaRepository<AbstractNotificationDispatcherConfig, Long> {
Optional<AbstractNotificationDispatcherConfig> findByServiceAndUser(NotificationDispatcherName service, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.mdeluise.plantit.notification.NotifyException;
import com.github.mdeluise.plantit.notification.dispatcher.NotificationDispatcher;
import com.github.mdeluise.plantit.notification.dispatcher.NotificationDispatcherName;
import com.github.mdeluise.plantit.notification.dispatcher.config.NotificationDispatcherConfig;
import com.github.mdeluise.plantit.notification.otp.OtpService;
import com.github.mdeluise.plantit.notification.password.TemporaryPasswordService;
import com.github.mdeluise.plantit.reminder.Reminder;
Expand Down Expand Up @@ -212,6 +213,16 @@ public boolean isEnabled() {
}


@Override
public void loadConfig(NotificationDispatcherConfig config) {
}


@Override
public void initConfig() {
}


private MimeMessageHelper createMessageHelper(MimeMessage mimeMessage, String to, String subject)
throws EmailException {
final MimeMessageHelper helper;
Expand Down
Loading

0 comments on commit a03d0a0

Please sign in to comment.