Skip to content

Commit

Permalink
Merge pull request #53 from wireapp/staging
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
dkovacevic authored Apr 16, 2021
2 parents ca98ae2 + 4abcc35 commit efb3fca
Show file tree
Hide file tree
Showing 29 changed files with 733 additions and 157 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
- db

db:
image: postgres:13.1
image: postgres:13
# just for local development
environment:
- POSTGRES_USER=roman
Expand Down
Binary file removed libs/jmx_prometheus_javaagent.jar
Binary file not shown.
3 changes: 0 additions & 3 deletions libs/metrics.yaml

This file was deleted.

7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<artifactId>roman</artifactId>
<groupId>com.wire.bots</groupId>
<version>0.2.1</version>
<version>0.2.3</version>

<repositories>
<repository>
Expand All @@ -19,7 +19,8 @@
</repositories>

<properties>
<lithium.version>3.0.4</lithium.version>
<lithium.version>3.0.11</lithium.version>
<dropwizard.version>2.0.15</dropwizard.version>
<maven.test.skip>true</maven.test.skip>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand All @@ -34,7 +35,7 @@
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<version>2.0.12</version>
<version>${dropwizard.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
11 changes: 10 additions & 1 deletion roman.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
server:
applicationConnectors:
- type: http
port: ${HTTP_PORT:-8080}
maxRequestHeaderSize: 16KiB

gzip:
bufferSize: 8KiB
enabled: true

requestLog:
appenders:
- type: ${APPENDER_TYPE:-console}
Expand All @@ -16,8 +25,8 @@ logging:
swagger:
resourcePackage: com.wire.bots.roman.resources
schemes:
# - http
- https
- http

jerseyClient:
timeout: 40s
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/wire/bots/roman/CachedClientRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public WireClient getClient(UUID botId) {
API api = new API(httpClient, state.token);
return new _BotClient(state, crypto, api);
} catch (Exception e) {
Logger.warning("CachedClientRepo: bot: %s %s", botId, e);
Logger.exception("CachedClientRepo: bot: %s %s", e, botId, e.getMessage());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public UUID map(ResultSet rs, int columnNumber, StatementContext ctx) {
try {
return getUuid(rs, "uuid");
} catch (SQLException e) {
Logger.error("UUIDMapper: i: %d, e: %s", columnNumber, e);
Logger.exception("UUIDMapper: i: %d, e: %s", e, columnNumber, e.getMessage());
return null;
}
}
Expand Down
111 changes: 73 additions & 38 deletions src/main/java/com/wire/bots/roman/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import com.wire.bots.roman.DAO.BotsDAO;
import com.wire.bots.roman.DAO.BroadcastDAO;
import com.wire.bots.roman.DAO.ProvidersDAO;
import com.wire.bots.roman.model.IncomingMessage;
import com.wire.bots.roman.model.OutgoingMessage;
import com.wire.bots.roman.model.Poll;
import com.wire.bots.roman.model.Provider;
import com.wire.bots.roman.model.*;
import com.wire.lithium.server.monitoring.MDCUtils;
import com.wire.xenon.MessageHandlerBase;
import com.wire.xenon.WireClient;
import com.wire.xenon.assets.DeliveryReceipt;
Expand Down Expand Up @@ -37,6 +35,7 @@
public class MessageHandler extends MessageHandlerBase {

private static final int TOKEN_DURATION = 20;
private final ObjectMapper mapper = new ObjectMapper();
private final Client jerseyClient;
private final ProvidersDAO providersDAO;
private final BotsDAO botsDAO;
Expand Down Expand Up @@ -71,8 +70,7 @@ public boolean onNewBot(NewBot newBot, String auth) {

@Override
public void onNewConversation(WireClient client, SystemMessage msg) {
UUID botId = client.getId();

final UUID botId = client.getId();
validate(botId);

OutgoingMessage message = new OutgoingMessage();
Expand All @@ -86,16 +84,14 @@ public void onNewConversation(WireClient client, SystemMessage msg) {

boolean send = send(message);
if (!send)
Logger.warning("onNewConversation: failed to deliver message to: bot: %s", botId);
Logger.warning("onNewConversation: failed to deliver message");
}

@Override
public void onText(WireClient client, TextMessage msg) {
final String type = "conversation.new_text";

UUID botId = client.getId();

final UUID botId = client.getId();
validate(botId);
final String type = "conversation.new_text";

OutgoingMessage message = getOutgoingMessage(botId, type, msg);
message.conversationId = client.getConversationId();
Expand Down Expand Up @@ -145,7 +141,7 @@ public void onImage(WireClient client, ImageMessage msg) {

send(message);
} catch (Exception e) {
Logger.error("onImage: %s %s", botId, e);
Logger.exception("onImage: %s", e, e.getMessage());
}
}

Expand All @@ -170,7 +166,35 @@ public void onAttachment(WireClient client, AttachmentMessage msg) {

send(message);
} catch (Exception e) {
Logger.error("onAttachment: %s %s", botId, e);
Logger.exception("onAttachment: %s", e, e.getMessage());
}
}

@Override
public void onAudio(WireClient client, AudioMessage msg) {
final String type = "conversation.audio.new";

UUID botId = client.getId();

validate(botId);

try {
OutgoingMessage message = getOutgoingMessage(botId, type, msg);

byte[] img = client.downloadAsset(msg.getAssetKey(),
msg.getAssetToken(),
msg.getSha256(),
msg.getOtrKey());
message.attachment = Base64.getEncoder().encodeToString(img);
message.text = msg.getName();
message.mimeType = msg.getMimeType();
message.duration = msg.getDuration();
message.levels = msg.getLevels();
message.conversationId = client.getConversationId();

send(message);
} catch (Exception e) {
Logger.exception("onAudio: %s", e, e.getMessage());
}
}

Expand All @@ -196,7 +220,7 @@ public void onConfirmation(WireClient client, ConfirmationMessage msg) {

broadcastDAO.insertStatus(messageId, type == ConfirmationMessage.Type.DELIVERED ? DELIVERED.ordinal() : READ.ordinal());
} catch (Exception e) {
Logger.error("onConfirmation: %s %s", client.getId(), e);
Logger.exception("onConfirmation: %s", e, e.getMessage());
}
}

Expand Down Expand Up @@ -224,7 +248,7 @@ private void onComposite(UUID botId, UUID userId, Messages.GenericMessage event)
}

if (!send(message)) {
Logger.warning("onEvent: failed to deliver message to bot: %s", botId);
Logger.warning("onComposite: failed to deliver message");
}
}

Expand All @@ -243,13 +267,32 @@ private void onButtonAction(UUID botId, UUID userId, Messages.GenericMessage eve
message.poll.offset = Integer.parseInt(action.getButtonId());

if (!send(message)) {
Logger.warning("onEvent: failed to deliver message to bot: %s", botId);
Logger.warning("onButtonAction: failed to deliver message");
}
}

@Override
public void onCalling(WireClient client, CallingMessage msg) {
try {
final String type = "conversation.call";

final UUID botId = client.getId();

validate(botId);

OutgoingMessage message = getOutgoingMessage(botId, type, msg);
message.conversationId = client.getConversationId();
message.call = mapper.readValue(msg.getContent(), Call.class);

send(message);
} catch (Exception e) {
Logger.exception("onCalling: error: %s", e, e.getMessage());
}
}

@Override
public void onMemberJoin(WireClient client, SystemMessage msg) {
UUID botId = client.getId();
final UUID botId = client.getId();
validate(botId);

OutgoingMessage message = new OutgoingMessage();
Expand All @@ -265,7 +308,7 @@ public void onMemberJoin(WireClient client, SystemMessage msg) {
message.handle = user.handle;
send(message);
} catch (Exception e) {
Logger.error("onMemberJoin: %s %s", botId, e);
Logger.exception("onMemberJoin: %s", e, e.getMessage());
}
}
}
Expand All @@ -279,7 +322,7 @@ public void onBotRemoved(UUID botId, SystemMessage msg) {
message.type = "conversation.bot_removed";

if (!send(message))
Logger.warning("onBotRemoved: failed to deliver message to: bot: %s", botId);
Logger.warning("onBotRemoved: failed to deliver message");

botsDAO.remove(botId);
}
Expand All @@ -295,13 +338,12 @@ private OutgoingMessage getOutgoingMessage(UUID botId, String type, MessageBase
}

