Signal-Android/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/bottomsheet/RecipientDialogRepository.java

146 lines
6.0 KiB
Java
Raw Normal View History

2020-04-22 21:25:28 +02:00
package org.thoughtcrime.securesms.recipients.ui.bottomsheet;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2020-05-07 16:27:39 +02:00
import androidx.core.util.Consumer;
2020-04-22 21:25:28 +02:00
import org.thoughtcrime.securesms.contacts.sync.DirectoryHelper;
2020-04-22 21:25:28 +02:00
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.GroupDatabase;
2020-04-22 21:25:28 +02:00
import org.thoughtcrime.securesms.database.IdentityDatabase;
2020-05-07 16:27:39 +02:00
import org.thoughtcrime.securesms.groups.GroupChangeBusyException;
import org.thoughtcrime.securesms.groups.GroupChangeFailedException;
2020-04-22 21:25:28 +02:00
import org.thoughtcrime.securesms.groups.GroupId;
2020-05-07 16:27:39 +02:00
import org.thoughtcrime.securesms.groups.GroupInsufficientRightsException;
import org.thoughtcrime.securesms.groups.GroupManager;
import org.thoughtcrime.securesms.groups.GroupNotAMemberException;
import org.thoughtcrime.securesms.groups.ui.GroupChangeErrorCallback;
import org.thoughtcrime.securesms.groups.ui.GroupChangeFailureReason;
import org.thoughtcrime.securesms.logging.Log;
2020-04-22 21:25:28 +02:00
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
import org.thoughtcrime.securesms.util.concurrent.SimpleTask;
2020-05-07 16:27:39 +02:00
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
2020-05-07 16:27:39 +02:00
import java.util.Objects;
2020-04-22 21:25:28 +02:00
final class RecipientDialogRepository {
2020-05-07 16:27:39 +02:00
private static final String TAG = Log.tag(RecipientDialogRepository.class);
2020-04-27 21:27:31 +02:00
@NonNull private final Context context;
@NonNull private final RecipientId recipientId;
@Nullable private final GroupId groupId;
2020-04-22 21:25:28 +02:00
RecipientDialogRepository(@NonNull Context context,
@NonNull RecipientId recipientId,
@Nullable GroupId groupId)
{
2020-04-27 21:27:31 +02:00
this.context = context;
this.recipientId = recipientId;
this.groupId = groupId;
2020-04-22 21:25:28 +02:00
}
2020-05-13 18:36:57 +02:00
@NonNull RecipientId getRecipientId() {
2020-04-22 21:25:28 +02:00
return recipientId;
}
2020-05-13 18:36:57 +02:00
@Nullable GroupId getGroupId() {
2020-04-22 21:25:28 +02:00
return groupId;
}
void getIdentity(@NonNull Consumer<IdentityDatabase.IdentityRecord> callback) {
SignalExecutors.BOUNDED.execute(
() -> callback.accept(DatabaseFactory.getIdentityDatabase(context)
.getIdentity(recipientId)
.orNull()));
2020-04-22 21:25:28 +02:00
}
2020-04-27 21:27:31 +02:00
void getRecipient(@NonNull RecipientCallback recipientCallback) {
2020-04-22 21:25:28 +02:00
SimpleTask.run(SignalExecutors.BOUNDED,
() -> Recipient.resolved(recipientId),
recipientCallback::onRecipient);
}
void refreshRecipient() {
SignalExecutors.UNBOUNDED.execute(() -> {
try {
DirectoryHelper.refreshDirectoryFor(context, Recipient.resolved(recipientId), false);
} catch (IOException e) {
Log.w(TAG, "Failed to refresh user after adding to contacts.");
}
});
}
2020-05-07 16:27:39 +02:00
void getGroupName(@NonNull Consumer<String> stringConsumer) {
SimpleTask.run(SignalExecutors.BOUNDED,
() -> DatabaseFactory.getGroupDatabase(context).requireGroup(Objects.requireNonNull(groupId)).getTitle(),
stringConsumer::accept);
}
void removeMember(@NonNull Consumer<Boolean> onComplete, @NonNull GroupChangeErrorCallback error) {
SimpleTask.run(SignalExecutors.UNBOUNDED,
() -> {
try {
GroupManager.ejectFromGroup(context, Objects.requireNonNull(groupId).requireV2(), Recipient.resolved(recipientId));
return true;
} catch (GroupInsufficientRightsException | GroupNotAMemberException e) {
Log.w(TAG, e);
error.onError(GroupChangeFailureReason.NO_RIGHTS);
} catch (GroupChangeFailedException | GroupChangeBusyException | IOException e) {
Log.w(TAG, e);
error.onError(GroupChangeFailureReason.OTHER);
}
return false;
},
onComplete::accept);
}
void setMemberAdmin(boolean admin, @NonNull Consumer<Boolean> onComplete, @NonNull GroupChangeErrorCallback error) {
SimpleTask.run(SignalExecutors.UNBOUNDED,
() -> {
try {
GroupManager.setMemberAdmin(context, Objects.requireNonNull(groupId).requireV2(), recipientId, admin);
return true;
} catch (GroupInsufficientRightsException | GroupNotAMemberException e) {
Log.w(TAG, e);
error.onError(GroupChangeFailureReason.NO_RIGHTS);
} catch (GroupChangeFailedException | GroupChangeBusyException | IOException e) {
Log.w(TAG, e);
error.onError(GroupChangeFailureReason.OTHER);
}
return false;
},
onComplete::accept);
}
void getGroupMembership(@NonNull Consumer<List<RecipientId>> onComplete) {
SimpleTask.run(SignalExecutors.UNBOUNDED,
() -> {
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
List<GroupDatabase.GroupRecord> groupRecords = groupDatabase.getPushGroupsContainingMember(recipientId);
ArrayList<RecipientId> groupRecipients = new ArrayList<>(groupRecords.size());
for (GroupDatabase.GroupRecord groupRecord : groupRecords) {
groupRecipients.add(groupRecord.getRecipientId());
}
return groupRecipients;
},
onComplete::accept);
}
public void getActiveGroupCount(@NonNull Consumer<Integer> onComplete) {
SignalExecutors.BOUNDED.execute(() -> onComplete.accept(DatabaseFactory.getGroupDatabase(context).getActiveGroupCount()));
}
2020-04-22 21:25:28 +02:00
interface RecipientCallback {
void onRecipient(@NonNull Recipient recipient);
}
}