Skip to content

Commit

Permalink
feat: change approach to updateMethod method call
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippFr committed Nov 4, 2022
1 parent d710d58 commit 13626ee
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Service
@Slf4j
@RequiredArgsConstructor
public class MessageMapper {
public class RocketChatMapper {

@SuppressWarnings("java:S2245")
// Using pseudorandom number generators (PRNGs) is security-sensitive
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -92,7 +88,7 @@ public class RocketChatServiceTest {
@Mock
private UploadErrorHelper uploadErrorHelper;
@Mock
private MessageMapper mapper;
private RocketChatMapper mapper;

@Captor
private ArgumentCaptor<HttpEntity<MultiValueMap<String, Object>>> mapArgumentCaptor;
Expand Down

0 comments on commit 13626ee

Please sign in to comment.