-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add gotify notification (#260)
- Loading branch information
Showing
20 changed files
with
520 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
public enum NotificationDispatcherName { | ||
CONSOLE, | ||
EMAIL, | ||
GOTIFY, | ||
NTFY | ||
} |
98 changes: 98 additions & 0 deletions
98
...in/java/com/github/mdeluise/plantit/notification/gotify/GotifyNotificationDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.github.mdeluise.plantit.notification.gotify; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
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.reminder.Reminder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class GotifyNotificationDispatcher implements NotificationDispatcher { | ||
private final boolean enabled; | ||
private String url; | ||
private String token; | ||
private final Logger logger = LoggerFactory.getLogger(GotifyNotificationDispatcher.class); | ||
|
||
|
||
public GotifyNotificationDispatcher(@Value("${server.notification.gotify.enabled}") boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
|
||
public void notifyReminder(Reminder reminder) throws NotifyException { | ||
final HttpClient httpClient = HttpClient.newHttpClient(); | ||
final URI uri = URI.create(url).resolve("/message?token=" + token); | ||
final String title = reminder.getTarget().getInfo().getPersonalName(); | ||
final String message = "Time to take care of " + title + ", action required: " + reminder.getAction(); | ||
final String body = String.format("{\"title\": \"%s\", \"message\": \"%s\"}", title, message); | ||
|
||
final HttpRequest request = HttpRequest.newBuilder() | ||
.uri(uri) | ||
.header(HttpHeaders.CONTENT_TYPE, "application/json") | ||
.POST(HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8)) | ||
.build(); | ||
|
||
try { | ||
final HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||
|
||
final int statusCode = response.statusCode(); | ||
if (statusCode < 200 || statusCode >= 300) { | ||
throw new NotifyException("Failed to send notification. Response code: " + statusCode + | ||
" Response body: " + response.body()); | ||
} | ||
} catch (Exception e) { | ||
throw new NotifyException(e); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public NotificationDispatcherName getName() { | ||
return NotificationDispatcherName.GOTIFY; | ||
} | ||
|
||
|
||
@Override | ||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
|
||
@Override | ||
public void loadConfig(NotificationDispatcherConfig config) { | ||
if (!(config instanceof GotifyNotificationDispatcherConfig gotifyConfig)) { | ||
throw new UnsupportedOperationException( | ||
"Configuration provided must be of type GotifyNotificationDispatcherConfig"); | ||
} | ||
checkConfigParameters(gotifyConfig); | ||
url = gotifyConfig.getUrl(); | ||
token = gotifyConfig.getToken(); | ||
} | ||
|
||
|
||
@Override | ||
public void initConfig() { | ||
url = null; | ||
token = null; | ||
} | ||
|
||
|
||
private void checkConfigParameters(GotifyNotificationDispatcherConfig gotifyConfig) { | ||
if (gotifyConfig.getUrl() == null || gotifyConfig.getToken() == null) { | ||
final String errorMsg = "Gotify url and token must be provided."; | ||
logger.error(errorMsg); | ||
throw new IllegalArgumentException(errorMsg); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...a/com/github/mdeluise/plantit/notification/gotify/GotifyNotificationDispatcherConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.github.mdeluise.plantit.notification.gotify; | ||
|
||
import com.github.mdeluise.plantit.notification.dispatcher.config.AbstractNotificationDispatcherConfig; | ||
import com.github.mdeluise.plantit.notification.dispatcher.config.NotificationDispatcherConfig; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "gotify_notification_configs") | ||
public class GotifyNotificationDispatcherConfig extends AbstractNotificationDispatcherConfig { | ||
private String url; | ||
private String token; | ||
|
||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
|
||
public void setToken(String token) { | ||
this.token = token; | ||
} | ||
|
||
|
||
@Override | ||
public void update(NotificationDispatcherConfig updated) { | ||
if (!(updated instanceof GotifyNotificationDispatcherConfig)) { | ||
throw new UnsupportedOperationException("Updated class must be of type GotifyNotificationDispatcherConfig"); | ||
} | ||
final GotifyNotificationDispatcherConfig gotifyUpdated = (GotifyNotificationDispatcherConfig) updated; | ||
this.setUrl(gotifyUpdated.getUrl()); | ||
this.setToken(gotifyUpdated.getToken()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...om/github/mdeluise/plantit/notification/gotify/GotifyNotificationDispatcherConfigDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.github.mdeluise.plantit.notification.gotify; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
@Schema(name = "Gotify notification config", description = "Represents the gotify notifier configuration") | ||
public class GotifyNotificationDispatcherConfigDTO { | ||
@Schema(description = "id of the config", accessMode = Schema.AccessMode.READ_ONLY) | ||
private String id; | ||
@Schema(description = "url of the gotify server") | ||
private String url; | ||
@Schema(description = "token for auth in the gotify server") | ||
private String token; | ||
|
||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
|
||
public String getToken() { | ||
return token; | ||
} | ||
|
||
|
||
public void setToken(String token) { | ||
this.token = token; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...github/mdeluise/plantit/notification/gotify/GotifyNotificationDispatcherDTOConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.mdeluise.plantit.notification.gotify; | ||
|
||
import com.github.mdeluise.plantit.common.AbstractDTOConverter; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class GotifyNotificationDispatcherDTOConverter extends | ||
AbstractDTOConverter<GotifyNotificationDispatcherConfig, GotifyNotificationDispatcherConfigDTO> { | ||
public GotifyNotificationDispatcherDTOConverter(ModelMapper modelMapper) { | ||
super(modelMapper); | ||
} | ||
|
||
|
||
@Override | ||
public GotifyNotificationDispatcherConfig convertFromDTO( | ||
GotifyNotificationDispatcherConfigDTO dto) { | ||
return modelMapper.map(dto, GotifyNotificationDispatcherConfig.class); | ||
} | ||
|
||
|
||
@Override | ||
public GotifyNotificationDispatcherConfigDTO convertToDTO( | ||
GotifyNotificationDispatcherConfig data) { | ||
return modelMapper.map(data, GotifyNotificationDispatcherConfigDTO.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.