2014-11-08 11:35:58 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-07-26 09:59:15 -07:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
import com.annimon.stream.Collectors;
|
2017-09-30 08:45:45 -07:00
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2016-08-04 00:24:21 +02:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
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;
|
2017-09-30 08:45:45 -07:00
|
|
|
import org.thoughtcrime.securesms.database.GroupReceiptDatabase.GroupReceiptInfo;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.thoughtcrime.securesms.database.documents.IdentityKeyMismatch;
|
2015-01-15 13:35:35 -08:00
|
|
|
import org.thoughtcrime.securesms.database.documents.NetworkFailure;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2016-08-04 00:24:21 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.MediaConstraints;
|
2017-05-08 15:32:59 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage;
|
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
2015-04-07 14:16:05 -07:00
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2017-08-22 11:51:01 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.whispersystems.signalservice.api.messages.SendMessageResult;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
|
2018-02-07 14:01:37 -08:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage.Quote;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
|
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.util.InvalidNumberException;
|
|
|
|
import org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-05-22 02:13:10 -07:00
|
|
|
import java.util.Collections;
|
2014-11-08 11:35:58 -08:00
|
|
|
import java.util.List;
|
2018-05-22 02:13:10 -07:00
|
|
|
import java.util.Set;
|
2018-07-05 09:30:29 -07:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.Data;
|
2018-11-27 12:34:42 -08:00
|
|
|
import androidx.work.WorkerParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
public class PushGroupSendJob extends PushSendJob implements InjectableType {
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2015-06-24 18:26:51 -07:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static final String TAG = PushGroupSendJob.class.getSimpleName();
|
|
|
|
|
2017-09-15 22:38:53 -07:00
|
|
|
@Inject transient SignalServiceMessageSender messageSender;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_MESSAGE_ID = "message_id";
|
|
|
|
private static final String KEY_FILTER_ADDRESS = "filter_address";
|
|
|
|
|
|
|
|
private long messageId;
|
|
|
|
private long filterRecipientId; // Deprecated
|
|
|
|
private String filterAddress;
|
|
|
|
|
2018-11-27 12:34:42 -08:00
|
|
|
public PushGroupSendJob(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
|
|
|
|
super(context, workerParameters);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public PushGroupSendJob(Context context, long messageId, @NonNull Address destination, @Nullable Address filterAddress) {
|
2014-11-08 11:35:58 -08:00
|
|
|
super(context, JobParameters.newBuilder()
|
2017-07-26 09:59:15 -07:00
|
|
|
.withGroupId(destination.toGroupString())
|
2018-08-09 10:15:43 -04:00
|
|
|
.withNetworkRequirement()
|
2018-07-05 09:30:29 -07:00
|
|
|
.withRetryDuration(TimeUnit.DAYS.toMillis(1))
|
2014-11-08 11:35:58 -08:00
|
|
|
.create());
|
|
|
|
|
2015-01-15 13:35:35 -08:00
|
|
|
this.messageId = messageId;
|
2017-07-26 09:59:15 -07:00
|
|
|
this.filterAddress = filterAddress == null ? null :filterAddress.toPhoneString();
|
|
|
|
this.filterRecipientId = -1;
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 10:15:43 -04:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
messageId = data.getLong(KEY_MESSAGE_ID);
|
2018-10-02 12:31:12 -07:00
|
|
|
filterAddress = data.getString(KEY_FILTER_ADDRESS);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.putLong(KEY_MESSAGE_ID, messageId)
|
|
|
|
.putString(KEY_FILTER_ADDRESS, filterAddress)
|
|
|
|
.build();
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2018-10-11 16:45:22 -07:00
|
|
|
@Override
|
|
|
|
protected void onAdded() {
|
|
|
|
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()
|
2018-05-22 02:13:10 -07:00
|
|
|
throws IOException, MmsException, NoSuchMessageException, RetryLaterException
|
2015-01-15 13:35:35 -08:00
|
|
|
{
|
2018-05-22 02:13:10 -07:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
|
|
|
|
List<NetworkFailure> existingNetworkFailures = message.getNetworkFailures();
|
|
|
|
List<IdentityKeyMismatch> existingIdentityMismatches = message.getIdentityKeyMismatches();
|
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
|
|
|
log(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);
|
2018-10-11 16:45:22 -07:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
List<Address> target;
|
|
|
|
|
|
|
|
if (filterAddress != null) target = Collections.singletonList(Address.fromSerialized(filterAddress));
|
|
|
|
else if (!existingNetworkFailures.isEmpty()) target = Stream.of(existingNetworkFailures).map(NetworkFailure::getAddress).toList();
|
|
|
|
else target = getGroupMessageRecipients(message.getRecipient().getAddress().toGroupString(), messageId);
|
|
|
|
|
|
|
|
List<SendMessageResult> results = deliver(message, target);
|
|
|
|
List<NetworkFailure> networkFailures = Stream.of(results).filter(SendMessageResult::isNetworkFailure).map(result -> new NetworkFailure(Address.fromSerialized(result.getAddress().getNumber()))).toList();
|
|
|
|
List<IdentityKeyMismatch> identityMismatches = Stream.of(results).filter(result -> result.getIdentityFailure() != null).map(result -> new IdentityKeyMismatch(Address.fromSerialized(result.getAddress().getNumber()), result.getIdentityFailure().getIdentityKey())).toList();
|
|
|
|
Set<Address> successAddresses = Stream.of(results).filter(result -> result.getSuccess() != null).map(result -> Address.fromSerialized(result.getAddress().getNumber())).collect(Collectors.toSet());
|
|
|
|
List<NetworkFailure> resolvedNetworkFailures = Stream.of(existingNetworkFailures).filter(failure -> successAddresses.contains(failure.getAddress())).toList();
|
|
|
|
List<IdentityKeyMismatch> resolvedIdentityFailures = Stream.of(existingIdentityMismatches).filter(failure -> successAddresses.contains(failure.getAddress())).toList();
|
|
|
|
List<SendMessageResult> successes = Stream.of(results).filter(result -> result.getSuccess() != null).toList();
|
|
|
|
|
|
|
|
for (NetworkFailure resolvedFailure : resolvedNetworkFailures) {
|
|
|
|
database.removeFailure(messageId, resolvedFailure);
|
|
|
|
existingNetworkFailures.remove(resolvedFailure);
|
2016-08-15 20:23:56 -07:00
|
|
|
}
|
2018-08-02 09:50:36 -04:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
for (IdentityKeyMismatch resolvedIdentity : resolvedIdentityFailures) {
|
|
|
|
database.removeMismatchedIdentity(messageId, resolvedIdentity.getAddress(), resolvedIdentity.getIdentityKey());
|
|
|
|
existingIdentityMismatches.remove(resolvedIdentity);
|
|
|
|
}
|
2015-01-15 13:35:35 -08:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
if (!networkFailures.isEmpty()) {
|
|
|
|
database.addFailures(messageId, networkFailures);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
for (IdentityKeyMismatch mismatch : identityMismatches) {
|
|
|
|
database.addMismatchedIdentity(messageId, mismatch.getAddress(), mismatch.getIdentityKey());
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
for (SendMessageResult success : successes) {
|
|
|
|
DatabaseFactory.getGroupReceiptDatabase(context).setUnidentified(Address.fromSerialized(success.getAddress().getNumber()),
|
|
|
|
messageId,
|
|
|
|
success.getSuccess().isUnidentified());
|
|
|
|
}
|
2015-01-15 13:35:35 -08:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
if (existingNetworkFailures.isEmpty() && networkFailures.isEmpty() && identityMismatches.isEmpty() && existingIdentityMismatches.isEmpty()) {
|
2016-12-21 09:49:01 -08:00
|
|
|
database.markAsSent(messageId, true);
|
2018-05-22 02:13:10 -07:00
|
|
|
|
2016-03-09 00:18:35 +01:00
|
|
|
markAttachmentsUploaded(messageId, message.getAttachments());
|
2018-04-23 17:46:57 -07:00
|
|
|
|
|
|
|
if (message.getExpiresIn() > 0 && !message.isExpirationUpdate()) {
|
|
|
|
database.markExpireStarted(messageId);
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getExpiringMessageManager()
|
|
|
|
.scheduleDeletion(messageId, true, message.getExpiresIn());
|
|
|
|
}
|
2018-05-22 02:13:10 -07:00
|
|
|
} else if (!networkFailures.isEmpty()) {
|
|
|
|
throw new RetryLaterException();
|
|
|
|
} else if (!identityMismatches.isEmpty()) {
|
2016-03-09 00:18:35 +01:00
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
}
|
2018-05-22 02:13:10 -07:00
|
|
|
|
|
|
|
} catch (InvalidNumberException | RecipientFormattingException | UndeliverableMessageException e) {
|
2018-12-06 12:14:20 -08:00
|
|
|
warn(TAG, e);
|
2018-05-22 02:13:10 -07:00
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
|
|
|
} catch (UntrustedIdentityException e) {
|
2018-12-06 12:14:20 -08:00
|
|
|
warn(TAG, e);
|
2018-05-22 02:13:10 -07:00
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
notifyMediaMessageDeliveryFailed(context, messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public boolean onShouldRetry(Exception exception) {
|
2018-05-22 02:13:10 -07:00
|
|
|
if (exception instanceof IOException) return true;
|
|
|
|
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
|
2014-11-11 19:57:53 -08:00
|
|
|
public void onCanceled() {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markAsSentFailed(messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
private List<SendMessageResult> deliver(OutgoingMediaMessage message, @NonNull List<Address> destinations)
|
2015-04-07 14:16:05 -07:00
|
|
|
throws IOException, RecipientFormattingException, InvalidNumberException,
|
2018-05-22 02:13:10 -07:00
|
|
|
UndeliverableMessageException, UntrustedIdentityException
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
2018-11-21 17:26:06 -08:00
|
|
|
rotateSenderCertificateIfNecessary();
|
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
String groupId = message.getRecipient().getAddress().toGroupString();
|
2017-08-22 11:51:01 -07:00
|
|
|
Optional<byte[]> profileKey = getProfileKey(message.getRecipient());
|
2017-05-08 15:32:59 -07:00
|
|
|
MediaConstraints mediaConstraints = MediaConstraints.getPushMediaConstraints();
|
2018-03-19 11:22:39 -07:00
|
|
|
List<Attachment> scaledAttachments = scaleAndStripExifFromAttachments(mediaConstraints, message.getAttachments());
|
2018-01-24 19:17:44 -08:00
|
|
|
List<SignalServiceAttachment> attachmentStreams = getAttachmentsFor(scaledAttachments);
|
2018-02-07 14:01:37 -08:00
|
|
|
Optional<Quote> quote = getQuoteFor(message);
|
2018-04-26 17:03:54 -07:00
|
|
|
List<SharedContact> sharedContacts = getSharedContactsFor(message);
|
2018-05-22 02:13:10 -07:00
|
|
|
List<SignalServiceAddress> addresses = Stream.of(destinations).map(this::getPushAddress).toList();
|
2017-07-26 09:59:15 -07:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
List<Optional<UnidentifiedAccessPair>> unidentifiedAccess = Stream.of(addresses)
|
|
|
|
.map(address -> Address.fromSerialized(address.getNumber()))
|
|
|
|
.map(address -> Recipient.from(context, address, false))
|
|
|
|
.map(recipient -> UnidentifiedAccessUtil.getAccessFor(context, recipient))
|
|
|
|
.toList();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
if (message.isGroup()) {
|
|
|
|
OutgoingGroupMediaMessage groupMessage = (OutgoingGroupMediaMessage) message;
|
|
|
|
GroupContext groupContext = groupMessage.getGroupContext();
|
2016-08-04 00:24:21 +02:00
|
|
|
SignalServiceAttachment avatar = attachmentStreams.isEmpty() ? null : attachmentStreams.get(0);
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceGroup.Type type = groupMessage.isGroupQuit() ? SignalServiceGroup.Type.QUIT : SignalServiceGroup.Type.UPDATE;
|
2017-08-01 08:56:00 -07:00
|
|
|
SignalServiceGroup group = new SignalServiceGroup(type, GroupUtil.getDecodedId(groupId), groupContext.getName(), groupContext.getMembersList(), avatar);
|
2017-08-22 11:51:01 -07:00
|
|
|
SignalServiceDataMessage groupDataMessage = SignalServiceDataMessage.newBuilder()
|
|
|
|
.withTimestamp(message.getSentTimeMillis())
|
2018-06-21 08:13:10 -07:00
|
|
|
.withExpiration(message.getRecipient().getExpireMessages())
|
2017-08-22 11:51:01 -07:00
|
|
|
.asGroupMessage(group)
|
|
|
|
.build();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
return messageSender.sendMessage(addresses, unidentifiedAccess, groupDataMessage);
|
2014-11-08 11:35:58 -08:00
|
|
|
} else {
|
2017-08-01 08:56:00 -07:00
|
|
|
SignalServiceGroup group = new SignalServiceGroup(GroupUtil.getDecodedId(groupId));
|
2017-08-22 11:51:01 -07:00
|
|
|
SignalServiceDataMessage groupMessage = SignalServiceDataMessage.newBuilder()
|
|
|
|
.withTimestamp(message.getSentTimeMillis())
|
|
|
|
.asGroupMessage(group)
|
|
|
|
.withAttachments(attachmentStreams)
|
|
|
|
.withBody(message.getBody())
|
|
|
|
.withExpiration((int)(message.getExpiresIn() / 1000))
|
|
|
|
.asExpirationUpdate(message.isExpirationUpdate())
|
|
|
|
.withProfileKey(profileKey.orNull())
|
2018-02-07 14:01:37 -08:00
|
|
|
.withQuote(quote.orNull())
|
2018-04-26 17:03:54 -07:00
|
|
|
.withSharedContacts(sharedContacts)
|
2017-08-22 11:51:01 -07:00
|
|
|
.build();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
return messageSender.sendMessage(addresses, unidentifiedAccess, groupMessage);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 08:45:45 -07:00
|
|
|
private @NonNull List<Address> getGroupMessageRecipients(String groupId, long messageId) {
|
|
|
|
List<GroupReceiptInfo> destinations = DatabaseFactory.getGroupReceiptDatabase(context).getGroupReceiptInfo(messageId);
|
|
|
|
if (!destinations.isEmpty()) return Stream.of(destinations).map(GroupReceiptInfo::getAddress).toList();
|
2017-07-26 09:59:15 -07:00
|
|
|
|
2017-09-30 08:45:45 -07:00
|
|
|
List<Recipient> members = DatabaseFactory.getGroupDatabase(context).getGroupMembers(groupId, false);
|
|
|
|
return Stream.of(members).map(Recipient::getAddress).toList();
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|