mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-24 02:25:19 +00:00
Refactor PushTextSendJob
This commit is contained in:
parent
32ae74c442
commit
361083f790
@ -33,8 +33,6 @@ import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSy
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
||||
import org.whispersystems.signalservice.loki.api.LokiAPI;
|
||||
import org.whispersystems.signalservice.loki.protocol.multidevice.LokiDeviceLinkUtilities;
|
||||
import org.whispersystems.signalservice.loki.protocol.syncmessages.LokiSyncMessage;
|
||||
import org.whispersystems.signalservice.loki.utilities.PromiseUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -60,25 +58,29 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
||||
private long templateMessageId; // The message ID of the message to template this send job from
|
||||
|
||||
// Loki - Multi device
|
||||
private Address destination; // Destination to check whether this is another device we're sending to
|
||||
private boolean isFriendRequest; // Whether this is a friend request message
|
||||
private Address destination; // Used to check whether this is another device we're sending to
|
||||
private boolean isLokiPreKeyBundleMessage; // Whether this is a friend request message
|
||||
private String customFriendRequestMessage; // If this isn't set then we use the message body
|
||||
private boolean shouldSendSyncMessage;
|
||||
|
||||
public PushTextSendJob(long messageId, Address destination) { this(messageId, messageId, destination, false); }
|
||||
public PushTextSendJob(long templateMessageId, long messageId, Address destination, boolean shouldSendSyncMessage) { this(templateMessageId, messageId, destination, false, null, shouldSendSyncMessage); }
|
||||
public PushTextSendJob(long templateMessageId, long messageId, Address destination, boolean isFriendRequest, String customFriendRequestMessage, boolean shouldSendSyncMessage) {
|
||||
this(constructParameters(destination), templateMessageId, messageId, destination, isFriendRequest, customFriendRequestMessage, shouldSendSyncMessage);
|
||||
public PushTextSendJob(long messageId, Address destination) {
|
||||
this(messageId, messageId, destination);
|
||||
}
|
||||
|
||||
private PushTextSendJob(@NonNull Job.Parameters parameters, long templateMessageId, long messageId, Address destination, boolean isFriendRequest, String customFriendRequestMessage, boolean shouldSendSyncMessage) {
|
||||
public PushTextSendJob(long templateMessageId, long messageId, Address destination) {
|
||||
this(templateMessageId, messageId, destination, false, null);
|
||||
}
|
||||
|
||||
public PushTextSendJob(long templateMessageId, long messageId, Address destination, boolean isLokiPreKeyBundleMessage, String customFriendRequestMessage) {
|
||||
this(constructParameters(destination), templateMessageId, messageId, destination, isLokiPreKeyBundleMessage, customFriendRequestMessage);
|
||||
}
|
||||
|
||||
private PushTextSendJob(@NonNull Job.Parameters parameters, long templateMessageId, long messageId, Address destination, boolean isLokiPreKeyBundleMessage, String customFriendRequestMessage) {
|
||||
super(parameters);
|
||||
this.templateMessageId = templateMessageId;
|
||||
this.messageId = messageId;
|
||||
this.destination = destination;
|
||||
this.isFriendRequest = isFriendRequest;
|
||||
this.isLokiPreKeyBundleMessage = isLokiPreKeyBundleMessage;
|
||||
this.customFriendRequestMessage = customFriendRequestMessage;
|
||||
this.shouldSendSyncMessage = shouldSendSyncMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -87,7 +89,7 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
||||
.putLong(KEY_TEMPLATE_MESSAGE_ID, templateMessageId)
|
||||
.putLong(KEY_MESSAGE_ID, messageId)
|
||||
.putString(KEY_DESTINATION, destination.serialize())
|
||||
.putBoolean(KEY_IS_FRIEND_REQUEST, isFriendRequest)
|
||||
.putBoolean(KEY_IS_FRIEND_REQUEST, isLokiPreKeyBundleMessage)
|
||||
.putBoolean(KEY_SHOULD_SEND_SYNC_MESSAGE, shouldSendSyncMessage);
|
||||
|
||||
if (customFriendRequestMessage != null) { builder.putString(KEY_CUSTOM_FR_MESSAGE, customFriendRequestMessage); }
|
||||
@ -116,12 +118,12 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
||||
boolean hasSameDestination = destination.equals(recordRecipient.getAddress());
|
||||
|
||||
if (hasSameDestination && !record.isPending() && !record.isFailed()) {
|
||||
warn(TAG, "Message " + templateMessageId + " was already sent. Ignoring.");
|
||||
Log.d("Loki", "Message with ID: " + templateMessageId + " was already sent; ignoring.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
log(TAG, "Sending message: " + templateMessageId + (hasSameDestination ? "" : "to another device."));
|
||||
log(TAG, "Sending message: " + templateMessageId + (hasSameDestination ? "" : "to a linked device."));
|
||||
|
||||
Recipient recipient = Recipient.from(context, destination, false);
|
||||
byte[] profileKey = recipient.getProfileKey();
|
||||
@ -158,16 +160,16 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
||||
expirationManager.scheduleDeletion(record.getId(), record.isMms(), record.getExpiresIn());
|
||||
}
|
||||
|
||||
log(TAG, "Sent message: " + templateMessageId + (hasSameDestination ? "" : "to another device."));
|
||||
log(TAG, "Sent message: " + templateMessageId + (hasSameDestination ? "" : "to a linked device."));
|
||||
|
||||
} catch (InsecureFallbackApprovalException e) {
|
||||
warn(TAG, "Failure", e);
|
||||
warn(TAG, "Couldn't send message due to error: ", e);
|
||||
if (messageId >= 0) {
|
||||
database.markAsPendingInsecureSmsFallback(record.getId());
|
||||
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipient(), record.getThreadId());
|
||||
}
|
||||
} catch (UntrustedIdentityException e) {
|
||||
warn(TAG, "Failure", e);
|
||||
warn(TAG, "Couldn't send message due to error: ", e);
|
||||
if (messageId >= 0) {
|
||||
database.addMismatchedIdentity(record.getId(), Address.fromSerialized(e.getE164Number()), e.getIdentityKey());
|
||||
database.markAsSentFailed(record.getId());
|
||||
@ -175,18 +177,17 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
||||
}
|
||||
} catch (LokiAPI.Error e) {
|
||||
Log.d("Loki", "Couldn't send message due to error: " + e.getDescription());
|
||||
if (messageId < 0) { return; }
|
||||
if (messageId >= 0) {
|
||||
LokiMessageDatabase lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context);
|
||||
lokiMessageDatabase.setErrorMessage(record.getId(), e.getDescription());
|
||||
database.markAsSentFailed(record.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onShouldRetry(@NonNull Exception exception) {
|
||||
// Loki - Disable since we have our own retrying
|
||||
// if (exception instanceof RetryLaterException) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -208,7 +209,6 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
||||
throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException, LokiAPI.Error
|
||||
{
|
||||
try {
|
||||
// rotateSenderCertificateIfNecessary();
|
||||
Recipient recipient = Recipient.from(context, destination, false);
|
||||
SignalServiceAddress address = getPushAddress(recipient.getAddress());
|
||||
Optional<byte[]> profileKey = getProfileKey(recipient);
|
||||
@ -218,20 +218,20 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
||||
|
||||
// Loki - Include a pre key bundle if the message is a friend request or an end session message
|
||||
PreKeyBundle preKeyBundle;
|
||||
if (isFriendRequest || message.isEndSession()) {
|
||||
if (isLokiPreKeyBundleMessage || message.isEndSession()) {
|
||||
preKeyBundle = DatabaseFactory.getLokiPreKeyBundleDatabase(context).generatePreKeyBundle(address.getNumber());
|
||||
} else {
|
||||
preKeyBundle = null;
|
||||
}
|
||||
|
||||
String body = (isFriendRequest && customFriendRequestMessage != null) ? customFriendRequestMessage : message.getBody();
|
||||
String body = (isLokiPreKeyBundleMessage && customFriendRequestMessage != null) ? customFriendRequestMessage : message.getBody();
|
||||
SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder()
|
||||
.withTimestamp(message.getDateSent())
|
||||
.withBody(body)
|
||||
.withExpiration((int)(message.getExpiresIn() / 1000))
|
||||
.withProfileKey(profileKey.orNull())
|
||||
.asEndSessionMessage(message.isEndSession())
|
||||
.asFriendRequest(isFriendRequest)
|
||||
.asFriendRequest(isLokiPreKeyBundleMessage)
|
||||
.withPreKeyBundle(preKeyBundle)
|
||||
.build();
|
||||
|
||||
@ -239,18 +239,10 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
||||
Optional<UnidentifiedAccessPair> syncAccess = UnidentifiedAccessUtil.getAccessForSync(context);
|
||||
SignalServiceSyncMessage syncMessage = buildSelfSendSyncMessage(context, textSecureMessage, syncAccess);
|
||||
|
||||
messageSender.sendMessage(templateMessageId, syncMessage, syncAccess);
|
||||
messageSender.sendMessage(syncMessage, syncAccess);
|
||||
return syncAccess.isPresent();
|
||||
} else {
|
||||
LokiSyncMessage syncMessage = null;
|
||||
if (shouldSendSyncMessage) {
|
||||
// Set the sync message destination to the master device, this way it will show that we sent a message to the master device and not the slave device
|
||||
String masterDevice = PromiseUtil.get(LokiDeviceLinkUtilities.INSTANCE.getMasterHexEncodedPublicKey(address.getNumber()), null);
|
||||
SignalServiceAddress masterAddress = masterDevice == null ? address : new SignalServiceAddress(masterDevice);
|
||||
// We also need to use the original message ID and not -1
|
||||
syncMessage = new LokiSyncMessage(masterAddress, templateMessageId);
|
||||
}
|
||||
SendMessageResult result = messageSender.sendMessage(messageId, address, unidentifiedAccess, textSecureMessage, Optional.fromNullable(syncMessage));
|
||||
SendMessageResult result = messageSender.sendMessage(messageId, address, unidentifiedAccess, textSecureMessage);
|
||||
if (result.getLokiAPIError() != null) {
|
||||
throw result.getLokiAPIError();
|
||||
} else {
|
||||
@ -272,10 +264,9 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
||||
long templateMessageID = data.getLong(KEY_TEMPLATE_MESSAGE_ID);
|
||||
long messageID = data.getLong(KEY_MESSAGE_ID);
|
||||
Address destination = Address.fromSerialized(data.getString(KEY_DESTINATION));
|
||||
boolean isFriendRequest = data.getBoolean(KEY_IS_FRIEND_REQUEST);
|
||||
String frMessage = data.hasString(KEY_CUSTOM_FR_MESSAGE) ? data.getString(KEY_CUSTOM_FR_MESSAGE) : null;
|
||||
boolean shouldSendSyncMessage = data.getBoolean(KEY_SHOULD_SEND_SYNC_MESSAGE);
|
||||
return new PushTextSendJob(parameters, templateMessageID, messageID, destination, isFriendRequest, frMessage, shouldSendSyncMessage);
|
||||
boolean isLokiPreKeyBundleMessage = data.getBoolean(KEY_IS_FRIEND_REQUEST);
|
||||
String customFRMessage = data.hasString(KEY_CUSTOM_FR_MESSAGE) ? data.getString(KEY_CUSTOM_FR_MESSAGE) : null;
|
||||
return new PushTextSendJob(parameters, templateMessageID, messageID, destination, isLokiPreKeyBundleMessage, customFRMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user