Store envelopes using envelope proto

This commit is contained in:
AsamK 2023-04-23 19:39:36 +02:00
parent f6aebb5917
commit 60ad582012

View file

@ -6,6 +6,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import org.whispersystems.signalservice.api.push.ServiceId;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.UuidUtil;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@ -19,7 +20,7 @@ public class MessageCacheUtils {
private final static Logger logger = LoggerFactory.getLogger(MessageCacheUtils.class);
final static int CURRENT_VERSION = 8;
final static int CURRENT_VERSION = 9;
public static SignalServiceEnvelope loadEnvelope(File file) throws IOException {
try (var f = new FileInputStream(file)) {
@ -31,6 +32,11 @@ public class MessageCacheUtils {
// Unsupported envelope version
return null;
}
if (version >= 9) {
final var serverReceivedTimestamp = in.readLong();
final var envelope = SignalServiceProtos.Envelope.parseFrom(in.readAllBytes());
return new SignalServiceEnvelope(envelope, serverReceivedTimestamp);
} else {
var type = in.readInt();
var source = in.readUTF();
ServiceId sourceServiceId = null;
@ -101,31 +107,14 @@ public class MessageCacheUtils {
updatedPni == null ? "" : updatedPni);
}
}
}
public static void storeEnvelope(SignalServiceEnvelope envelope, File file) throws IOException {
try (var f = new FileOutputStream(file)) {
try (var out = new DataOutputStream(f)) {
out.writeInt(CURRENT_VERSION); // version
out.writeInt(envelope.getType());
out.writeUTF(""); // legacy number
out.writeUTF(envelope.getSourceUuid().isPresent() ? envelope.getSourceUuid().get() : "");
out.writeInt(envelope.getSourceDevice());
out.writeUTF(envelope.getDestinationUuid() == null ? "" : envelope.getDestinationUuid());
out.writeLong(envelope.getTimestamp());
if (envelope.hasContent()) {
out.writeInt(envelope.getContent().length);
out.write(envelope.getContent());
} else {
out.writeInt(0);
}
out.writeInt(0); // legacy message
out.writeLong(envelope.getServerReceivedTimestamp());
var uuid = envelope.getServerGuid();
out.writeUTF(uuid == null ? "" : uuid);
out.writeLong(envelope.getServerDeliveredTimestamp());
out.writeBoolean(envelope.isUrgent());
out.writeBoolean(envelope.isStory());
out.writeUTF(envelope.getUpdatedPni() == null ? "" : envelope.getUpdatedPni());
envelope.getProto().writeTo(out);
}
}
}