Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ulasakdeniz committed Sep 22, 2016
1 parent 000c438 commit 8b45c57
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/bots/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public interface Bot {

void sendMessage(String recipientId, String message);

default void checkAndSend(String senderId, String text, Function<Message, Void> send) {
default void checkAndSend(String senderId, String senderClient, String text, Function<Message, Void> send) {
Set<String> recipients = MessageParser.extractRecipients(text);
if(!recipients.isEmpty()) {
Message message = new Message(senderId, recipients, text);
Message message = new Message(senderId, senderClient, recipients, text);
send.apply(message);
}
else {
Expand Down
7 changes: 4 additions & 3 deletions app/bots/BotFather.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public BotFather(RedisHelper redisHelper, DBHelper dbHelper) {
Set<String> recipients = message.getRecipients();
if (!recipients.isEmpty()) {
for (String userId : recipients) {
CompletionStage<String> senderId = redisHelper.getUserId(message.getSenderClient(), message.getSender());
CompletionStage<User> recipient = redisHelper.getUser(userId);
recipient.thenCombineAsync(senderId, (recipientUser, sId) -> {
recipient.thenApplyAsync(recipientUser -> {
//TODO: multiple recipient case!!!
String sId = redisHelper.getUserId(message.getSenderClient(), message.getSender());
if(recipientUser != null && sId != null) {
send(recipientUser, sId, text);
}
Expand All @@ -54,7 +55,7 @@ public BotFather(RedisHelper redisHelper, DBHelper dbHelper) {
private boolean send(User recipient, String senderId, String text) {
String client = recipient.getClient();
String clientId = recipient.getClientId();
text = "[@" + senderId + "]: " + text;
text = "[+" + senderId + "]: " + text;
Logger.debug("sent text: " + text);
try {
Bot recipientBot = botRegistry.get(client);
Expand Down
2 changes: 1 addition & 1 deletion app/bots/FacebookBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public boolean receive(JsonNode json) {
return name;
});
} else {
checkAndSend(senderId, text, botFather.sendMessage);
checkAndSend(senderId, Contract.FACEBOOK, text, botFather.sendMessage);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/bots/TelegramBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void onUpdateReceived(Update update) {
});
}
} else {
checkAndSend(senderId, text, botFather.sendMessage);
checkAndSend(senderId, Contract.TELEGRAM, text, botFather.sendMessage);
}
}
} catch (Exception ex) {
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/RedisHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public CompletionStage<User> getUser(String id) {
});
}

public CompletionStage<String> getUserId(String client, String clientId) {
return connection.async().get(client + "-" + clientId);
public String getUserId(String client, String clientId) {
return connection.sync().get(client + "-" + clientId);
}

public void terminate() {
Expand Down
3 changes: 2 additions & 1 deletion app/models/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ public class Message {
private String sender, text, senderClient, recipientClient;
private Set<String> recipients;

public Message(String sender, Set<String> recipients, String text) {
public Message(String sender, String senderClient, Set<String> recipients, String text) {
this.sender = sender;
this.senderClient = senderClient;
this.recipients = recipients;
this.text = text;
}
Expand Down
17 changes: 17 additions & 0 deletions conf/evolutions/default/2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Users schema

# --- !Ups

CREATE TABLE User (
id varchar(255) NOT NULL,
name varchar(255) NOT NULL,
client varchar(255) NOT NULL,
clientId varchar(255) NOT NULL,
PRIMARY KEY (id)
) CHARACTER SET=utf8;

CREATE UNIQUE INDEX clientIdPair ON User (client, clientId);

# --- !Downs

DROP TABLE User;

0 comments on commit 8b45c57

Please sign in to comment.