issue-86 #102
@ -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<User> 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> 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"));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -42,6 +42,11 @@ public class User {
 | 
			
		||||
                inverseJoinColumns = @JoinColumn(name = "role_id"))
 | 
			
		||||
    private Set<RoleEntity> 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;
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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,9 +54,11 @@ public class TaskSchedulingService {
 | 
			
		||||
    public void scheduleStartReminders() throws SchedulerException {
 | 
			
		||||
        List<AdvancedTaskSchedule> advancedTaskSchedules = taskScheduleService.findSchedulesByDate(LocalDate.now());
 | 
			
		||||
        for(AdvancedTaskSchedule advancedTaskSchedule : advancedTaskSchedules) {
 | 
			
		||||
            if(advancedTaskSchedule.getTask().getTaskgroup().getUser().getNtfy_host() != null) {
 | 
			
		||||
                scheduleStartingTask(advancedTaskSchedule);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void stopStartReminderNotification(AdvancedTaskSchedule schedule) throws SchedulerException {
 | 
			
		||||
        scheduler.deleteJob(JobKey.jobKey(String.valueOf(schedule.getScheduleID())));
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user