Skip to content

Commit

Permalink
Allow RetrieveProfileJob to be used for self.
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-signal authored and greyson-signal committed Apr 21, 2020
1 parent 2d60d5f commit f5e6fd6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@ private void initializeProfiles() {
return;
}

ApplicationDependencies.getJobManager().add(new RetrieveProfileJob(recipient.get()));
ApplicationDependencies.getJobManager().add(RetrieveProfileJob.forRecipient(recipient.get()));
}

private void onRecipientChanged(@NonNull Recipient recipient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ private void handleProfileKey(@NonNull SignalServiceContent content,

if (messageProfileKey != null) {
if (database.setProfileKey(recipient.getId(), messageProfileKey)) {
ApplicationDependencies.getJobManager().add(new RetrieveProfileJob(recipient));
ApplicationDependencies.getJobManager().add(RetrieveProfileJob.forRecipient(recipient));
}
} else {
Log.w(TAG, "Ignored invalid profile key seen in message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
import java.util.List;

/**
* Retrieves a users profile and sets the appropriate local fields. If fetching the profile of the
* local user, use {@link RefreshOwnProfileJob} instead.
* Retrieves a users profile and sets the appropriate local fields.
* <p>
* Recipient can be self if you use {@link #forRecipient} and it will delegate to {@link RefreshOwnProfileJob}.
*/
public class RetrieveProfileJob extends BaseJob {

Expand All @@ -49,24 +50,36 @@ public class RetrieveProfileJob extends BaseJob {

private static final String KEY_RECIPIENT = "recipient";

private final Recipient recipient;
private final RecipientId recipientId;

public RetrieveProfileJob(@NonNull Recipient recipient) {
public static Job forRecipient(@NonNull Recipient recipient) {
return forRecipient(recipient.getId());
}

public static Job forRecipient(@NonNull RecipientId recipientId) {
if (Recipient.self().getId().equals(recipientId)) {
return new RefreshOwnProfileJob();
} else {
return new RetrieveProfileJob(recipientId);
}
}

private RetrieveProfileJob(@NonNull RecipientId recipientId) {
this(new Job.Parameters.Builder()
.addConstraint(NetworkConstraint.KEY)
.setMaxAttempts(3)
.build(),
recipient);
recipientId);
}

private RetrieveProfileJob(@NonNull Job.Parameters parameters, @NonNull Recipient recipient) {
private RetrieveProfileJob(@NonNull Job.Parameters parameters, @NonNull RecipientId recipientId) {
super(parameters);
this.recipient = recipient;
this.recipientId = recipientId;
}

@Override
public @NonNull Data serialize() {
return new Data.Builder().putString(KEY_RECIPIENT, recipient.getId().serialize()).build();
return new Data.Builder().putString(KEY_RECIPIENT, recipientId.serialize()).build();
}

@Override
Expand All @@ -76,9 +89,9 @@ private RetrieveProfileJob(@NonNull Job.Parameters parameters, @NonNull Recipien

@Override
public void onRun() throws IOException {
Log.i(TAG, "Retrieving profile of " + recipient.getId());
Log.i(TAG, "Retrieving profile of " + recipientId);

Recipient resolved = recipient.resolve();
Recipient resolved = Recipient.resolved(recipientId);

if (resolved.isGroup()) handleGroupRecipient(resolved);
else handleIndividualRecipient(resolved);
Expand Down Expand Up @@ -248,7 +261,7 @@ public static final class Factory implements Job.Factory<RetrieveProfileJob> {

@Override
public @NonNull RetrieveProfileJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new RetrieveProfileJob(parameters, Recipient.resolved(RecipientId.from(data.getString(KEY_RECIPIENT))));
return new RetrieveProfileJob(parameters, RecipientId.from(data.getString(KEY_RECIPIENT)));
}
}
}

0 comments on commit f5e6fd6

Please sign in to comment.