That fuck shit the fascists are using
at master 103 lines 4.8 kB view raw
1package org.tm.archive.database; 2 3import android.content.ContentValues; 4import android.content.Context; 5import android.database.Cursor; 6 7import androidx.annotation.NonNull; 8 9import net.zetetic.database.sqlcipher.SQLiteDatabase; 10 11import org.tm.archive.database.model.PendingRetryReceiptModel; 12import org.tm.archive.dependencies.ApplicationDependencies; 13import org.tm.archive.recipients.RecipientId; 14import org.signal.core.util.CursorUtil; 15import org.signal.core.util.SqlUtil; 16 17import java.util.LinkedList; 18import java.util.List; 19 20/** 21 * Holds information about messages we've sent out retry receipts for. 22 * 23 * Do not use directly! The only class that should be accessing this is {@link PendingRetryReceiptCache} 24 */ 25public final class PendingRetryReceiptTable extends DatabaseTable implements RecipientIdDatabaseReference, ThreadIdDatabaseReference { 26 27 public static final String TABLE_NAME = "pending_retry_receipts"; 28 29 private static final String ID = "_id"; 30 private static final String AUTHOR = "author"; 31 private static final String DEVICE = "device"; 32 private static final String SENT_TIMESTAMP = "sent_timestamp"; 33 private static final String RECEIVED_TIMESTAMP = "received_timestamp"; 34 private static final String THREAD_ID = "thread_id"; 35 36 public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 37 AUTHOR + " TEXT NOT NULL, " + 38 DEVICE + " INTEGER NOT NULL, " + 39 SENT_TIMESTAMP + " INTEGER NOT NULL, " + 40 RECEIVED_TIMESTAMP + " TEXT NOT NULL, " + 41 THREAD_ID + " INTEGER NOT NULL, " + 42 "UNIQUE(" + AUTHOR + "," + SENT_TIMESTAMP + ") ON CONFLICT REPLACE);"; 43 44 PendingRetryReceiptTable(Context context, SignalDatabase databaseHelper) { 45 super(context, databaseHelper); 46 } 47 48 @NonNull PendingRetryReceiptModel insert(@NonNull RecipientId author, int authorDevice, long sentTimestamp, long receivedTimestamp, long threadId) { 49 ContentValues values = new ContentValues(); 50 values.put(AUTHOR, author.serialize()); 51 values.put(DEVICE, authorDevice); 52 values.put(SENT_TIMESTAMP, sentTimestamp); 53 values.put(RECEIVED_TIMESTAMP, receivedTimestamp); 54 values.put(THREAD_ID, threadId); 55 56 long id = databaseHelper.getSignalWritableDatabase().insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE); 57 58 return new PendingRetryReceiptModel(id, author, authorDevice, sentTimestamp, receivedTimestamp, threadId); 59 } 60 61 @NonNull List<PendingRetryReceiptModel> getAll() { 62 List<PendingRetryReceiptModel> models = new LinkedList<>(); 63 64 try (Cursor cursor = databaseHelper.getSignalReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null)) { 65 while (cursor.moveToNext()) { 66 models.add(fromCursor(cursor)); 67 } 68 } 69 70 return models; 71 } 72 73 void delete(@NonNull PendingRetryReceiptModel model) { 74 databaseHelper.getSignalWritableDatabase().delete(TABLE_NAME, ID_WHERE, SqlUtil.buildArgs(model.getId())); 75 } 76 77 private static @NonNull PendingRetryReceiptModel fromCursor(@NonNull Cursor cursor) { 78 return new PendingRetryReceiptModel(CursorUtil.requireLong(cursor, ID), 79 RecipientId.from(CursorUtil.requireString(cursor, AUTHOR)), 80 CursorUtil.requireInt(cursor, DEVICE), 81 CursorUtil.requireLong(cursor, SENT_TIMESTAMP), 82 CursorUtil.requireLong(cursor, RECEIVED_TIMESTAMP), 83 CursorUtil.requireLong(cursor, THREAD_ID)); 84 } 85 86 @Override 87 public void remapRecipient(@NonNull RecipientId fromId, @NonNull RecipientId toId) { 88 ContentValues values = new ContentValues(); 89 values.put(AUTHOR, toId.serialize()); 90 getWritableDatabase().update(TABLE_NAME, values, AUTHOR + " = ?", SqlUtil.buildArgs(fromId)); 91 92 ApplicationDependencies.getPendingRetryReceiptCache().clear(); 93 } 94 95 @Override 96 public void remapThread(long fromId, long toId) { 97 ContentValues values = new ContentValues(); 98 values.put(THREAD_ID, toId); 99 getWritableDatabase().update(TABLE_NAME, values, THREAD_ID + " = ?", SqlUtil.buildArgs(fromId)); 100 101 ApplicationDependencies.getPendingRetryReceiptCache().clear(); 102 } 103}