private boolean send(OutgoingMessage message) {
UUID providerId = botsDAO.getProviderId(message.botId);
final UUID providerId = validate(message.botId);

try {
Provider provider = providersDAO.get(providerId);
if (provider == null) {
Logger.error("MessageHandler.send: provider == null. providerId: %s, bot: %s",
providerId, message.botId);
Logger.error("MessageHandler.send: Unknown provider");
return false;
}

Expand All @@ -314,21 +356,12 @@ private boolean send(OutgoingMessage message) {
.header("Authorization", "Bearer " + provider.serviceAuth)
.post(Entity.entity(message, MediaType.APPLICATION_JSON));

Logger.debug("MessageHandler.send: Sent: `%s` bot: %s, provider: %s, status: %d",
message.type,
message.botId,
providerId,
post.getStatus());
Logger.debug("MessageHandler.send: Sent: `%s` status: %d", message.type, post.getStatus());

if (post.hasEntity()) {
final IncomingMessage incomingMessage = getIncomingMessage(post);
if (incomingMessage != null) {
Logger.debug("MessageHandler.send: `%s` bot: %s, provider: %s, posting IncomingMessage: type: %s",
message.type,
message.botId,
providerId,
incomingMessage.type
);
if (incomingMessage != null && incomingMessage.type != null) {
Logger.debug("MessageHandler.send: Posting `%s` into conversation.", incomingMessage.type);
sender.send(incomingMessage, message.botId);
}
}
Expand All @@ -338,7 +371,7 @@ private boolean send(OutgoingMessage message) {
return WebSocket.send(provider.id, message);
}
} catch (Exception e) {
Logger.error("MessageHandler.send: bot: %s, provider: %s, error %s", message.botId, providerId, e);
Logger.exception("MessageHandler.send: error %s", e, e.getMessage());
return false;
}
}
Expand All @@ -355,9 +388,9 @@ private void sendDeliveryReceipt(WireClient client, UUID messageId, UUID userId)
try {
client.send(new DeliveryReceipt(messageId), userId);
} catch (Exception e) {
Logger.error("sendDeliveryReceipt: failed to deliver the receipt for message: %s, bot: %s",
Logger.exception("sendDeliveryReceipt: failed to deliver the receipt for message: %s",
e,
client.getId());
e.getMessage());
}
}

Expand All @@ -376,17 +409,19 @@ private Provider getProvider(String auth) {
final Provider provider = providersDAO.getByAuth(auth);
if (provider == null)
throw new RuntimeException("Unknown auth");
MDCUtils.put("providerId", provider.id);
return provider;
}

private UUID validate(UUID botId) {
UUID providerId = botsDAO.getProviderId(botId);
if (providerId == null)
throw new RuntimeException("Unknown botId: " + botId.toString());
MDCUtils.put("providerId", providerId);
return providerId;
}

public void setSender(Sender sender) {
this.sender = sender;
}
}
}
Loading

0 comments on commit efb3fca

Please sign in to comment.