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;
|
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;
|
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;
|
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;
|
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-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();
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_MESSAGE_ID = "message_id";
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
@Inject SignalServiceMessageSender messageSender;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private long messageId;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public PushMediaSendJob(long messageId, Address destination) {
|
|
|
|
this(constructParameters(destination), messageId);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private PushMediaSendJob(Job.Parameters parameters, long messageId) {
|
|
|
|
super(parameters);
|
2014-11-08 11:35:58 -08:00
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:39 -08:00
|
|
|
@WorkerThread
|
|
|
|
public static void enqueue(@NonNull Context context, @NonNull JobManager jobManager, long messageId, @NonNull Address destination) {
|
|
|
|
try {
|
2019-01-15 00:41:05 -08:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
|
|
|
|
List<Attachment> attachments = new LinkedList<>();
|
|
|
|
|
|
|
|
attachments.addAll(message.getAttachments());
|
2019-09-09 13:53:23 +10:00
|
|
|
// attachments.addAll(Stream.of(message.getLinkPreviews()).filter(p -> p.getThumbnail().isPresent()).map(p -> p.getThumbnail().get()).toList());
|
2019-01-15 00:41:05 -08:00
|
|
|
attachments.addAll(Stream.of(message.getSharedContacts()).filter(c -> c.getAvatar() != null).map(c -> c.getAvatar().getAttachment()).withoutNulls().toList());
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
List<AttachmentUploadJob> attachmentJobs = Stream.of(attachments).map(a -> new AttachmentUploadJob(((DatabaseAttachment) a).getAttachmentId())).toList();
|
2018-12-07 18:31:39 -08:00
|
|
|
|
|
|
|
if (attachmentJobs.isEmpty()) {
|
2019-03-28 08:56:35 -07:00
|
|
|
jobManager.add(new PushMediaSendJob(messageId, destination));
|
2018-12-07 18:31:39 -08:00
|
|
|
} else {
|
|
|
|
jobManager.startChain(attachmentJobs)
|
2019-03-28 08:56:35 -07:00
|
|
|
.then(new PushMediaSendJob(messageId, destination))
|
|
|
|
.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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return new Data.Builder().putLong(KEY_MESSAGE_ID, messageId).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);
|
2018-01-24 19:17:44 -08:00
|
|
|
OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-11-14 11:39:23 -08:00
|
|
|
if (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
|
|
|
|
2018-10-30 16:59:00 -07:00
|
|
|
Recipient recipient = message.getRecipient().resolve();
|
|
|
|
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
|
|
|
|
2016-12-21 09:49:01 -08:00
|
|
|
database.markAsSent(messageId, true);
|
2015-10-12 18:25:05 -07:00
|
|
|
markAttachmentsUploaded(messageId, message.getAttachments());
|
2018-05-22 02:13:10 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
if (message.getExpiresIn() > 0 && !message.isExpirationUpdate()) {
|
|
|
|
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);
|
2014-11-08 11:35:58 -08:00
|
|
|
database.markAsPendingInsecureSmsFallback(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
2019-03-28 08:56:35 -07:00
|
|
|
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() {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-08-01 08:56:00 -07:00
|
|
|
if (message.getRecipient() == null) {
|
2015-10-26 11:02:23 -07:00
|
|
|
throw new UndeliverableMessageException("No destination address.");
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
try {
|
2019-07-24 15:05:39 +10:00
|
|
|
// rotateSenderCertificateIfNecessary();
|
2018-11-21 17:26:06 -08:00
|
|
|
|
2019-04-17 10:21:30 -04:00
|
|
|
SignalServiceAddress address = getPushAddress(message.getRecipient().getAddress());
|
|
|
|
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);
|
|
|
|
SignalServiceDataMessage mediaMessage = SignalServiceDataMessage.newBuilder()
|
2018-12-07 18:31:39 -08:00
|
|
|
.withBody(message.getBody())
|
|
|
|
.withAttachments(serviceAttachments)
|
|
|
|
.withTimestamp(message.getSentTimeMillis())
|
|
|
|
.withExpiration((int)(message.getExpiresIn() / 1000))
|
|
|
|
.withProfileKey(profileKey.orNull())
|
|
|
|
.withQuote(quote.orNull())
|
2019-04-17 10:21:30 -04:00
|
|
|
.withSticker(sticker.orNull())
|
2018-12-07 18:31:39 -08:00
|
|
|
.withSharedContacts(sharedContacts)
|
2019-01-15 00:41:05 -08:00
|
|
|
.withPreviews(previews)
|
2018-12-07 18:31:39 -08:00
|
|
|
.asExpirationUpdate(message.isExpirationUpdate())
|
|
|
|
.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-06-24 16:00:15 +10:00
|
|
|
messageSender.sendMessage(messageId, syncMessage, syncAccess);
|
2019-01-13 23:30:54 -08:00
|
|
|
return syncAccess.isPresent();
|
|
|
|
} else {
|
2019-06-24 16:00:15 +10:00
|
|
|
return messageSender.sendMessage(messageId, address, UnidentifiedAccessUtil.getAccessFor(context, message.getRecipient()), mediaMessage).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) {
|
|
|
|
return new PushMediaSendJob(parameters, data.getLong(KEY_MESSAGE_ID));
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|