Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SES-2966 - Fix group message deletion #826

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import network.loki.messenger.R
import network.loki.messenger.libsession_util.ConfigBase.Companion.PRIORITY_HIDDEN
import network.loki.messenger.libsession_util.ConfigBase.Companion.PRIORITY_PINNED
import network.loki.messenger.libsession_util.ReadableGroupInfoConfig
Expand Down Expand Up @@ -39,12 +40,15 @@ import org.session.libsignal.messages.SignalServiceGroup
import org.session.libsignal.utilities.AccountId
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.database.MmsDatabase
import org.thoughtcrime.securesms.database.MmsSmsDatabase
import org.thoughtcrime.securesms.database.RecipientDatabase
import org.thoughtcrime.securesms.database.ThreadDatabase
import org.thoughtcrime.securesms.dependencies.PollerFactory
import org.thoughtcrime.securesms.groups.ClosedGroupManager
import org.thoughtcrime.securesms.groups.OpenGroupManager
import org.thoughtcrime.securesms.repository.ConversationRepository
import org.thoughtcrime.securesms.sskenvironment.ProfileManager
import java.util.concurrent.TimeUnit
import javax.inject.Inject

private const val TAG = "ConfigToDatabaseSync"
Expand All @@ -65,6 +69,8 @@ class ConfigToDatabaseSync @Inject constructor(
private val clock: SnodeClock,
private val profileManager: ProfileManager,
private val preferences: TextSecurePreferences,
private val conversationRepository: ConversationRepository,
private val mmsSmsDatabase: MmsSmsDatabase,
) {
init {
if (!preferences.migratedToGroupV2Config) {
Expand Down Expand Up @@ -194,10 +200,11 @@ class ConfigToDatabaseSync @Inject constructor(
)
} else {
groupInfoConfig.deleteBefore?.let { removeBefore ->
storage.trimThreadBefore(threadId, removeBefore)
val messages = mmsSmsDatabase.getAllMessageRecordsBefore(threadId, TimeUnit.SECONDS.toMillis(removeBefore))
conversationRepository.markAsDeletedLocally(messages, context.getString(R.string.deleteMessageDeletedGlobally))
}
groupInfoConfig.deleteAttachmentsBefore?.let { removeAttachmentsBefore ->
mmsDatabase.deleteMessagesInThreadBeforeDate(threadId, removeAttachmentsBefore, onlyMedia = true)
mmsDatabase.deleteMessagesInThreadBeforeDate(threadId, TimeUnit.SECONDS.toMillis(removeAttachmentsBefore), onlyMedia = true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@ public Set<MessageRecord> getAllMessageRecordsFromSenderInThread(long threadId,
return identifiedMessages;
}

public Set<MessageRecord> getAllMessageRecordsBefore(long threadId, long timestampMills) {
String selection = MmsSmsColumns.THREAD_ID + " = " + threadId + " AND " + MmsSmsColumns.NORMALIZED_DATE_SENT + " < " + timestampMills;
Set<MessageRecord> identifiedMessages = new HashSet<>();

// Try everything with resources so that they auto-close on end of scope
try (Cursor cursor = queryTables(PROJECTION, selection, null, null)) {
try (MmsSmsDatabase.Reader reader = readerFor(cursor)) {
MessageRecord messageRecord;
while ((messageRecord = reader.getNext()) != null) {
identifiedMessages.add(messageRecord);
}
}
}
return identifiedMessages;
}

public long getLastOutgoingTimestamp(long threadId) {
String order = MmsSmsColumns.NORMALIZED_DATE_SENT + " DESC";
String selection = MmsSmsColumns.THREAD_ID + " = " + threadId;
Expand Down