Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#279] Add update delivery state message accessor #339

Merged
merged 1 commit into from Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions backend/avro/communication/BUILD
Expand Up @@ -8,15 +8,24 @@ avro_java_library(
)

avro_java_library(
name = "message",
name = "webhook",
)

avro_java_library(
name = "webhook",
name = "read-receipt",
)

custom_java_library(
name = "message",
srcs = ["message/src/main/java/co/airy/avro/communication/MessageRepository.java"],
exports = [":message-avro"],
deps = [":message-avro"],
)

avro_java_library(
name = "read-receipt",
name = "message-avro",
srcs = ["message.avsc"],
visibility = ["//visibility:private"],
)

custom_java_library(
Expand Down
@@ -0,0 +1,11 @@
package co.airy.avro.communication;

import java.time.Instant;

public class MessageRepository {
public static Message updateDeliveryState(Message message, DeliveryState state) {
message.setDeliveryState(state);
message.setUpdatedAt(Instant.now().toEpochMilli());
return message;
}
}
Expand Up @@ -26,6 +26,8 @@

import java.util.concurrent.ExecutionException;

import static co.airy.avro.communication.MessageRepository.updateDeliveryState;

@Component
public class Stores implements HealthIndicator, ApplicationListener<ApplicationStartedEvent>, DisposableBean {
private static final Logger log = AiryLoggerFactory.getLogger(Stores.class);
Expand Down Expand Up @@ -60,7 +62,7 @@ private void startStream() {
&& message.getDeliveryState().equals(DeliveryState.PENDING)
)
.mapValues((messageId, message) -> {
message.setDeliveryState(DeliveryState.DELIVERED);
updateDeliveryState(message, DeliveryState.DELIVERED);
try {
webSocketController.onNewMessage(message);
} catch (Exception e) {
Expand Down
Expand Up @@ -25,6 +25,8 @@

import java.time.Instant;

import static co.airy.avro.communication.MessageRepository.updateDeliveryState;

@Component
public class Sender implements DisposableBean, ApplicationListener<ApplicationReadyEvent> {
private static final Logger log = AiryLoggerFactory.getLogger(Sender.class);
Expand Down Expand Up @@ -88,21 +90,15 @@ private Message sendMessage(SendMessageRequest sendMessageRequest) {

api.sendMessage(pageToken, fbSendMessagePayload);

//TODO move the change state logic to backend/avro/message
message.setDeliveryState(DeliveryState.DELIVERED);
message.setUpdatedAt(Instant.now().toEpochMilli());

updateDeliveryState(message, DeliveryState.DELIVERED);
return message;
} catch (ApiException e) {
log.error(String.format("Failed to send a message to Facebook \n SendMessageRequest: %s \n FB Error Message: %s \n", sendMessageRequest, e.getMessage()), e);
} catch (Exception e) {
log.error(String.format("Failed to send a message to Facebook \n SendMessageRequest: %s", sendMessageRequest), e);
}

//TODO move the change state logic to backend/avro/message
message.setDeliveryState(DeliveryState.FAILED);
message.setUpdatedAt(Instant.now().toEpochMilli());

updateDeliveryState(message, DeliveryState.FAILED);
return message;
}

Expand Down
Expand Up @@ -22,6 +22,8 @@

import java.time.Instant;

import static co.airy.avro.communication.MessageRepository.updateDeliveryState;

@Component
public class Sender implements DisposableBean, ApplicationListener<ApplicationReadyEvent> {
private static final Logger log = AiryLoggerFactory.getLogger(Sender.class);
Expand Down Expand Up @@ -72,21 +74,15 @@ private Message sendMessage(SendMessageRequest sendMessageRequest) {

api.sendMessage(sendMessageRequest.getSourceConversationId(), sendMessagePayload);

// TODO move the change state logic to backend/avro/message
message.setUpdatedAt(Instant.now().toEpochMilli());
message.setDeliveryState(DeliveryState.DELIVERED);

updateDeliveryState(message, DeliveryState.DELIVERED);
return message;
} catch (ApiException e) {
log.error(String.format("Failed to send a message to Facebook \n SendMessageRequest: %s \n FB Error Message: %s \n", sendMessageRequest, e.getMessage()), e);
} catch (Exception e) {
log.error(String.format("Failed to send a message to Facebook \n SendMessageRequest: %s", sendMessageRequest), e);
}

// TODO move the change state logic to backend/avro/message
message.setDeliveryState(DeliveryState.FAILED);
message.setUpdatedAt(Instant.now().toEpochMilli());

updateDeliveryState(message, DeliveryState.FAILED);
return message;
}

Expand Down