From 13626ee3da53a5be159bccd065f388c8588333a0 Mon Sep 17 00:00:00 2001 From: Philipp <26636274+PhilippFr@users.noreply.github.com> Date: Fri, 4 Nov 2022 15:26:15 +0100 Subject: [PATCH] feat: change approach to updateMethod method call --- .../api/facade/UploadFacade.java | 3 +++ ...ssageMapper.java => RocketChatMapper.java} | 21 +++++++++++++++++- .../api/service/RocketChatService.java | 22 ++++++++++++++++++- src/main/resources/application.properties | 1 + .../api/service/RocketChatServiceTest.java | 6 +---- 5 files changed, 46 insertions(+), 7 deletions(-) rename src/main/java/de/caritas/cob/uploadservice/api/service/{MessageMapper.java => RocketChatMapper.java} (67%) diff --git a/src/main/java/de/caritas/cob/uploadservice/api/facade/UploadFacade.java b/src/main/java/de/caritas/cob/uploadservice/api/facade/UploadFacade.java index 0250f65..525aa43 100644 --- a/src/main/java/de/caritas/cob/uploadservice/api/facade/UploadFacade.java +++ b/src/main/java/de/caritas/cob/uploadservice/api/facade/UploadFacade.java @@ -151,6 +151,7 @@ private void sanitizeAndEncryptParametersAndUploadToRocketChatRoom( if (doAttachmentE2e) { // TEMP DEV TryCatch TODO: REMOVE try { + /* if (rocketChatService.deleteMessage(rocketChatCredentials, uploadResponse.getMessage().getId())) { SendMessageDto sendMessageDTO = SendMessageDto.builder() @@ -169,6 +170,8 @@ private void sanitizeAndEncryptParametersAndUploadToRocketChatRoom( rocketChatCredentials, sendMessageDTO); log.debug("Post Response: {}", postResponse); } + */ + rocketChatService.setE2eType(rocketChatCredentials, uploadResponse.getMessage().getId()); } catch (Exception e) { log.warn("Exception during E2E attachment message recreation", e); } diff --git a/src/main/java/de/caritas/cob/uploadservice/api/service/MessageMapper.java b/src/main/java/de/caritas/cob/uploadservice/api/service/RocketChatMapper.java similarity index 67% rename from src/main/java/de/caritas/cob/uploadservice/api/service/MessageMapper.java rename to src/main/java/de/caritas/cob/uploadservice/api/service/RocketChatMapper.java index b41f9a2..092bad8 100644 --- a/src/main/java/de/caritas/cob/uploadservice/api/service/MessageMapper.java +++ b/src/main/java/de/caritas/cob/uploadservice/api/service/RocketChatMapper.java @@ -14,7 +14,7 @@ @Service @Slf4j @RequiredArgsConstructor -public class MessageMapper { +public class RocketChatMapper { @SuppressWarnings("java:S2245") // Using pseudorandom number generators (PRNGs) is security-sensitive @@ -40,4 +40,23 @@ public MethodCall deleteMessageOf(String messageId) { return deleteMessage; } + + public MethodCall e2eUpdateMessage(String messageId) { + var params = Map.of("_id", messageId, "t", "e2e"); + + var message = new MethodMessageWithParamMap(); + message.setParams(List.of(params)); + message.setId(random.nextInt(100)); + message.setMethod("updateMessage"); + + var updateMessage = new MethodCall(); + try { + var messageString = objectMapper.writeValueAsString(message); + updateMessage.setMessage(messageString); + } catch (JsonProcessingException e) { + log.error("Serializing {} did not work.", message); + } + + return updateMessage; + } } diff --git a/src/main/java/de/caritas/cob/uploadservice/api/service/RocketChatService.java b/src/main/java/de/caritas/cob/uploadservice/api/service/RocketChatService.java index 2b9f3ab..ad05949 100644 --- a/src/main/java/de/caritas/cob/uploadservice/api/service/RocketChatService.java +++ b/src/main/java/de/caritas/cob/uploadservice/api/service/RocketChatService.java @@ -57,6 +57,9 @@ public class RocketChatService { @Value("${rocket.chat.api.send.message.url}") private String rcSendMessageUrl; + @Value("${rocket.chat.api.update.message.url}") + private String rcUpdateMessageUrl; + @Value("${rocket.chat.header.auth.token}") private String rcHeaderAuthToken; @@ -81,7 +84,7 @@ public class RocketChatService { private final @NonNull RestTemplate restTemplate; private final @NonNull RocketChatCredentialsHelper rcCredentialHelper; private final @NonNull UploadErrorHelper uploadErrorHelper; - private final @NonNull MessageMapper mapper; + private final @NonNull RocketChatMapper mapper; private HttpHeaders getRocketChatHeader(String rcToken, String rcUserId) { HttpHeaders headers = new HttpHeaders(); @@ -205,6 +208,23 @@ public SendMessageResponseDTO sendGroupMessage(RocketChatCredentials rocketChatC } } + public boolean setE2eType(RocketChatCredentials rocketChatCredentials, String messageId) { + String updateMessageUrl = rcUpdateMessageUrl; + var updateMessage = mapper.e2eUpdateMessage(messageId); + var entity = new HttpEntity<>(updateMessage, + getRocketChatHeader(rocketChatCredentials.getRocketChatToken(), + rocketChatCredentials.getRocketChatUserId())); + + try { + var response = restTemplate.postForEntity(updateMessageUrl, entity, + StringifiedMessageResponse.class); + return isSuccessful(response); + } catch (HttpClientErrorException exception) { + log.error("Updating message with attachment E2E type failed.", exception); + return false; + } + } + public boolean deleteMessage(RocketChatCredentials rocketChatCredentials, String messageId) { String deleteUrl = rcDelete; var deleteMessage = mapper.deleteMessageOf(messageId); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 78641bf..551ace0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -30,6 +30,7 @@ rocket.chat.api.user.logout=${rocket.chat.api.url}/logout rocket.chat.api.post.group.messages.read.url=${rocket.chat.api.url}/subscriptions.read rocket.chat.api.rooms.upload.url=${rocket.chat.api.url}/rooms.upload rocket.chat.api.rooms.delete.url=${rocket.chat.api.url}/method.call/deleteMessage +rocket.chat.api.update.message.url=${rocket.chat.api.url}/method.call/updateMessage rocket.chat.api.send.message.url=${rocket.chat.api.url}/chat.sendMessage rocket.technical.username= rocket.credentialscheduler.cron=0 0 0/1 * * ? diff --git a/src/test/java/de/caritas/cob/uploadservice/api/service/RocketChatServiceTest.java b/src/test/java/de/caritas/cob/uploadservice/api/service/RocketChatServiceTest.java index 68d705d..91a86a3 100644 --- a/src/test/java/de/caritas/cob/uploadservice/api/service/RocketChatServiceTest.java +++ b/src/test/java/de/caritas/cob/uploadservice/api/service/RocketChatServiceTest.java @@ -24,7 +24,6 @@ import static de.caritas.cob.uploadservice.helper.TestConstants.RC_UPLOAD_ERROR_RESPONSE_BODY_UNKNOWN_ERROR; import static de.caritas.cob.uploadservice.helper.TestConstants.RC_UPLOAD_ERROR_RESPONSE_DTO_ENTITY_TOO_LARGE; import static de.caritas.cob.uploadservice.helper.TestConstants.RC_UPLOAD_ERROR_RESPONSE_DTO_INVALID_FILE_TYPE; -import static de.caritas.cob.uploadservice.helper.TestConstants.RC_UPLOAD_ERROR_RESPONSE_DTO_SUCCESS; import static de.caritas.cob.uploadservice.helper.TestConstants.RC_UPLOAD_ERROR_RESPONSE_DTO_UNKNOWN_ERROR; import static de.caritas.cob.uploadservice.helper.TestConstants.RC_UPLOAD_ERROR_UNKNOWN_ERROR; import static de.caritas.cob.uploadservice.helper.TestConstants.RC_USER_ID; @@ -53,7 +52,6 @@ import de.caritas.cob.uploadservice.api.service.helper.RocketChatCredentialsHelper; import de.caritas.cob.uploadservice.rocketchat.generated.web.model.FullUploadResponseDto; import java.nio.charset.StandardCharsets; -import liquibase.pro.packaged.M; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -66,8 +64,6 @@ import org.mockito.internal.util.reflection.FieldSetter; import org.mockito.junit.MockitoJUnitRunner; import org.slf4j.Logger; -import org.springframework.amqp.core.Message; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.util.MultiValueMap; @@ -92,7 +88,7 @@ public class RocketChatServiceTest { @Mock private UploadErrorHelper uploadErrorHelper; @Mock - private MessageMapper mapper; + private RocketChatMapper mapper; @Captor private ArgumentCaptor>> mapArgumentCaptor;