Fix storing of username

This commit is contained in:
AsamK 2023-11-12 18:50:56 +01:00
parent 0d60c4d464
commit 77f284661b
3 changed files with 27 additions and 11 deletions

View file

@ -17,12 +17,12 @@ public class MergeRecipientHelper {
static Pair<RecipientId, List<RecipientId>> resolveRecipientTrustedLocked(
Store store, RecipientAddress address
) throws SQLException {
// address has serviceId and number, optionally also pni
// address has at least one of serviceId/pni and optionally number/username
final var recipients = store.findAllByAddress(address);
if (recipients.isEmpty()) {
logger.debug("Got new recipient, serviceId, PNI and number are unknown");
logger.debug("Got new recipient, serviceId, PNI, number, username are unknown");
return new Pair<>(store.addNewRecipient(address), List.of());
}
@ -47,7 +47,7 @@ public class MergeRecipientHelper {
}
logger.debug(
"Got recipient {} existing with number/pni, but different serviceId, so stripping its number and adding new recipient",
"Got recipient {} existing with number/pni/username, but different serviceId, so stripping its number and adding new recipient",
recipient.id());
store.updateRecipientAddress(recipient.id(), recipient.address().removeIdentifiersFrom(address));

View file

@ -793,8 +793,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
) throws SQLException {
final var sql = (
"""
INSERT INTO %s (number, uuid, pni)
VALUES (?, ?, ?)
INSERT INTO %s (number, uuid, pni, username)
VALUES (?, ?, ?, ?)
RETURNING _id
"""
).formatted(TABLE_RECIPIENT);
@ -803,6 +803,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
statement.setBytes(2,
address.serviceId().map(ServiceId::getRawUuid).map(UuidUtil::toByteArray).orElse(null));
statement.setBytes(3, address.pni().map(PNI::getRawUuid).map(UuidUtil::toByteArray).orElse(null));
statement.setString(4, address.username().orElse(null));
final var generatedKey = Utils.executeQueryForOptional(statement, Utils::getIdMapper);
if (generatedKey.isPresent()) {
final var recipientId = new RecipientId(generatedKey.get(), this);
@ -820,7 +821,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
final var sql = (
"""
UPDATE %s
SET number = NULL, uuid = NULL, pni = NULL
SET number = NULL, uuid = NULL, pni = NULL, username = NULL
WHERE _id = ?
"""
).formatted(TABLE_RECIPIENT);