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;
|
2019-01-15 12:43:38 -08:00
|
|
|
import android.support.annotation.WorkerThread;
|
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;
|
2019-01-15 12:43:38 -08:00
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
2019-12-13 16:04:24 +11:00
|
|
|
import org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore;
|
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-12-12 10:07:17 +11: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;
|
2019-10-10 11:38:43 +11:00
|
|
|
import org.thoughtcrime.securesms.groups.GroupManager;
|
2019-03-28 08:56:35 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
2019-01-15 12:43:38 -08:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobManager;
|
2019-03-28 08:56:35 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
2019-01-15 12:43:38 -08:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2020-02-04 10:09:32 +11:00
|
|
|
import org.thoughtcrime.securesms.loki.MultiDeviceUtilities;
|
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;
|
2019-12-13 16:04:24 +11:00
|
|
|
import org.thoughtcrime.securesms.sms.MessageSender;
|
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;
|
2020-02-04 10:09:32 +11:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2019-12-13 16:04:24 +11:00
|
|
|
import org.whispersystems.libsignal.SignalProtocolAddress;
|
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;
|
2019-01-15 00:41:05 -08:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage.Preview;
|
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.internal.push.SignalServiceProtos.GroupContext;
|
2019-10-15 13:39:17 +11:00
|
|
|
import org.whispersystems.signalservice.loki.api.LokiPublicChat;
|
2019-12-13 11:24:47 +11:00
|
|
|
import org.whispersystems.signalservice.loki.api.LokiStorageAPI;
|
|
|
|
import org.whispersystems.signalservice.loki.utilities.PromiseUtil;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2019-08-08 17:01:57 +10:00
|
|
|
import java.util.ArrayList;
|
2018-05-22 02:13:10 -07:00
|
|
|
import java.util.Collections;
|
2020-02-04 10:09:32 +11:00
|
|
|
import java.util.HashSet;
|
2019-01-15 00:41:05 -08:00
|
|
|
import java.util.LinkedList;
|
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;
|
|
|
|
|
|
|
|
public class PushGroupSendJob 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 = "PushGroupSendJob";
|
2015-06-24 18:26:51 -07:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static final String TAG = PushGroupSendJob.class.getSimpleName();
|
|
|
|
|
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 static final String KEY_MESSAGE_ID = "message_id";
|
|
|
|
private static final String KEY_FILTER_ADDRESS = "filter_address";
|
|
|
|
|
|
|
|
private long messageId;
|
|
|
|
private String filterAddress;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public PushGroupSendJob(long messageId, @NonNull Address destination, @Nullable Address filterAddress) {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.setQueue(destination.toGroupString())
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
|
|
|
.setMaxAttempts(Parameters.UNLIMITED)
|
|
|
|
.build(),
|
|
|
|
messageId, filterAddress);
|
|
|
|
|
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 PushGroupSendJob(@NonNull Job.Parameters parameters, long messageId, @Nullable Address filterAddress) {
|
|
|
|
super(parameters);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
this.messageId = messageId;
|
|
|
|
this.filterAddress = filterAddress == null ? null :filterAddress.toPhoneString();
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2019-01-15 12:43:38 -08:00
|
|
|
@WorkerThread
|
|
|
|
public static void enqueue(@NonNull Context context, @NonNull JobManager jobManager, long messageId, @NonNull Address destination, @Nullable Address filterAddress) {
|
|
|
|
try {
|
2019-01-15 00:41:05 -08:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
|
|
|
|
List<Attachment> attachments = new LinkedList<>();
|
2019-01-20 12:00:18 -08:00
|
|
|
|
2019-10-21 10:52:53 +11:00
|
|
|
attachments.addAll(message.getAttachments());
|
|
|
|
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-01-20 12:00:18 -08:00
|
|
|
|
2019-10-21 10:52:53 +11:00
|
|
|
List<AttachmentUploadJob> attachmentJobs = Stream.of(attachments).map(a -> new AttachmentUploadJob(((DatabaseAttachment) a).getAttachmentId(), destination)).toList();
|
2019-01-15 12:43:38 -08:00
|
|
|
|
|
|
|
if (attachmentJobs.isEmpty()) {
|
2019-03-28 08:56:35 -07:00
|
|
|
jobManager.add(new PushGroupSendJob(messageId, destination, filterAddress));
|
2019-01-15 12:43:38 -08:00
|
|
|
} else {
|
|
|
|
jobManager.startChain(attachmentJobs)
|
2019-03-28 08:56:35 -07:00
|
|
|
.then(new PushGroupSendJob(messageId, destination, filterAddress))
|
|
|
|
.enqueue();
|
2019-01-15 12:43:38 -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)
|
|
|
|
.putString(KEY_FILTER_ADDRESS, filterAddress)
|
|
|
|
.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()
|
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);
|
|
|
|
|
2020-02-04 10:09:32 +11:00
|
|
|
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
|
|
|
|
2019-12-13 16:04:24 +11:00
|
|
|
// Only send messages to the contacts we have sessions with
|
|
|
|
List<Address> validTargets = Stream.of(target).filter(member -> {
|
2020-02-04 10:09:32 +11:00
|
|
|
// Our device is always valid
|
|
|
|
if (member.serialize().equalsIgnoreCase(localNumber)) { return true; }
|
|
|
|
|
2019-12-13 16:04:24 +11:00
|
|
|
SignalProtocolAddress protocolAddress = new SignalProtocolAddress(member.toPhoneString(), SignalServiceAddress.DEFAULT_DEVICE_ID);
|
|
|
|
boolean hasSession = new TextSecureSessionStore(context).containsSession(protocolAddress);
|
|
|
|
if (hasSession) { return true; }
|
|
|
|
|
|
|
|
// We should allow sending if we have a prekeybundle for the contact
|
|
|
|
return DatabaseFactory.getLokiPreKeyBundleDatabase(context).hasPreKeyBundle(member.toPhoneString());
|
|
|
|
}).toList();
|
|
|
|
|
|
|
|
// Send a session request to the other devices
|
|
|
|
List<Address> others = Stream.of(target).filter(t -> !validTargets.contains(t)).toList();
|
|
|
|
for (Address device : others) {
|
|
|
|
MessageSender.sendBackgroundSessionRequest(context, device.toPhoneString());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<SendMessageResult> results = deliver(message, validTargets);
|
2018-05-22 02:13:10 -07:00
|
|
|
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);
|
|
|
|
}
|
2019-01-20 12:00:18 -08:00
|
|
|
} catch (UntrustedIdentityException | 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);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public boolean onShouldRetry(@NonNull Exception exception) {
|
2018-05-22 02:13:10 -07:00
|
|
|
if (exception instanceof IOException) return true;
|
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
|
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)
|
2019-01-20 12:00:18 -08:00
|
|
|
throws IOException, UntrustedIdentityException, UndeliverableMessageException {
|
2019-08-08 17:01:57 +10:00
|
|
|
// rotateSenderCertificateIfNecessary();
|
2018-11-21 17:26:06 -08:00
|
|
|
|
2019-12-12 10:07:17 +11:00
|
|
|
// Messages shouldn't be able to be sent to RSS Feeds
|
|
|
|
Address groupAddress = message.getRecipient().getAddress();
|
|
|
|
if (groupAddress.isRSSFeed()) {
|
|
|
|
List<SendMessageResult> results = new ArrayList<>();
|
|
|
|
for (Address destination : destinations) results.add(SendMessageResult.networkFailure(new SignalServiceAddress(destination.toPhoneString())));
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
String groupId = groupAddress.toGroupString();
|
2019-04-17 10:21:30 -04:00
|
|
|
Optional<byte[]> profileKey = getProfileKey(message.getRecipient());
|
|
|
|
Optional<Quote> quote = getQuoteFor(message);
|
|
|
|
Optional<SignalServiceDataMessage.Sticker> sticker = getStickerFor(message);
|
|
|
|
List<SharedContact> sharedContacts = getSharedContactsFor(message);
|
|
|
|
List<Preview> previews = getPreviewsFor(message);
|
|
|
|
List<SignalServiceAddress> addresses = Stream.of(destinations).map(this::getPushAddress).toList();
|
|
|
|
List<Attachment> attachments = Stream.of(message.getAttachments()).filterNot(Attachment::isSticker).toList();
|
|
|
|
List<SignalServiceAttachment> attachmentPointers = getAttachmentPointersFor(attachments);
|
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
|
|
|
|
2019-12-12 10:07:17 +11:00
|
|
|
SignalServiceGroup.GroupType groupType = SignalServiceGroup.GroupType.SIGNAL;
|
|
|
|
if (groupAddress.isPublicChat()) {
|
|
|
|
groupType = SignalServiceGroup.GroupType.PUBLIC_CHAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.isGroup() && groupAddress.isSignalGroup()) {
|
|
|
|
// Loki - Only send GroupUpdate or GroupQuit to signal groups
|
2015-10-12 18:25:05 -07:00
|
|
|
OutgoingGroupMediaMessage groupMessage = (OutgoingGroupMediaMessage) message;
|
|
|
|
GroupContext groupContext = groupMessage.getGroupContext();
|
2019-01-15 00:41:05 -08:00
|
|
|
SignalServiceAttachment avatar = attachmentPointers.isEmpty() ? null : attachmentPointers.get(0);
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceGroup.Type type = groupMessage.isGroupQuit() ? SignalServiceGroup.Type.QUIT : SignalServiceGroup.Type.UPDATE;
|
2019-12-13 10:25:53 +11:00
|
|
|
SignalServiceGroup group = new SignalServiceGroup(type, GroupUtil.getDecodedId(groupId), groupType, groupContext.getName(), groupContext.getMembersList(), avatar, groupContext.getAdminsList());
|
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())
|
2019-12-12 10:07:17 +11:00
|
|
|
.withBody(message.getBody())
|
2017-08-22 11:51:01 -07:00
|
|
|
.asGroupMessage(group)
|
|
|
|
.build();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2019-12-12 10:07:17 +11:00
|
|
|
return messageSender.sendMessage(messageId, addresses, unidentifiedAccess, groupDataMessage);
|
2014-11-08 11:35:58 -08:00
|
|
|
} else {
|
2019-12-12 10:07:17 +11:00
|
|
|
SignalServiceGroup group = new SignalServiceGroup(GroupUtil.getDecodedId(groupId), groupType);
|
2017-08-22 11:51:01 -07:00
|
|
|
SignalServiceDataMessage groupMessage = SignalServiceDataMessage.newBuilder()
|
|
|
|
.withTimestamp(message.getSentTimeMillis())
|
|
|
|
.asGroupMessage(group)
|
2019-01-15 12:43:38 -08:00
|
|
|
.withAttachments(attachmentPointers)
|
2017-08-22 11:51:01 -07:00
|
|
|
.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())
|
2019-04-17 10:21:30 -04:00
|
|
|
.withSticker(sticker.orNull())
|
2018-04-26 17:03:54 -07:00
|
|
|
.withSharedContacts(sharedContacts)
|
2019-01-15 00:41:05 -08:00
|
|
|
.withPreviews(previews)
|
2017-08-22 11:51:01 -07:00
|
|
|
.build();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2019-08-09 10:11:30 +10:00
|
|
|
return messageSender.sendMessage(messageId, 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) {
|
2019-12-12 10:07:17 +11:00
|
|
|
if (GroupUtil.isRssFeed(groupId)) { return new ArrayList<>(); }
|
|
|
|
|
|
|
|
// Loki - All public chat group messages should be directed to their respective servers
|
|
|
|
if (GroupUtil.isPublicChat(groupId)) {
|
|
|
|
ArrayList<Address> result = new ArrayList<>();
|
|
|
|
long threadID = GroupManager.getThreadIdFromGroupId(groupId, context);
|
|
|
|
LokiPublicChat publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID);
|
|
|
|
if (publicChat != null) {
|
|
|
|
result.add(Address.fromSerialized(groupId));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else {
|
2020-02-04 10:09:32 +11:00
|
|
|
/*
|
|
|
|
Our biggest assumption here is that group members will only consist of primary devices.
|
|
|
|
No secondary device should be able to be added to a group.
|
|
|
|
*/
|
2019-12-12 10:07:17 +11:00
|
|
|
List<GroupReceiptInfo> destinations = DatabaseFactory.getGroupReceiptDatabase(context).getGroupReceiptInfo(messageId);
|
2017-07-26 09:59:15 -07:00
|
|
|
|
2020-02-04 10:09:32 +11:00
|
|
|
Set<Address> memberSet = new HashSet<>();
|
|
|
|
if (destinations.isEmpty()) {
|
|
|
|
List<Recipient> groupMembers = DatabaseFactory.getGroupDatabase(context).getGroupMembers(groupId, false);
|
|
|
|
memberSet.addAll(Stream.of(groupMembers).map(Recipient::getAddress).toList());
|
|
|
|
} else {
|
|
|
|
memberSet.addAll(Stream.of(destinations).map(GroupReceiptInfo::getAddress).toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace primary device public key with ours so message syncing works correctly
|
|
|
|
String masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context);
|
|
|
|
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
|
|
|
if (masterHexEncodedPublicKey != null && memberSet.contains(Address.fromSerialized(masterHexEncodedPublicKey))) {
|
|
|
|
memberSet.remove(Address.fromSerialized(masterHexEncodedPublicKey));
|
|
|
|
memberSet.add(Address.fromSerialized(localNumber));
|
|
|
|
}
|
2019-12-13 11:24:47 +11:00
|
|
|
|
2020-02-04 10:09:32 +11:00
|
|
|
// Add secondary devices to the list. We shouldn't add our secondary devices
|
2020-02-04 14:52:25 +11:00
|
|
|
try {
|
|
|
|
Set<Address> originalMemberSet = new HashSet<>(memberSet);
|
|
|
|
for (Address member : originalMemberSet) {
|
|
|
|
if (!member.isPhone() || member.serialize().equalsIgnoreCase(localNumber)) { continue; }
|
|
|
|
|
|
|
|
try {
|
|
|
|
List<String> secondaryDevices = PromiseUtil.timeout(LokiStorageAPI.shared.getSecondaryDevicePublicKeys(member.serialize()), 5000).get();
|
|
|
|
memberSet.addAll(Stream.of(secondaryDevices).map(string -> {
|
|
|
|
// Loki - Calling .map(Address::fromSerialized) is causing errors, thus we use the long method :(
|
|
|
|
return Address.fromSerialized(string);
|
|
|
|
}).toList());
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Timed out, go to the next member
|
|
|
|
}
|
2019-12-13 11:24:47 +11:00
|
|
|
}
|
2020-02-04 14:52:25 +11:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e("PushGroupSend", "Error occurred while adding secondary devices: " + e);
|
2019-12-13 11:24:47 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return new LinkedList<>(memberSet);
|
2019-12-12 10:07:17 +11:00
|
|
|
}
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2019-03-28 08:56:35 -07:00
|
|
|
|
|
|
|
public static class Factory implements Job.Factory<PushGroupSendJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull PushGroupSendJob create(@NonNull Parameters parameters, @NonNull org.thoughtcrime.securesms.jobmanager.Data data) {
|
|
|
|
String address = data.getString(KEY_FILTER_ADDRESS);
|
|
|
|
Address filter = address != null ? Address.fromSerialized(data.getString(KEY_FILTER_ADDRESS)) : null;
|
|
|
|
|
|
|
|
return new PushGroupSendJob(parameters, data.getLong(KEY_MESSAGE_ID), filter);
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|