Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
dkovacevic committed Feb 18, 2021
2 parents 2e72147 + ab9b54d commit d191a65
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 71 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<artifactId>dropwizard-websockets</artifactId>
<version>1.3.14</version>
</dependency>
<dependency>
<groupId>io.dropwizard-bundles</groupId>
<artifactId>dropwizard-redirect-bundle</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
2 changes: 1 addition & 1 deletion roman.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ logging:
swagger:
resourcePackage: com.wire.bots.roman.resources
schemes:
- http
# - http
- https

jerseyClient:
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/wire/bots/roman/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.wire.xenon.MessageHandlerBase;
import com.wire.xenon.factories.CryptoFactory;
import com.wire.xenon.factories.StorageFactory;
import io.dropwizard.bundles.redirect.PathRedirect;
import io.dropwizard.bundles.redirect.RedirectBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.websockets.WebsocketBundle;
Expand Down Expand Up @@ -61,6 +63,7 @@ public void initialize(Bootstrap<Config> bootstrap) {

bootstrap.addBundle(new WebsocketBundle(WebSocket.class));
bootstrap.addCommand(new UpdateCertCommand());
bootstrap.addBundle(new RedirectBundle(new PathRedirect("/", "/swagger#/default")));
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/wire/bots/roman/DAO/BotsDAO.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.wire.bots.roman.DAO;

import com.wire.bots.roman.DAO.mappers.BotsMapper;
import com.wire.bots.roman.DAO.mappers.UUIDMapper;
import org.jdbi.v3.sqlobject.config.RegisterColumnMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
Expand All @@ -15,12 +15,12 @@ int insert(@Bind("bot") UUID bot,
@Bind("provider") UUID provider);

@SqlQuery("SELECT provider AS uuid FROM Bots WHERE id = :bot")
@RegisterColumnMapper(BotsMapper.class)
@RegisterColumnMapper(UUIDMapper.class)
UUID getProviderId(@Bind("bot") UUID bot);


@SqlQuery("SELECT id AS uuid FROM Bots WHERE provider = :providerId")
@RegisterColumnMapper(BotsMapper.class)
@RegisterColumnMapper(UUIDMapper.class)
List<UUID> getBotIds(@Bind("providerId") UUID providerId);

@SqlUpdate("DELETE FROM Bots WHERE id = :botId")
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/wire/bots/roman/DAO/BroadcastDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.wire.bots.roman.DAO;

import com.wire.bots.roman.DAO.mappers.UUIDMapper;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.sqlobject.config.RegisterColumnMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

import javax.annotation.Nullable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

public interface BroadcastDAO {
@SqlUpdate("INSERT INTO Broadcast (broadcast_id, bot_id, provider, message_status, message_id) " +
"VALUES (:broadcastId, :botId, :provider, :status, :messageId) " +
"ON CONFLICT(broadcast_id, message_id, message_status) DO NOTHING")
int insert(@Bind("broadcastId") UUID broadcastId,
@Bind("botId") UUID botId,
@Bind("provider") UUID provider,
@Bind("messageId") UUID messageId,
@Bind("status") int status);

@SqlUpdate("INSERT INTO Broadcast (broadcast_id, bot_id, provider, message_status, message_id) " +
"(SELECT B.broadcast_id, B.bot_id, B.provider, :status, B.message_id " +
"FROM Broadcast AS B " +
"WHERE message_id = :messageId FETCH FIRST ROW ONLY)")
int insertStatus(@Bind("messageId") UUID messageId, @Bind("status") int status);

@SqlQuery("SELECT message_status, count(*) AS count " +
"FROM Broadcast " +
"WHERE broadcast_id = :broadcastId " +
"GROUP BY message_status")
@RegisterColumnMapper(ReportMapper.class)
List<Pair> report(@Bind("broadcastId") UUID broadcastId);

@SqlQuery("SELECT broadcast_id AS uuid " +
"FROM Broadcast " +
"WHERE provider = :provider " +
"ORDER BY created DESC " +
"FETCH FIRST ROW ONLY")
@RegisterColumnMapper(UUIDMapper.class)
@Nullable
UUID getBroadcastId(@Bind("provider") UUID provider);

enum Type {
SENT,
DELIVERED,
READ,
FAILED
}

class Pair {
public Type type;
public int count;
}

class ReportMapper implements ColumnMapper<Pair> {
@Override
public Pair map(ResultSet rs, int columnNumber, StatementContext ctx) throws SQLException {
Pair ret = new Pair();
ret.type = Type.values()[rs.getInt("message_status")];
ret.count = rs.getInt("count");
return ret;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import java.sql.SQLException;
import java.util.UUID;

public class BotsMapper implements ColumnMapper<UUID> {
public class UUIDMapper implements ColumnMapper<UUID> {
@Override
@Nullable
public UUID map(ResultSet rs, int columnNumber, StatementContext ctx) {
try {
return getUuid(rs, "uuid");
} catch (SQLException e) {
Logger.error("BotsMapper: i: %d, e: %s", columnNumber, e);
Logger.error("UUIDMapper: i: %d, e: %s", columnNumber, e);
return null;
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/wire/bots/roman/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.waz.model.Messages;
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;
Expand All @@ -29,6 +30,8 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import static com.wire.bots.roman.DAO.BroadcastDAO.Type.DELIVERED;
import static com.wire.bots.roman.DAO.BroadcastDAO.Type.READ;
import static com.wire.bots.roman.Tools.generateToken;

public class MessageHandler extends MessageHandlerBase {
Expand All @@ -37,12 +40,14 @@ public class MessageHandler extends MessageHandlerBase {
private final Client jerseyClient;
private final ProvidersDAO providersDAO;
private final BotsDAO botsDAO;
private final BroadcastDAO broadcastDAO;
private Sender sender;

MessageHandler(Jdbi jdbi, Client jerseyClient) {
this.jerseyClient = jerseyClient;
providersDAO = jdbi.onDemand(ProvidersDAO.class);
botsDAO = jdbi.onDemand(BotsDAO.class);
broadcastDAO = jdbi.onDemand(BroadcastDAO.class);
}

@Override
Expand Down Expand Up @@ -183,6 +188,18 @@ public void onEvent(WireClient client, UUID userId, Messages.GenericMessage even
}
}

@Override
public void onConfirmation(WireClient client, ConfirmationMessage msg) {
try {
final UUID messageId = msg.getConfirmationMessageId();
final ConfirmationMessage.Type type = msg.getType();

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

private void onComposite(UUID botId, UUID userId, Messages.GenericMessage event) {
final Messages.Composite composite = event.getComposite();
final UUID messageId = UUID.fromString(event.getMessageId());
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/wire/bots/roman/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ public Sender(ClientRepo repo) {
this.repo = repo;
}

@Nullable
public UUID send(IncomingMessage message, UUID botId) throws Exception {
try (WireClient client = repo.getClient(botId)) {
if (client == null)
return null;

return send(message, client);
}
}

@Nullable
private UUID send(IncomingMessage message, @Nullable WireClient client) throws Exception {
if (client == null)
return null;

private UUID send(IncomingMessage message, WireClient client) throws Exception {
switch (message.type) {
case "text": {
MessageText text = new MessageText(message.text.data);
text.setExpectsReadConfirmation(true);
if (message.text.mentions != null) {
for (Mention mention : message.text.mentions)
text.addMention(mention.userId, mention.offset, mention.length);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/wire/bots/roman/model/Report.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.wire.bots.roman.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.wire.bots.roman.DAO.BroadcastDAO;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.UUID;

public class Report {
@JsonProperty
@NotNull
public UUID broadcastId;

@JsonProperty
@NotNull
public List<BroadcastDAO.Pair> report;
}
Loading

0 comments on commit d191a65

Please sign in to comment.