Add report spam in message request state.

This commit is contained in:
Cody Henthorne
2021-05-17 09:43:37 -04:00
committed by Alex Hart
parent c47dcd5720
commit ef5b68eb35
31 changed files with 393 additions and 145 deletions

View File

@@ -34,6 +34,7 @@ import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.revealable.ViewOnceExpirationInfo;
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
import org.thoughtcrime.securesms.util.CursorUtil;
import org.thoughtcrime.securesms.util.JsonUtils;
import org.thoughtcrime.securesms.util.SqlUtil;
import org.thoughtcrime.securesms.util.Util;
@@ -403,6 +404,25 @@ public abstract class MessageDatabase extends Database implements MmsSmsColumns
}
}
public @NonNull List<ReportSpamData> getReportSpamMessageServerGuids(long threadId, long timestamp) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String query = THREAD_ID + " = ? AND " + getDateReceivedColumnName() + " <= ?";
String[] args = SqlUtil.buildArgs(threadId, timestamp);
List<ReportSpamData> data = new ArrayList<>();
try (Cursor cursor = db.query(getTableName(), new String[] { RECIPIENT_ID, SERVER_GUID, getDateReceivedColumnName() }, query, args, null, null, getDateReceivedColumnName() + " DESC", "3")) {
while (cursor.moveToNext()) {
RecipientId id = RecipientId.from(CursorUtil.requireLong(cursor, RECIPIENT_ID));
String serverGuid = CursorUtil.requireString(cursor, SERVER_GUID);
long dateReceived = CursorUtil.requireLong(cursor, getDateReceivedColumnName());
if (!Util.isEmpty(serverGuid)) {
data.add(new ReportSpamData(id, serverGuid, dateReceived));
}
}
}
return data;
}
protected static List<ReactionRecord> parseReactions(@NonNull Cursor cursor) {
byte[] raw = cursor.getBlob(cursor.getColumnIndexOrThrow(REACTIONS));
@@ -770,4 +790,28 @@ public abstract class MessageDatabase extends Database implements MmsSmsColumns
MessageRecord getCurrent();
void close();
}
public static class ReportSpamData {
private final RecipientId recipientId;
private final String serverGuid;
private final long dateReceived;
public ReportSpamData(RecipientId recipientId, String serverGuid, long dateReceived) {
this.recipientId = recipientId;
this.serverGuid = serverGuid;
this.dateReceived = dateReceived;
}
public @NonNull RecipientId getRecipientId() {
return recipientId;
}
public @NonNull String getServerGuid() {
return serverGuid;
}
public long getDateReceived() {
return dateReceived;
}
}
}

View File

