Skip to content

Commit

Permalink
Exclude inactive groups from search results where appropriate.
Browse files Browse the repository at this point in the history
Fixes #9091
Fixes #9080
  • Loading branch information
greyson-signal committed Oct 22, 2019
1 parent 9089fc4 commit f1ca5fc
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void onPreCreate() {
protected void onCreate(Bundle icicle, boolean ready) {
if (!getIntent().hasExtra(ContactSelectionListFragment.DISPLAY_MODE)) {
int displayMode = TextSecurePreferences.isSmsEnabled(this) ? DisplayMode.FLAG_ALL
: DisplayMode.FLAG_PUSH | DisplayMode.FLAG_GROUPS;
: DisplayMode.FLAG_PUSH | DisplayMode.FLAG_ACTIVE_GROUPS | DisplayMode.FLAG_INACTIVE_GROUPS;
getIntent().putExtra(ContactSelectionListFragment.DISPLAY_MODE, displayMode);
}

Expand Down
11 changes: 7 additions & 4 deletions src/org/thoughtcrime/securesms/ShareActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ protected void onPreCreate() {
@Override
protected void onCreate(Bundle icicle, boolean ready) {
if (!getIntent().hasExtra(ContactSelectionListFragment.DISPLAY_MODE)) {
getIntent().putExtra(ContactSelectionListFragment.DISPLAY_MODE,
TextSecurePreferences.isSmsEnabled(this)
? DisplayMode.FLAG_ALL
: DisplayMode.FLAG_PUSH | DisplayMode.FLAG_GROUPS);
int mode = DisplayMode.FLAG_PUSH | DisplayMode.FLAG_ACTIVE_GROUPS;

if (TextSecurePreferences.isSmsEnabled(this)) {
mode |= DisplayMode.FLAG_SMS;

}
getIntent().putExtra(ContactSelectionListFragment.DISPLAY_MODE, mode);
}

getIntent().putExtra(ContactSelectionListFragment.REFRESHABLE, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public List<String> getNumbersForThreadSearchFilter(Context context, String cons
GroupRecord record;

try {
reader = DatabaseFactory.getGroupDatabase(context).getGroupsFilteredByTitle(constraint);
reader = DatabaseFactory.getGroupDatabase(context).getGroupsFilteredByTitle(constraint, true);

while ((record = reader.getNext()) != null) {
numberList.add(record.getEncodedId());
Expand Down
27 changes: 18 additions & 9 deletions src/org/thoughtcrime/securesms/contacts/ContactsCursorLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ public class ContactsCursorLoader extends CursorLoader {
private static final String TAG = ContactsCursorLoader.class.getSimpleName();

public static final class DisplayMode {
public static final int FLAG_PUSH = 1;
public static final int FLAG_SMS = 1 << 1;
public static final int FLAG_GROUPS = 1 << 2;
public static final int FLAG_ALL = FLAG_PUSH | FLAG_SMS | FLAG_GROUPS;
public static final int FLAG_PUSH = 1;
public static final int FLAG_SMS = 1 << 1;
public static final int FLAG_ACTIVE_GROUPS = 1 << 2;
public static final int FLAG_INACTIVE_GROUPS = 1 << 3;
public static final int FLAG_ALL = FLAG_PUSH | FLAG_SMS | FLAG_ACTIVE_GROUPS | FLAG_INACTIVE_GROUPS;
}

private static final String[] CONTACT_PROJECTION = new String[]{ContactRepository.ID_COLUMN,
Expand All @@ -76,6 +77,10 @@ public ContactsCursorLoader(@NonNull Context context, int mode, String filter, b
{
super(context);

if (flagSet(mode, DisplayMode.FLAG_INACTIVE_GROUPS) && !flagSet(mode, DisplayMode.FLAG_ACTIVE_GROUPS)) {
throw new AssertionError("Inactive group flag set, but the active group flag isn't!");
}

this.filter = filter == null ? "" : filter;
this.mode = mode;
this.recents = recents;
Expand Down Expand Up @@ -172,7 +177,7 @@ private Cursor getRecentConversationsCursor() {
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(getContext());

MatrixCursor recentConversations = new MatrixCursor(CONTACT_PROJECTION, RECENT_CONVERSATION_MAX);
try (Cursor rawConversations = threadDatabase.getRecentConversationList(RECENT_CONVERSATION_MAX)) {
try (Cursor rawConversations = threadDatabase.getRecentConversationList(RECENT_CONVERSATION_MAX, flagSet(mode, DisplayMode.FLAG_INACTIVE_GROUPS))) {
ThreadDatabase.Reader reader = threadDatabase.readerFor(rawConversations);
ThreadRecord threadRecord;
while ((threadRecord = reader.getNext()) != null) {
Expand Down Expand Up @@ -208,7 +213,7 @@ private List<Cursor> getContactsCursors() {

private Cursor getGroupsCursor() {
MatrixCursor groupContacts = new MatrixCursor(CONTACT_PROJECTION);
try (GroupDatabase.Reader reader = DatabaseFactory.getGroupDatabase(getContext()).getGroupsFilteredByTitle(filter)) {
try (GroupDatabase.Reader reader = DatabaseFactory.getGroupDatabase(getContext()).getGroupsFilteredByTitle(filter, flagSet(mode, DisplayMode.FLAG_INACTIVE_GROUPS))) {
GroupDatabase.GroupRecord groupRecord;
while ((groupRecord = reader.getNext()) != null) {
groupContacts.addRow(new Object[] { groupRecord.getRecipientId().serialize(),
Expand Down Expand Up @@ -266,14 +271,18 @@ private static boolean isCursorListEmpty(List<Cursor> list) {
}

private static boolean pushEnabled(int mode) {
return (mode & DisplayMode.FLAG_PUSH) > 0;
return flagSet(mode, DisplayMode.FLAG_PUSH);
}

private static boolean smsEnabled(int mode) {
return (mode & DisplayMode.FLAG_SMS) > 0;
return flagSet(mode, DisplayMode.FLAG_SMS);
}

private static boolean groupsEnabled(int mode) {
return (mode & DisplayMode.FLAG_GROUPS) > 0;
return flagSet(mode, DisplayMode.FLAG_ACTIVE_GROUPS);
}

private static boolean flagSet(int mode, int flag) {
return (mode & flag) > 0;
}
}
20 changes: 14 additions & 6 deletions src/org/thoughtcrime/securesms/database/GroupDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class GroupDatabase extends Database {
private static final String AVATAR_RELAY = "avatar_relay";
private static final String AVATAR_DIGEST = "avatar_digest";
private static final String TIMESTAMP = "timestamp";
private static final String ACTIVE = "active";
static final String ACTIVE = "active";
static final String MMS = "mms";

public static final String CREATE_TABLE =
Expand Down Expand Up @@ -117,11 +117,19 @@ public boolean isUnknownGroup(String groupId) {
return !getGroup(groupId).isPresent();
}

public Reader getGroupsFilteredByTitle(String constraint) {
@SuppressLint("Recycle")
Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, TITLE + " LIKE ?",
new String[]{"%" + constraint + "%"},
null, null, null);
public Reader getGroupsFilteredByTitle(String constraint, boolean includeInactive) {
String query;
String[] queryArgs;

if (includeInactive) {
query = TITLE + " LIKE ? AND (" + ACTIVE + " = ? OR " + RECIPIENT_ID + " IN (SELECT " + ThreadDatabase.RECIPIENT_ID + " FROM " + ThreadDatabase.TABLE_NAME + "))";
queryArgs = new String[]{"%" + constraint + "%", "1"};
} else {
query = TITLE + " LIKE ? AND " + ACTIVE + " = ?";
queryArgs = new String[]{"%" + constraint + "%", "1"};
}

Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, query, queryArgs, null, null, TITLE + " COLLATE NOCASE ASC");

return new Reader(cursor);
}
Expand Down
41 changes: 18 additions & 23 deletions src/org/thoughtcrime/securesms/database/ThreadDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -374,23 +374,25 @@ public Cursor getFilteredConversationList(@Nullable List<RecipientId> filter) {
return cursor;
}

public Cursor getRecentConversationList(int limit) {
public Cursor getRecentConversationList(int limit, boolean includeInactiveGroups) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String query = createQuery(MESSAGE_COUNT + " != 0", limit);

return db.rawQuery(query, null);
}

public Cursor getRecentPushConversationList(int limit) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String where = MESSAGE_COUNT + " != 0 AND " +
"(" +
RecipientDatabase.REGISTERED + " = " + RecipientDatabase.RegisteredState.REGISTERED.getId() + " OR " +
"(" +
GroupDatabase.TABLE_NAME + "." + GroupDatabase.GROUP_ID + " NOT NULL AND " +
GroupDatabase.TABLE_NAME + "." + GroupDatabase.MMS + " = 0" +
")" +
")";
String query = !includeInactiveGroups ? MESSAGE_COUNT + " != 0 AND " + GroupDatabase.TABLE_NAME + "." + GroupDatabase.ACTIVE + " = 1"
: MESSAGE_COUNT + " != 0";
return db.rawQuery(createQuery(query, limit), null);
}

public Cursor getRecentPushConversationList(int limit, boolean includeInactiveGroups) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String activeGroupQuery = !includeInactiveGroups ? " AND " + GroupDatabase.TABLE_NAME + "." + GroupDatabase.ACTIVE + " = 1" : "";
String where = MESSAGE_COUNT + " != 0 AND " +
"(" +
RecipientDatabase.REGISTERED + " = " + RecipientDatabase.RegisteredState.REGISTERED.getId() + " OR " +
"(" +
GroupDatabase.TABLE_NAME + "." + GroupDatabase.GROUP_ID + " NOT NULL AND " +
GroupDatabase.TABLE_NAME + "." + GroupDatabase.MMS + " = 0" +
activeGroupQuery +
")" +
")";
String query = createQuery(where, limit);

return db.rawQuery(query, null);
Expand All @@ -414,13 +416,6 @@ private Cursor getConversationList(String archived) {
return cursor;
}

public Cursor getDirectShareList() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String query = createQuery(MESSAGE_COUNT + " != 0", 0);

return db.rawQuery(query, null);
}

public int getArchivedConversationListCount() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = null;
Expand Down
10 changes: 10 additions & 0 deletions src/org/thoughtcrime/securesms/jobs/PushGroupSendJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.GroupDatabase;
import org.thoughtcrime.securesms.database.GroupReceiptDatabase.GroupReceiptInfo;
import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.database.NoSuchMessageException;
Expand Down Expand Up @@ -90,6 +91,15 @@ public static void enqueue(@NonNull Context context,
@Nullable RecipientId filterAddress)
{
try {
Recipient group = Recipient.resolved(destination);
if (!group.isPushGroup()) {
throw new AssertionError("Not a group!");
}

if (!DatabaseFactory.getGroupDatabase(context).isActive(group.requireAddress().toGroupString())) {
throw new MmsException("Inactive group!");
}

MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
OutgoingMediaMessage message = database.getOutgoingMessage(messageId);
JobManager.Chain compressAndUploadAttachment = createCompressingAndUploadAttachmentsChain(jobManager, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void getCameraContacts(@NonNull String query, @NonNull Callback<CameraContacts>

List<Recipient> recipients = new ArrayList<>(RECENT_MAX);

try (ThreadDatabase.Reader threadReader = threadDatabase.readerFor(threadDatabase.getRecentPushConversationList(RECENT_MAX))) {
try (ThreadDatabase.Reader threadReader = threadDatabase.readerFor(threadDatabase.getRecentPushConversationList(RECENT_MAX, false))) {
ThreadRecord threadRecord;
while ((threadRecord = threadReader.getNext()) != null) {
recipients.add(threadRecord.getRecipient().resolve());
Expand Down Expand Up @@ -98,7 +98,7 @@ void getCameraContacts(@NonNull String query, @NonNull Callback<CameraContacts>

List<Recipient> recipients = new ArrayList<>();

try (GroupDatabase.Reader reader = groupDatabase.getGroupsFilteredByTitle(query)) {
try (GroupDatabase.Reader reader = groupDatabase.getGroupsFilteredByTitle(query, false)) {
GroupDatabase.GroupRecord groupRecord;
while ((groupRecord = reader.getNext()) != null) {
RecipientId recipientId = recipientDatabase.getOrInsertFromGroupId(groupRecord.getEncodedId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
List<ChooserTarget> results = new LinkedList<>();
ComponentName componentName = new ComponentName(this, ShareActivity.class);
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(this);
Cursor cursor = threadDatabase.getDirectShareList();
Cursor cursor = threadDatabase.getRecentConversationList(10, false);

try {
ThreadDatabase.Reader reader = threadDatabase.readerFor(cursor);
ThreadRecord record;

while ((record = reader.getNext()) != null && results.size() < 10) {
while ((record = reader.getNext()) != null) {
Recipient recipient = Recipient.resolved(record.getRecipient().getId());
String name = recipient.toShortString();

Expand Down

0 comments on commit f1ca5fc

Please sign in to comment.