Fix hot loop when trying to delete stories but only onboarding exists.

This commit is contained in:
Alex Hart
2022-07-13 14:52:32 -03:00
committed by Cody Henthorne
parent 15111b2792
commit 2f17963b2b
5 changed files with 73 additions and 12 deletions

View File

@@ -200,7 +200,7 @@ public abstract class MessageDatabase extends Database implements MmsSmsColumns
public abstract boolean hasSelfReplyInStory(long parentStoryId);
public abstract boolean hasSelfReplyInGroupStory(long parentStoryId);
public abstract @NonNull Cursor getStoryReplies(long parentStoryId);
public abstract @Nullable Long getOldestStorySendTimestamp();
public abstract @Nullable Long getOldestStorySendTimestamp(boolean hasSeenReleaseChannelStories);
public abstract int deleteStoriesOlderThan(long timestamp, boolean hasSeenReleaseChannelStories);
public abstract @NonNull MessageDatabase.Reader getUnreadStories(@NonNull RecipientId recipientId, int limit);
public abstract @Nullable ParentStoryId.GroupReply getParentStoryIdForGroupReply(long messageId);

View File

@@ -898,18 +898,37 @@ public class MmsDatabase extends MessageDatabase {
}
@Override
public @Nullable Long getOldestStorySendTimestamp() {
SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();
String[] columns = new String[]{DATE_SENT};
String where = IS_STORY_CLAUSE;
String orderBy = DATE_SENT + " ASC";
String limit = "1";
public @Nullable Long getOldestStorySendTimestamp(boolean hasSeenReleaseChannelStories) {
long releaseChannelThreadId = getReleaseChannelThreadId(hasSeenReleaseChannelStories);
SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();
String[] columns = new String[] { DATE_SENT };
String where = IS_STORY_CLAUSE + " AND " + THREAD_ID + " != ?";
String orderBy = DATE_SENT + " ASC";
String limit = "1";
try (Cursor cursor = db.query(TABLE_NAME, columns, where, null, null, null, orderBy, limit)) {
try (Cursor cursor = db.query(TABLE_NAME, columns, where, SqlUtil.buildArgs(releaseChannelThreadId), null, null, orderBy, limit)) {
return cursor != null && cursor.moveToNext() ? cursor.getLong(0) : null;
}
}
private static long getReleaseChannelThreadId(boolean hasSeenReleaseChannelStories) {
if (hasSeenReleaseChannelStories) {
return -1L;
}
RecipientId releaseChannelRecipientId = SignalStore.releaseChannelValues().getReleaseChannelRecipientId();
if (releaseChannelRecipientId == null) {
return -1L;
}
Long releaseChannelThreadId = SignalDatabase.threads().getThreadIdFor(releaseChannelRecipientId);
if (releaseChannelThreadId == null) {
return -1L;
}
return releaseChannelThreadId;
}
@Override
public void deleteGroupStoryReplies(long parentStoryId) {
SQLiteDatabase db = databaseHelper.getSignalWritableDatabase();
@@ -924,8 +943,7 @@ public class MmsDatabase extends MessageDatabase {
db.beginTransaction();
try {
RecipientId releaseChannelRecipient = hasSeenReleaseChannelStories ? null : SignalStore.releaseChannelValues().getReleaseChannelRecipientId();
long releaseChannelThreadId = releaseChannelRecipient != null ? SignalDatabase.threads().getOrCreateThreadIdFor(Recipient.resolved(releaseChannelRecipient)) : -1;
long releaseChannelThreadId = getReleaseChannelThreadId(hasSeenReleaseChannelStories);
String storiesBeforeTimestampWhere = IS_STORY_CLAUSE + " AND " + DATE_SENT + " < ? AND " + THREAD_ID + " != ?";
String[] sharedArgs = SqlUtil.buildArgs(timestamp, releaseChannelThreadId);
String deleteStoryRepliesQuery = "DELETE FROM " + TABLE_NAME + " " +

View File

@@ -1462,7 +1462,7 @@ public class SmsDatabase extends MessageDatabase {
}
@Override
public @Nullable Long getOldestStorySendTimestamp() {
public @Nullable Long getOldestStorySendTimestamp(boolean hasSeenReleaseChannelStories) {
throw new UnsupportedOperationException();
}

View File

@@ -32,7 +32,7 @@ class ExpiringStoriesManager(
@WorkerThread
override fun getNextClosestEvent(): Event? {
val oldestTimestamp = mmsDatabase.oldestStorySendTimestamp ?: return null
val oldestTimestamp = mmsDatabase.getOldestStorySendTimestamp(SignalStore.storyValues().userHasSeenOnboardingStory) ?: return null
val timeSinceSend = System.currentTimeMillis() - oldestTimestamp
val delay = (STORY_LIFESPAN - timeSinceSend).coerceAtLeast(0)