2016-11-20 22:50:41 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-09 14:15:43 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
2018-05-22 09:13:10 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2016-11-20 22:50:41 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-06-18 19:27:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2018-05-22 09:13:10 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-06-22 18:29:16 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2017-08-07 23:47:38 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2016-11-20 22:50:41 +00:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceGroup.Type;
|
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
2017-07-26 16:59:15 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2018-08-09 14:15:43 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2016-11-20 22:50:41 +00:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
import androidx.work.Data;
|
|
|
|
|
2016-11-20 22:50:41 +00:00
|
|
|
public class PushGroupUpdateJob extends ContextJob implements InjectableType {
|
|
|
|
|
2017-02-04 22:12:09 +00:00
|
|
|
private static final String TAG = PushGroupUpdateJob.class.getSimpleName();
|
2016-11-20 22:50:41 +00:00
|
|
|
|
|
|
|
private static final long serialVersionUID = 0L;
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
private static final String KEY_SOURCE = "source";
|
|
|
|
private static final String KEY_GROUP_ID = "group_id";
|
|
|
|
|
2017-09-16 05:38:53 +00:00
|
|
|
@Inject transient SignalServiceMessageSender messageSender;
|
2016-11-20 22:50:41 +00:00
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
private String source;
|
|
|
|
private byte[] groupId;
|
2016-11-20 22:50:41 +00:00
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
public PushGroupUpdateJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
2016-11-20 22:50:41 +00:00
|
|
|
|
|
|
|
public PushGroupUpdateJob(Context context, String source, byte[] groupId) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
2018-08-09 14:15:43 +00:00
|
|
|
.withNetworkRequirement()
|
|
|
|
.withRetryDuration(TimeUnit.DAYS.toMillis(1))
|
2016-11-20 22:50:41 +00:00
|
|
|
.create());
|
|
|
|
|
|
|
|
this.source = source;
|
|
|
|
this.groupId = groupId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 14:15:43 +00:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
source = data.getString(KEY_SOURCE);
|
|
|
|
try {
|
|
|
|
groupId = GroupUtil.getDecodedId(data.getString(KEY_GROUP_ID));
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.putString(KEY_SOURCE, source)
|
|
|
|
.putString(KEY_GROUP_ID, GroupUtil.getEncodedId(groupId, false))
|
|
|
|
.build();
|
|
|
|
}
|
2016-11-20 22:50:41 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException, UntrustedIdentityException {
|
2017-09-16 05:38:53 +00:00
|
|
|
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
|
|
|
Optional<GroupRecord> record = groupDatabase.getGroup(GroupUtil.getEncodedId(groupId, false));
|
|
|
|
SignalServiceAttachment avatar = null;
|
2016-11-20 22:50:41 +00:00
|
|
|
|
|
|
|
if (record == null) {
|
|
|
|
Log.w(TAG, "No information for group record info request: " + new String(groupId));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-07 23:47:38 +00:00
|
|
|
if (record.get().getAvatar() != null) {
|
2016-12-03 21:38:22 +00:00
|
|
|
avatar = SignalServiceAttachmentStream.newStreamBuilder()
|
|
|
|
.withContentType("image/jpeg")
|
2017-08-07 23:47:38 +00:00
|
|
|
.withStream(new ByteArrayInputStream(record.get().getAvatar()))
|
|
|
|
.withLength(record.get().getAvatar().length)
|
2016-12-03 21:38:22 +00:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
List<String> members = new LinkedList<>();
|
|
|
|
|
2017-08-07 23:47:38 +00:00
|
|
|
for (Address member : record.get().getMembers()) {
|
2017-07-26 16:59:15 +00:00
|
|
|
members.add(member.serialize());
|
|
|
|
}
|
2016-11-20 22:50:41 +00:00
|
|
|
|
|
|
|
SignalServiceGroup groupContext = SignalServiceGroup.newBuilder(Type.UPDATE)
|
|
|
|
.withAvatar(avatar)
|
|
|
|
.withId(groupId)
|
2017-07-26 16:59:15 +00:00
|
|
|
.withMembers(members)
|
2017-08-07 23:47:38 +00:00
|
|
|
.withName(record.get().getTitle())
|
2016-11-20 22:50:41 +00:00
|
|
|
.build();
|
|
|
|
|
2018-06-22 18:29:16 +00:00
|
|
|
Address groupAddress = Address.fromSerialized(GroupUtil.getEncodedId(groupId, false));
|
|
|
|
Recipient groupRecipient = Recipient.from(context, groupAddress, false);
|
|
|
|
|
2016-11-20 22:50:41 +00:00
|
|
|
SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder()
|
|
|
|
.asGroupMessage(groupContext)
|
|
|
|
.withTimestamp(System.currentTimeMillis())
|
2018-06-22 18:29:16 +00:00
|
|
|
.withExpiration(groupRecipient.getExpireMessages())
|
2016-11-20 22:50:41 +00:00
|
|
|
.build();
|
|
|
|
|
2018-05-22 09:13:10 +00:00
|
|
|
messageSender.sendMessage(new SignalServiceAddress(source),
|
|
|
|
UnidentifiedAccessUtil.getAccessFor(context, Recipient.from(context, Address.fromSerialized(source), false)),
|
|
|
|
message);
|
2016-11-20 22:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetry(Exception e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return e instanceof PushNetworkException;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|