@@ -185,7 +185,8 @@ public class MmsDatabase extends MessageDatabase {
REMOTE_DELETED + " INTEGER DEFAULT 0, " +
MENTIONS_SELF + " INTEGER DEFAULT 0, " +
NOTIFIED_TIMESTAMP + " INTEGER DEFAULT 0, " +
VIEWED_RECEIPT_COUNT + " INTEGER DEFAULT 0);";
VIEWED_RECEIPT_COUNT + " INTEGER DEFAULT 0, " +
SERVER_GUID + " TEXT DEFAULT NULL);";
public static final String[] CREATE_INDEXS = {
"CREATE INDEX IF NOT EXISTS mms_thread_id_index ON " + TABLE_NAME + " (" + THREAD_ID + ");",
@@ -1317,6 +1318,7 @@ public class MmsDatabase extends MessageDatabase {
contentValues.put(VIEW_ONCE, retrieved.isViewOnce() ? 1 : 0);
contentValues.put(READ, retrieved.isExpirationUpdate() ? 1 : 0);
contentValues.put(UNIDENTIFIED, retrieved.isUnidentified());
contentValues.put(SERVER_GUID, retrieved.getServerGuid());
if (!contentValues.containsKey(DATE_SENT)) {
contentValues.put(DATE_SENT, contentValues.getAsLong(DATE_RECEIVED));

View File

@@ -27,6 +27,7 @@ public interface MmsSmsColumns {
public static final String REACTIONS_UNREAD = "reactions_unread";
public static final String REACTIONS_LAST_SEEN = "reactions_last_seen";
public static final String REMOTE_DELETED = "remote_deleted";
public static final String SERVER_GUID = "server_guid";
/**
* For storage efficiency, all types are stored within a single 64-bit integer column in the

View File

@@ -38,11 +38,13 @@ import org.thoughtcrime.securesms.util.CursorUtil;
import org.whispersystems.libsignal.util.Pair;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class MmsSmsDatabase extends Database {
@@ -564,6 +566,16 @@ public class MmsSmsDatabase extends Database {
DatabaseFactory.getMmsDatabase(context).deleteAbandonedMessages();
}
public @NonNull List<MessageDatabase.ReportSpamData> getReportSpamMessageServerData(long threadId, long timestamp, int limit) {
List<MessageDatabase.ReportSpamData> data = new ArrayList<>();
data.addAll(DatabaseFactory.getSmsDatabase(context).getReportSpamMessageServerGuids(threadId, timestamp));
data.addAll(DatabaseFactory.getMmsDatabase(context).getReportSpamMessageServerGuids(threadId, timestamp));
return data.stream()
.sorted((l, r) -> -Long.compare(l.getDateReceived(), r.getDateReceived()))
.limit(limit)
.collect(Collectors.toList());
}
private Cursor queryTables(String[] projection, String selection, String order, String limit) {
String[] mmsProjection = {MmsDatabase.DATE_SENT + " AS " + MmsSmsColumns.NORMALIZED_DATE_SENT,
MmsDatabase.DATE_RECEIVED + " AS " + MmsSmsColumns.NORMALIZED_DATE_RECEIVED,

View File

@@ -9,7 +9,6 @@ import androidx.annotation.NonNull;
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
@@ -68,7 +67,7 @@ public class PushDatabase extends Database {
values.put(TIMESTAMP, envelope.getTimestamp());
values.put(SERVER_RECEIVED_TIMESTAMP, envelope.getServerReceivedTimestamp());
values.put(SERVER_DELIVERED_TIMESTAMP, envelope.getServerDeliveredTimestamp());
values.put(SERVER_GUID, envelope.getUuid());
values.put(SERVER_GUID, envelope.getServerGuid());
return databaseHelper.getWritableDatabase().insert(TABLE_NAME, null, values);
}

View File

@@ -123,7 +123,8 @@ public class SmsDatabase extends MessageDatabase {
REACTIONS_UNREAD + " INTEGER DEFAULT 0, " +
REACTIONS_LAST_SEEN + " INTEGER DEFAULT -1, " +
REMOTE_DELETED + " INTEGER DEFAULT 0, " +
NOTIFIED_TIMESTAMP + " INTEGER DEFAULT 0);";
NOTIFIED_TIMESTAMP + " INTEGER DEFAULT 0," +
SERVER_GUID + " TEXT DEFAULT NULL);";
public static final String[] CREATE_INDEXS = {
"CREATE INDEX IF NOT EXISTS sms_thread_id_index ON " + TABLE_NAME + " (" + THREAD_ID + ");",
@@ -1105,6 +1106,7 @@ public class SmsDatabase extends MessageDatabase {
values.put(BODY, message.getMessageBody());
values.put(TYPE, type);
values.put(THREAD_ID, threadId);
values.put(SERVER_GUID, message.getServerGuid());
if (message.isPush() && isDuplicate(message, threadId)) {
Log.w(TAG, "Duplicate message (" + message.getSentTimestampMillis() + "), ignoring...");

View File

@@ -181,8 +181,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper implements SignalDatab
private static final int CLEAN_REACTION_NOTIFICATIONS = 96;
private static final int STORAGE_SERVICE_REFACTOR = 97;
private static final int CLEAR_MMS_STORAGE_IDS = 98;
private static final int SERVER_GUID = 99;
private static final int DATABASE_VERSION = 98;
private static final int DATABASE_VERSION = 99;
private static final String DATABASE_NAME = "signal.db";
private final Context context;
@@ -1457,6 +1458,11 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper implements SignalDatab
Log.d(TAG, "Cleared storageIds from " + deleteCount + " rows. They were either MMS groups or empty contacts.");
}
if (oldVersion < SERVER_GUID) {
db.execSQL("ALTER TABLE sms ADD COLUMN server_guid TEXT DEFAULT NULL");
db.execSQL("ALTER TABLE mms ADD COLUMN server_guid TEXT DEFAULT NULL");
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();