Skip to content

Commit

Permalink
Paralellize profile fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
AsamK committed Dec 26, 2021
1 parent 3b81ba3 commit fba7a6a
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 138 deletions.
3 changes: 3 additions & 0 deletions graalvm-config-dir/resource-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
{
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PE\\E"
},
{
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PH\\E"
},
{
"pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PL\\E"
},
Expand Down
5 changes: 2 additions & 3 deletions lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ public Lock acquire() {
avatarStore,
unidentifiedAccessHelper::getAccessFor,
this::resolveSignalServiceAddress);
final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper::getRecipientProfileKeyCredential,
profileHelper::getRecipientProfile,
final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper,
account::getSelfRecipientId,
dependencies.getGroupsV2Operations(),
dependencies.getGroupsV2Api(),
Expand All @@ -210,7 +209,7 @@ public Lock acquire() {
account.getRecipientStore(),
this::handleIdentityFailure,
this::getGroupInfo,
profileHelper::getRecipientProfile,
profileHelper,
this::refreshRegisteredUser);
this.groupHelper = new GroupHelper(account,
dependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.asamk.signal.manager.storage.recipients.Profile;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.asamk.signal.manager.util.IOUtils;
import org.asamk.signal.manager.util.Utils;
import org.signal.storageservice.protos.groups.AccessControl;
import org.signal.storageservice.protos.groups.GroupChange;
import org.signal.storageservice.protos.groups.Member;
Expand Down Expand Up @@ -44,6 +45,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.UUID;
Expand All @@ -54,8 +56,7 @@ public class GroupV2Helper {

private final static Logger logger = LoggerFactory.getLogger(GroupV2Helper.class);

private final ProfileKeyCredentialProvider profileKeyCredentialProvider;
private final ProfileProvider profileProvider;
private final ProfileHelper profileHelper;
private final SelfRecipientIdProvider selfRecipientIdProvider;
private final GroupsV2Operations groupsV2Operations;
private final GroupsV2Api groupsV2Api;
Expand All @@ -64,15 +65,13 @@ public class GroupV2Helper {
private HashMap<Integer, AuthCredentialResponse> groupApiCredentials;

public GroupV2Helper(
final ProfileKeyCredentialProvider profileKeyCredentialProvider,
final ProfileProvider profileProvider,
final ProfileHelper profileHelper,
final SelfRecipientIdProvider selfRecipientIdProvider,
final GroupsV2Operations groupsV2Operations,
final GroupsV2Api groupsV2Api,
final SignalServiceAddressResolver addressResolver
) {
this.profileKeyCredentialProvider = profileKeyCredentialProvider;
this.profileProvider = profileProvider;
this.profileHelper = profileHelper;
this.selfRecipientIdProvider = selfRecipientIdProvider;
this.groupsV2Operations = groupsV2Operations;
this.groupsV2Api = groupsV2Api;
Expand Down Expand Up @@ -149,18 +148,22 @@ private byte[] readAvatarBytes(final File avatarFile) throws IOException {
private GroupsV2Operations.NewGroup buildNewGroup(
String name, Set<RecipientId> members, byte[] avatar
) {
final var profileKeyCredential = profileKeyCredentialProvider.getProfileKeyCredential(selfRecipientIdProvider.getSelfRecipientId());
final var profileKeyCredential = profileHelper.getRecipientProfileKeyCredential(selfRecipientIdProvider.getSelfRecipientId());
if (profileKeyCredential == null) {
logger.warn("Cannot create a V2 group as self does not have a versioned profile");
return null;
}

if (!areMembersValid(members)) return null;

var self = new GroupCandidate(getSelfAci().uuid(), Optional.fromNullable(profileKeyCredential));
var candidates = members.stream()
.map(member -> new GroupCandidate(addressResolver.resolveSignalServiceAddress(member).getAci().uuid(),
Optional.fromNullable(profileKeyCredentialProvider.getProfileKeyCredential(member))))
final var self = new GroupCandidate(getSelfAci().uuid(), Optional.fromNullable(profileKeyCredential));
final var memberList = new ArrayList<>(members);
final var credentials = profileHelper.getRecipientProfileKeyCredential(memberList).stream();
final var uuids = memberList.stream()
.map(member -> addressResolver.resolveSignalServiceAddress(member).getAci().uuid());
var candidates = Utils.zip(uuids,
credentials,
(uuid, credential) -> new GroupCandidate(uuid, Optional.fromNullable(credential)))
.collect(Collectors.toSet());

final var groupSecretParams = GroupSecretParams.generate();
Expand All @@ -174,8 +177,8 @@ private GroupsV2Operations.NewGroup buildNewGroup(
}

private boolean areMembersValid(final Set<RecipientId> members) {
final var noGv2Capability = members.stream()
.map(profileProvider::getProfile)
final var noGv2Capability = profileHelper.getRecipientProfile(new ArrayList<>(members))
.stream()
.filter(profile -> profile != null && !profile.getCapabilities().contains(Profile.Capability.gv2))
.collect(Collectors.toSet());
if (noGv2Capability.size() > 0) {
Expand Down Expand Up @@ -221,9 +224,13 @@ public Pair<DecryptedGroup, GroupChange> addMembers(
throw new IOException("Failed to update group");
}

var candidates = newMembers.stream()
.map(member -> new GroupCandidate(addressResolver.resolveSignalServiceAddress(member).getAci().uuid(),
Optional.fromNullable(profileKeyCredentialProvider.getProfileKeyCredential(member))))
final var memberList = new ArrayList<>(newMembers);
final var credentials = profileHelper.getRecipientProfileKeyCredential(memberList).stream();
final var uuids = memberList.stream()
.map(member -> addressResolver.resolveSignalServiceAddress(member).getAci().uuid());
var candidates = Utils.zip(uuids,
credentials,
(uuid, credential) -> new GroupCandidate(uuid, Optional.fromNullable(credential)))
.collect(Collectors.toSet());

final var aci = getSelfAci();
Expand Down Expand Up @@ -333,7 +340,7 @@ public GroupChange joinGroup(
final var groupOperations = groupsV2Operations.forGroup(groupSecretParams);

final var selfRecipientId = this.selfRecipientIdProvider.getSelfRecipientId();
final var profileKeyCredential = profileKeyCredentialProvider.getProfileKeyCredential(selfRecipientId);
final var profileKeyCredential = profileHelper.getRecipientProfileKeyCredential(selfRecipientId);
if (profileKeyCredential == null) {
throw new IOException("Cannot join a V2 group as self does not have a versioned profile");
}
Expand All @@ -352,7 +359,7 @@ public Pair<DecryptedGroup, GroupChange> acceptInvite(GroupInfoV2 groupInfoV2) t
final GroupsV2Operations.GroupOperations groupOperations = getGroupOperations(groupInfoV2);

final var selfRecipientId = this.selfRecipientIdProvider.getSelfRecipientId();
final var profileKeyCredential = profileKeyCredentialProvider.getProfileKeyCredential(selfRecipientId);
final var profileKeyCredential = profileHelper.getRecipientProfileKeyCredential(selfRecipientId);
if (profileKeyCredential == null) {
throw new IOException("Cannot join a V2 group as self does not have a versioned profile");
}
Expand Down
Loading

0 comments on commit fba7a6a

Please sign in to comment.