+ if (message.getGroupInfo().isPresent()) {
+ try {
+ messageSender.sendMessage(new ArrayList<>(recipientsTS), message);
+ } catch (EncapsulatedExceptions encapsulatedExceptions) {
+ for (UntrustedIdentityException e : encapsulatedExceptions.getUntrustedIdentityExceptions()) {
+ signalProtocolStore.saveIdentity(e.getE164Number(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
+ }
+ }
+ } else {
+ // Send to all individually, so sync messages are sent correctly
+ List<UntrustedIdentityException> untrustedIdentities = new LinkedList<>();
+ List<UnregisteredUserException> unregisteredUsers = new LinkedList<>();
+ List<NetworkFailureException> networkExceptions = new LinkedList<>();
+ for (SignalServiceAddress address : recipientsTS) {
+ try {
+ messageSender.sendMessage(address, message);
+ } catch (UntrustedIdentityException e) {
+ signalProtocolStore.saveIdentity(e.getE164Number(), e.getIdentityKey(), TrustLevel.UNTRUSTED);
+ untrustedIdentities.add(e);
+ } catch (UnregisteredUserException e) {
+ unregisteredUsers.add(e);
+ } catch (PushNetworkException e) {
+ networkExceptions.add(new NetworkFailureException(address.getNumber(), e));
+ }
+ }
+ if (!untrustedIdentities.isEmpty() || !unregisteredUsers.isEmpty() || !networkExceptions.isEmpty()) {
+ throw new EncapsulatedExceptions(untrustedIdentities, unregisteredUsers, networkExceptions);
+ }
+ }
+ } finally {
+ if (message.isEndSession()) {
+ for (SignalServiceAddress recipient : recipientsTS) {
+ handleEndSession(recipient.getNumber());
+ }