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.SignalServiceAddress;
|
||||||
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
||||||
import org.whispersystems.signalservice.loki.api.LokiAPI;
|
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 org.whispersystems.signalservice.loki.utilities.PromiseUtil;
|
||||||
|
|
||||||
import java.io.IOException;
|
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
|
private long templateMessageId; // The message ID of the message to template this send job from
|
||||||
|
|
||||||
// Loki - Multi device
|
// Loki - Multi device
|
||||||
private Address destination; // Destination to check whether this is another device we're sending to
|
private Address destination; // Used to check whether this is another device we're sending to
|
||||||
private boolean isFriendRequest; // Whether this is a friend request message
|
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 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 messageId, Address destination) {
|
||||||
public PushTextSendJob(long templateMessageId, long messageId, Address destination, boolean shouldSendSyncMessage) { this(templateMessageId, messageId, destination, false, null, shouldSendSyncMessage); }
|
this(messageId, messageId, destination);
|
||||||
public PushTextSendJob(long templateMessageId, long messageId, Address destination, boolean isFriendRequest, String customFriendRequestMessage, boolean shouldSendSyncMessage) {
|
|
||||||
this(constructParameters(destination), templateMessageId, messageId, destination, isFriendRequest, customFriendRequestMessage, shouldSendSyncMessage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
super(parameters);
|
||||||
this.templateMessageId = templateMessageId;
|
this.templateMessageId = templateMessageId;
|
||||||
this.messageId = messageId;
|
this.messageId = messageId;
|
||||||
this.destination = destination;
|
this.destination = destination;
|
||||||
this.isFriendRequest = isFriendRequest;
|
this.isLokiPreKeyBundleMessage = isLokiPreKeyBundleMessage;
|
||||||
this.customFriendRequestMessage = customFriendRequestMessage;
|
this.customFriendRequestMessage = customFriendRequestMessage;
|
||||||
this.shouldSendSyncMessage = shouldSendSyncMessage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -87,7 +89,7 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
.putLong(KEY_TEMPLATE_MESSAGE_ID, templateMessageId)
|
.putLong(KEY_TEMPLATE_MESSAGE_ID, templateMessageId)
|
||||||
.putLong(KEY_MESSAGE_ID, messageId)
|
.putLong(KEY_MESSAGE_ID, messageId)
|
||||||
.putString(KEY_DESTINATION, destination.serialize())
|
.putString(KEY_DESTINATION, destination.serialize())
|
||||||
.putBoolean(KEY_IS_FRIEND_REQUEST, isFriendRequest)
|
.putBoolean(KEY_IS_FRIEND_REQUEST, isLokiPreKeyBundleMessage)
|
||||||
.putBoolean(KEY_SHOULD_SEND_SYNC_MESSAGE, shouldSendSyncMessage);
|
.putBoolean(KEY_SHOULD_SEND_SYNC_MESSAGE, shouldSendSyncMessage);
|
||||||
|
|
||||||
if (customFriendRequestMessage != null) { builder.putString(KEY_CUSTOM_FR_MESSAGE, customFriendRequestMessage); }
|
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());
|
boolean hasSameDestination = destination.equals(recordRecipient.getAddress());
|
||||||
|
|
||||||
if (hasSameDestination && !record.isPending() && !record.isFailed()) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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);
|
Recipient recipient = Recipient.from(context, destination, false);
|
||||||
byte[] profileKey = recipient.getProfileKey();
|
byte[] profileKey = recipient.getProfileKey();
|
||||||
@ -158,16 +160,16 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
expirationManager.scheduleDeletion(record.getId(), record.isMms(), record.getExpiresIn());
|
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) {
|
} catch (InsecureFallbackApprovalException e) {
|
||||||
warn(TAG, "Failure", e);
|
warn(TAG, "Couldn't send message due to error: ", e);
|
||||||
if (messageId >= 0) {
|
if (messageId >= 0) {
|
||||||
database.markAsPendingInsecureSmsFallback(record.getId());
|
database.markAsPendingInsecureSmsFallback(record.getId());
|
||||||
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipient(), record.getThreadId());
|
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipient(), record.getThreadId());
|
||||||
}
|
}
|
||||||
} catch (UntrustedIdentityException e) {
|
} catch (UntrustedIdentityException e) {
|
||||||
warn(TAG, "Failure", e);
|
warn(TAG, "Couldn't send message due to error: ", e);
|
||||||
if (messageId >= 0) {
|
if (messageId >= 0) {
|
||||||
database.addMismatchedIdentity(record.getId(), Address.fromSerialized(e.getE164Number()), e.getIdentityKey());
|
database.addMismatchedIdentity(record.getId(), Address.fromSerialized(e.getE164Number()), e.getIdentityKey());
|
||||||
database.markAsSentFailed(record.getId());
|
database.markAsSentFailed(record.getId());
|
||||||
@ -175,18 +177,17 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
}
|
}
|
||||||
} catch (LokiAPI.Error e) {
|
} catch (LokiAPI.Error e) {
|
||||||
Log.d("Loki", "Couldn't send message due to error: " + e.getDescription());
|
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 lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context);
|
||||||
lokiMessageDatabase.setErrorMessage(record.getId(), e.getDescription());
|
lokiMessageDatabase.setErrorMessage(record.getId(), e.getDescription());
|
||||||
database.markAsSentFailed(record.getId());
|
database.markAsSentFailed(record.getId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onShouldRetry(@NonNull Exception exception) {
|
public boolean onShouldRetry(@NonNull Exception exception) {
|
||||||
// Loki - Disable since we have our own retrying
|
// Loki - Disable since we have our own retrying
|
||||||
// if (exception instanceof RetryLaterException) return true;
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,7 +209,6 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException, LokiAPI.Error
|
throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException, LokiAPI.Error
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// rotateSenderCertificateIfNecessary();
|
|
||||||
Recipient recipient = Recipient.from(context, destination, false);
|
Recipient recipient = Recipient.from(context, destination, false);
|
||||||
SignalServiceAddress address = getPushAddress(recipient.getAddress());
|
SignalServiceAddress address = getPushAddress(recipient.getAddress());
|
||||||
Optional<byte[]> profileKey = getProfileKey(recipient);
|
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
|
// Loki - Include a pre key bundle if the message is a friend request or an end session message
|
||||||
PreKeyBundle preKeyBundle;
|
PreKeyBundle preKeyBundle;
|
||||||
if (isFriendRequest || message.isEndSession()) {
|
if (isLokiPreKeyBundleMessage || message.isEndSession()) {
|
||||||
preKeyBundle = DatabaseFactory.getLokiPreKeyBundleDatabase(context).generatePreKeyBundle(address.getNumber());
|
preKeyBundle = DatabaseFactory.getLokiPreKeyBundleDatabase(context).generatePreKeyBundle(address.getNumber());
|
||||||
} else {
|
} else {
|
||||||
preKeyBundle = null;
|
preKeyBundle = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String body = (isFriendRequest && customFriendRequestMessage != null) ? customFriendRequestMessage : message.getBody();
|
String body = (isLokiPreKeyBundleMessage && customFriendRequestMessage != null) ? customFriendRequestMessage : message.getBody();
|
||||||
SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder()
|
SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder()
|
||||||
.withTimestamp(message.getDateSent())
|
.withTimestamp(message.getDateSent())
|
||||||
.withBody(body)
|
.withBody(body)
|
||||||
.withExpiration((int)(message.getExpiresIn() / 1000))
|
.withExpiration((int)(message.getExpiresIn() / 1000))
|
||||||
.withProfileKey(profileKey.orNull())
|
.withProfileKey(profileKey.orNull())
|
||||||
.asEndSessionMessage(message.isEndSession())
|
.asEndSessionMessage(message.isEndSession())
|
||||||
.asFriendRequest(isFriendRequest)
|
.asFriendRequest(isLokiPreKeyBundleMessage)
|
||||||
.withPreKeyBundle(preKeyBundle)
|
.withPreKeyBundle(preKeyBundle)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@ -239,18 +239,10 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
Optional<UnidentifiedAccessPair> syncAccess = UnidentifiedAccessUtil.getAccessForSync(context);
|
Optional<UnidentifiedAccessPair> syncAccess = UnidentifiedAccessUtil.getAccessForSync(context);
|
||||||
SignalServiceSyncMessage syncMessage = buildSelfSendSyncMessage(context, textSecureMessage, syncAccess);
|
SignalServiceSyncMessage syncMessage = buildSelfSendSyncMessage(context, textSecureMessage, syncAccess);
|
||||||
|
|
||||||
messageSender.sendMessage(templateMessageId, syncMessage, syncAccess);
|
messageSender.sendMessage(syncMessage, syncAccess);
|
||||||
return syncAccess.isPresent();
|
return syncAccess.isPresent();
|
||||||
} else {
|
} else {
|
||||||
LokiSyncMessage syncMessage = null;
|
SendMessageResult result = messageSender.sendMessage(messageId, address, unidentifiedAccess, textSecureMessage);
|
||||||
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));
|
|
||||||
if (result.getLokiAPIError() != null) {
|
if (result.getLokiAPIError() != null) {
|
||||||
throw result.getLokiAPIError();
|
throw result.getLokiAPIError();
|
||||||
} else {
|
} else {
|
||||||
@ -272,10 +264,9 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
long templateMessageID = data.getLong(KEY_TEMPLATE_MESSAGE_ID);
|
long templateMessageID = data.getLong(KEY_TEMPLATE_MESSAGE_ID);
|
||||||
long messageID = data.getLong(KEY_MESSAGE_ID);
|
long messageID = data.getLong(KEY_MESSAGE_ID);
|
||||||
Address destination = Address.fromSerialized(data.getString(KEY_DESTINATION));
|
Address destination = Address.fromSerialized(data.getString(KEY_DESTINATION));
|
||||||
boolean isFriendRequest = data.getBoolean(KEY_IS_FRIEND_REQUEST);
|
boolean isLokiPreKeyBundleMessage = data.getBoolean(KEY_IS_FRIEND_REQUEST);
|
||||||
String frMessage = data.hasString(KEY_CUSTOM_FR_MESSAGE) ? data.getString(KEY_CUSTOM_FR_MESSAGE) : null;
|
String customFRMessage = 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, isLokiPreKeyBundleMessage, customFRMessage);
|
||||||
return new PushTextSendJob(parameters, templateMessageID, messageID, destination, isFriendRequest, frMessage, shouldSendSyncMessage);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user