Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Refactoring/cache #10

Merged
merged 2 commits into from
Jul 8, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
#docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
mvn package -DskipTests=true -Dmaven.javadoc.skip=true
docker build -t $DOCKER_USERNAME/recording-bot:latest .
docker build -t $DOCKER_USERNAME/recording-bot:0.1.0 .
docker push $DOCKER_USERNAME/recording-bot
kubectl delete pod -l name=recording -n prod
kubectl get pods -l name=recording -n prod
Expand Down
9 changes: 0 additions & 9 deletions recording.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,12 @@ swagger:

auth: ${WIRE_RECORDING_SERVICE_TOKEN}

storage:
host: localhost
port: 5432
database: postgres
driver: postgresql
user: admin
password: ${POSTGRES_PASSWORD}

db:
host: localhost
port: 6379 #redis
user: admin
password: ${REDIS_PASSWORD}


database:
driverClass: org.postgresql.Driver
user: admin
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/com/wire/bots/recording/Cache.java

This file was deleted.

59 changes: 0 additions & 59 deletions src/main/java/com/wire/bots/recording/Helper.java

This file was deleted.

43 changes: 10 additions & 33 deletions src/main/java/com/wire/bots/recording/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.wire.bots.recording.DAO.HistoryDAO;
import com.wire.bots.recording.model.DBRecord;
import com.wire.bots.recording.utils.Collector;
import com.wire.bots.recording.utils.Formatter;
import com.wire.bots.sdk.MessageHandlerBase;
import com.wire.bots.sdk.WireClient;
import com.wire.bots.sdk.models.AttachmentMessage;
Expand All @@ -11,16 +13,10 @@
import com.wire.bots.sdk.server.model.User;
import com.wire.bots.sdk.tools.Logger;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

import static com.wire.bots.recording.UrlUtil.getFile;

public class MessageHandler extends MessageHandlerBase {
private static final String WELCOME_LABEL = "Recording was enabled.\nAvailable commands:\n" +
"`/history` - receive previous messages\n" +
Expand Down Expand Up @@ -55,7 +51,7 @@ public void onMemberJoin(WireClient client, ArrayList<String> userIds) {
Collector collector = collect(client, botId);

for (String userId : userIds) {
collector.sendPDF(client, UUID.fromString(userId));
collector.sendPDF(UUID.fromString(userId));
}
} catch (Exception e) {
Logger.error("onMemberJoin: %s %s", botId, e);
Expand Down Expand Up @@ -93,14 +89,14 @@ public void onText(WireClient client, TextMessage msg) {
if (cmd.equals("/pdf")) {
client.sendDirectText("Generating PDF...", userId.toString());
Collector collector = collect(client, botId);
collector.sendPDF(client, userId);
collector.sendPDF(userId);
return;
}

if (cmd.equals("/html")) {
client.sendDirectText("Generating HTML...", userId.toString());
Collector collector = collect(client, botId);
collector.sendHtml(client, userId);
collector.sendHtml(userId);
return;
}

Expand Down Expand Up @@ -256,33 +252,14 @@ public void onAttachment(WireClient client, AttachmentMessage msg) {
}

private Collector collect(WireClient client, UUID botId) {
Collector collector = new Collector();
Collector collector = new Collector(client);
for (DBRecord record : historyDAO.getRecords(botId)) {
if (record.mimeType.startsWith("image")) {
downloadImage(client, record);
try {
collector.add(record);
} catch (Exception e) {
Logger.warning("collect: %s", e);
}
collector.add(record);
}
return collector;
}

private void downloadImage(WireClient client, DBRecord record) {
try {
byte[] image = client.downloadAsset(record.assetKey, record.assetToken, record.sha256, record.otrKey);
File file = saveImage(image, record.assetKey, record.mimeType);
assert file.exists();
} catch (Exception e) {
e.printStackTrace();
}
}

private File saveImage(byte[] image, String assetKey, String mimeType) throws IOException {
File file = getFile(assetKey, mimeType);
if (!file.exists()) {
try (DataOutputStream os = new DataOutputStream(new FileOutputStream(file))) {
os.write(image);
}
}
return file;
}
}
5 changes: 0 additions & 5 deletions src/main/java/com/wire/bots/recording/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,4 @@ protected MessageHandlerBase createHandler(Config config, Environment env) {

return new MessageHandler(historyDAO);
}

@Override
protected void onRun(Config config, Environment env) {
env.healthChecks().register("User login", new UserHealthCheck());
}
}
22 changes: 0 additions & 22 deletions src/main/java/com/wire/bots/recording/UserHealthCheck.java

This file was deleted.

8 changes: 0 additions & 8 deletions src/main/java/com/wire/bots/recording/model/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public class Config extends Configuration {
@JsonProperty
@NotNull
public DB storage;

public DB getStorage() {
return storage;
}

@Valid
@NotNull
@JsonProperty
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/com/wire/bots/recording/utils/Cache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.wire.bots.recording.utils;

import com.wire.bots.recording.Service;
import com.wire.bots.recording.model.DBRecord;
import com.wire.bots.sdk.Configuration;
import com.wire.bots.sdk.WireClient;
import com.wire.bots.sdk.assets.Picture;
import com.wire.bots.sdk.tools.Logger;
import com.wire.bots.sdk.user.API;
import com.wire.bots.sdk.user.LoginClient;
import com.wire.bots.sdk.user.model.User;

import javax.annotation.Nullable;
import javax.ws.rs.client.Client;
import java.io.File;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

class Cache {
private static final ConcurrentHashMap<String, Picture> pictures = new ConcurrentHashMap<>();//<Url, Picture>
private static final ConcurrentHashMap<String, File> assets = new ConcurrentHashMap<>();//<assetKey, File>
private static final ConcurrentHashMap<UUID, File> profiles = new ConcurrentHashMap<>();//<userId, File>
private static API api;

static {
api = getApi();
}

static File downloadImage(WireClient client, DBRecord record) {
return assets.computeIfAbsent(record.assetKey, k -> {
try {
return Helper.downloadImage(client, record);
} catch (Exception e) {
Logger.warning("Cache.downloadImage: assetId: %s, ex: %s", k, e);
return null;
}
});
}

@Nullable
static File getProfile(UUID userId) {
return profiles.computeIfAbsent(userId, k -> {
try {
return Helper.getProfile(api, userId);
} catch (Exception e) {
Logger.warning("Cache.getProfile: userId: %s, ex: %s", k, e);
api = getApi();
return null;
}
});
}

@Nullable
static Picture getPictureUrl(WireClient client, String url) {
return pictures.computeIfAbsent(url, k -> {
try {
return Helper.upload(client, url);
} catch (Exception e) {
Logger.warning("Cache.getPicture: url: %s, ex: %s", url, e);
return null;
}
});
}

private static API getApi() {
Client client = Service.instance.getClient();

try {
String email = Configuration.propOrEnv("email", true);
String password = Configuration.propOrEnv("password", true);

LoginClient loginClient = new LoginClient(client);
User robin = loginClient.login(email, password);
String token = robin.getToken();
return new API(client, null, token);
} catch (Exception e) {
Logger.warning("getAPI: %s", e);
return new API(client, null, null);
}
}
}
Loading