diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/GroupDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/GroupDatabase.java index d6a7ba7076..2a61610090 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/GroupDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/GroupDatabase.java @@ -26,6 +26,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPoin import java.io.Closeable; import java.io.IOException; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -163,25 +164,31 @@ public class GroupDatabase extends Database { } public List getGroupNamesContainingMember(RecipientId recipientId) { + return Stream.of(getGroupsContainingMember(recipientId)) + .map(GroupRecord::getTitle) + .toList(); + } + + public List getGroupsContainingMember(RecipientId recipientId) { SQLiteDatabase database = databaseHelper.getReadableDatabase(); String table = TABLE_NAME + " INNER JOIN " + ThreadDatabase.TABLE_NAME + " ON " + TABLE_NAME + "." + RECIPIENT_ID + " = " + ThreadDatabase.TABLE_NAME + "." + ThreadDatabase.RECIPIENT_ID; - List groupNames = new LinkedList<>(); - String[] projection = new String[]{TITLE, MEMBERS}; String query = MEMBERS + " LIKE ?"; String[] args = new String[]{"%" + recipientId.serialize() + "%"}; String orderBy = ThreadDatabase.TABLE_NAME + "." + ThreadDatabase.DATE + " DESC"; - try (Cursor cursor = database.query(table, projection, query, args, null, null, orderBy)) { + List groups = new LinkedList<>(); + + try (Cursor cursor = database.query(table, null, query, args, null, null, orderBy)) { while (cursor != null && cursor.moveToNext()) { List members = Util.split(cursor.getString(cursor.getColumnIndexOrThrow(MEMBERS)), ","); if (members.contains(recipientId.serialize())) { - groupNames.add(cursor.getString(cursor.getColumnIndexOrThrow(TITLE))); + groups.add(new Reader(cursor).getCurrent()); } } } - return groupNames; + return groups; } public Reader getGroups() { diff --git a/app/src/main/java/org/thoughtcrime/securesms/recipients/RecipientUtil.java b/app/src/main/java/org/thoughtcrime/securesms/recipients/RecipientUtil.java index e9bd30fa86..c5d89aef57 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recipients/RecipientUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/recipients/RecipientUtil.java @@ -7,6 +7,8 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; +import com.annimon.stream.Stream; + import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.contacts.sync.DirectoryHelper; import org.thoughtcrime.securesms.database.DatabaseFactory; @@ -81,7 +83,7 @@ public class RecipientUtil { leaveGroup(context, recipient); } - if (resolved.isSystemContact() || resolved.isProfileSharing()) { + if (resolved.isSystemContact() || resolved.isProfileSharing() || isProfileSharedViaGroup(context,resolved)) { ApplicationDependencies.getJobManager().add(new RotateProfileKeyJob()); DatabaseFactory.getRecipientDatabase(context).setProfileSharing(resolved.getId(), false); } @@ -229,4 +231,10 @@ public class RecipientUtil { private static boolean noSecureMessagesInThread(@NonNull Context context, long threadId) { return DatabaseFactory.getMmsSmsDatabase(context).getSecureConversationCount(threadId) == 0; } + + @WorkerThread + private static boolean isProfileSharedViaGroup(@NonNull Context context, @NonNull Recipient recipient) { + return Stream.of(DatabaseFactory.getGroupDatabase(context).getGroupsContainingMember(recipient.getId())) + .anyMatch(group -> Recipient.resolved(group.getRecipientId()).isProfileSharing()); + } }