2016-11-20 22:50:41 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
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;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.SignalCommunicationModule.SignalMessageSenderFactory;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2016-11-20 22:50:41 +00:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
|
|
|
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;
|
2016-11-20 22:50:41 +00:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-12-20 17:55:52 +00:00
|
|
|
@Inject transient SignalMessageSenderFactory messageSenderFactory;
|
2016-11-20 22:50:41 +00:00
|
|
|
|
|
|
|
private final String source;
|
|
|
|
private final byte[] groupId;
|
|
|
|
|
|
|
|
|
|
|
|
public PushGroupUpdateJob(Context context, String source, byte[] groupId) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withRetryCount(50)
|
|
|
|
.create());
|
|
|
|
|
|
|
|
this.source = source;
|
|
|
|
this.groupId = groupId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException, UntrustedIdentityException {
|
|
|
|
SignalServiceMessageSender messageSender = messageSenderFactory.create();
|
|
|
|
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
2017-08-01 15:56:00 +00:00
|
|
|
GroupRecord record = groupDatabase.getGroup(GroupUtil.getEncodedId(groupId, false));
|
2016-12-03 21:38:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-03 21:38:22 +00:00
|
|
|
if (record.getAvatar() != null) {
|
|
|
|
avatar = SignalServiceAttachmentStream.newStreamBuilder()
|
|
|
|
.withContentType("image/jpeg")
|
|
|
|
.withStream(new ByteArrayInputStream(record.getAvatar()))
|
|
|
|
.withLength(record.getAvatar().length)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
List<String> members = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Address member : record.getMembers()) {
|
|
|
|
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)
|
2016-11-20 22:50:41 +00:00
|
|
|
.withName(record.getTitle())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder()
|
|
|
|
.asGroupMessage(groupContext)
|
|
|
|
.withTimestamp(System.currentTimeMillis())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
messageSender.sendMessage(new SignalServiceAddress(source), message);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetry(Exception e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return e instanceof PushNetworkException;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|