mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 09:17:44 +00:00
Multi-device sending text messages.
This commit is contained in:
parent
8e495d087b
commit
fb3967db55
@ -40,24 +40,48 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
|
|
||||||
private static final String TAG = PushTextSendJob.class.getSimpleName();
|
private static final String TAG = PushTextSendJob.class.getSimpleName();
|
||||||
|
|
||||||
|
private static final String KEY_TEMPLATE_MESSAGE_ID = "template_message_id";
|
||||||
private static final String KEY_MESSAGE_ID = "message_id";
|
private static final String KEY_MESSAGE_ID = "message_id";
|
||||||
|
private static final String KEY_DESTINATION = "destination";
|
||||||
|
private static final String KEY_IS_FRIEND_REQUEST = "is_friend_request";
|
||||||
|
private static final String KEY_CUSTOM_FR_MESSAGE = "custom_friend_request_message";
|
||||||
|
|
||||||
@Inject SignalServiceMessageSender messageSender;
|
@Inject SignalServiceMessageSender messageSender;
|
||||||
|
|
||||||
private long messageId;
|
private long messageId; // The message id
|
||||||
|
private long templateMessageId; // The message id of the message to template this send job from
|
||||||
|
|
||||||
public PushTextSendJob(long messageId, Address destination) {
|
// Loki - Multi-device
|
||||||
this(constructParameters(destination), messageId);
|
|
||||||
|
private Address destination = null; // Destination to check whether this is another device we're sending to
|
||||||
|
private boolean isFriendRequest = false; // Whether this is a friend request message
|
||||||
|
private String customFriendRequestMessage = null; // If this isn't set then we use the message body
|
||||||
|
|
||||||
|
public PushTextSendJob(long messageId, Address destination) { this(messageId, messageId, destination); }
|
||||||
|
public PushTextSendJob(long templateMessageId, long messageId, Address destination) { this(templateMessageId, messageId, destination, false, null); }
|
||||||
|
public PushTextSendJob(long templateMessageId, long messageId, Address destination, boolean isFriendRequest, String customFriendRequestMessage) {
|
||||||
|
this(constructParameters(destination), templateMessageId, messageId, destination, isFriendRequest, customFriendRequestMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
private PushTextSendJob(@NonNull Job.Parameters parameters, long messageId) {
|
private PushTextSendJob(@NonNull Job.Parameters parameters, long templateMessageId, long messageId, Address destination, boolean isFriendRequest, String customFriendRequestMessage) {
|
||||||
super(parameters);
|
super(parameters);
|
||||||
|
this.templateMessageId = templateMessageId;
|
||||||
this.messageId = messageId;
|
this.messageId = messageId;
|
||||||
|
this.destination = destination;
|
||||||
|
this.isFriendRequest = isFriendRequest;
|
||||||
|
this.customFriendRequestMessage = customFriendRequestMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull Data serialize() {
|
public @NonNull Data serialize() {
|
||||||
return new Data.Builder().putLong(KEY_MESSAGE_ID, messageId).build();
|
Data.Builder builder = new Data.Builder()
|
||||||
|
.putLong(KEY_TEMPLATE_MESSAGE_ID, templateMessageId)
|
||||||
|
.putLong(KEY_MESSAGE_ID, messageId)
|
||||||
|
.putString(KEY_DESTINATION, destination.serialize())
|
||||||
|
.putBoolean(KEY_IS_FRIEND_REQUEST, isFriendRequest);
|
||||||
|
|
||||||
|
if (customFriendRequestMessage != null) { builder.putString(KEY_CUSTOM_FR_MESSAGE, customFriendRequestMessage); }
|
||||||
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -67,31 +91,38 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAdded() {
|
public void onAdded() {
|
||||||
DatabaseFactory.getSmsDatabase(context).markAsSending(messageId);
|
if (messageId >= 0) {
|
||||||
|
DatabaseFactory.getSmsDatabase(context).markAsSending(messageId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPushSend() throws NoSuchMessageException, RetryLaterException {
|
public void onPushSend() throws NoSuchMessageException, RetryLaterException {
|
||||||
ExpiringMessageManager expirationManager = ApplicationContext.getInstance(context).getExpiringMessageManager();
|
ExpiringMessageManager expirationManager = ApplicationContext.getInstance(context).getExpiringMessageManager();
|
||||||
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
|
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
|
||||||
SmsMessageRecord record = database.getMessage(messageId);
|
SmsMessageRecord record = database.getMessage(templateMessageId);
|
||||||
|
|
||||||
if (!record.isPending() && !record.isFailed()) {
|
Recipient recordRecipient = record.getRecipient().resolve();
|
||||||
warn(TAG, "Message " + messageId + " was already sent. Ignoring.");
|
boolean hasSameDestination = destination.equals(recordRecipient.getAddress());
|
||||||
|
|
||||||
|
if (hasSameDestination && !record.isPending() && !record.isFailed()) {
|
||||||
|
warn(TAG, "Message " + templateMessageId + " was already sent. Ignoring.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
log(TAG, "Sending message: " + messageId);
|
log(TAG, "Sending message: " + templateMessageId + (hasSameDestination ? "" : "to another device."));
|
||||||
|
|
||||||
Recipient recipient = record.getRecipient().resolve();
|
Recipient recipient = Recipient.from(context, destination, false);
|
||||||
byte[] profileKey = recipient.getProfileKey();
|
byte[] profileKey = recipient.getProfileKey();
|
||||||
UnidentifiedAccessMode accessMode = recipient.getUnidentifiedAccessMode();
|
UnidentifiedAccessMode accessMode = recipient.getUnidentifiedAccessMode();
|
||||||
|
|
||||||
boolean unidentified = deliver(record);
|
boolean unidentified = deliver(messageId, recipient, record);
|
||||||
|
|
||||||
database.markAsSent(messageId, true);
|
if (messageId >= 0) {
|
||||||
database.markUnidentified(messageId, unidentified);
|
database.markAsSent(messageId, true);
|
||||||
|
database.markUnidentified(messageId, unidentified);
|
||||||
|
}
|
||||||
|
|
||||||
if (recipient.isLocalNumber()) {
|
if (recipient.isLocalNumber()) {
|
||||||
SyncMessageId id = new SyncMessageId(recipient.getAddress(), record.getDateSent());
|
SyncMessageId id = new SyncMessageId(recipient.getAddress(), record.getDateSent());
|
||||||
@ -112,12 +143,12 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (record.getExpiresIn() > 0) {
|
if (record.getExpiresIn() > 0 && messageId >= 0) {
|
||||||
database.markExpireStarted(messageId);
|
database.markExpireStarted(messageId);
|
||||||
expirationManager.scheduleDeletion(record.getId(), record.isMms(), record.getExpiresIn());
|
expirationManager.scheduleDeletion(record.getId(), record.isMms(), record.getExpiresIn());
|
||||||
}
|
}
|
||||||
|
|
||||||
log(TAG, "Sent message: " + messageId);
|
log(TAG, "Sent message: " + templateMessageId + (hasSameDestination ? "" : "to another device."));
|
||||||
|
|
||||||
} catch (InsecureFallbackApprovalException e) {
|
} catch (InsecureFallbackApprovalException e) {
|
||||||
warn(TAG, "Failure", e);
|
warn(TAG, "Failure", e);
|
||||||
@ -142,43 +173,45 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCanceled() {
|
public void onCanceled() {
|
||||||
DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageId);
|
if (messageId >= 0) {
|
||||||
|
DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageId);
|
||||||
|
|
||||||
long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId);
|
long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId);
|
||||||
Recipient recipient = DatabaseFactory.getThreadDatabase(context).getRecipientForThreadId(threadId);
|
Recipient recipient = DatabaseFactory.getThreadDatabase(context).getRecipientForThreadId(threadId);
|
||||||
|
|
||||||
if (threadId != -1 && recipient != null) {
|
if (threadId != -1 && recipient != null) {
|
||||||
MessageNotifier.notifyMessageDeliveryFailed(context, recipient, threadId);
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipient, threadId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean deliver(SmsMessageRecord message)
|
private boolean deliver(long messageId, Recipient recipient, SmsMessageRecord message)
|
||||||
throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException
|
throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// rotateSenderCertificateIfNecessary();
|
// rotateSenderCertificateIfNecessary();
|
||||||
|
SignalServiceAddress address = getPushAddress(recipient.getAddress());
|
||||||
SignalServiceAddress address = getPushAddress(message.getIndividualRecipient().getAddress());
|
Optional<byte[]> profileKey = getProfileKey(recipient);
|
||||||
Optional<byte[]> profileKey = getProfileKey(message.getIndividualRecipient());
|
Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient);
|
||||||
Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, message.getIndividualRecipient());
|
|
||||||
|
|
||||||
log(TAG, "Have access key to use: " + unidentifiedAccess.isPresent());
|
log(TAG, "Have access key to use: " + unidentifiedAccess.isPresent());
|
||||||
|
|
||||||
// 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 (message.isFriendRequest() || message.isEndSession()) {
|
if (isFriendRequest|| 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();
|
||||||
SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder()
|
SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder()
|
||||||
.withTimestamp(message.getDateSent())
|
.withTimestamp(message.getDateSent())
|
||||||
.withBody(message.getBody())
|
.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(message.isFriendRequest())
|
.asFriendRequest(isFriendRequest)
|
||||||
.withPreKeyBundle(preKeyBundle)
|
.withPreKeyBundle(preKeyBundle)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@ -203,7 +236,12 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
|
|||||||
public static class Factory implements Job.Factory<PushTextSendJob> {
|
public static class Factory implements Job.Factory<PushTextSendJob> {
|
||||||
@Override
|
@Override
|
||||||
public @NonNull PushTextSendJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
public @NonNull PushTextSendJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
||||||
return new PushTextSendJob(parameters, data.getLong(KEY_MESSAGE_ID));
|
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;
|
||||||
|
return new PushTextSendJob(parameters, templateMessageID, messageID, destination, isFriendRequest, frMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,28 @@ import org.whispersystems.signalservice.api.messages.SignalServiceContent
|
|||||||
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage
|
||||||
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
|
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope
|
||||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress
|
||||||
|
import org.whispersystems.signalservice.loki.api.LokiGroupChatAPI
|
||||||
import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation
|
import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation
|
||||||
import org.whispersystems.signalservice.loki.api.LokiStorageAPI
|
import org.whispersystems.signalservice.loki.api.LokiStorageAPI
|
||||||
import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus
|
import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus
|
||||||
|
|
||||||
|
fun isGroupChat(pubKey: String): Boolean {
|
||||||
|
return (LokiGroupChatAPI.publicChatServer == pubKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getFriends(context: Context, devices: Set<String>): Set<String> {
|
||||||
|
val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context)
|
||||||
|
|
||||||
|
return devices.mapNotNull { device ->
|
||||||
|
val address = Address.fromSerialized(device)
|
||||||
|
val recipient = Recipient.from(context, address, false)
|
||||||
|
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient)
|
||||||
|
if (threadID < 0) { return@mapNotNull null }
|
||||||
|
|
||||||
|
if (lokiThreadDatabase.getFriendRequestStatus(threadID) == LokiThreadFriendRequestStatus.FRIENDS) device else null
|
||||||
|
}.toSet()
|
||||||
|
}
|
||||||
|
|
||||||
fun shouldAutomaticallyBecomeFriendsWithDevice(pubKey: String, context: Context): Promise<Boolean, Unit> {
|
fun shouldAutomaticallyBecomeFriendsWithDevice(pubKey: String, context: Context): Promise<Boolean, Unit> {
|
||||||
val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context)
|
val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context)
|
||||||
val storageAPI = LokiStorageAPI.shared ?: return Promise.ofSuccess(false)
|
val storageAPI = LokiStorageAPI.shared ?: return Promise.ofSuccess(false)
|
||||||
|
@ -42,6 +42,7 @@ import org.thoughtcrime.securesms.jobs.SmsSendJob;
|
|||||||
import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository;
|
import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository;
|
||||||
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
|
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
|
||||||
import org.thoughtcrime.securesms.logging.Log;
|
import org.thoughtcrime.securesms.logging.Log;
|
||||||
|
import org.thoughtcrime.securesms.loki.UtilitiesKt;
|
||||||
import org.thoughtcrime.securesms.mms.MmsException;
|
import org.thoughtcrime.securesms.mms.MmsException;
|
||||||
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
||||||
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
||||||
@ -52,9 +53,15 @@ import org.thoughtcrime.securesms.util.Util;
|
|||||||
import org.whispersystems.libsignal.util.guava.Optional;
|
import org.whispersystems.libsignal.util.guava.Optional;
|
||||||
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
||||||
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
||||||
|
import org.whispersystems.signalservice.loki.api.LokiStorageAPI;
|
||||||
import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus;
|
import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import kotlin.Unit;
|
||||||
|
|
||||||
public class MessageSender {
|
public class MessageSender {
|
||||||
|
|
||||||
@ -203,8 +210,47 @@ public class MessageSender {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void sendTextPush(Context context, Recipient recipient, long messageId) {
|
private static void sendTextPush(Context context, Recipient recipient, long messageId) {
|
||||||
|
LokiStorageAPI storageAPI = LokiStorageAPI.Companion.getShared();
|
||||||
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
||||||
jobManager.add(new PushTextSendJob(messageId, recipient.getAddress()));
|
|
||||||
|
// Just send the message normally if the job manager failed or if it's a group message
|
||||||
|
String recipientPubKey = recipient.getAddress().serialize();
|
||||||
|
if (storageAPI == null || UtilitiesKt.isGroupChat(recipientPubKey)) {
|
||||||
|
jobManager.add(new PushTextSendJob(messageId, recipient.getAddress()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String ourPubKey = TextSecurePreferences.getLocalNumber(context);
|
||||||
|
|
||||||
|
// Get all the devices and run our logic on them
|
||||||
|
storageAPI.getAllDevices(recipientPubKey).success(devices -> {
|
||||||
|
// Remove our self if we intended this message to go to another recipient
|
||||||
|
if (!recipientPubKey.equals(ourPubKey)) { devices.remove(ourPubKey); }
|
||||||
|
|
||||||
|
Set<String> friends = UtilitiesKt.getFriends(context, devices);
|
||||||
|
Set<String> nonFriends = new HashSet<>(devices);
|
||||||
|
nonFriends.removeAll(friends);
|
||||||
|
|
||||||
|
// Send a normal message to our friends
|
||||||
|
for (String friend : friends) {
|
||||||
|
// If this message had the same recipient then point it to the correct message id otherwise point it to a non-existing message
|
||||||
|
long messageIdToUse = recipientPubKey.equals(friend) ? messageId : -1L;
|
||||||
|
jobManager.add(new PushTextSendJob(messageId, messageIdToUse, Address.fromSerialized(friend)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send friend requests to non friends
|
||||||
|
for (String stranger : nonFriends) {
|
||||||
|
// If this message had the same recipient then point it to the correct message id otherwise point it to a non-existing message
|
||||||
|
long messageIdToUse = recipientPubKey.equals(stranger) ? messageId : -1L;
|
||||||
|
|
||||||
|
// If we're friends with one of the devices then send out a default friend request message
|
||||||
|
boolean isFriendsWithAny = friends.size() > 0;
|
||||||
|
String defaultFriendRequestMessage = isFriendsWithAny ? "This is a friend request for devices linked to " + recipientPubKey : null;
|
||||||
|
jobManager.add(new PushTextSendJob(messageId, messageIdToUse, Address.fromSerialized(stranger), true, defaultFriendRequestMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Unit.INSTANCE;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sendMediaPush(Context context, Recipient recipient, long messageId) {
|
private static void sendMediaPush(Context context, Recipient recipient, long messageId) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user