2014-11-08 11:35:58 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
2019-10-07 10:02:15 +11:00
|
|
|
import android.support.annotation.Nullable;
|
2018-12-07 18:31:39 -08:00
|
|
|
import android.support.annotation.WorkerThread;
|
|
|
|
|
|
|
|
import com.annimon.stream.Stream;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2019-01-15 00:41:05 -08:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2018-12-07 18:31:39 -08:00
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2019-01-13 23:30:54 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2018-10-11 16:45:22 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase.UnidentifiedAccessMode;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2019-03-28 08:56:35 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
2018-12-07 18:31:39 -08:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobManager;
|
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2019-10-30 09:59:11 +11:00
|
|
|
import org.thoughtcrime.securesms.loki.MultiDeviceUtilities;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
2018-10-11 16:45:22 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2016-08-15 20:23:56 -07:00
|
|
|
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException;
|
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
2015-01-02 15:43:28 -08:00
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
2018-10-11 16:45:22 -07:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2019-09-18 17:33:34 +10:00
|
|
|
import org.whispersystems.libsignal.state.PreKeyBundle;
|
2017-08-14 18:11:13 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
2019-01-13 23:30:54 -08:00
|
|
|
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
|
2019-01-15 00:41:05 -08:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage.Preview;
|
2019-01-13 23:30:54 -08:00
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
|
2018-04-26 17:03:54 -07:00
|
|
|
import org.whispersystems.signalservice.api.messages.shared.SharedContact;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
2019-10-30 09:59:11 +11:00
|
|
|
import org.whispersystems.signalservice.loki.api.LokiStorageAPI;
|
2019-10-24 17:16:53 +11:00
|
|
|
import org.whispersystems.signalservice.loki.messaging.LokiSyncMessage;
|
2019-11-01 12:15:40 +11:00
|
|
|
import org.whispersystems.signalservice.loki.utilities.PromiseUtil;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2015-11-12 11:14:45 -08:00
|
|
|
import java.io.FileNotFoundException;
|
2014-11-08 11:35:58 -08:00
|
|
|
import java.io.IOException;
|
2019-11-14 10:44:55 +11:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
2019-01-15 00:41:05 -08:00
|
|
|
import java.util.LinkedList;
|
2014-11-08 11:35:58 -08:00
|
|
|
import java.util.List;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
public class PushMediaSendJob extends PushSendJob implements InjectableType {
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static final String KEY = "PushMediaSendJob";
|
2015-06-24 18:26:51 -07:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static final String TAG = PushMediaSendJob.class.getSimpleName();
|
|
|
|
|
2019-10-07 10:02:15 +11:00
|
|
|
private static final String KEY_TEMPLATE_MESSAGE_ID = "template_message_id";
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_MESSAGE_ID = "message_id";
|
2019-10-07 10:02:15 +11:00
|
|
|
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";
|
2019-10-24 17:16:53 +11:00
|
|
|
private static final String KEY_SHOULD_SEND_SYNC_MESSAGE = "should_send_sync_message";
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
@Inject SignalServiceMessageSender messageSender;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
2019-10-07 16:53:26 +11:00
|
|
|
private long messageId; // The message ID
|
|
|
|
private long templateMessageId; // The message ID of the message to template this send job from
|
2019-10-07 10:02:15 +11:00
|
|
|
|
2019-10-07 16:53:26 +11:00
|
|
|
// Loki - Multi device
|
2019-10-07 10:02:15 +11:00
|
|
|
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 String customFriendRequestMessage; // If this isn't set then we use the message body
|
2019-10-24 17:16:53 +11:00
|
|
|
private boolean shouldSendSyncMessage;
|
2019-10-07 10:02:15 +11:00
|
|
|
|
|
|
|
public PushMediaSendJob(long messageId, Address destination) { this(messageId, messageId, destination); }
|
2019-10-24 17:16:53 +11:00
|
|
|
public PushMediaSendJob(long templateMessageId, long messageId, Address destination) { this(templateMessageId, messageId, destination, false, null, false); }
|
|
|
|
public PushMediaSendJob(long templateMessageId, long messageId, Address destination, boolean isFriendRequest, String customFriendRequestMessage, boolean shouldSendSyncMessage) {
|
|
|
|
this(constructParameters(destination), templateMessageId, messageId, destination, isFriendRequest, customFriendRequestMessage, shouldSendSyncMessage);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2019-10-24 17:16:53 +11:00
|
|
|
private PushMediaSendJob(@NonNull Job.Parameters parameters, long templateMessageId, long messageId, Address destination, boolean isFriendRequest, String customFriendRequestMessage, boolean shouldSendSyncMessage) {
|
2019-03-28 08:56:35 -07:00
|
|
|
super(parameters);
|
2019-10-07 10:02:15 +11:00
|
|
|
this.templateMessageId = templateMessageId;
|
2014-11-08 11:35:58 -08:00
|
|
|
this.messageId = messageId;
|
2019-10-07 10:02:15 +11:00
|
|
|
this.destination = destination;
|
|
|
|
this.isFriendRequest = isFriendRequest;
|
|
|
|
this.customFriendRequestMessage = customFriendRequestMessage;
|
2019-10-24 17:16:53 +11:00
|
|
|
this.shouldSendSyncMessage = shouldSendSyncMessage;
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2019-10-24 17:16:53 +11:00
|
|
|
public static void enqueue(@NonNull Context context, @NonNull JobManager jobManager, long messageId, @NonNull Address destination, boolean shouldSendSyncMessage) {
|
2019-11-14 10:44:55 +11:00
|
|
|
enqueue(context, jobManager, messageId, messageId, destination, false, null, shouldSendSyncMessage);
|
2019-10-07 10:02:15 +11:00
|
|
|
}
|
|
|
|
|
2019-11-14 10:44:55 +11:00
|
|
|
public static void enqueue(@NonNull Context context, @NonNull JobManager jobManager, long templateMessageId, long messageId, @NonNull Address destination, Boolean isFriendRequest, @Nullable String customFriendRequestMessage, boolean shouldSendSyncMessage) {
|
|
|
|
enqueue(context, jobManager, Collections.singletonList(new PushMediaSendJob(templateMessageId, messageId, destination, isFriendRequest, customFriendRequestMessage, shouldSendSyncMessage)));
|
2019-10-07 10:02:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@WorkerThread
|
2019-11-14 10:44:55 +11:00
|
|
|
public static void enqueue(@NonNull Context context, @NonNull JobManager jobManager, List<PushMediaSendJob> jobs) {
|
|
|
|
if (jobs.size() == 0) { return; }
|
|
|
|
PushMediaSendJob first = jobs.get(0);
|
|
|
|
long messageId = first.templateMessageId;
|
2018-12-07 18:31:39 -08:00
|
|
|
try {
|
2019-11-14 10:44:55 +11:00
|
|
|
List<AttachmentUploadJob> attachmentJobs = getAttachmentUploadJobs(context, messageId, first.destination);
|
2018-12-07 18:31:39 -08:00
|
|
|
|
|
|
|
if (attachmentJobs.isEmpty()) {
|
2019-11-14 10:44:55 +11:00
|
|
|
for (PushMediaSendJob job : jobs) { jobManager.add(job); }
|
2018-12-07 18:31:39 -08:00
|
|
|
} else {
|
|
|
|
jobManager.startChain(attachmentJobs)
|
2019-11-14 10:44:55 +11:00
|
|
|
.then((List<Job>)(List)jobs)
|
|
|
|
.enqueue();
|
2018-12-07 18:31:39 -08:00
|
|
|
}
|
|
|
|
} catch (NoSuchMessageException | MmsException e) {
|
|
|
|
Log.w(TAG, "Failed to enqueue message.", e);
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 10:44:55 +11:00
|
|
|
public static List<AttachmentUploadJob> getAttachmentUploadJobs(@NonNull Context context, long messageId, @NonNull Address destination)
|
|
|
|
throws NoSuchMessageException, MmsException
|
|
|
|
{
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
|
|
|
|
List<Attachment> attachments = new LinkedList<>();
|
|
|
|
|
|
|
|
attachments.addAll(message.getAttachments());
|
|
|
|
attachments.addAll(Stream.of(message.getLinkPreviews()).filter(p -> p.getThumbnail().isPresent()).map(p -> p.getThumbnail().get()).toList());
|
|
|
|
attachments.addAll(Stream.of(message.getSharedContacts()).filter(c -> c.getAvatar() != null).map(c -> c.getAvatar().getAttachment()).withoutNulls().toList());
|
|
|
|
|
|
|
|
return Stream.of(attachments).map(a -> new AttachmentUploadJob(((DatabaseAttachment) a).getAttachmentId(), destination)).toList();
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull Data serialize() {
|
2019-10-07 10:02:15 +11:00
|
|
|
Data.Builder builder = new Data.Builder()
|
2019-10-24 17:16:53 +11:00
|
|
|
.putLong(KEY_TEMPLATE_MESSAGE_ID, templateMessageId)
|
|
|
|
.putLong(KEY_MESSAGE_ID, messageId)
|
|
|
|
.putString(KEY_DESTINATION, destination.serialize())
|
|
|
|
.putBoolean(KEY_IS_FRIEND_REQUEST, isFriendRequest)
|
|
|
|
.putBoolean(KEY_SHOULD_SEND_SYNC_MESSAGE, shouldSendSyncMessage);
|
2019-10-07 10:02:15 +11:00
|
|
|
|
|
|
|
if (customFriendRequestMessage != null) { builder.putString(KEY_CUSTOM_FR_MESSAGE, customFriendRequestMessage); }
|
|
|
|
return builder.build();
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2018-10-11 16:45:22 -07:00
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public void onAdded() {
|
2018-10-11 16:45:22 -07:00
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSending(messageId);
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
@Override
|
2018-02-01 19:22:48 -08:00
|
|
|
public void onPushSend()
|
2015-01-15 13:35:35 -08:00
|
|
|
throws RetryLaterException, MmsException, NoSuchMessageException,
|
2015-03-03 11:44:49 -08:00
|
|
|
UndeliverableMessageException
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
2016-08-15 20:23:56 -07:00
|
|
|
ExpiringMessageManager expirationManager = ApplicationContext.getInstance(context).getExpiringMessageManager();
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
2019-10-07 10:02:15 +11:00
|
|
|
OutgoingMediaMessage message = database.getOutgoingMessage(templateMessageId);
|
2019-09-18 17:33:34 +10:00
|
|
|
|
2019-10-07 10:02:15 +11:00
|
|
|
if (messageId >= 0 && database.isSent(messageId)) {
|
2018-12-06 12:14:20 -08:00
|
|
|
warn(TAG, "Message " + messageId + " was already sent. Ignoring.");
|
2018-11-14 11:39:23 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
try {
|
2018-12-06 12:14:20 -08:00
|
|
|
log(TAG, "Sending message: " + messageId);
|
2019-01-13 23:30:54 -08:00
|
|
|
|
2019-10-07 10:02:15 +11:00
|
|
|
Recipient recipient = Recipient.from(context, destination, false);
|
2018-10-30 16:59:00 -07:00
|
|
|
byte[] profileKey = recipient.getProfileKey();
|
|
|
|
UnidentifiedAccessMode accessMode = recipient.getUnidentifiedAccessMode();
|
2018-08-02 09:50:36 -04:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
boolean unidentified = deliver(message);
|
2018-10-30 16:59:00 -07:00
|
|
|
|
2019-10-07 10:02:15 +11:00
|
|
|
if (messageId >= 0) {
|
|
|
|
database.markAsSent(messageId, true);
|
|
|
|
markAttachmentsUploaded(messageId, message.getAttachments());
|
|
|
|
database.markUnidentified(messageId, unidentified);
|
|
|
|
}
|
2016-08-15 20:23:56 -07:00
|
|
|
|
2019-01-13 23:30:54 -08:00
|
|
|
if (recipient.isLocalNumber()) {
|
|
|
|
SyncMessageId id = new SyncMessageId(recipient.getAddress(), message.getSentTimeMillis());
|
|
|
|
DatabaseFactory.getMmsSmsDatabase(context).incrementDeliveryReceiptCount(id, System.currentTimeMillis());
|
|
|
|
DatabaseFactory.getMmsSmsDatabase(context).incrementReadReceiptCount(id, System.currentTimeMillis());
|
|
|
|
}
|
|
|
|
|
2018-10-11 16:45:22 -07:00
|
|
|
if (TextSecurePreferences.isUnidentifiedDeliveryEnabled(context)) {
|
2018-10-30 16:59:00 -07:00
|
|
|
if (unidentified && accessMode == UnidentifiedAccessMode.UNKNOWN && profileKey == null) {
|
2018-12-06 12:14:20 -08:00
|
|
|
log(TAG, "Marking recipient as UD-unrestricted following a UD send.");
|
2018-10-30 16:59:00 -07:00
|
|
|
DatabaseFactory.getRecipientDatabase(context).setUnidentifiedAccessMode(recipient, UnidentifiedAccessMode.UNRESTRICTED);
|
|
|
|
} else if (unidentified && accessMode == UnidentifiedAccessMode.UNKNOWN) {
|
2018-12-06 12:14:20 -08:00
|
|
|
log(TAG, "Marking recipient as UD-enabled following a UD send.");
|
2018-10-11 16:45:22 -07:00
|
|
|
DatabaseFactory.getRecipientDatabase(context).setUnidentifiedAccessMode(recipient, UnidentifiedAccessMode.ENABLED);
|
|
|
|
} else if (!unidentified && accessMode != UnidentifiedAccessMode.DISABLED) {
|
2018-12-06 12:14:20 -08:00
|
|
|
log(TAG, "Marking recipient as UD-disabled following a non-UD send.");
|
2018-10-11 16:45:22 -07:00
|
|
|
DatabaseFactory.getRecipientDatabase(context).setUnidentifiedAccessMode(recipient, UnidentifiedAccessMode.DISABLED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 10:02:15 +11:00
|
|
|
if (messageId > 0 && message.getExpiresIn() > 0 && !message.isExpirationUpdate()) {
|
2016-08-15 20:23:56 -07:00
|
|
|
database.markExpireStarted(messageId);
|
|
|
|
expirationManager.scheduleDeletion(messageId, true, message.getExpiresIn());
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:14:20 -08:00
|
|
|
log(TAG, "Sent message: " + messageId);
|
2018-08-02 09:50:36 -04:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (InsecureFallbackApprovalException ifae) {
|
2018-12-06 12:14:20 -08:00
|
|
|
warn(TAG, "Failure", ifae);
|
2019-10-07 10:02:15 +11:00
|
|
|
if (messageId >= 0) {
|
|
|
|
database.markAsPendingInsecureSmsFallback(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(false));
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (UntrustedIdentityException uie) {
|
2018-12-06 12:14:20 -08:00
|
|
|
warn(TAG, "Failure", uie);
|
2017-07-26 09:59:15 -07:00
|
|
|
database.addMismatchedIdentity(messageId, Address.fromSerialized(uie.getE164Number()), uie.getIdentityKey());
|
2014-11-08 11:35:58 -08:00
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public boolean onShouldRetry(@NonNull Exception exception) {
|
2019-09-13 11:56:36 +10:00
|
|
|
// Loki - Disable since we have our own retrying
|
|
|
|
// if (exception instanceof RetryLaterException) return true;
|
2014-11-11 19:57:53 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
2019-10-07 10:02:15 +11:00
|
|
|
if (messageId >= 0) {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
private boolean deliver(OutgoingMediaMessage message)
|
2015-03-11 14:23:45 -07:00
|
|
|
throws RetryLaterException, InsecureFallbackApprovalException, UntrustedIdentityException,
|
2015-01-02 15:43:28 -08:00
|
|
|
UndeliverableMessageException
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
|
|
|
try {
|
2019-10-07 10:02:15 +11:00
|
|
|
Recipient recipient = Recipient.from(context, destination, false);
|
|
|
|
SignalServiceAddress address = getPushAddress(recipient.getAddress());
|
2019-04-17 10:21:30 -04:00
|
|
|
List<Attachment> attachments = Stream.of(message.getAttachments()).filterNot(Attachment::isSticker).toList();
|
|
|
|
List<SignalServiceAttachment> serviceAttachments = getAttachmentPointersFor(attachments);
|
|
|
|
Optional<byte[]> profileKey = getProfileKey(message.getRecipient());
|
|
|
|
Optional<SignalServiceDataMessage.Quote> quote = getQuoteFor(message);
|
|
|
|
Optional<SignalServiceDataMessage.Sticker> sticker = getStickerFor(message);
|
|
|
|
List<SharedContact> sharedContacts = getSharedContactsFor(message);
|
|
|
|
List<Preview> previews = getPreviewsFor(message);
|
2019-09-18 17:33:34 +10:00
|
|
|
|
|
|
|
// Loki - Include a pre key bundle if the message is a friend request or an end session message
|
2019-10-07 10:02:15 +11:00
|
|
|
PreKeyBundle preKeyBundle = isFriendRequest ? DatabaseFactory.getLokiPreKeyBundleDatabase(context).generatePreKeyBundle(address.getNumber()) : null;
|
|
|
|
String body = (isFriendRequest && customFriendRequestMessage != null) ? customFriendRequestMessage : message.getBody();
|
2019-09-18 17:33:34 +10:00
|
|
|
SignalServiceDataMessage mediaMessage = SignalServiceDataMessage.newBuilder()
|
2019-10-07 10:02:15 +11:00
|
|
|
.withBody(body)
|
2019-09-18 17:33:34 +10:00
|
|
|
.withAttachments(serviceAttachments)
|
|
|
|
.withTimestamp(message.getSentTimeMillis())
|
|
|
|
.withExpiration((int)(message.getExpiresIn() / 1000))
|
|
|
|
.withProfileKey(profileKey.orNull())
|
|
|
|
.withQuote(quote.orNull())
|
|
|
|
.withSticker(sticker.orNull())
|
|
|
|
.withSharedContacts(sharedContacts)
|
|
|
|
.withPreviews(previews)
|
|
|
|
.asExpirationUpdate(message.isExpirationUpdate())
|
|
|
|
.withPreKeyBundle(preKeyBundle)
|
2019-10-07 10:02:15 +11:00
|
|
|
.asFriendRequest(isFriendRequest)
|
2019-09-18 17:33:34 +10:00
|
|
|
.build();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2019-01-13 23:30:54 -08:00
|
|
|
if (address.getNumber().equals(TextSecurePreferences.getLocalNumber(context))) {
|
|
|
|
Optional<UnidentifiedAccessPair> syncAccess = UnidentifiedAccessUtil.getAccessForSync(context);
|
|
|
|
SignalServiceSyncMessage syncMessage = buildSelfSendSyncMessage(context, mediaMessage, syncAccess);
|
|
|
|
|
2019-10-29 14:03:32 +11:00
|
|
|
messageSender.sendMessage(templateMessageId, syncMessage, syncAccess);
|
2019-01-13 23:30:54 -08:00
|
|
|
return syncAccess.isPresent();
|
|
|
|
} else {
|
2019-10-24 17:16:53 +11:00
|
|
|
LokiSyncMessage syncMessage = null;
|
|
|
|
if (shouldSendSyncMessage) {
|
|
|
|
// Set the sync message destination the primary device, this way it will show that we sent a message to the primary device and not a secondary device
|
2019-11-01 12:15:40 +11:00
|
|
|
String primaryDevice = PromiseUtil.get(LokiStorageAPI.shared.getPrimaryDevicePublicKey(address.getNumber()), null);
|
2019-10-24 17:16:53 +11:00
|
|
|
SignalServiceAddress primaryAddress = primaryDevice == null ? address : new SignalServiceAddress(primaryDevice);
|
|
|
|
// We also need to use the original message id and not -1
|
|
|
|
syncMessage = new LokiSyncMessage(primaryAddress, templateMessageId);
|
|
|
|
}
|
|
|
|
return messageSender.sendMessage(messageId, address, UnidentifiedAccessUtil.getAccessFor(context, recipient), mediaMessage, Optional.fromNullable(syncMessage)).getSuccess().isUnidentified();
|
2019-01-13 23:30:54 -08:00
|
|
|
}
|
2017-07-26 09:59:15 -07:00
|
|
|
} catch (UnregisteredUserException e) {
|
2018-12-06 12:14:20 -08:00
|
|
|
warn(TAG, e);
|
2015-03-11 14:23:45 -07:00
|
|
|
throw new InsecureFallbackApprovalException(e);
|
2015-11-12 11:14:45 -08:00
|
|
|
} catch (FileNotFoundException e) {
|
2018-12-06 12:14:20 -08:00
|
|
|
warn(TAG, e);
|
2015-11-12 11:14:45 -08:00
|
|
|
throw new UndeliverableMessageException(e);
|
2015-03-03 11:44:49 -08:00
|
|
|
} catch (IOException e) {
|
2018-12-06 12:14:20 -08:00
|
|
|
warn(TAG, e);
|
2015-03-11 14:23:45 -07:00
|
|
|
throw new RetryLaterException(e);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 08:56:35 -07:00
|
|
|
|
|
|
|
public static final class Factory implements Job.Factory<PushMediaSendJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull PushMediaSendJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
2019-10-07 10:02:15 +11:00
|
|
|
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);
|
2019-10-24 17:16:53 +11:00
|
|
|
boolean shouldSendSyncMessage = data.getBoolean(KEY_SHOULD_SEND_SYNC_MESSAGE);
|
2019-10-07 10:02:15 +11:00
|
|
|
String frMessage = data.hasString(KEY_CUSTOM_FR_MESSAGE) ? data.getString(KEY_CUSTOM_FR_MESSAGE) : null;
|
2019-10-24 17:16:53 +11:00
|
|
|
return new PushMediaSendJob(parameters, templateMessageID, messageID, destination, isFriendRequest, frMessage, shouldSendSyncMessage);
|
2019-03-28 08:56:35 -07:00
|
|
|
}
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|