Skip to content

Commit

Permalink
Fix stale thread id when a conversation is deleted.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal committed Feb 10, 2023
1 parent fba4c88 commit c741e32
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
import org.thoughtcrime.securesms.conversation.mutiselect.forward.MultiselectForwardFragmentArgs;
import org.thoughtcrime.securesms.conversation.quotes.MessageQuotesBottomSheet;
import org.thoughtcrime.securesms.conversation.ui.error.EnableCallNotificationSettingsDialog;
import org.thoughtcrime.securesms.database.DatabaseObserver;
import org.thoughtcrime.securesms.database.MessageTable;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.database.model.InMemoryMessageRecord;
Expand Down Expand Up @@ -264,6 +265,8 @@ public class ConversationFragment extends LoggingFragment implements Multiselect
private @Nullable ConversationData conversationData;
private @Nullable ChatWallpaper chatWallpaper;

private final DatabaseObserver.Observer threadDeletedObserver = this::onThreadDelete;

public static void prepare(@NonNull Context context) {
FrameLayout parent = new FrameLayout(context);
parent.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
Expand Down Expand Up @@ -526,26 +529,6 @@ public void onConfigurationChanged(@NonNull Configuration newConfig) {
updateToolbarDependentMargins();
}

public void onNewIntent() {
Log.d(TAG, "[onNewIntent]");

if (actionMode != null) {
actionMode.finish();
}

long oldThreadId = threadId;

initializeResources();
messageRequestViewModel.setConversationInfo(recipient.getId(), threadId);

int startingPosition = getStartPosition();
if (startingPosition != -1 && oldThreadId == threadId) {
list.post(() -> moveToPosition(startingPosition, () -> Log.w(TAG, "Could not scroll to requested message.")));
} else {
initializeListAdapter();
}
}

public void moveToLastSeen() {
int lastSeenPosition = conversationData != null ? conversationData.getLastSeenPosition() : 0;
if (lastSeenPosition <= 0) {
Expand Down Expand Up @@ -697,8 +680,8 @@ private void initializeResources() {
long oldThreadId = threadId;
int startingPosition = getStartPosition();

this.recipient = Recipient.live(conversationViewModel.getArgs().getRecipientId());
this.threadId = conversationViewModel.getArgs().getThreadId();
this.recipient = Recipient.live(conversationViewModel.getArgs().getRecipientId());
setThreadId(conversationViewModel.getArgs().getThreadId());
this.markReadHelper = new MarkReadHelper(ConversationId.forConversation(threadId), requireContext(), getViewLifecycleOwner());

conversationViewModel.onConversationDataAvailable(recipient.getId(), threadId, startingPosition);
Expand All @@ -718,6 +701,12 @@ private void initializeResources() {
}
}

private void setThreadId(long threadId) {
this.threadId = threadId;
ApplicationDependencies.getDatabaseObserver().unregisterObserver(threadDeletedObserver);
ApplicationDependencies.getDatabaseObserver().registerConversationDeleteObserver(this.threadId, threadDeletedObserver);
}

private void initializeListAdapter() {
if (this.recipient != null) {
if (getListAdapter() != null && getListAdapter().isForRecipientId(this.recipient.getId())) {
Expand Down Expand Up @@ -945,7 +934,7 @@ public void reload(Recipient recipient, long threadId) {
if (this.threadId != threadId) {
Log.i(TAG, "ThreadId changed from " + this.threadId + " to " + threadId + ". Recipient was " + this.recipient.getId() + " and is now " + recipient.getId());

this.threadId = threadId;
setThreadId(threadId);
messageRequestViewModel.setConversationInfo(recipient.getId(), threadId);

snapToTopDataObserver.requestScrollPosition(0);
Expand Down Expand Up @@ -1060,14 +1049,7 @@ private AlertDialog.Builder buildRemoteDeleteConfirmationDialog(Set<MessageRecor
@Override
protected Void doInBackground(Void... voids) {
for (MessageRecord messageRecord : messageRecords) {
boolean threadDeleted = SignalDatabase.messages().deleteMessage(messageRecord.getId());

if (threadDeleted) {
threadId = -1;
conversationViewModel.clearThreadId();
messageCountsViewModel.clearThreadId();
listener.setThreadId(threadId);
}
SignalDatabase.messages().deleteMessage(messageRecord.getId());
}

return null;
Expand All @@ -1085,6 +1067,13 @@ protected Void doInBackground(Void... voids) {
return builder;
}

private void onThreadDelete() {
setThreadId(-1);
conversationViewModel.clearThreadId();
messageCountsViewModel.clearThreadId();
listener.setThreadId(threadId);
}

private static boolean isNoteToSelfDelete(Set<MessageRecord> messageRecords) {
return messageRecords.stream().allMatch(messageRecord -> messageRecord.isOutgoing() && messageRecord.getRecipient().isSelf());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ public class DatabaseObserver {
private static final String KEY_RECIPIENT = "Recipient";
private static final String KEY_STORY_OBSERVER = "Story";
private static final String KEY_SCHEDULED_MESSAGES = "ScheduledMessages";
private static final String KEY_CONVERSATION_DELETES = "ConversationDeletes";

private final Application application;
private final Executor executor;

private final Set<Observer> conversationListObservers;
private final Map<Long, Set<Observer>> conversationObservers;
private final Map<Long, Set<Observer>> verboseConversationObservers;
private final Map<Long, Set<Observer>> conversationDeleteObservers;
private final Map<UUID, Set<Observer>> paymentObservers;
private final Map<Long, Set<Observer>> scheduledMessageObservers;
private final Set<Observer> allPaymentsObservers;
Expand All @@ -68,6 +70,7 @@ public DatabaseObserver(Application application) {
this.conversationListObservers = new HashSet<>();
this.conversationObservers = new HashMap<>();
this.verboseConversationObservers = new HashMap<>();
this.conversationDeleteObservers = new HashMap<>();
this.paymentObservers = new HashMap<>();
this.allPaymentsObservers = new HashSet<>();
this.chatColorsObservers = new HashSet<>();
Expand Down Expand Up @@ -99,6 +102,12 @@ public void registerVerboseConversationObserver(long threadId, @NonNull Observer
});
}

public void registerConversationDeleteObserver(long threadId, @NonNull Observer listener) {
executor.execute(() -> {
registerMapped(conversationDeleteObservers, threadId, listener);
});
}

public void registerPaymentObserver(@NonNull UUID paymentId, @NonNull Observer listener) {
executor.execute(() -> {
registerMapped(paymentObservers, paymentId, listener);
Expand Down Expand Up @@ -181,6 +190,7 @@ public void unregisterObserver(@NonNull Observer listener) {
notificationProfileObservers.remove(listener);
unregisterMapped(storyObservers, listener);
unregisterMapped(scheduledMessageObservers, listener);
unregisterMapped(conversationDeleteObservers, listener);
});
}

Expand Down Expand Up @@ -212,6 +222,18 @@ public void notifyVerboseConversationListeners(Set<Long> threadIds) {
}
}

public void notifyConversationDeleteListeners(Set<Long> threadIds) {
for (long threadId : threadIds) {
notifyConversationDeleteListeners(threadId);
}
}

public void notifyConversationDeleteListeners(long threadId) {
runPostSuccessfulTransaction(KEY_CONVERSATION_DELETES + threadId, () -> {
notifyMapped(conversationDeleteObservers, threadId);
});
}

public void notifyConversationListListeners() {
runPostSuccessfulTransaction(KEY_CONVERSATION_LIST, () -> {
for (Observer listener : conversationListObservers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import org.thoughtcrime.securesms.database.model.MmsMessageRecord
import org.thoughtcrime.securesms.database.model.ThreadRecord
import org.thoughtcrime.securesms.database.model.databaseprotos.BodyRangeList
import org.thoughtcrime.securesms.database.model.serialize
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.groups.BadGroupIdException
import org.thoughtcrime.securesms.groups.GroupId
import org.thoughtcrime.securesms.jobs.OptimizeMessageSearchIndexJob
Expand Down Expand Up @@ -1040,6 +1041,7 @@ class ThreadTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa

notifyConversationListListeners()
notifyConversationListeners(threadId)
ApplicationDependencies.getDatabaseObserver().notifyConversationDeleteListeners(threadId)
ConversationUtil.clearShortcuts(context, setOf(recipientIdForThreadId))
}

Expand All @@ -1056,6 +1058,7 @@ class ThreadTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa

notifyConversationListListeners()
notifyConversationListeners(selectedConversations)
ApplicationDependencies.getDatabaseObserver().notifyConversationDeleteListeners(selectedConversations)
ConversationUtil.clearShortcuts(context, recipientIdsForThreadIds)
}

Expand Down

0 comments on commit c741e32

Please sign in to comment.