Refactor form submission to use dynamic Discord URLs and emails
Updated form classes to return Optional URLs for Discord bot submissions. Refactored VerifyController to handle these Optionals and improved error handling when sending forms. Added receiver email method in form classes for more flexible form submissions.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.alttd.forms.verify_mail;
|
||||
|
||||
import com.alttd.forms.form.Form;
|
||||
import com.alttd.forms.mail.mail_forms.MailForm;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import jakarta.validation.Valid;
|
||||
@@ -18,6 +19,7 @@ import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@RestController
|
||||
@@ -32,34 +34,14 @@ public class VerifyController {
|
||||
logger.trace("verificationData: {}", verificationData);
|
||||
return new FormQuery().getFormForCode(verificationData.code, verificationData.eMail).thenApply(result -> result.form()
|
||||
.map(form -> {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
//Discord bot url
|
||||
.uri(new URI(form.getDiscordBotUrl()))//TODO get uri from form
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(form.toJsonString(), StandardCharsets.UTF_8))
|
||||
.build();
|
||||
logger.trace("request: {}", request);
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
logger.trace("response: {}", response);
|
||||
if (response.statusCode() < 200 || response.statusCode() > 200) {
|
||||
logger.error(String.format("Failed to send form to Discord. Got status code [%d], with body\n%s", response.statusCode(), response.body()));
|
||||
//TODO handle failure
|
||||
//TODO strings to config
|
||||
return ResponseEntity.ok("Failed to send form to Discord, please contact us at [email protected]");
|
||||
Optional<String> discordBotUrl = form.getDiscordBotUrl();
|
||||
if (discordBotUrl.isPresent()){
|
||||
Optional<ResponseEntity<String>> stringResponseEntity = sendDiscordMessage(discordBotUrl.get(), form);
|
||||
if (stringResponseEntity.isPresent()){
|
||||
return stringResponseEntity.get();
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
logger.error("Unable to create URI for posting form", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to create URI for posting form, please contact us at [email protected]");
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Unable to send form to Discord, invalid JSON", e);
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]");
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Unable to send form to Discord", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]");
|
||||
}
|
||||
MailForm.sendForm("[email protected]", form);
|
||||
MailForm.sendForm(form.getReceiver(), form);
|
||||
try {
|
||||
return ResponseEntity.ok(form.toJsonString());
|
||||
} catch (JsonProcessingException e) {
|
||||
@@ -71,4 +53,34 @@ public class VerifyController {
|
||||
).exceptionally(throwable -> ResponseEntity.internalServerError()
|
||||
.body("The server was unable to process your request, if this issue persists please contact [email protected]"));
|
||||
}
|
||||
|
||||
private Optional<ResponseEntity<String>> sendDiscordMessage(String discordBotUrl, Form form) {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI(discordBotUrl))
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(form.toJsonString(), StandardCharsets.UTF_8))
|
||||
.build();
|
||||
logger.trace("request: {}", request);
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
logger.trace("response: {}", response);
|
||||
if (response.statusCode() < 200 || response.statusCode() > 200) {
|
||||
logger.error(String.format("Failed to send form to Discord. Got status code [%d], with body\n%s", response.statusCode(), response.body()));
|
||||
//TODO handle failure
|
||||
//TODO strings to config
|
||||
return Optional.of(ResponseEntity.ok("Failed to send form to Discord, please contact us at [email protected]"));
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
logger.error("Unable to create URI for posting form", e); //TODO more clear
|
||||
return Optional.of(ResponseEntity.ok("Unable to create URI for posting form, please contact us at [email protected]"));
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Unable to send form to Discord, invalid JSON", e);
|
||||
return Optional.of(ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]"));
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Unable to send form to Discord", e); //TODO more clear
|
||||
return Optional.of(ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]"));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user