mirror of
https://github.com/oxen-io/session-android.git
synced 2025-06-09 17:58:34 +00:00
Delete unused megaphones from the database.
This commit is contained in:
parent
e1b75c78ab
commit
37f85d6deb
@ -10,17 +10,22 @@ import net.sqlcipher.database.SQLiteDatabase;
|
|||||||
|
|
||||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
||||||
import org.thoughtcrime.securesms.database.model.MegaphoneRecord;
|
import org.thoughtcrime.securesms.database.model.MegaphoneRecord;
|
||||||
|
import org.thoughtcrime.securesms.logging.Log;
|
||||||
import org.thoughtcrime.securesms.megaphone.Megaphones.Event;
|
import org.thoughtcrime.securesms.megaphone.Megaphones.Event;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IMPORTANT: Writes should only be made through {@link org.thoughtcrime.securesms.megaphone.MegaphoneRepository}.
|
* IMPORTANT: Writes should only be made through {@link org.thoughtcrime.securesms.megaphone.MegaphoneRepository}.
|
||||||
*/
|
*/
|
||||||
public class MegaphoneDatabase extends Database {
|
public class MegaphoneDatabase extends Database {
|
||||||
|
|
||||||
|
private static final String TAG = Log.tag(MegaphoneDatabase.class);
|
||||||
|
|
||||||
private static final String TABLE_NAME = "megaphone";
|
private static final String TABLE_NAME = "megaphone";
|
||||||
|
|
||||||
private static final String ID = "_id";
|
private static final String ID = "_id";
|
||||||
@ -59,19 +64,41 @@ public class MegaphoneDatabase extends Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NonNull List<MegaphoneRecord> getAll() {
|
public @NonNull List<MegaphoneRecord> getAllAndDeleteMissing() {
|
||||||
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||||
List<MegaphoneRecord> records = new ArrayList<>();
|
List<MegaphoneRecord> records = new ArrayList<>();
|
||||||
|
|
||||||
try (Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null)) {
|
db.beginTransaction();
|
||||||
while (cursor != null && cursor.moveToNext()) {
|
try {
|
||||||
String event = cursor.getString(cursor.getColumnIndexOrThrow(EVENT));
|
Set<String> missingKeys = new HashSet<>();
|
||||||
int seenCount = cursor.getInt(cursor.getColumnIndexOrThrow(SEEN_COUNT));
|
|
||||||
long lastSeen = cursor.getLong(cursor.getColumnIndexOrThrow(LAST_SEEN));
|
|
||||||
long firstVisible = cursor.getLong(cursor.getColumnIndexOrThrow(FIRST_VISIBLE));
|
|
||||||
boolean finished = cursor.getInt(cursor.getColumnIndexOrThrow(FINISHED)) == 1;
|
|
||||||
|
|
||||||
records.add(new MegaphoneRecord(Event.fromKey(event), seenCount, lastSeen, firstVisible, finished));
|
try (Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null)) {
|
||||||
|
while (cursor != null && cursor.moveToNext()) {
|
||||||
|
String event = cursor.getString(cursor.getColumnIndexOrThrow(EVENT));
|
||||||
|
int seenCount = cursor.getInt(cursor.getColumnIndexOrThrow(SEEN_COUNT));
|
||||||
|
long lastSeen = cursor.getLong(cursor.getColumnIndexOrThrow(LAST_SEEN));
|
||||||
|
long firstVisible = cursor.getLong(cursor.getColumnIndexOrThrow(FIRST_VISIBLE));
|
||||||
|
boolean finished = cursor.getInt(cursor.getColumnIndexOrThrow(FINISHED)) == 1;
|
||||||
|
|
||||||
|
if (Event.hasKey(event)) {
|
||||||
|
records.add(new MegaphoneRecord(Event.fromKey(event), seenCount, lastSeen, firstVisible, finished));
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "No in-app handing for event '" + event + "'! Deleting it from the database.");
|
||||||
|
missingKeys.add(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (String missing : missingKeys) {
|
||||||
|
String query = EVENT + " = ?";
|
||||||
|
String[] args = new String[]{missing};
|
||||||
|
|
||||||
|
databaseHelper.getWritableDatabase().delete(TABLE_NAME, query, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.setTransactionSuccessful();
|
||||||
|
} finally {
|
||||||
|
db.endTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
return records;
|
return records;
|
||||||
@ -107,4 +134,11 @@ public class MegaphoneDatabase extends Database {
|
|||||||
|
|
||||||
databaseHelper.getWritableDatabase().update(TABLE_NAME, values, query, args);
|
databaseHelper.getWritableDatabase().update(TABLE_NAME, values, query, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void delete(@NonNull Event event) {
|
||||||
|
String query = EVENT + " = ?";
|
||||||
|
String[] args = new String[]{event.getKey()};
|
||||||
|
|
||||||
|
databaseHelper.getWritableDatabase().delete(TABLE_NAME, query, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,7 +103,7 @@ public class MegaphoneRepository {
|
|||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
private void init() {
|
private void init() {
|
||||||
List<MegaphoneRecord> records = database.getAll();
|
List<MegaphoneRecord> records = database.getAllAndDeleteMissing();
|
||||||
Set<Event> events = Stream.of(records).map(MegaphoneRecord::getEvent).collect(Collectors.toSet());
|
Set<Event> events = Stream.of(records).map(MegaphoneRecord::getEvent).collect(Collectors.toSet());
|
||||||
Set<Event> missing = Stream.of(Megaphones.Event.values()).filterNot(events::contains).collect(Collectors.toSet());
|
Set<Event> missing = Stream.of(Megaphones.Event.values()).filterNot(events::contains).collect(Collectors.toSet());
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ public class MegaphoneRepository {
|
|||||||
@WorkerThread
|
@WorkerThread
|
||||||
private void resetDatabaseCache() {
|
private void resetDatabaseCache() {
|
||||||
databaseCache.clear();
|
databaseCache.clear();
|
||||||
databaseCache.putAll(Stream.of(database.getAll()).collect(Collectors.toMap(MegaphoneRecord::getEvent, m -> m)));
|
databaseCache.putAll(Stream.of(database.getAllAndDeleteMissing()).collect(Collectors.toMap(MegaphoneRecord::getEvent, m -> m)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Callback<E> {
|
public interface Callback<E> {
|
||||||
|
@ -170,5 +170,14 @@ public final class Megaphones {
|
|||||||
}
|
}
|
||||||
throw new IllegalArgumentException("No event for key: " + key);
|
throw new IllegalArgumentException("No event for key: " + key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean hasKey(@NonNull String key) {
|
||||||
|
for (Event event : values()) {
|
||||||
|
if (event.getKey().equals(key)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user