mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Do recipient merge in one transaction
This commit is contained in:
parent
f2b334b57a
commit
7ab013cee9
3 changed files with 18 additions and 15 deletions
|
@ -88,6 +88,7 @@ import java.nio.channels.FileChannel;
|
|||
import java.nio.channels.FileLock;
|
||||
import java.nio.file.Files;
|
||||
import java.security.SecureRandom;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
|
@ -408,9 +409,11 @@ public class SignalAccount implements Closeable {
|
|||
}
|
||||
}
|
||||
|
||||
private void mergeRecipients(RecipientId recipientId, RecipientId toBeMergedRecipientId) {
|
||||
private void mergeRecipients(
|
||||
final Connection connection, RecipientId recipientId, RecipientId toBeMergedRecipientId
|
||||
) throws SQLException {
|
||||
getMessageCache().mergeRecipients(recipientId, toBeMergedRecipientId);
|
||||
getGroupStore().mergeRecipients(recipientId, toBeMergedRecipientId);
|
||||
getGroupStore().mergeRecipients(connection, recipientId, toBeMergedRecipientId);
|
||||
}
|
||||
|
||||
public void removeRecipient(final RecipientId recipientId) {
|
||||
|
|
|
@ -192,7 +192,9 @@ public class GroupStore {
|
|||
return Stream.concat(getGroupsV2().stream(), getGroupsV1().stream()).toList();
|
||||
}
|
||||
|
||||
public void mergeRecipients(final RecipientId recipientId, final RecipientId toBeMergedRecipientId) {
|
||||
public void mergeRecipients(
|
||||
final Connection connection, final RecipientId recipientId, final RecipientId toBeMergedRecipientId
|
||||
) throws SQLException {
|
||||
final var sql = (
|
||||
"""
|
||||
UPDATE OR REPLACE %s
|
||||
|
@ -200,17 +202,13 @@ public class GroupStore {
|
|||
WHERE recipient_id = ?
|
||||
"""
|
||||
).formatted(TABLE_GROUP_V1_MEMBER);
|
||||
try (final var connection = database.getConnection()) {
|
||||
try (final var statement = connection.prepareStatement(sql)) {
|
||||
statement.setLong(1, recipientId.id());
|
||||
statement.setLong(2, toBeMergedRecipientId.id());
|
||||
final var updatedRows = statement.executeUpdate();
|
||||
if (updatedRows > 0) {
|
||||
logger.info("Updated {} group members when merging recipients", updatedRows);
|
||||
}
|
||||
try (final var statement = connection.prepareStatement(sql)) {
|
||||
statement.setLong(1, recipientId.id());
|
||||
statement.setLong(2, toBeMergedRecipientId.id());
|
||||
final var updatedRows = statement.executeUpdate();
|
||||
if (updatedRows > 0) {
|
||||
logger.info("Updated {} group members when merging recipients", updatedRows);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed update group store", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -569,8 +569,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
|||
}
|
||||
|
||||
if (pair.second().isPresent()) {
|
||||
recipientMergeHandler.mergeRecipients(pair.first(), pair.second().get());
|
||||
try (final var connection = database.getConnection()) {
|
||||
recipientMergeHandler.mergeRecipients(connection, pair.first(), pair.second().get());
|
||||
deleteRecipient(connection, pair.second().get());
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed update recipient store", e);
|
||||
|
@ -931,7 +931,9 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
|||
|
||||
public interface RecipientMergeHandler {
|
||||
|
||||
void mergeRecipients(RecipientId recipientId, RecipientId toBeMergedRecipientId);
|
||||
void mergeRecipients(
|
||||
final Connection connection, RecipientId recipientId, RecipientId toBeMergedRecipientId
|
||||
) throws SQLException;
|
||||
}
|
||||
|
||||
private record RecipientWithAddress(RecipientId id, RecipientAddress address) {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue