Add ConversationManager method for deleting all messages

Note that this does not yet delete special conversation messages
such as invitations or introductions and their responses.
This commit is contained in:
Torsten Grote
2019-10-03 14:47:12 -03:00
parent eda17449be
commit c46fdce277
6 changed files with 103 additions and 1 deletions

View File

@@ -24,7 +24,7 @@ public interface ConversationManager {
/**
* Returns the headers of all messages in the given private conversation.
*
* <p>
* Only {@link MessagingManager} returns only headers.
* The others also return the message text.
*/
@@ -36,6 +36,13 @@ public interface ConversationManager {
*/
GroupCount getGroupCount(ContactId c) throws DbException;
/**
* Deletes all messages exchanged with the given contact.
*
* @return true if all messages could be deleted, false otherwise
*/
boolean deleteAllMessages(ContactId c) throws DbException;
@NotNullByDefault
interface ConversationClient {
@@ -49,6 +56,14 @@ public interface ConversationManager {
void setReadFlag(GroupId g, MessageId m, boolean read)
throws DbException;
/**
* Deletes all messages associated with the given contact.
*
* @return true if all messages could be deleted, false otherwise
*/
boolean deleteAllMessages(Transaction txn,
ContactId c) throws DbException;
}
}

View File

@@ -45,6 +45,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
@@ -557,6 +558,26 @@ class IntroductionManagerImpl extends ConversationClientImpl
}
}
@Override
public boolean deleteAllMessages(Transaction txn, ContactId c)
throws DbException {
// TODO actually delete messages (#1627 and #1629)
return getMessageIds(txn, c).size() == 0;
}
private Set<MessageId> getMessageIds(Transaction txn, ContactId c)
throws DbException {
GroupId g = getContactGroup(db.getContact(txn, c)).getId();
BdfDictionary query = messageParser.getMessagesVisibleInUiQuery();
try {
Map<MessageId, BdfDictionary> results =
clientHelper.getMessageMetadataAsDictionary(txn, g, query);
return results.keySet();
} catch (FormatException e) {
throw new DbException(e);
}
}
private static class StoredSession {
private final MessageId storageId;

View File

@@ -73,4 +73,15 @@ class ConversationManagerImpl implements ConversationManager {
return new GroupCount(msgCount, unreadCount, latestTime);
}
@Override
public boolean deleteAllMessages(ContactId c) throws DbException {
return db.transactionWithResult(false, txn -> {
boolean allDeleted = true;
for (ConversationClient client : clients) {
allDeleted = client.deleteAllMessages(txn, c) && allDeleted;
}
return allDeleted;
});
}
}

View File

@@ -404,4 +404,17 @@ class MessagingManagerImpl implements MessagingManager, IncomingMessageHook,
return minorVersion > 0;
}
@Override
public boolean deleteAllMessages(Transaction txn, ContactId c)
throws DbException {
GroupId g = getContactGroup(db.getContact(txn, c)).getId();
// this indiscriminately deletes all raw messages in this group
// also attachments
for (MessageId messageId : db.getMessageIds(txn, g)) {
db.deleteMessage(txn, messageId);
db.deleteMessageMetadata(txn, messageId);
}
return true;
}
}

View File

@@ -43,6 +43,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
@@ -629,6 +630,26 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
return m;
}
@Override
public boolean deleteAllMessages(Transaction txn, ContactId c)
throws DbException {
// TODO actually delete messages (#1627 and #1629)
return getMessageIds(txn, c).size() == 0;
}
private Set<MessageId> getMessageIds(Transaction txn, ContactId c)
throws DbException {
GroupId g = getContactGroup(db.getContact(txn, c)).getId();
BdfDictionary query = messageParser.getMessagesVisibleInUiQuery();
try {
Map<MessageId, BdfDictionary> results =
clientHelper.getMessageMetadataAsDictionary(txn, g, query);
return results.keySet();
} catch (FormatException e) {
throw new DbException(e);
}
}
private static class StoredSession {
private final MessageId storageId;

View File

@@ -40,6 +40,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.Nullable;
@@ -537,6 +538,26 @@ abstract class SharingManagerImpl<S extends Shareable>
return m;
}
@Override
public boolean deleteAllMessages(Transaction txn, ContactId c)
throws DbException {
// TODO actually delete messages (#1627 and #1629)
return getMessageIds(txn, c).size() == 0;
}
private Set<MessageId> getMessageIds(Transaction txn, ContactId c)
throws DbException {
GroupId g = getContactGroup(db.getContact(txn, c)).getId();
BdfDictionary query = messageParser.getMessagesVisibleInUiQuery();
try {
Map<MessageId, BdfDictionary> results =
clientHelper.getMessageMetadataAsDictionary(txn, g, query);
return results.keySet();
} catch (FormatException e) {
throw new DbException(e);
}
}
private static class StoredSession {
private final MessageId storageId;