From 4653ab9866f6956f4de81fa19cda9e9d0953000d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ckelmann?= Date: Thu, 14 Mar 2024 14:43:45 +0100 Subject: [PATCH] Use user dependent ntfy data --- .../core/api/controller/NtfyController.java | 55 +++++++++++++++++++ .../api/models/users/NtfyInformation.java | 53 ++++++++++++++++++ backend/src/main/java/core/entities/User.java | 37 +++++++++++++ .../main/java/core/services/UserService.java | 10 ++++ .../services/ntfy/TaskSchedulingService.java | 36 ++---------- 5 files changed, 160 insertions(+), 31 deletions(-) create mode 100644 backend/src/main/java/core/api/controller/NtfyController.java create mode 100644 backend/src/main/java/core/api/models/users/NtfyInformation.java diff --git a/backend/src/main/java/core/api/controller/NtfyController.java b/backend/src/main/java/core/api/controller/NtfyController.java new file mode 100644 index 0000000..dd14b76 --- /dev/null +++ b/backend/src/main/java/core/api/controller/NtfyController.java @@ -0,0 +1,55 @@ +package core.api.controller; + +import core.api.models.auth.SimpleStatusResponse; +import core.api.models.users.NtfyInformation; +import core.entities.User; +import core.repositories.UserRepository; +import core.services.UserService; +import core.services.ntfy.TaskSchedulingService; +import org.quartz.SchedulerException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.Optional; + +@CrossOrigin(origins = "*", maxAge = 3600) +@RestController +@RequestMapping("/api") +public class NtfyController { + + @Autowired private UserRepository userRepository; + @Autowired private UserService userService; + @Autowired private TaskSchedulingService taskSchedulingService; + @GetMapping("/ntfy") + public ResponseEntity getNtfyInformation() { + String username = SecurityContextHolder.getContext().getAuthentication().getName(); + + Optional userOptional = userRepository.findByUsername(username); + if(userOptional.isPresent()) { + return ResponseEntity.ok(new NtfyInformation(userOptional.get())); + } else { + return ResponseEntity.status(404).body(new SimpleStatusResponse("failed")); + } + } + + @PostMapping("/ntfy") + public ResponseEntity editNtfyInformation(@Valid @RequestBody NtfyInformation ntfyInformation) { + String username = SecurityContextHolder.getContext().getAuthentication().getName(); + Optional user = userRepository.findByUsername(username); + if(user.isPresent()) { + userService.editNtfyInformation(user.get(), ntfyInformation); + try { + taskSchedulingService.scheduleStartReminders(); + } catch (SchedulerException e) { + throw new RuntimeException(e); + } + return ResponseEntity.ok(new SimpleStatusResponse("success")); + } else { + return ResponseEntity.status(404).body(new SimpleStatusResponse("failed")); + } + } +} diff --git a/backend/src/main/java/core/api/models/users/NtfyInformation.java b/backend/src/main/java/core/api/models/users/NtfyInformation.java new file mode 100644 index 0000000..4aa8726 --- /dev/null +++ b/backend/src/main/java/core/api/models/users/NtfyInformation.java @@ -0,0 +1,53 @@ +package core.api.models.users; + +import core.entities.User; + +public class NtfyInformation { + + private String ntfy_host; + private String ntfy_topic; + private String ntfy_user; + private String ntfy_token; + + public NtfyInformation(User user) { + this.ntfy_host = user.getNtfy_host(); + this.ntfy_topic = user.getNtfy_topic(); + this.ntfy_user = "****"; + this.ntfy_token = "****"; + } + + public NtfyInformation() { + } + + public String getNtfy_host() { + return ntfy_host; + } + + public void setNtfy_host(String ntfy_host) { + this.ntfy_host = ntfy_host; + } + + public String getNtfy_topic() { + return ntfy_topic; + } + + public void setNtfy_topic(String ntfy_topic) { + this.ntfy_topic = ntfy_topic; + } + + public String getNtfy_user() { + return ntfy_user; + } + + public void setNtfy_user(String ntfy_user) { + this.ntfy_user = ntfy_user; + } + + public String getNtfy_token() { + return ntfy_token; + } + + public void setNtfy_token(String ntfy_token) { + this.ntfy_token = ntfy_token; + } +} diff --git a/backend/src/main/java/core/entities/User.java b/backend/src/main/java/core/entities/User.java index 3b0849c..68271d4 100644 --- a/backend/src/main/java/core/entities/User.java +++ b/backend/src/main/java/core/entities/User.java @@ -42,6 +42,11 @@ public class User { inverseJoinColumns = @JoinColumn(name = "role_id")) private Set roles = new HashSet<>(); + private String ntfy_host; + private String ntfy_topic; + private String ntfy_username; + private String ntfy_token; + public User() { } @@ -98,6 +103,38 @@ public class User { this.roles = roles; } + public String getNtfy_host() { + return ntfy_host; + } + + public void setNtfy_host(String ntfy_host) { + this.ntfy_host = ntfy_host; + } + + public String getNtfy_topic() { + return ntfy_topic; + } + + public void setNtfy_topic(String ntfy_topic) { + this.ntfy_topic = ntfy_topic; + } + + public String getNtfy_username() { + return ntfy_username; + } + + public void setNtfy_username(String ntfy_username) { + this.ntfy_username = ntfy_username; + } + + public String getNtfy_token() { + return ntfy_token; + } + + public void setNtfy_token(String ntfy_token) { + this.ntfy_token = ntfy_token; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/backend/src/main/java/core/services/UserService.java b/backend/src/main/java/core/services/UserService.java index e149fbe..5d6ca7b 100644 --- a/backend/src/main/java/core/services/UserService.java +++ b/backend/src/main/java/core/services/UserService.java @@ -3,6 +3,7 @@ package core.services; import core.api.models.account.AccountDeleteRequest; import core.api.models.account.EmailChangeRequest; import core.api.models.account.PasswordChangeRequest; +import core.api.models.users.NtfyInformation; import core.api.models.users.UserAddInfo; import core.api.models.users.UserInfo; import core.api.models.users.UserUpdateInfo; @@ -152,4 +153,13 @@ public class UserService { userRepository.deleteByUsername(username); return 0; } + + public void editNtfyInformation(User user, NtfyInformation ntfyInformation) { + user.setNtfy_host(ntfyInformation.getNtfy_host()); + user.setNtfy_topic(ntfyInformation.getNtfy_topic()); + user.setNtfy_username(ntfyInformation.getNtfy_user()); + user.setNtfy_token(ntfyInformation.getNtfy_token()); + + userRepository.save(user); + } } diff --git a/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java b/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java index ca34cb9..e8fe225 100644 --- a/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java +++ b/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java @@ -22,12 +22,6 @@ import java.util.List; @Service public class TaskSchedulingService { - @Value("${ntfy.host}") - private String ntfy_host; - - @Value("${ntfy.topic}") - private String ntfy_topic; - @Value("${frontend.domain}") private String frontend_domain; @@ -43,8 +37,8 @@ public class TaskSchedulingService { .withIdentity(String.valueOf(advancedTaskSchedule.getScheduleID())) .build(); job.getJobDataMap().put("task", advancedTaskSchedule.getTask().getTaskName()); - job.getJobDataMap().put("ntfy_host", ntfy_host); - job.getJobDataMap().put("ntfy_topic", ntfy_topic); + job.getJobDataMap().put("ntfy_host", advancedTaskSchedule.getTask().getTaskgroup().getUser().getNtfy_host()); + job.getJobDataMap().put("ntfy_topic", advancedTaskSchedule.getTask().getTaskgroup().getUser().getNtfy_topic()); job.getJobDataMap().put("frontend_domain", frontend_domain); Trigger trigger = TriggerBuilder.newTrigger().startAt(calculateDelayInMillis(advancedTaskSchedule.getScheduleStart())).build(); @@ -52,28 +46,6 @@ public class TaskSchedulingService { scheduler.scheduleJob(job, trigger); } - public static void scheduleTask() throws SchedulerException { - Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); - scheduler.start(); - - JobDetail job = JobBuilder.newJob(NtfyTask.class).build(); - - job.getJobDataMap().put("data", "A simple Job 1 Message"); - - LocalDateTime executionTime = LocalDateTime.of(2024, 3, 14, 9, 18); // Example: March 15, 2024, 10:00 AM - - Trigger trigger = TriggerBuilder.newTrigger() - .withIdentity("trigger1", "Group 1") - .startAt(calculateDelayInMillis(executionTime)).build(); - Trigger trigger2 = TriggerBuilder.newTrigger() - .withIdentity("trigger2", "Group 1") - .startAt(calculateDelayInMillis(executionTime.plusMinutes(1))).build(); - - Trigger immediatly = TriggerBuilder.newTrigger().withIdentity("Immediately", "Group 1").startNow().build(); - - scheduler.scheduleJob(job, immediatly); - } - private static Date calculateDelayInMillis(LocalDateTime executionTime) { return Date.from(executionTime.atZone(ZoneId.systemDefault()).toInstant()); } @@ -82,7 +54,9 @@ public class TaskSchedulingService { public void scheduleStartReminders() throws SchedulerException { List advancedTaskSchedules = taskScheduleService.findSchedulesByDate(LocalDate.now()); for(AdvancedTaskSchedule advancedTaskSchedule : advancedTaskSchedules) { - scheduleStartingTask(advancedTaskSchedule); + if(advancedTaskSchedule.getTask().getTaskgroup().getUser().getNtfy_host() != null) { + scheduleStartingTask(advancedTaskSchedule); + } } }